c++简单程序设计 实验一

实验内容:

2-28

实现一个简单的菜单程序,运行时显示“Menu:A(dd) D(elete) S(ort) Q(uit),Selete one:”提示用户输入。A表示增加,D表示删除,

S表示排序,Q表示退出。输入为A、D、S时分别提示“数据已经增加、删除、排序。”,输入Q时程序结束。

以下分别是我用两种语句写出的简单菜单程序:

  • if...else语句
  • #include<iostream>
    #include<cstdlib> 
    using namespace std;
    int main()
    {
        char option;
        while(1)
        {
            cout<<"Menu:A(dd) D(elete) S(ort) Q(uit),Selet one:";
            cin>>option;
            if(option=='A')
            {
                cout<<"data has added"<<endl;
                continue;
            }
            else if(option=='D')
            {
                cout<<"data has deleted"<<endl;
                continue;
            }
            else if(option=='S')
            {
                cout<<"data has sorted"<<endl;
                continue;
            }
            else if(option=='Q')
            break;
            else
            {
                cout<<"There is a mistake in the input,please re-enter."<<endl;
                continue;
            }
        }
    }
  • switch语句
  • #include<iostream>
    #include<cstdlib>
    using namespace std;
    int main()
    {
        char option;
        while(1)
        {
            cout<<"Menu:A(dd) D(elete) S(ort) Q(uit),Selet one:";
            cin>>option;
            switch(option)
            {
            case'A':cout<<"data has added "<<endl;break;
            case'D':cout<<"data has deleted"<<endl;break;
            case'S':cout<<"data has sorted"<<endl;break;
            case'Q':exit(0);break;
            default:cout<<"There is a mistake in the input,please re-enter."<<endl;
            }
        
        }
        return 0;
    }

2-29

用穷举法找出1~100间的质数并显示出来。

这个程序我用了while和for循环两种实现方法。算法设计:用一个数除以2到sqrt这个数,其中只要有一个能被整除,那这个数就不是素数,反之是素数。具体代码如下:

  • while循环
  • #include<iostream>
    #include<math.h>
    using namespace std;
    int main()
    {
        int i,j,k,sign;
        i=2;
        while(i<=100)
        {
            sign=1;
            k=sqrt(i);
            j=2;
            while(j<=k)
            {
                if(i%j==0)
                {
                    sign=0;
                    break;
                }
                j++;
            }
            if(sign)
            cout<<i<<endl;
            i++;
        }
    }
  • for循环
  • #include<iostream>
    #include<math.h>
    using namespace std;
    int main()
    {
        int i,j,k,a;
        for(i=2;i<=100;i++)
        {
            a=1;
            k=sqrt(i);
            for(j=2;j<=k;j++)
            {
                if(i%j==0)
                { 
                a=0;
                break;
                }
            }
            if(a)
            cout<<i<<endl;
        }
    } 

    运行结果如下:

发现这些素数的排列不美观,于是我用setw()函数美化了一下这些数的排列方式,代码及运行结果如下:

#include<iostream>
#include<math.h>
#include<iomanip>
using namespace std;
int main()
{
    int i,j,k,sign,m=0;
    for(i=2;i<=100;i++)
    {
        sign=1;
        k=sqrt(i);
        for(j=2;j<=k;j++)
        {
            if(i%j==0)
            { 
            sign=0;
            break;
            }
        }
        if(sign)
        {
            for(i=2;i<=100;i++)
            {
                m++;
                if(m%6!=0)
                cout<<setw(5)<<i;
                if(m%6==0)
                cout<<setw(5)<<i<<endl;
            }
        }
    }
} 

这个实验题我只用了两种循环语句,do...while和while语句相似,就不拿来占篇幅了。关于这些循环语句不做过多陈述。

2-32

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

这个题目我才开始做的时候是自己在程序里定义了用户要猜的数,这样程序的重复利用率就比较低,先放源码:

  • # include<iostream>
    using namespace std;
    int main()
    {
        int guessnumber=26;
        int m=0;
        while(m!=guessnumber)
        {
            cout<<"please guess the number(1~100):";
            cin>>m;
            if(m>guessnumber)
            cout<<"You guessed a big number"<<endl;
            else if(m<guessnumber)
            cout<<"You guessed a small number"<<endl;
            else
            cout<<"You has guessed the number!"<<endl;
        }
    }

    考虑到程序的实用价值,进行了一次小升级:

  • # include<iostream>
    using namespace std;
    int main()
    {
        int guessnumber=26;
        int m=0;
        while(m!=guessnumber)
        {
            cout<<"please guess the number(1~100):";
            cin>>m;
            if(m>guessnumber)
            {
                int i;
                i=m-guessnumber;
                if(i>=10)
                cout<<"Your guessnumber is too big"<<endl;
                else
                cout<<"Your guessnumber is very close,but it's still a little big"<<endl;
                
            }
            else if(m<guessnumber)
            {
                int j;
                j=guessnumber-m;
                if(j>=10)
                cout<<"Your guessnumber is too small"<<endl;
                else
                cout<<"Your guessnumber is very close,but it's still a little small"<<endl;
            }
            else
            cout<<"You has guessed the number!"<<endl;
        }
    }

    两个程序运行结果如下:

