实验内容 Part1:验证性内容
1,在c++开发环境中编写、编译、运行、调试一个简单的c++程序;
2,运行2.4节所有编程示例,理解c++编程基础及I/O流对象cin,cout的基本用法
Part2: 编程练习
1,教材第2章习题2-28
2,教材第2章习题2-29
3,教材第2章习题2-32
4,教材第2章习题2-34
1,(书P63习题2-28)
题目要求:实现一个简单得菜单程序,运行时显示“Menu:A(dd) D(elete) S(ort) Q(uit),Select one:”提示用户输入。A表示删除,S表示排序,Q表示退出。输入为A,D,S时分别提示“数据已经增加,删除,排序。”,输入为Q时程序结束。(1)要求使用if…else语句进行判断,用break,continue控制程序流程。(2)要求使用switch语句。
(1)
1 #include2 using namespace std; 3 #define N cout<<"Menu:A(dd) D(elete) S(ort) Q(uit),Select one:" 4 int main() 5 { 6 char n;//插入字符 7 int i; 8 N;//由于多次出现,使用了宏定义 9 for(i=1;i>=1;i++) 10 { 11 cin>>n; 12 if(n=='A')//分支判断,下同 13 { 14 cout<<"Data has been added."<<endl; 15 N; 16 continue; 17 } 18 if(n=='D') 19 { 20 cout<<"Data has been delete."<<endl; 21 N; 22 continue; 23 } 24 if(n=='S') 25 { 26 cout<<"Data has been sorted."<<endl; 27 N; 28 continue; 29 } 30 if(n=='Q') 31 { 32 i=0; 33 break; 34 } 35 else//应题目要求使用if…else能处理除了“A,D,S,Q“以外的情况 36 { 37 cout<<"No such choice,please select again."<<endl; 38 N; 39 } 40 } 41 return 0; 42 }
程序结果如下:
(2)
1 #include2 using namespace std; 3 #define N cout<<"Menu: A(dd) D(elete) S(ort) Q(uit),Select one: "; 4 int main() 5 { 6 char n; 7 cout<<"Menu: A(dd) D(elete) S(ort) Q(uit),Select one: "; 8 int i=0; 9 while(i==0)//应题目要求使用while语句 10 { 11 cin>>n; 12 switch(n) 13 { 14 case 'A':cout<<"Data has been added."<<endl; 15 N;break;//依然使用了宏定义 16 case 'D':cout<<"Data has been deleted."<<endl; 17 N;break; 18 case 'S':cout<<"Data has been sorted."<<endl; 19 N;break; 20 case 'Q':i=1;break; 21 default:cout<<"No such choice,please select again."<<endl; 22 //while语句的default也能起到撇除A,S,D,Q选择外的情况 23 N;break; 24 } 25 } 26 return 0; 27 }
运行结果如下:
1,(书P63习题2-29)
题目要求:用穷举法找出1~100间的质数并显示出来。分别使用while,do…while,for循环语句实现。
(1~while语句)
1 #include2 #include 3 #include 4 using namespace std; 5 int main() 6 { 7 int n=2,i,a=0; 8 while(n<=100)//让循环进行到n=100时结束,题目要求 9 { 10 for(i=2;i<=10;i++)//10为sqrt(100) 11 if(n%i==0)break; 12 if(i>sqrt(n)) 13 { 14 a++; 15 cout< 5)< " ";//控制间距 16 if(a%5==0)//控制换行 17 cout<<endl; 18 } 19 n++; 20 } 21 return 0; 22 }
运行结果如下:
(2~do…while)
1 #include2 #include 3 #include 4 using namespace std; 5 int main() 6 { 7 int n=2,i,a=0; 8 do 9 { 10 for(i=2;i<=n;i++) 11 if(n%i==0)break; 12 if(i>sqrt(n)) 13 { 14 a++; 15 cout< 5)< " "; 16 if(a%5==0)//控制换行 17 cout<<endl; 18 } 19 n++; 20 }while(n<=100);//循环,题目要求 21 return 0; 22 }
(3~for)
1 #include2 #include 3 #include 4 using namespace std; 5 int main() 6 { 7 int n,i,a=0; 8 for(n=2;n<=100;n++) //循环,题目要求 9 { 10 for(i=2;i<=10;i++) 11 if(n%i==0)break; 12 if(i>sqrt(n)) 13 { 14 a++; 15 cout< 5)< " "; 16 if(a%5==0)//控制输出格式 17 cout<<endl; 18 } 19 } 20 return 0; 21 }
程序结果如下:
1,(书P63习题2-32)
题目要求:在程序中定义一个整型变量,赋予1~100的值。要求用户猜这个数,比较两个数的大小,把结果提示给用户,知道猜对为止。分别使用while,do…while语句实现循环。
(1~while)
1 #include2 #include 3 #include 4 using namespace std; 5 int R()//插入一个函数,生成随机数 6 { 7 int r=0; 8 srand(time(0)); 9 return rand()%100; 10 } 11 12 int main() 13 { 14 int a,i; 15 a=R();//将随机数赋给a 16 cout<<"Your guess number(1~100) is "; 17 cin>>i; 18 while(i!=a)//循环,题目要求 19 { 20 if(i>a)//判断大小 21 cout<<"Lower than the number that you guess."< "Guess the number(1~100) again : "; 22 else if(i<a) 23 cout<<"Bigger than the number that you guess."< "Guess the number(1~100) again : "; 24 cin>>i; 25 } 26 cout<<"Congratulations!You guess the number!"; 27 return 0; 28 }
运行结果如下:
(2~do…while)
1 #include2 #include 3 #include 4 using namespace std; 5 int R()//插入函数,每次生成不同的随机数 6 { 7 int r=0; 8 srand(time(0)); 9 return rand()%100;//控制随机数在1~100之间 10 } 11 int main() 12 { 13 int a,i; 14 a=R(); 15 cout<<"Your guess number(1~100) is "; 16 cin>>i; 17 do 18 { 19 if(i>a)//判断语句 20 cout<<"Lower than the number that you guess."< "Guess the number(1~100) again : "; 21 else if(i<a) 22 cout<<"Bigger than the number that you guess."< "Guess the number(1~100) again : "; 23 cin>>i; 24 }while(i!=a);//循环,题目要求 25 cout<<"Congratulations!You guess the number!"; 26 return 0; 27 }
运行结果如下:
1,(书P63习题2-34)
题目要求:口袋中有红,黄,蓝,白,黑5钟颜色的球若干个。每次从口袋中取出3个不同颜色的球,问有多少种取法?
1 #include2 #include 3 using namespace std; 4 enum color{red,yellow,blue,white,black};//设计枚举类型 5 int main() 6 { 7 enum color i,j,k; 8 int l,c=0; 9 int m,n,q,p=0; 10 for(m=0;m<=4;m++)//第一个摸出的颜色循环 11 { 12 for(n=0;n<=4;n++)//第二个摸出的颜色循环 13 { 14 for(q=0;q<=4;q++)//第三次摸出的颜色循环 15 { 16 if(m!=n&&n!=q&&m!=q)//控制三次摸出的颜色互不相同 17 { 18 c++; 19 for(l=0;l<=3;l++)//将整型转换成枚举类型 20 { 21 switch(l) 22 { 23 case 0:i=color(m);break; 24 case 1:j=color(n);break; 25 case 2:k=color(q);break; 26 } 27 } 28 cout< //实现左对齐 29 cout< 2)< " "; 30 switch(i)//最后输出颜色 31 { 32 case red:cout< 7)<<"red";break; 33 case yellow:cout< 7)<<"yellow";break; 34 case blue:cout< 7)<<"blue";break; 35 case white:cout< 7)<<"white";break; 36 case black:cout< 7)<<"black";break; 37 } 38 switch(j)//最后输出颜色 39 { 40 case red:cout< 7)<<"red";break; 41 case yellow:cout< 7)<<"yellow";break; 42 case blue:cout< 7)<<"blue";break; 43 case white:cout< 7)<<"white";break; 44 case black:cout< 7)<<"black";break; 45 } 46 switch(k)//最后输出颜色 47 { 48 case red:cout< 7)<<"red"< break; 49 case yellow:cout< 7)<<"yellow"< break; 50 case blue:cout< 7)<<"blue"< break; 51 case white:cout< 7)<<"white"< break; 52 case black:cout< 7)<<"black"< break; 53 } 54 p++;//计算符合要求的案例有多少个 55 } 56 else 57 continue;//若此次没有符合要求的案例则继续循环 58 } 59 } 60 } 61 cout< "When the balls are ordered, the total number of cases is:"< endl; 62 return 0; 63 }
实验结果如下:
其中:(1)左对齐参考网站:https://blog.csdn.net/stevanxiao/article/details/81436290
(2)将整型转换为color枚举类型参考网站:https://blog.csdn.net/lihao21/article/details/6825722
实验总结
刚开始思考一个个程序结构的时候,运用c的知识能够应对且能有清晰的思路,但仅仅是面向前两个程序。个人原因,在质数方面,始终不太理解该算法,第四个程序中的枚举类型在c中也没有得到实践,并且对于一些头文件的包含还掌握得不是很熟练。第四个枚举类型的程序,刚开始的时候,由于知识面匮乏,于是就只在程序中插入了两个函数来计算组合数,但通过这些只能单调得计算出一个数学数值。后来知道了enum枚举类型,开始应用之后,又苦于只能将颜色用整型数字输出,反复尝试,插入了switch函数,希望可以由数字对应输出颜色,多次尝试失败后,查阅了将整型与枚举类型相互转换的资料,终于将最后的颜色输出,但是第一次成功输出后的格式我是用setw()来控制的,由于单词字母数不同,于是希望格式能够左对齐,发现可以用“cout< 实验一互评地址: https://www.cnblogs.com/lyc1103/ https://www.cnblogs.com/sunny0202/ https://www.cnblogs.com/dadadacy/