实践练习二

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

首先,使用if···else语句进行编程:

#include<iostream>
using namespace std;

int main()                  //使用if...else语句编译
{
    cout << "Menu:A(dd) D(elete) S(ort) Q(uit),Select one:" ;
    char a;


    while(cin >> a)
    {
        if(a == 'A')
            cout << "数据已经增加" << endl;
        else if(a == 'D')
            cout << "数据已经删除" << endl;
        else if(a == 'S')
            cout << "数据已经排序" << endl;
        else if(a == 'Q')
            break;
    }
    return 0;
}

得到的结果截图为

其次,下面是使用switch进行编程

#include<iostream>
using namespace std;

int main()                  //使用switch语句编译
{
    cout << "Menu:A(dd) D(elete) S(ort) Q(uit),Select one:" ;
    char a;
    while(cin >> a)
    {
        switch(a)
        {
            case 'A':cout << "数据已经增加" << endl;
                    continue;
            case 'D':cout << "数据已经删除" << endl;
                    continue;
            case 'S':cout << "数据已经排序" << endl;
                    continue;
        }
        if(a == 'Q')           //因为break放在switch中只能跳出switch,不能所以放在这里用于跳出while
            break;
    }

    return 0;
}

得到的结果截图为

题2-29:用穷举法找出1-100间的质数并显示

用for循环

#include<iostream>
using namespace std;

int check(int a)                     //该函数用于检查a是否为质数,若是则返回1,否则返回0
{
    for(int j = 2;j < a;j++)
    {
        if(a % j == 0)               //满足if则数a不是质数
        {
            return 0;
        }
    }
    return 1;
}

int main()
{
    for(int i = 2;i <= 100;i++)
    {
        if(check(i))
            cout << i << "  ";
    }
    return 0;
}

得到的代码截图为

用while循环

#include<iostream>
using namespace std;

int check(int a)                     //该函数用于检查a是否为质数,若是则返回1,否则返回0
{
    int j = 2;
    while(j < a)
    {
        if(a % j == 0)               //满足if则数a不是质数
        {
            return 0;
        }
        ++j;
    }
    return 1;
}

int main()
{
    int i = 2;
    while(i <= 100)
    {
        if(check(i))
            cout << i << "  ";
        ++i;
    }
    return 0;
}

得到的代码截图为

使用do···while循环

#include<iostream>
using namespace std;

int check(int a)                     //该函数用于检查a是否为质数,若是则返回1,否则返回0
{
    int j = 2;
    do{
        if(a % j == 0)               //满足if则数a不是质数
        {
            if(a == j)               //这个if是专门为质数2准备的
                return 1;
            return 0;
        }
        ++j;
    }while(j < a);
    return 1;
}

int main()
{
    int i = 2;
    do{
        if(check(i))
            cout << i << "  ";
        ++i;
    }while(i < 100);
    return 0;
}

得到的截图为

题2-32:在程序中定义一个0-100的整形变量,要求用户猜这个数

使用while循环

#include<iostream>
#include<ctime>
#include<cstdlib>
using namespace std;

int main()
{
    srand(time(0));                                      //这里用于产生一个随机数
    int number=rand()%10;

    cout<<"猜测一个在0到100间(含0和100)的数字";

    int guess=101;
    while (guess!=number)
    {
        cout<<"\n输入你的猜测:";
        int guess;
        cin>>guess;

        if(guess==number)
        {
            cout<<"恭喜你答对了,这个数字就是"<<number;
            break;
        }
        else if(guess>number)
          cout<<"你猜测的数字大了";
        else
          cout<<"你猜测的数字小了";
    }

    return 0;
 }

结果截图

使用do···while循环

#include<iostream>
#include<ctime>
#include<cstdlib>
using namespace std;
int main()
{
    srand(time(0));                                      //这里用于产生一个随机数
    int number=rand()%10;

    cout<<"猜测一个在0到100间(含0和100)的数字";

    int guess=101;
    do{
        cout<<"\n输入你的猜测:";
        int guess;
        cin>>guess;

        if(guess==number)
        {
            cout<<"恭喜你答对了,这个数字就是"<<number;
            break;
        }
        else if(guess>number)
          cout<<"你猜测的数字大了";
        else
          cout<<"你猜测的数字小了";
    } 
    while (guess!=number);
    return 0;
 }

结果截图

题2-34:口袋中有五种不同颜色的球若干,每次取3个不同颜色的球,问有多少种不同的取法

当取得球没有顺序的时候

#include<iostream>
using namespace std;

int main()
{
    int number = 0;                         //number用于记录有多少种取法 
    for(int i = 0;i < 5;++i)                //摸第一个球 
    {
        for(int j = 0;j < i;++j)            //摸第二个球 ,并且保证不会出现重复的情况 
        {
            if(j == i)                      //确保第一个球和第二个球不重复 
                continue;
            for(int m = 0;m < j;++m)        //摸第三个球 ,并且保证不会出现重复的情况 
            {
                if(m == i || m == j)        //确保第三个球和第一二个球不重复 
                    continue;
                ++number;
                cout << "i=" << i << "  " << "j=" << j << "  " << "m=" << m <<endl;
            }
        }
    }
    cout << number <<endl;
    return 0;
}

结果截图

当取得的球有顺序的时候

#include<iostream>
using namespace std;

int main()
{
    int number = 0;                         //number用于记录有多少种取法 
    for(int i = 0;i < 5;++i)                //摸第一个球 
    {
        for(int j = 0;j < 5;++j)            //摸第二个球 
        {
            if(j == i)                      //确保第一个球和第二个球不重复 
                continue;
            for(int m = 0;m < 5;++m)        //摸第三个球 
            {
                if(m == i || m == j)        //确保第三个球和第一二个球不重复 
                    continue;
                ++number;
                cout << "i=" << i << "  " << "j=" << j << "  " << "m=" << m <<endl;
            }
        }
    }
    cout << number <<endl;
    return 0;
}

结果截图

posted @ 2018-03-25 19:58  这名必没人用  阅读(140)  评论(2编辑  收藏  举报