改变不大,但是从甲方考虑,显然2更适合。

在第三次升级的时候,考虑到程序的实用性,我加入了随机数,并允许在用户猜对数字后重开(清屏),給用户更多选择,下面是升级的代码及运行结果:

#include <cstdlib>
#include <iostream>
#include <ctime>
using namespace std;
int main()

{
srand(time(0));
int m= rand() % 100 + 1;//将随机数控制在1-100之间
int guessnumber;//用户输入的数字
char choice;//作出选择,是否继续 
cout<< "Hello,master.My name is Xiao U."<<endl
<<"I had a problems,can you help me?The method is simple."<<endl
<<"Here's a bunch of numbers(0~100),but there's only one right,you should find the right number."<<endl
<<"Starting right now"<<endl;
do{
cout<< "Please enter a number: ";
cin>> guessnumber;
if( guessnumber >m)
{
cout<<"You guessed too high!,it's a pity";
}
if( guessnumber <m)
{
cout<<"You guessed too low!,it's a pity!";
}
if( guessnumber ==m)
{
cout<< "You get it!You have helped me solve the problem.    HaHa,The number is: " <<m<<endl; 
cout<< "Do you want to play again?(Y/N):";
cin>>choice;
if(choice== 'Y')
{
system("cls");//清空屏幕 
cout<<"welcome back,master!"<<endl;
continue;
}
else if(choice== 'N')
{
cout<< "bye,master.";
break;
}
else
{
cout<< "You entered a wrong character! program will exit!";
break;
}
}
}while(1);
system("pause"); 
return EXIT_SUCCESS;//结束程序 
}

2-34

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

代码如下:

#include<iostream> 
using namespace std;
int fac(int x)
{
    int i,j;
    j=1;
    for(i=1;i<=x;i++)
    j*=i;
    return j;
}
int main()
{
    int al;
    int a,b,c,d,e;
    cout<<"Please enter the number of each colour ball" <<endl;
    cout<<"red,yellow,blue,white,black:";
    while(cin>> a>> b>> c>> d>> e)
    {
    al=fac(5)/fac(3)/fac(2)*a*b*c*d*e;
    cout<<"There are"<<al<<"methods to choose three kinds of the balls";
    system("pause");
    }
    return 0;
}

运行结果:

这一题我还写了一个拓展版,将5个球拓展到自定义的情况,但是不符合题目要求,就不放代码了~

实验反思:

  • 2-28这是一个很简单的c++基础编程题,但是关于这一题还是有一些话说,作为一个伪小白,平时会阅读一些别人写的程序,关于if语句,个人很反对把复杂的难以理解的代码语句放到if的条件里面,这会严重影响到代码阅读,尽管很多时候在语法和逻辑上没有问题。if...else语句还要注意的一个问题就是如何用break和continue控制程序流程,这里不多说。关于switch语句,它的目的,学过c/c++的人应该都知道,就是为了实现深层嵌套的if...else逻辑,switch语句我觉得比较重要的就是定义变量方面的问题,switch结构只能在最后一个标号(这个标号可以是case或default)中定义变量。除此以外,没有特别需要说明的地方。
  • 2-32关于最后升级版的程序,我查了关于srand()函数和清屏函数的相关内容,这个升级给用户提供了选择继续还是再开的选择,而且在程序实用性上也得到了加强,相比于前两个,显然这个价值更高。但是程序现在仍然存在一个bug,就是清屏后产生的随机数没有改变,还是和第一次一样,这样就达不到对这串代码的预期。由于这是课堂实验作业,时间有限,暂时先提交,在日后我会抓紧修改,如果有同学有好的想法,欢迎交流。还有就是,我想在这个程序内加一个计数器,记录重开的次数,有兴趣欢迎交流~

猜你喜欢

转载自www.cnblogs.com/sq102217/p/10527710.html