实验一:C++简单程序设计
实验结论:
2-28 实现一个简单的菜单程序,运行时显示“Menu:A(dd) D(elete) S(ort) Q(uit),Selete one:”提示用户输入。A表示增加,D表示删除,S表示排序,Q表示退出。输入为A、D、S时分别提示“数据已经增加、删除、排序。”,输入Q时程序结束。 (1)if...else...语句,break,continue控制 (2)switch语句
(1) if ...else...
#include <iostream> using namespace std; int main() {char a; while(true) {cout<<"Menu:A(dd) D(elete) S(ort) Q(uit),Select one:"<< endl; cin>>a; if(a=='A') {cout<<"数据已增加"<<endl;continue;} else if(a=='D') {cout<<"数据已删除"<<endl;continue;} else if(a=='S') {cout<<"数据已排序"<<endl;continue;} else if(a=='Q') break; else {cout<<"数据不符合"<<endl;continue;} } return 0; }
(2) switch
#include<iostream> using namespace std; int main() {char a; cout<<"Menu:A(dd) D(elete) S(ort) Q(uit),select one:"<<endl; while(true) {cin>>a; switch(a) {case 'A':{cout<<"数据已增加"<<endl;continue;} case 'D':{cout<<"数据已删除"<<endl;continue;} case 'S':{cout<<"数据已排序"<<endl;continue;} case 'Q':return 0; default:cout<<"error!"<<endl; } } return 0; }
运行截图
2-29 用穷举法找出1~100间的质数并显示出来。使用while,do...while,for语句实现。
(1)while
#include <iostream> #include <cmath> using namespace std; int main() {int n=2,t=0,f,i; while(n<=100) {f=1; for(i=2;i<=sqrt(n);i++) if(n%i==0) {f=0;break;} if(f==1) {t++; cout<<n<<'\t'; if(t%5==0) cout<<endl; } n++; } return 0; }
(2)do...while
#include <iostream> #include <cmath> using namespace std; int main() {int n=2,t=0,f,i; do {f=1; for(i=2;i<=sqrt(n);i++) if(n%i==0) {f=0;break;} if(f==1) {t++; cout<<n<<'\t'; if(t%5==0) cout<<endl; } n++; } while(n<=100); return 0; }
(3)for
#include <iostream> #include <cmath> using namespace std; int main() {int n=2,t=0,f,i; for(n=2;n<=100;n++) {f=1; for(i=2;i<=sqrt(n);i++) if(n%i==0) {f=0;break;} if(f==1) {t++; cout<<n<<'\t'; if(t%5==0) cout<<endl; } } return 0; }
运行截图
2-32 在程序中定义一个整型变量,赋予1-100的值。要求用户猜这个数,比较两个数的大小,把结果提示给用户,直到猜对为止。分别使用while,do...while语句实现。
(1) while
#include <iostream> using namespace std; int main() {int a=10,b; cout<<"please guess a number between 1 and 100:"<<endl; while(true) {cin>>b; if(a<b) {cout<<"it is bigger than the unknown number"<<endl;continue;} else if(a>b) {cout<<"it is smaller than the unknown number"<<endl;continue;} else {cout<<"Congradulations!You are right!"<<endl;return 0;} } return 0; }
(2) do...while
#include <iostream> using namespace std; int main() {int a=10,b; cout<<"please guess a number between 1 and 100:"<<endl; do {cin>>b; if(a<b) {cout<<"it is bigger than the unknown number"<<endl;continue;} else if(a>b) {cout<<"it is smaller than the unknown number"<<endl;continue;} else {cout<<"Congradulations!You are right!"<<endl;return 0;} }while(true); return 0; }
运行截图
2-34 口袋中有红、黄。蓝、白、黑5种颜色的球若干个。每次从口袋中取出3个不同颜色的球,问有多少种取法?
#include<iostream>
using namespace std;
void cp(int a)
{ switch (a)
{
case 0:cout<<"red "<<'\t';break;
case 1:cout<<"yellow "<<'\t';break;
case 2:cout<<"blue "<<'\t';break;
case 3:cout<<"white "<<'\t';break;
case 4:cout<<"black "<<'\t';break;
}
}
int main(){
int i,j,k,s=0;
for (i=0;i<=4;i++)
for (j=i+1;j<=4;j++)
for (k=j+1;k<=4;k++)
{ s++;
cp(i);
cp(j);
cp(k);
cout<<endl;
}
cout<<s<<endl;
return 0;
}
运行截图
实验总结与体验
1 在对while(true)的使用中,因语句位置导致程序的死循环,了解break,continue的区别和使用场合
2 while、do...while、for三种循环嵌套的使用
3 随机变量和枚举类型的应用不熟悉
已评:
https://www.cnblogs.com/jiyuanxiangzhouziying/
https://www.cnblogs.com/DADABu-21/
https://www.cnblogs.com/aiwenzhuo/