1.(1)用if...else实现简单的菜单程序
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include<iostream> using namespace std; int main() { char c; cout << "Menu: A(dd) D(elete) S(ort) Q(uit):"; cin >> c; while (c) { if (c == 'A') cout << "number has been added" << endl; else if (c == 'D') cout << "number has been deleted" << endl; else if (c == 'S') cout << "number has been sorted" << endl; else if (c == 'Q') break; else cout << "input error!" << endl; cout << "Menu: A(dd) D(elete) S(ort) Q(uit):"; cin >> c; } return 0; }
1.(2)用switch实现简单的菜单程序
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include<iostream> using namespace std; int main() { char c; cout << "Menu: A(dd) D(elete) S(ort) Q(uit):"; cin >> c; while (c!='Q') { switch (c) { case 'A':cout << "number has been added" << endl; break; case 'D':cout << "number has been deleted" << endl; break; case 'S':cout << "number has been sorted" << endl; break; default:cout << "input error!" << endl; } cout << "Menu: A(dd) D(elete) S(ort) Q(uit):"; cin >> c; } return 0; }
2.(1)用while输出1-100的素数
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include<iostream> #include<iomanip> using namespace std; int main() { int j = 0, n = 2, i; while (n < 100) { i = 2; while (i <= n - 1) { if (n%i++ == 0) { break; } } if (i > n - 1) { cout << setw(5) << n; j++; if (j % 5 == 0) cout << endl; } n++; } return 0; }
2.(2)用for输出1-100的素数
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include<iostream> #include<iomanip> using namespace std; int main() { int j = 0, n, i,f=1; for (n = 2; n < 100; n++) { for(i=2;i<=n-1;i++) { if (n%i == 0) { f = 0; break; } } if(i>n-1) { cout << setw(5) << n; j++; if(j%5==0) cout << endl; } } return 0; }
2.(3)用do...while输出1-100的素数
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include<iostream> #include<iomanip> using namespace std; int main() { int j = 0, n = 2, i; do { i = 2; do { if (n%i++ == 0) { break; } } while (i <= n - 1); if (i > n - 1) { cout << setw(5) << n; j++; if (j % 5 == 0) { cout << endl; } } n++; } while (n < 100); return 0; }
3.(1)用while生成一个随机数并猜测
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include<iostream> using namespace std; #include<cstdlib> #include<ctime> int main() { int n, x; srand((unsigned)time(0)); n = (100*(int)rand() / 100 + 1)%100; cin >> x; while(x!=n) { if (x < n) cout << "The number is bigger" << endl; else if (x > n) cout << "The number is lower" << endl; cin >> x; } cout << "You are right" << endl; }
3.(2)用do...while生成一个随机数并猜测
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include<iostream> using namespace std; #include<cstdlib> #include<ctime> int main() { int n, x; srand((unsigned)time(0)); n = (100 * (int)rand() / 100 + 1) % 100; cin >> x; do { if (x < n) cout << "The number is bigger" << endl; else if (x > n) cout << "The number is lower" << endl; cin >> x; } while (x != n); cout << "You are right" << endl; }
4.五种球,任取三个,有多少种取法
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include<iostream> #include<iomanip> using namespace std; int main() { int i, j, k; for (i = 0; i <= 4; i++) { for (j = i + 1; j <= 4; j++) { if (j == i) continue; for (k = j + 1; k <= 4; k++) { if (k == i || k == j) continue; cout << setw(5) << i << setw(5) << j << setw(5) << k << endl; } } } }
实验总结与体会
1.这次实验让我练习了一下c++的格式
2.知道了随机函数的用法,虽然可能是错的
3.复习了一下上学期学的C语言知识