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 #include <iostream>
 2 #include <string.h>
 3 using namespace std;
 4 int main ()
 5 {
 6     char a;
 7     while(1)
 8     {
 9     
10         cout<<"Menu: A(dd) D(elete) S(ort) Q(uit),Select one:";
11         cin>>a;
12         if(a=='A')
13         {
14             cout<<"data has been added"<<endl;
15             continue;
16         }
17         else if(a=='D')
18         {
19             cout<<"data has been deleted"<<endl;
20             continue;
21         }
22         else if(a=='S')
23         {
24             cout<<"data has been sorted"<<endl;
25             continue;
26         }
27         else if(a=='Q')
28         {
29             
30             break;
31         }
32         else
33         {
34             cout<<"no such choice concluded"<<endl;
35             continue;
36         }
37     }
38     return 0;
39 }

说一句题外话,原来在C语言中实现循环输入用的是while(scanf("%d",&n)!=EOF)或者scanf("%d",&n);while(n--),但是在C++里没有scanf()函数的用法,所以用while(1)来控制循环输入。

下面是第二个小问的解答:

 1 #include <iostream>
 2 using namespace std;
 3 int main ()
 4 {
 5     char a;
 6     while(1)
 7     {
 8         cout<<"Menu: A(dd) D(elete) S(ort) Q(uit),Select one:";
 9         cin>>a;
10         switch(a)
11         {
12             case 'A':
13             {
14                 cout<<"data has been added"<<endl;
15                 continue;
16             }
17             case 'D':
18             {
19                 cout<<"data has been deleted"<<endl;
20                 continue;
21             }
22             case 'S':
23             {
24                 cout<<"data has been sorted"<<endl;
25                 continue;    
26             }
27             case 'Q':
28             {
29                 break;
30             }
31             default:
32             {
33                 cout<<"no such choice concluded"<<endl;
34                 continue;
35             }
36         }
37         if(a=='Q')break;
38     }
39     return 0;
40 }

2-29:用穷举法找出1~100间的质数并显示出来。分别使用while,do...while,for循环语句实现。

下面是第一个小问的解答:

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

个人认为二三两小问于第一问的区别不大,只是把while()改成do...while()或者for()即可。

我在这里使用了math.h的头文件来使用求根sqrt()函数来提高效率,并用setw()来实现右对齐美化。

2-32:在程序中定义一个整型变量,赋以1~100的值,要求用户猜这个数,比较两个数的大小,把结果提示给用户,直到猜对为止。

 1 #include <iostream>
 2 #include <cstdlib>
 3 using namespace std;
 4 int main ()
 5 {
 6     int thenumber;
 7     int seed;
 8     int guess;
 9      cout<<"please enter an unsigned number:";
10         cin>>seed;
11         srand(seed);
12         thenumber=rand()%100+1;
13     while(1)
14     {
15 
16         cout<<"please guess the number:";
17         cin>>guess;
18         if(guess>thenumber)
19         {
20             cout<<"the answer is smaller than the number you have just given"<<endl;
21             continue;
22         }
23         if(guess<thenumber)
24         {
25             cout<<"the answer is bigger than the number you have just given"<<endl;
26             continue;
27         }
28         if(guess==thenumber)
29         {
30             cout<<"the answer is just been guessed"<<endl;
31             break;
32         }
33     }
34     return 0;
35 }

原本我是自己设置的一个要猜的数,但是不能重复利用,所以用随机数和种子函数,先自己输入一个无符号整数作为种子,这样就能每次都生成不同的随机数。

值得注意的是系统生成的随机数是任意大小的但是题目只要求1-100的数,所以需要把它%100+1。do...while类似,把while改成do...while即可。

2-34:口袋中有红黄蓝白黑5种颜色的球若干个。没词葱口袋中取出3个颜色不同的球,问有多少种取法。

 1 #include <iostream>
 2 
 3 using namespace std;
 4 int main ()
 5 {
 6     int a=5,b=3,i,n,m,s;
 7     n=a,m=b,s=b;
 8     for(i=1;i<s;i++)
 9     {
10         n--;
11         a=a*n;
12     }
13     //cout<<a;
14     for(i=1;i<s;i++)
15     {
16         m--;
17         b=b*m;
18     //    cout<<m;
19     }
20 //    cout<<b;
21     a=a/b;
22     cout<<a<<endl;
23     return 0;
24 }

这个只是求出不考虑顺序的取法总数,而且没有显示是哪几种取法。所以升级求考虑顺序且能显示是哪几种顺序的程序。

 1 #include <iostream>
 2 #include <iomanip>
 3 using namespace std;
 4 void swamp(int & ,int&);
 5 void sperm(int color[],int,int );
 6 int sum;
 7 int main ()
 8 {
 9     int i,color[5],k,m;
10     for(i=0;i<=4;i++)
11         color[i]=i+1;
12     k=0;
13     m=5;
14     sperm(color,k,m);
15     cout<<endl;
16     cout<<sum<<endl;
17 
18     return 0;
19 }
20 void swamp(int &a,int &b)
21 {
22     int t;
23     t=a;
24     a=b;
25     b=t;
26 }
27 void sperm (int color[],int k,int m)
28 {
29     int i;
30     if(k==3)
31     {
32         sum++;
33       for  (i=0;i<=2;i++)
34       {
35          cout<<color[i];
36       }
37       if(sum%5==0)cout<<endl;
38       else cout<<' ';
39     }
40     else
41     {
42         for(i=k;i<m;i++)
43         {
44             swamp(color[k],color[i]);
45             sperm(color,k+1,m);
46             swamp(color[k],color[i]);
47         }
48     }
49 }

这原本是个全排列算法,我是通过学习他人的程序得到的,但是这里不是全排列而是只要把5个中的3个进行排列,所以做了略微的调整,还有我用了数字1-5来代替颜色,进行优化可以用二维字符数组。

实验反思:

1.顺序,循环,条件语句的熟练掌握是学好编程的关键,刚开始不会可以通过模仿,之后自己总结开发出自己的程序设计。

2.swrt(),setw(),srand(),rand()等函数的掌握能让程序设计变得事半功倍,同时有美化作用。

3.对于枚举类型和字符数组的掌握还是不够,要多加练习。

猜你喜欢

转载自www.cnblogs.com/knight04/p/10541527.html