//文中有格式错误请无视
//这个编辑器一言难尽
实验目的
1. 掌握c++中类c部分的编程知识: 数据类型,常量,变量,运算符,表达式,分支结构,循环结构
2. 掌握C++中数据输入和输出的基本方法
3. 熟练使用c++程序开发环境,掌握c++程序编写、编译、运行、调试的方法
实验准备
1. 简单的c++程序结构 学习/复习教材「2.1.3 C++程序实例」
2. c++中数据输入输出的基本方法 学习/复习教材2.3节,学习C++中I/O流、预定义的插入符<<和提取符>>的基本用法。
3. if语句、switch语句、while语句、do…while语句的用法 学习/复习教材2.4节,通过示例理解背后简单算法及c++分支语句、循环语句的用法
4. 自定义数据类型: typedef,枚举类型用法 学习/复习教材2.5节,结合示例理解枚举类型和int型在类型转换时的注意事项
实验内容
Part2: 编程练习
教材第2章习题2-28 简单的菜单程序
教材第2章习题2-29 穷举法求质数
教材第2章习题2-32 猜数
教材第2章习题2-34排列组合
实验结论
2-28简单的菜单程序(if-else
Code:
1 #include<iostream> 2 using namespace std; 3 int main() 4 { 5 cout<<"Menu:A(dd) D(elete) S(ort) Q(uit)"; 6 cout<<"Select one:"; 7 char k; 8 while(cin>>k) 9 { 10 if(k==‘A‘) 11 cout<<"Data has added.\n"<<"Menu:A(dd) D(elete) S(ort) Q(uit)"<<"Select one:"; 12 else if(k==‘D‘) 13 cout<<"Data has deleted.\n"<<"Menu:A(dd) D(elete) S(ort) Q(uit)"<<"Select one:"; 14 else if(k==‘S‘) 15 cout<<"Data has sorted.\n"<<"Menu:A(dd) D(elete) S(ort) Q(uit)"<<"Select one:"; 16 else if(k==‘Q‘) 17 break; 18 else 19 cout<<"No such choice,please select again.\n"<<"Menu:A(dd) D(elete) S(ort) Q(uit)"<<"Select one:"; 20 } 21 return 0; 22 }
Screenshot:
(switch
Code:
1 #include<iostream> 2 using namespace std; 3 int main() 4 { 5 cout<<"Menu:A(dd) D(elete) S(ort) Q(uit)"; 6 cout<<"Select one:"; 7 char k; 8 while(cin>>k&&k!=‘Q‘) 9 { 10 switch(k) 11 { 12 case ‘A‘: 13 cout<<"Data has added.\n"<<"Menu:A(dd) D(elete) S(ort) Q(uit)"<<"Select one:"; 14 continue; 15 case ‘D‘: 16 cout<<"Data has deleted.\n"<<"Menu:A(dd) D(elete) S(ort) Q(uit)"<<"Select one:"; 17 continue; 18 case ‘S‘: 19 cout<<"Data has sorted.\n"<<"Menu:A(dd) D(elete) S(ort) Q(uit)"<<"Select one:"; 20 continue; 21 default:cout<<"No such choice,please select again.\n"<<"Menu:A(dd) D(elete) S(ort) Q(uit)"<<"Select one:"; 22 } 23 } 24 return 0; 25 }
Screenshop:
2-29 穷举法求质数(while
Code:
1 #include<iostream> 2 #include<cmath> 3 #include<iomanip> 4 using namespace std; 5 int main() 6 { 7 cout<<"1~100间的质数为:"<<endl; 8 int x=1; 9 int a,b,c=0; 10 while(x<=100) 11 { 12 a=sqrt(x); 13 if(x!=1) 14 { 15 b=2; 16 while(x%b!=0&&b<=a) 17 b++; 18 if(b>a) 19 { 20 c++; 21 cout<<setw(5)<<x; 22 if(c%5==0) 23 cout<<"\n"; 24 } 25 } 26 x++; 27 } 28 return 0; 29 }
Screenshop:
(for
Code:
1 #include<iostream> 2 #include<cmath> 3 #include<iomanip> 4 using namespace std; 5 int main() 6 { 7 cout<<"1~100间的质数为:"<<endl; 8 int x; 9 int a,b,c=0; 10 for(x=1;x<=100;x++) 11 { 12 a=sqrt(x); 13 if(x==1) 14 continue; 15 else 16 { 17 for(b=2;b<=a;b++) 18 if(x%b==0) 19 break; 20 if(b>a) 21 { 22 c++; 23 cout<<setw(5)<<x; 24 if(c%5==0) 25 cout<<"\n"; 26 } 27 } 28 } 29 return 0; 30 }
Screenshop:
(do-while
Code:
1 #include<iostream> 2 #include<cmath> 3 #include<iomanip> 4 using namespace std; 5 int main() 6 { 7 cout<<"1~100间的质数为:"<<endl; 8 int x=0; 9 int a,b,c=0; 10 do 11 { 12 x++; 13 a=sqrt(x); 14 if(x!=1) 15 { 16 b=1; 17 do 18 { 19 b++; 20 }while(x%b!=0&&b<=a); 21 if(b>a) 22 { 23 c++; 24 cout<<setw(5)<<x; 25 if(c%5==0) 26 cout<<"\n"; 27 } 28 } 29 }while(x<100); 30 return 0; 31 }
Screenshop:
2-32 猜数(while
Code:
1 #include<iostream> 2 #include<ctime> 3 #include<cstdlib> 4 using namespace std; 5 int main() 6 { 7 int x; 8 int a; 9 srand(time(NULL)); 10 x=1+rand()%(100-1+1); 11 cout<<"Your guess number is(1~100): "; 12 cin>>a; 13 while(a!=x) 14 { 15 if(a<x) 16 { 17 cout<<"Bigger than the number."<<endl; 18 cout<<"Your guess number is(1~100): "; 19 cin>>a; 20 } 21 else 22 { 23 cout<<"Lower than the number. "<<endl; 24 cout<<"Your guess number is(1~100): "; 25 cin>>a; 26 } 27 } 28 cout<<"Congretulations.You guessed it.~"; 29 return 0; 30 }
Screenshop:
(do-while
Code:
1 #include<iostream> 2 #include<ctime> 3 #include<cstdlib> 4 using namespace std; 5 int main() 6 { 7 int x; 8 int a; 9 srand(time(NULL)); 10 x=1+rand()%(100-1+1); 11 cout<<"Your guess number is(1~100): "; 12 do 13 { 14 cin>>a; 15 if(a<x) 16 { 17 cout<<"Bigger than the number."<<endl; 18 cout<<"Your guess number is(1~100): "; 19 } 20 else if(a>x) 21 { 22 cout<<"Lower than the number. "<<endl; 23 cout<<"Your guess number is(1~100): "; 24 } 25 }while(a!=x); 26 cout<<"Congretulations.You guessed it.~"; 27 return 0; 28 }
Screenshop:
//暴力循环
2-34排列组合(排列
Code:
1 #include<iostream> 2 #include<iomanip> 3 using namespace std; 4 int main() 5 { 6 int x[3],k=0,i; 7 for(x[0]=1;x[0]<=5;x[0]++) 8 { 9 for(x[1]=1;x[1]<=5;x[1]++) 10 { 11 if(x[1]==x[0]) 12 continue; 13 else 14 { 15 for(x[2]=1;x[2]<=5;x[2]++) 16 { 17 if(x[2]==x[0]||x[2]==x[1]) 18 continue; 19 else 20 { 21 k++; 22 for(i=0;i<=2;i++) 23 { 24 if(i!=2) 25 { 26 switch(x[i]) 27 { 28 case 1: 29 cout<<"red "; 30 continue; 31 case 2: 32 cout<<"yellow "; 33 continue; 34 case 3: 35 cout<<"blue "; 36 continue; 37 case 4: 38 cout<<"white "; 39 continue; 40 case 5: 41 cout<<"black "; 42 } 43 } 44 else 45 { 46 switch(x[i]) 47 { 48 case 1: 49 cout<<"red\n"; 50 continue; 51 case 2: 52 cout<<"yellow\n"; 53 continue; 54 case 3: 55 cout<<"blue\n"; 56 continue; 57 case 4: 58 cout<<"white\n"; 59 continue; 60 case 5: 61 cout<<"black\n"; 62 } 63 } 64 } 65 } 66 } 67 } 68 } 69 } 70 cout<<"Total: "<<k; 71 return 0; 72 }
Screenshop:
(组合
Code:
1 #include<iostream> 2 #include<iomanip> 3 using namespace std; 4 int main() 5 { 6 int x[3],k=0,i; 7 for(x[0]=1;x[0]<=5;x[0]++) 8 for(x[1]=x[0]+1;x[1]<=5;x[1]++) 9 for(x[2]=x[1]+1;x[2]<=5;x[2]++) 10 { 11 k++; 12 for(i=0;i<=2;i++) 13 { 14 if(i!=2) 15 { 16 switch(x[i]) 17 { 18 case 1: 19 cout<<"red "; 20 continue; 21 case 2: 22 cout<<"yellow "; 23 continue; 24 case 3: 25 cout<<"blue "; 26 continue; 27 case 4: 28 cout<<"white "; 29 continue; 30 case 5: 31 cout<<"black "; 32 } 33 } 34 else 35 { 36 switch(x[i]) 37 { 38 case 1: 39 cout<<"red\n"; 40 continue; 41 case 2: 42 cout<<"yellow\n"; 43 continue; 44 case 3: 45 cout<<"blue\n"; 46 continue; 47 case 4: 48 cout<<"white\n"; 49 continue; 50 case 5: 51 cout<<"black\n"; 52 } 53 } 54 } 55 } 56 cout<<"Total: "<<k; 57 return 0; 58 }
Screenshop:
实验总结
这次试验主要是类C的部分,CPP的特性我还没有完全体会到(从I/O流已经可以看出些特点的程度)。
在进行多次判断时还是用switch更方便。
多数时候for循环较为好用。
写程序时尽量避免嵌套过多层循环,严重拖慢编译和运行速度。
srand(time(NULL));
x=1+rand()%(100-1+1);
以此来取一定范围内的随机数。
枚举数据类型不同于整型或字符型。
原文地址:https://www.cnblogs.com/wyf-blogs/p/10527198.html