C++ 实验一
2-28-1 用if...else语句实现简单程序
#include <iostream> using namespace std; int main () { char n; cout<<"Menu: A(dd) D(elete) S(ort) Q(uit),Select one:"; cin>>n; while(n){ if(n=='A') cout<<"Date add"<<endl; else if(n=='D') cout<<"Date delete"<<endl; else if(n=='S') cout<<"Date sort"<<endl; else if(n=='Q') break; cout<<"Menu: A(dd) D(elete) S(ort) Q(uit),Select one:"; cin>>n; } return 0; }
2-28-2 用switch语句实现简单程序
#include <iostream> using namespace std; int main () { char n; cout<<"Menu: A(dd) D(elete) S(ort) Q(uit),Select one:"; cin>>n; while(n){ switch(n){ case 'A': cout<<"Date add"<<endl;break; case 'D': cout<<"Date delete"<<endl;break; case 'S': cout<<"Date sort"<<endl;break; } cout<<"Menu: A(dd) D(elete) S(ort) Q(uit),Select one:"; cin>>n; if(n=='Q') break; } return 0; }
2-29-1 用while语句找出1~100间的质数
#include<iostream> #include<iomanip> using namespace std; int main() { int i=2,j,k,n=0; while (i<100) { k=1; j=2; while (j<=i-1) { if(i%j++==0) { k=0;break;} } if(k==1) { n++; cout<<setw(5)<<i; if(n%5==0) cout<<endl; } i++; } return 0; }
2-29-2 用do....while语句找出1~100间的质数
#include<iostream> #include<iomanip> using namespace std; int main() { int i=2,j,k,n=0; do { k=1; j=2; do { if(i%j++==0) { k=0;break;} } while (j<=i-1); if(j>i-1) { n++; cout<<setw(5)<<i; if(n%5==0) cout<<endl; } i++; }while (i<100); return 0; }
2-29-3 用for语句找出1~100间的质数
#include<iostream> #include<iomanip> using namespace std; int main() { int i=2,j,k,n=0; for(;i<=100;i++) { k=1; for (j=2;j<=i-1;j++) { if(i%j==0) { k=0;break;} } if(j>i-1) { n++; cout<<setw(5)<<i; if(n%5==0) cout<<endl; } } return 0; }
2-32-1 用while语句猜数
#include<iostream> using namespace std; int main() { int a=88,b; cin>>b; while(b!=a) {if (b>a) {cout<<"bigger than this number: ";cin>>b;} if (b<a) {cout<<"smaller than this number: ";cin>>b;} } cout<<"Congratulations!You are right!"<<endl; return 0; }
2-32-2 用do.....while语句猜数
#include<iostream> using namespace std; int main() { int a=88,b; cin>>b; do {if (b>a) {cout<<"bigger than this number: ";cin>>b;} if (b<a) {cout<<"smaller than this number: ";cin>>b;} } while(b!=a); cout<<"Congratulations!You are right!"<<endl; return 0; }
2-34 球的取法
#include <iostream> using namespace std; int main() { int i,j,k; for(i=0;i<=4;i++) {for(j=i+1;j<=4;j++) {for(k=j+1;k<=4;k++) {cout<<i<<" "<<j<<" "<<k<<" "<<endl;} } } return 0; }
实验总结
1.对于C++还是不太熟悉,在编程过程中还是困难的,有待提高。
2.以上程序有不足处请大家多包涵,多指教。
评论的第一位同学:https://www.cnblogs.com/KOKODA/p/10512049.html
评论的第二位同学:https://www.cnblogs.com/msag/p/10556200.html#4212110
评论的第三位同学:https://www.cnblogs.com/21savage-code/p/10515962.html