c++第一次作业
2-28
#include<iostream> using namespace std; int main(){ char enter; cout<<"Menu:A(dd) D(elete) S(ort) Q(uit),Select one:"; while(cin>>enter) { if(enter=='A') cout<<"Data has been added!"<<endl; else if(enter=='D') cout<<"Data has been deleted!"<<endl; else if(enter=='S') cout<<"Data has been Sorted!"<<endl; else if(enter=='Q') break; else cout<<"no such answer,please select one again!"<<endl; cout<<"Menu:A(dd) D(elete) S(ort) Q(uit),Select one:"; } return 0; } 使用switch #include<iostream> using namespace std; int main(){ char enter; cout<<"Menu:A(dd) D(elete) S(ort) Q(uit),Select one:"; while(cin>>enter) { switch(enter){ case 'A':cout<<"Data has been added!"<<endl;break; case 'D':cout<<"Data has been deleted!"<<endl;break; case 'S':cout<<"Data has been Sorted!"<<endl;break; case 'Q':break; default:cout<<"no such answer,please select one again!"<<endl; } if(enter=='Q') break; cout<<"Menu:A(dd) D(elete) S(ort) Q(uit),Select one:"; } return 0; }
2-29
(使用while)
#include<iostream> #include<iomanip> #include<cmath> using namespace std; int main() { int i=2,j=2,k=0; while(i<=100){ while(j<=sqrt((double)i)){ if(i%j==0) break; j++; } if(j>sqrt((double)i)) { cout<<setw(5)<<i; if(++k%5==0) cout<<endl; } i++;j=2; } return 0; }
(使用do while)
#include<iostream> #include<iomanip> #include<cmath> using namespace std; int main() { int i=2,j=2,k=0; do{ do{ if(i%j==0) break; j++; }while(j<=sqrt((double)i)); if(j>sqrt((double)i)){ cout<<setw(5)<<i; if(++k%5==0) cout<<endl; } i++;j=2; }while(i<=100); return 0; }
(使用for循环)
#include<iostream> #include<iomanip> #include<cmath> using namespace std; int main() { int i,j,k=0; for(i=2;i<=100;i++) { for(j=2;j<=sqrt((double)i);j++){ if(i%j==0) break; } if(j>sqrt((double)i)) { cout<<setw(5)<<i; if(++k%5==0) cout<<endl; } } return 0; }
2-32
猜一个数字
(1)使用while
#include<iostream> using namespace std; #include<cstdlib> #include<ctime> int main(){ srand((time(0))); int guessnumber=rand()%100+1;int guess; cout<<"please have a guess at number(1~100):"; while(cin>>guess){ if(guess==guessnumber) { cout<<"you do it!"<<endl; break; } else if(guess>guessnumber) cout<<"your guess is larger than guessnumber!"<<endl; else cout<<"your guess is smaller than guessnumber!"<<endl; cout<<"please have a guess at number(1~100):"; } return 0; }
(2)使用 do...while
#include<iostream> using namespace std; #include<cstdlib> #include<ctime> int main(){ srand((time(0))); int guessnumber=rand()%100+1; int guess; cout<<"please have a guess at number(1~100):"; cin>>guess; do{ if(guess==guessnumber) { cout<<"you do it!"<<endl; break; } else if(guess>guessnumber) cout<<"your guess is larger than guessnumber!"<<endl; else cout<<"your guess is smaller than guessnumber!"<<endl; cout<<"please have a guess at number(1~100):"; }while(cin>>guess); return 0; }
2-34
(1)五种颜色取三个,10个的情况。
#include<iostream> using namespace std; enum colour{red,yellow,blue,white,black}; void print(int); //it is used to transform int to colour and then output int main() { int x,y,z,k=0; for(x=red;x<=black;x++) for(y=x+1;y<=black;y++) for(z=y+1;z<=black;z++) { { print(x); cout<<" "; print(y); cout<<" "; print(z); cout<<endl; }//it is used to printf x,y,z; k++; } cout<<k; return 0; } void print(int x){ switch(x) { {case 0:cout<<"red";break;} {case 1:cout<<"yellow";break;} {case 2:cout<<"blue";break;} {case 3:cout<<"white";break;} {case 4:cout<<"black";break;} } }
(2)有顺序的情况 ,60
#include<iostream> using namespace std; enum colour{red,yellow,blue,white,black}; void print(int);//it is used to transform int to colour and then output int main() { int x,y,z,k=0; for(x=red;x<=black;x++) for(y=red;y<=black;y++) { if(y==x) continue; for(z=red;z<=black;z++) { if(z==x||z==y) continue; { print(x); cout<<" "; print(y); cout<<" "; print(z); cout<<endl; }//it is used to printf x,y,z; k++; } } cout<<k; return 0; } void print(int x){ switch(x) { {case 0:cout<<"red";break;} {case 1:cout<<"yellow";break;} {case 2:cout<<"blue";break;} {case 3:cout<<"white";break;} {case 4:cout<<"black";break;} } }
总结:在第一次实验的过程中,我学会了如何去使用devc,已经codeblocks等一些软件,也初步对c++这个语言有了一些了解,在这个过程中,也在不断地解决问题和学习中学到了新的知识,比如说随机数,枚举型的使用。
https://www.cnblogs.com/mxueyyqx/
https://www.cnblogs.com/aiyy492903331/
https://www.cnblogs.com/sora5934/
https://www.cnblogs.com/qiuqiuwr/