C++ (实验一)

实验结论

2-28  实现一个简单的菜单程序

(1)if

 1 #include <iostream>
 2 using namespace std;
 3 
 4 int main(){
 5   char control;
 6   cout<<"Menu: A(dd) D(elect) S(ort) Q(uit),select one:";
 7   cin>>control;
 8   while(control!='Q'){
 9   if (control=='A')
10       cout<<"Data has been added"<<endl;
11   else if (control=='D')
12       cout<<"Data has been deleted"<<endl;
13   else if (control=='S')
14       cout<<"Data has been sorted"<<endl;
15   else
16       cout<<"Input error"<<endl;
17   cout<<"Menu: A(dd) D(elete) S(ort) Q(uit), Select one:";
18   cin>>control;}
19     cout<<"Program end"<<endl;
20   return 0;
21 }

运行截图

(2)switch

 1 #include <iostream>
 2 using namespace std;
 3 
 4 int main() {
 5     char control;
 6     cout<<"Menu: A(dd) D(elect) S(ort) Q(uit),select one:";
 7     cin>>control;
 8     while(control!='Q'){
 9         switch (control) {
10             case 'A':
11                 cout<<"Data has been added"<<endl; break;
12             case 'D':
13                 cout<<"Data has been deleted"<<endl; break;
14             case 'S':
15                 cout<<"Data has been sorted"<<endl; break;
16             default :
17                 cout<<"Input error"<<endl;
18         }
19     cout<<"Menu: A(dd) D(elete) S(ort) Q(uit), Select one:";
20     cin>>control;}
21     cout<<"Program end"<<endl;
22     return 0;
23 }

运行截图

2-29  穷举法找1~100间的质数

(1)while

 1 #include <iostream>
 2 #include <iomanip>
 3 #include <cmath>
 4 using namespace std;
 5 
 6 int main(){
 7     int i,j,n,f;
 8     n=0;
 9     i=2;
10     while(i<=100)
11     {f=1;
12         j=2;
13         while(j<=sqrt(i)){
14          if(i%j==0)
15             {f=0;
16              break;}
17             j++;}
18         if(f==1)
19         {cout<<setw(5)<<i;
20             n++;
21             if(n%5==0)
22                 cout<<endl;}
23             i++;}
24     cout<<endl;
25     return 0;
26 }

运行截图

(2)do...while

 1 #include <iostream>
 2 #include <iomanip>
 3 #include <cmath>
 4 using namespace std;
 5 
 6 int main(){
 7     int i,j,n,f;
 8     n=1;
 9     i=2;
10     cout<<setw(5)<<i;
11     do{
12         f=1;
13         j=2;
14         do
15         { if(i%j==0)
16             {f=0;
17              break;}
18             j++;
19         }while(j<=sqrt(i));
20         if(f==1)
21         {cout<<setw(5)<<i;
22             n++;
23             if(n%5==0)
24                 cout<<endl;}
25         i++;}while(i<=100);
26     cout<<endl;
27     return 0;
28 }

(3)for

 1 #include <iostream>
 2 #include <iomanip>
 3 #include <cmath>
 4 using namespace std;
 5 
 6 int main(){
 7     int i,j,n,f;
 8     n=0;
 9     for(i=2;i<=100;i++)
10     {f=1;
11         for(j=2;j<=sqrt(i);j++)
12             if(i%j==0)
13             {f=0;
14              break;}
15         if(f==1)
16         {cout<<setw(5)<<i;
17             n++;
18             if(n%5==0)
19                 cout<<endl;}
20     }
21     cout<<endl;
22     return 0;
23 }

2-32  在1~100内用户猜数字

(1)while

 1 #include <iostream>
 2 #include <cstdlib>
 3 using namespace std;
 4 
 5 int main(){
 6     int number;
 7     unsigned seed;
 8     cout<<"please enter an arbitrary number from 1 to 100 :";
 9     cin>>number;
10     srand(number);
11     seed=rand()%100;
12     while(number!=seed)
13     {if(number<seed)
14         cout<<"The answer is bigger than the input number"<<endl;
15         else
16             cout<<"The answer is smaller than the input number"<<endl;
17      cout<<"please enter an arbitrary number:";
18      cin>>number;
19     }
20     cout<<"you are right!"<<endl;
21     return 0;
22 }

运行截图

(2)do...while

 1 #include <iostream>
 2 using namespace std;
 3 
 4 int main(){
 5     int number;
 6     int answer=44;
 7     do{
 8     cout<<"please enter an arbitrary number from 1 to 100 :";
 9     cin>>number;
10     {if(number<answer)
11         cout<<"The answer is bigger than the input number"<<endl;
12      else if(number>answer)
13         cout<<"The answer is smaller than the input number"<<endl;}
14     }while(number!=answer);
15     cout<<"you are right!"<<endl;
16     return 0;
17 }

运行截图

2-34 5种颜色球中抽3个不同颜色的球

 1 #include <iostream>
 2 using namespace std;
 3 
 4 int main(){
 5     int i,j,k,total=0;
 6     for(i=1;i<=5;i++)
 7         for(j=i+1;j<=5;j++)
 8             for(k=j+1;k<=5;k++)
 9             total++;
10              cout<<total<<endl;
11     return 0;
12 }

运行截图

 

实验总结与体会

  1. break和continue只能用在switch或是循环语句中。
  2. 注意C++与C的一些函数使用的差异,如#include<math.h>(c中),#include<cmath>(C++中),abs与fabs等。
  3. 本次实验熟悉了C++的数据输入与输出与一些基础的算法。
  4. 随机数的运用是薄弱点,还需深入了解相关知识。

 

posted @ 2019-03-18 21:52  糕点点  阅读(443)  评论(3编辑  收藏  举报