实验一

2-28-(1)
#include<iostream> using namespace std;
int main() { char n; cout<<"Menu:A(dd) D(elect) S(ort) Q(uit),select one:"; cin>>n; while (n!='Q') { if (n=='A') cout<<"The data has been added"<<endl; else if(n=='D') cout<<"The data has been delected"<<endl; else if(n=='S') cout<<"The data has been sorted"<<endl; else cout<<"The data is wrong,please input again:"<<endl; cin>>n; } return 0; }

 

 

 

2-28-(2)

#include<iostream> using namespace std; int main() { char n; cout<<"Menu:A(dd) D(elect) S(ort) Q(uit),select one:"; cin>>n; while (n!='Q') { switch(n) { case'A': cout<<"The data has been added"<<endl;break; case'D': cout<<"The data has been delected"<<endl;break; case'S': cout<<"The data has been sorted"<<endl;break; default: cout<<"The data is wrong,please input again:"<<endl;break; } cin>>n; } return 0; }

 


2-29-while

#include<iostream> #include<cmath> using namespace std; int main () { int i=2,j,k; while(i<=100) { k=sqrt(i); for (j=2;j<=k;j++) if (i%j==0) break; if (j>k) cout<<i<<endl; i++; } return 0; }

 

 

 

2-29-do...while
#include<iostream> #include<cmath> using namespace std; int main () { int i=2,j,k; do { k=sqrt(i); for (j=2;j<=k;j++) if (i%j==0) break; if (j>k) cout<<i<<endl; i++; }while(i<=100); return 0; }

 

 

 

2-29-for

#include<iostream> #include<cmath> using namespace std; int main () { int i,j,k; for (i=2;i<=100;i++) { k=sqrt(i); for (j=2;j<=k;j++) if (i%j==0) break; if (j>k) cout<<i<<endl; } return 0; }

 


 

2-32-while

#include <iostream> #include <cstdlib> using namespace std; int main () { int n; unsigned seed; cout<<"Please input the seed:"; cin>>seed; srand (seed); n=rand()%100; int a; cout<<"Please guess the number:"<<endl; cin>>a; while (a>=0&&a<=100) { if (a>n) { cout<<"Bigger than the number,please guess again:"<<endl; cin>>a; } else if (a<n) { cout<<"lower than the number,please guess again:"<<endl; cin>>a; } else { cout<<"Great!you guess the number"<<endl; break; } } return 0; }
2-32-do...while

#include <iostream> #include <cstdlib> using namespace std; int main () { int n; unsigned seed; cout<<"Please input the seed:"; cin>>seed; srand (seed); n=rand()%100; int a; cout<<"Please guess the number:"<<endl; cin>>a; do { if (a>n) { cout<<"Bigger than the number,please guess again:"<<endl; cin>>a; } else if (a<n) { cout<<"lower than the number,please guess again:"<<endl; cin>>a; } else { cout<<"Great!you guess the number"<<endl; break; } }while (a>=0&&a<=100); return 0; }

 

 

 

 

3-34

#include <iostream> #include<iomanip> using namespace std; enum Color{red,yellow,blue,white,black}; void colorput(int a) { Color color; color=Color(a); switch (a) { case 0:cout<<setprecision(8)<<"red ";break; case 1:cout<<setprecision(8)<<"yellow ";break; case 2:cout<<setprecision(8)<<"blue ";break; case 3:cout<<setprecision(8)<<"white ";break; case 4:cout<<setprecision(8)<<"black ";break; } } int main () { int count=0; int i,j,k; for (i=0;i<=4;i++) for (j=0;j<=4;j++) for (k=0;k<=4;k++) { if (i!=j&&i!=k&&j!=k) { count++; colorput(i); colorput(j); colorput(k); cout<<endl; } } cout<<count<<endl; return 0; }

 

 

 

 

  

 

posted on 2019-03-15 17:04  happy-every-day  阅读(108)  评论(2编辑  收藏  举报