• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

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

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

实验结论

编程练习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-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-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-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威力加强版运行截图:

实验总结与体会

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

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


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C#文件压缩加解密发布时间:2022-07-13
下一篇:
C++Builder操作MYSQL中文乱码的解决方案发布时间:2022-07-13
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap