实验一

2-28-1

#include<iostream>
using namespace std;
int main()
{char r;
 cout<<"Menu:A(dd) D(elete) S(ort) Q(uit),Select one:"<<endl;
 cin>>r;
 while(r!='Q')
 {if(r=='A')
   {cout<<"数据已经增加"<<endl;
   }
  else if(r=='D')
   {cout<<"数据已经删除"<<endl;
   }
  else if(r=='S')
   {cout<<"数据已经排序"<<endl;
   }
  else
   {cout<<"数据错误"<<endl; 
   }
  cin>>r;
 }
 return 0;
}
2-28-1

2-28-2

#include<iostream>
using namespace std;
int main()
{char r;
 cout<<"Menu:A(dd) D(elete) S(ort) Q(uit),Select one:"<<endl;
 cin>>r;
 while(r!='Q')
 {switch(r)
  {case'A': 
   {cout<<"数据已经增加"<<endl;break;
   }
   case'D':
   {cout<<"数据已经删除"<<endl;break; 
   }
   case'S':
   {cout<<"数据已经排序"<<endl;break; 
   }
  }
  cin>>r;
 }
 return 0;
}
2-28-2

2-29-1

#include<iostream>
#include<iomanip>
using namespace std;
int main()
{int i,j,n,m,t=0;
 i=2;
 while(i<=100)
 {m=1;n=i/2;j=2;
 while(j<=n)
 {if(i%j==0)
    {m=0;
    break;
    }
  j++;
 }
 if(m)
 {cout<<setw(5)<<i;
  t++;
  if(t%5==0) cout<<endl;}
 i++;
 }
}
2-29-1

 

2-29-2

#include<iostream>
#include<iomanip>
using namespace std;
int main()
{int i,j,n,m,t=0;
 i=2;
  do
 {m=1;n=i/2;j=2;
 while(j<=n)
 {if(i%j==0)
    {m=0;
    break;
    }
  j++;
 }
 if(m)
 {cout<<setw(5)<<i;
  t++;
  if(t%5==0) cout<<endl;}
 i++;
 }
  while(i<=100);
}
2-29-2

2-29-3

#include<iostream>
#include<iomanip>
using namespace std;
int main()
{int i,j,n,m,t=0;
 i=2;
 for(;i<=100;i++)
 {m=1;n=i/2;j=2;
 for(;j<=n;j++)
 {if(i%j==0)
    {m=0;
    break;
    }
 }
 if(m)
 {cout<<setw(5)<<i;
  t++;
  if(t%5==0) cout<<endl;}
 }
}
2-29-3

2-32-1

#include<iostream>
#include<ctime>
#include<cstdlib>
using namespace std;
int main()
{int i,j ;
 cout<<"输入1~100以内的数:"<<endl;
 srand((unsigned)time(0));
 i=rand()%(100)+1;
 cin>>j;
 while(i!=j)
 {if(i<j)
  cout<<"The number is bigger."<<endl;
  if(i>j)
  cout<<"The number is lower."<<endl;
  cin>>j;
 }
 cout<<"You are right."<<endl;
}
2-32-1

2-32-2

#include<iostream>
#include<ctime>
#include<cstdlib>
using namespace std;
int main()
{int i,j ;
 cout<<"输入1~100以内的数:"<<endl;
 srand((unsigned)time(0));
 i=rand()%(100)+1;
 cin>>j;
 do
 {if(i<j)
  cout<<"The number is bigger."<<endl;
  if(i>j)
  cout<<"The number is lower."<<endl;
  cin>>j;
 }while(i!=j);
 cout<<"You are right."<<endl;
}
2-32-2

2-34

#include<iostream> 
#include<iomanip>
using namespace std;
int main()
{int i,j,k,n=0;
 for(i=1;i<=5;i++)
 {for(j=i+1;j<=5;j++)
  {for(k=j+1;k<=5;k++)
   {n++;
   cout<<i<<setw(3)<<j<<setw(3)<<k<<endl;}
  }
 }
 cout<<"Total:"<<n<<endl; 
}
2-34

总结:通过不断调整程序的代码,可以用更简洁的方法实现程序的输出;感觉自己的程序还有可以改进的地方,循环语句运用的不是很熟练;学到了包括控制代码间距的setw()和随机函数。

三个评论:

1.https://www.cnblogs.com/0906mxyd/p/10549332.html

2.https://www.cnblogs.com/99563220-fhy/p/10541807.html

3.https://www.cnblogs.com/DADABu-21/p/10529599.html

posted @ 2019-03-17 00:01  星星会打烊  阅读(134)  评论(2编辑  收藏  举报