实验一:c++简单程序设计(1)

实验结论

编程练习2-28

switch版源码:

#include <iostream>
using namespace std;

int main(void)
{
    cout << "Menu:A(dd) D(elete) S(ort) Q(uit)" << endl;
    while (1)
    {
        char choice;
        cin >> choice;
        switch (choice)
        {
        case 'A':
            cout << "Data has been added" << endl;
            break;
        case 'D':
            cout << "Data has been deleted" << endl;
            break;
        case 'S':
            cout << "Data has been sorted" << endl;
            break;
        }
        if(choice=='Q')
            break;
    }
}

if版源码:

#include <iostream>
using namespace std;

int main(void)
{
    cout << "Menu:A(dd) D(elete) S(ort) Q(uit)" << endl;
    while (1)
    {
        char choice;
        cin >> choice;
        if(choice=='A')
            cout << "Data has been added" << endl;
        if(choice=='D')
            cout << "Data has been deleted" << endl;
        if(choice=='S')
            cout << "Data has been sorted" << endl;
        if(choice=='Q')
            break;
    }
}

2-28运行截图:
2-28

编程练习2-29

do_while版源码:

#include <iostream>
using namespace std;

int main(void)
{
    int prime = 2;
    do
    {
        int i = 2;
        do
        {
            if (prime % i == 0)
                break;
            i++;
        } while (i <= prime);
        if (prime == i)
            cout
                << prime << endl;
        prime++;
    } while (prime <= 100);
    cin.get();
}

while版源码

#include <iostream>
using namespace std;

int main(void)
{
    int prime=2;
    while (prime <= 100)
    {
        int i=2;
        while (i <= prime)
        {
            if (prime % i == 0)
                break;
            i++;
        }
        if (prime == i)
            cout << prime << endl;
        prime++;
    }
    cin.get();
}

for版源码:

#include <iostream>
using namespace std;

int main(void)
{
    int prime;
    for (prime = 2; prime <= 100; prime++)
    {
        int i;
        for (i = 2; i <= prime; i++)
        {
            if (prime % i == 0)
                break;
        }
        if (prime == i)
            cout << prime << endl;
    }
    cin.get();
}

2-29运行截图:
2-29

编程练习2-32

因为程序全程只有while(1)循环内容 do while与while没有什么差异 故只写了一版
源码:

#include <ctime>
#include <iostream>
#include <random>
#include <windows.h>
using namespace std;

int main(void)
{
    cout << "Game Start" << endl;
    cout << "Enter\"0\" to exit." << endl;
    cout << "Have Fun!" << endl;
    Sleep(2000);      //暂停2秒
    while (1)
    {
        mt19937 gen(time(NULL));                //用时间作为种子
        uniform_int_distribution<> dis(1, 100); //设定随机数的范围为1~100
        int num2gess = dis(gen);
        int numUgess;
        cout << "Please enter the number(1~100) you gess" << endl;
        while (1)
        {
            cin >> numUgess;
            if (numUgess == 0)
                break;
            if (numUgess < num2gess)
                cout << "The number you gess is too small! Try again!" << endl;
            else if (numUgess == num2gess)
            {
                cout << "Bingo! Congratulation!" << endl;
                break;
            }
            else
                cout << "The number you gess is too large! Try again!" << endl;
        }
        if (numUgess == 0)
            break;
    }
}

2-32运行截图:
2-32

编程练习2-34

采用顺序无关的写法,并稍加拓展

//原题实现太简单 现将程序改为从m个颜色不同的小球中取n个(n<=m<=20)
#include <iostream>

using namespace std;
long long int fac(long long int num) //阶乘函数
{
    long long int r = 1, i;
    for (i = 1; i <= num; i++)
    {
        r *= i;
    }
    return r;
}

int main(void)
{
    long long int Ball, Take;
    cout << "Please enter the number of balls  and how many balls you want to take." << endl;
    cout << "Enter\"0\" to exit" << endl;
    while (1)
    {
        cin >> Ball;
        if (Ball == 0)
            break;
        cin >> Take;
        cout << "There are " << fac(Ball) / fac(Take) / fac(Ball - Take) << " possibilities." << endl;
    }
}

2-34威力加强版运行截图:
2-34

实验总结与体会

  • 这四道编程练习题带我们回顾了c++中与c一样的语法知识,并使用了c++中新添加的特性,如:新添加的输入输出流函数与新的头文件。
  • 同时我在翻阅往届学长学姐的blog时发现了很多新的知识,万能头文件#include<bits/stdc++.h>在这里推荐给大家
  • 我在做编程练习2-23时查阅了一些资料发现在c++11标准中新加了uniform_int_distribution函数用于生成整数随机数,用法我会贴在下方的引用栏中

cppreference:uniform_int_distribution
CSDN:用时间作为生成随机数的种子
CSDN:如何在c++中暂停特定时间

posted @ 2019-03-11 17:40  KOKODA  阅读(857)  评论(4编辑  收藏  举报