Demo005 小学四则运算自动生成程序

小学四则运算自动生成程序



0.传送门

领航员: 嚯唶

Coding:Rst321


1.题目要求

本次作业要求两个人合作完成,驾驶员和导航员角色自定,鼓励大家在工作期间角色随时互换,这里会布置两个题目,请各组成员根据自己的爱好任选一题。

  • 题目1:

    我们在刚开始上课的时候介绍过一个小学四则运算自动生成程序的例子,请实现它,要求:
    • [x] 能够自动生成四则运算练习题
    • [x] 可以定制题目数量
    • [x] 用户可以选择运算符
    • [x] 用户设置最大数(如十以内、百以内等)
    • [x] 用户选择是否有括号、是否有小数
    • [x] 用户选择输出方式(如输出到文件、打印机等)
    • [x] 最好能提供图形用户界面(根据自己能力选做,以完成上述功能为主)

2.功能实现

2.1 总体设计

头文件:

/*#include<bits/stdc++.h>*/
#include <iostream>
#include <cstdio>
#include <cerrno>
#include <ctime>
#include <cstdlib>
#include <fstream>
#include <string>
using namespace std;

预处理块:

#define FILE_PATH "D:/Demo.txt"  // 文件路径

版权声明:

PS:在 codeblocks 不要使用©符号,否则.......

/**
 * Copyright © Ryanjie
 *
 * @program: Demo05 Arithmetic
 * @description: Arithmetic
 * @author: Ryanjie 嚯唶
 * @create: 2018-04-11 18:59
 *
 * version 1.0
 **/

全局变量:

char Arithmetic_brackets[4] = {'(' , ' ' , ')' , ' '}; //括号
char Arithmetic_operators[4] = {'+' , '-' , '*' , '/'}; //运算符
const int Arithmetic_operation = 4; //生成随机运算符
const int Arithmetic_bracket = 2; //生成随机括号
int Arithmetic_number; //题目数目
int Arithmetic_max; //最大数
int Arithmetic_iffile;   //确定是否打印
int Arithmetic_ifdecimal;  //确定是否有小数
int Arithmetic_ifbrackets;  //确定是否有括号
char* dt;   //当地时间
FILE *fp;   //文件地址

函数及功能 :

void showMenu();    //欢迎界面
void showExit();    //退出界面
void Arithmetic_Output_Screen();//屏幕输出函数
void Arithmetic_Output_File();  //文本输出函数
void getTime(); //获取日期和时间

核心:随机生成函数rand()。关于rand() 的使用请看:rand与srand函数的使用 ,详细解释请查询维基百科。

2.2 用户欢迎界面

程序是让其他人使用,而良好的界面能大大提高他人使用的效率。

程序欢迎界面

Demo005 Arithmetic-Welcome

函数如下:

void showMenu()
{
    system("title 小学四则运算自动生成程序@Ryanjie@嚯唶");
    system("mode con cols=90");//改变控制台宽度
    system("color 2f");//改变背景颜色
    system("echo.");

    getTime();

    system("echo.");
    cout << "※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※" << endl;
    cout << "※                                                                                    ※" << endl;
    cout << "※                欢迎您使用小学四则运算自动生成程序                                  ※" << endl;
    cout << "※                                                                                    ※" << endl;
    cout << "※                                                                                    ※" << endl;
    cout << "※                             version: 1.0                                          ※" << endl;
    cout << "※                                                                                    ※" << endl;
    cout << "※                             author: Ryanjie 嚯唶                                   ※" << endl;
    cout << "※                                                                                    ※" << endl;
    cout << "※                                                                                    ※" << endl;
    cout << "※                                                                                    ※" << endl;
    cout << "※                                                                                    ※" << endl;
    cout << "※                                                                                    ※" << endl;
    cout << "※                                                                                    ※" << endl;
    cout << "※                请您按照步骤来生成四则运算练习题:                                  ※" << endl;
    cout << "※                                                                                    ※" << endl;
    cout << "※                第1步:请设置题目数量 <1-100>                                       ※" << endl;
    cout << "※                第2步:请设置最大数 <1-1000>                                        ※" << endl;
    cout << "※                第3步:请选择是否有小数                                             ※" << endl;
    cout << "※                第4步:请选择是否有括号                                             ※" << endl;
    cout << "※                第5步:请选择是否打印到文件                                         ※" << endl;
    cout << "※                                                                                    ※" << endl;
    cout << "※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※" << endl;
    system(" echo.");
}

2.3 用户功能界面

用户按需求设置相关参数,程序每个参数设置好临界点,并判断其是否越界,函数如下:


void menu1()
{
    system(" echo.");
    showMenu();
    cout << "第1步:请设置题目数量 <1-100> :" << endl;
    cin >> Arithmetic_number;
    while((Arithmetic_number > 100) || (Arithmetic_number < 1 ))
    {
        cout << "您设置的题目数目不符合要求(太多/太少)。 < 1 - 100 > " << endl;
        cout << endl;
        cout << "请按确认键重新输入,谢谢!" << endl;
        system("Pause >nul");
        cin >> Arithmetic_number;
    }
}

void menu2()
{
    system("echo.");
    cout << "第2步:请设置最大数 <1-1000> :" << endl;
    cin>>Arithmetic_max;
    while((Arithmetic_max > 1000) || (Arithmetic_max < 1 ))
    {
        cout << "您设置的最大数不符合要求(太大/太小)。 < 1 - 1000 > " << endl;
        cout << endl;
        cout << "请按确认键重新输入,谢谢!" << endl;
        system("Pause >nul");
        cin >> Arithmetic_max;
    }
}

void menu3()
{
    system("echo.");
    cout << "第3步:请选择是否有小数:(输入 <0> 生成整数 , 输入 <1> 生成小数) "<<endl;
    cin>>Arithmetic_ifdecimal;
    while((Arithmetic_ifdecimal !=0 ) && (Arithmetic_ifdecimal != 1 ))
    {
        cout << "您输入的数不符合要求。(输入 <0> 生成整数 , 输入 <1> 生成小数) " << endl;
        cout << endl;
        cout << "请按确认键重新输入!" << endl;
        system("Pause >nul");
        cin >> Arithmetic_ifdecimal;
    }
}

void menu4()
{
    system("echo.");
    cout << "第4步:请选择是否有括号:(输入 <0> 无括号 , 输入 <1> 有括号)" << endl;
    cin>>Arithmetic_ifbrackets;
    while((Arithmetic_ifbrackets !=0 ) && (Arithmetic_ifbrackets != 1 ))
    {
        cout << "您输入的数不符合要求。(输入 <0> 无括号 , 输入 <1> 有括号) " << endl;
        cout << endl;
        cout << "请按确认键重新输入!" << endl;
        system("Pause >nul");
        cin >> Arithmetic_ifbrackets;
    }
}

void menu5()
{
    system("echo.");
    cout << "第5步:请选择是否打印到文件:(输入 <0> 不打印(屏幕显示) , 输入 <1> 打印)" << endl;
    cin>>Arithmetic_iffile;
    while((Arithmetic_iffile !=0 ) && (Arithmetic_iffile != 1 ))
    {
        cout << "您输入的数不符合要求。(输入 <0> 不打印(屏幕显示) , 输入 <1> 打印) " << endl;
        cout << endl;
        cout << "请按确认键重新输入!" << endl;
        system("Pause >nul");
        cin >> Arithmetic_iffile;
    }
}


void menu()
{
    menu1();
    menu2();
    menu3();
    menu4();
    menu5();
}

2.4 屏幕输出

屏幕输出:

Demo005 Arithmetic-Output_Screen

函数如下:


void Arithmetic_Output_Screen()
{
    cout <<"+----------------以下为*小学四则运算自动生成程序*所生成的四则运算练习题----------------+" << endl;

    for(int i=0; i<Arithmetic_number; ++i)
    {
        /*随机生成四个整数*/
        int number1 = rand() % Arithmetic_max;
        int number2 = rand() % Arithmetic_max;
        int number3 = rand() % Arithmetic_max;
        int number4 = rand() % Arithmetic_max;

        /*随机生成四个小数*/
        float number5 = (float)rand() / Arithmetic_max;
        float number6 = (float)rand() / Arithmetic_max;
        float number7 = (float)rand() / Arithmetic_max;
        float number8 = (float)rand() / Arithmetic_max;

        /*随机生成三个运算符*/
        int operation1 = rand() % Arithmetic_operation;
        int operation2 = rand() % Arithmetic_operation;
        int operation3 = rand() % Arithmetic_operation;
        char cur_operation1 = Arithmetic_operators[operation1];
        char cur_operation2 = Arithmetic_operators[operation2];
        char cur_operation3 = Arithmetic_operators[operation3];

        /*随机产生括号()*/
        int barcket1 = rand() % Arithmetic_bracket;
        char cur_barckets1 = Arithmetic_brackets[barcket1];
        char cur_barckets2 = Arithmetic_brackets[barcket1+2];

        if(Arithmetic_ifdecimal)
        {
            if(Arithmetic_ifbrackets)
            {
                cout << "NO." << i << " : "<< cur_barckets1 << number5 << " " << cur_operation1 << " " << number6 << cur_barckets2 << " " << cur_operation2 << " " << number7 << " " << cur_operation3 << " " << number8 << "=" << endl;
            }
            else
            {
                cout << "NO." << i << " : "<< number5 << " " << cur_operation1 << " " << number6 << " " << cur_operation2 << " " << number7 << " " << cur_operation3 << " " << number8 << "=" << endl;
            }
        }
        else
        {
            if(Arithmetic_ifbrackets)
            {
                cout << "NO." << i << " : "<< cur_barckets1 << number1 << " " << cur_operation1 << " " << number2 << cur_barckets2 << " " << cur_operation2 << " " << number3 << " " << cur_operation3 << " " << number4 << "=" << endl;
            }
            else
            {
                cout << "NO." << i << " : "<< number1 << " " << cur_operation1 << " " << number2 << " " << cur_operation2 << " " << number3 << " " << cur_operation3 << " " << number4 << "=" << endl;
            }
        }
    }

    cout << "+--------------------------------------------------------------------------------------+" << endl;

}

2.5 文本输出

由于原始文件里已经存在上一次打印的四则运算练习题,所以写入文件需要使用追加命令

    fp = fopen(FILE_PATH, "at+");

函数如下:

void Arithmetic_Output_File()
{
    cout << Arithmetic_number <<endl;
    fp = fopen(FILE_PATH, "at+");
    if (fp != NULL)
    {
        fprintf( fp ,"\n");
        fprintf( fp,"+----------------以下为*小学四则运算自动生成程序*所生成的四则运算练习题----------------+\n");
        time_t now = time(0);
        char* dt = ctime(&now);
        tm *gmtm = gmtime(&now);
        dt = asctime(gmtm);
        fprintf( fp ,"                                                UTC 日期和时间:%s \n" , dt );

        for(int i = 0; i < Arithmetic_number; ++i)
        {
            /*随机生成四个整数*/
            int number1 = rand() % Arithmetic_max;
            int number2 = rand() % Arithmetic_max;
            int number3 = rand() % Arithmetic_max;
            int number4 = rand() % Arithmetic_max;

            /*随机生成四个小数*/
            float number5 = (float)rand() / Arithmetic_max;
            float number6 = (float)rand() / Arithmetic_max;
            float number7 = (float)rand() / Arithmetic_max;
            float number8 = (float)rand() / Arithmetic_max;

            /*随机生成三个运算符*/
            int operation1 = rand() % Arithmetic_operation;
            int operation2 = rand() % Arithmetic_operation;
            int operation3 = rand() % Arithmetic_operation;
            char cur_operation1 = Arithmetic_operators[operation1];
            char cur_operation2 = Arithmetic_operators[operation2];
            char cur_operation3 = Arithmetic_operators[operation3];

            /*随机产生括号()*/
            int barcket1 = rand() % Arithmetic_bracket;
            char cur_barckets1 = Arithmetic_brackets[barcket1];
            char cur_barckets2 = Arithmetic_brackets[barcket1+2];
            if(Arithmetic_ifdecimal)  //判断是否有小数
            {
                if(Arithmetic_ifbrackets)   //判断是否有括号
                {
                    fprintf( fp, "NO. %2d : %c %.2f %c %.2f %c %c %.2f %c %.2f = \n" ,i, cur_barckets1 , number5 , cur_operation1 , number6 , cur_barckets2 , cur_operation2 , number7 , cur_operation3 , number8 );
                    //fprint( << "NO." << i << " : "<< cur_barckets1 << number5 << " " << cur_operation1 << " " << number6 << cur_barckets2 << " " << cur_operation2 << " " << number7 << " " << cur_operation3 << " " << number8 << "=" << endl;
                }
                else
                {
                    fprintf( fp,"NO. %2d : %.2f %c %.2f %c %.2f %c %.2f = \n" ,i, number5 , cur_operation1 , number6, cur_operation2 , number7 , cur_operation3 , number8 );
                    //fprint( << "NO." << i << " : "<< number5 << " " << cur_operation1 << " " << number6 << " " << cur_operation2 << " " << number7 << " " << cur_operation3 << " " << number8 << "=" << endl;
                }
            }
            else
            {
                if(Arithmetic_ifbrackets)
                {
                    fprintf( fp,"NO. %2d : %c %d %c %d %c %c %d %c %d = \n" ,i, cur_barckets1 , number1 , cur_operation1 , number2 , cur_barckets2 , cur_operation2 , number3 , cur_operation3 , number4 );
                    //fprint( << "NO." << i << " : "<< cur_barckets1 << number1 << " " << cur_operation1 << " " << number2 << cur_barckets2 << " " << cur_operation2 << " " << number3 << " " << cur_operation3 << " " << number4 << "=" << endl;
                }
                else
                {
                    fprintf( fp,"NO. %2d : %d %c %d %c %d %c %d = \n" ,i, number1 , cur_operation1 , number2, cur_operation2 , number3 , cur_operation3 , number4 );
                    //fprint( << "NO." << i << " : "<< number1 << " " << cur_operation1 << " " << number2 << " " << cur_operation2 << " " << number3 << " " << cur_operation3 << " " << number4 << "=" << endl;
                }
            }
        }
    }
    else
    {
        perror(FILE_PATH);
        exit(-1);
    }
    fprintf( fp,"+--------------------------------------------------------------------------------------+\n");

    fprintf( fp, "\n");
}

2.6 获取时间

获取UTC 日期和时间,函数如下:

void getTime()
{
    system("cls");
    system("echo.");
    time_t now = time(0);
    char* dt = ctime(&now);
    tm *gmtm = gmtime(&now);
    dt = asctime(gmtm);
    cout << "UTC 日期和时间:" << dt << endl;
    cout << "本地日期和时间:" << dt << endl;
    system("echo.");
}

2.7 用户退出界面

用户退出界面

Demo005 Arithmetic-ShowExit

函数如下:

void showExit()
{
    getTime();
    cout<<"☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★"<<endl;
    cout<<"★                                                                                    ☆"<<endl;
    cout<<"☆       恭喜您,四则运算练习题已经成功生成!                                         ★"<<endl;
    cout<<"★                                                                                    ☆"<<endl;
    cout<<"☆       谢谢您的使用,欢迎您下次再来!                                               ★"<<endl;
    cout<<"★                                                                                    ☆"<<endl;
    cout<<"☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★"<<endl;
    cout << "请按确认键退出!" << endl;
    system("Pause >nul");
}

3. 评价&总结

邹欣老师在《现代软件工程讲义 3 结对编程和两人合作》一文中提到:

在结对编程模式下,一对程序员肩并肩地、平等地、互补地进行开发工作。两个程序员并排坐在一台电脑前,面对同一个显示器,使用同一个键盘,同一个鼠标一起工作。他们一起分析,一起设计,一起写测试用例,一起编码,一起单元测试,一起集成测试,一起写文档等。

我们把结对编程中两位合作者的关系看作驾驶员和领航员,其中:

  • 驾驶员(Driver)是控制键盘输入的人
  • 领航员(Navigator)起到领航、提醒的作用

本次结对编程中的两位合作者:

  • 驾驶员:Ryanjie(我)
  • 领航员:嚯唶

在本次结对编程中,我担任驾驶员(Driver)工作,主要完成代码编写工作,而嚯唶在此次作业中担任领航员(Navigator)工作,它主要提醒我以及对代码的单元测试。这次作业对于我来说这是一次全新的体验,两个人将更容易发现问题,也更容易想出更好的解决方案。

此次结对编程中,嚯唶在很多地方给我提供了准备工作以及思路(像我么这样底子比较薄弱的很难编出来这个程序)。此外,他还设计出了合理的测试用例,将此次编写的代码进行了合理的测试,同时指出了我在编写代码时存在的一些问题,改进了我的编程方法,为以后编程积累了宝贵的经验。结对编程可以提高代码的质量,并且在审查过程中可以及时吸取建议进而增进整体的质量。这次结对编程过程相比一个人编程来说也不那么枯燥了,可以互相讨论问题,一同陷入沉思。

总体来说,我们这一次作业较好的完成了预定的要求,实现了基本的功能,完成了结对编程的功能。由于时间的原因(要准备考验),所以没有使用界面。结对编程培养了我们的协助能力,问题解决能力,希望在之后学习生活中,能够有较多这样互相学习、互相进步的机会,在以后的学习过程中希望有机会好好的体验结对编程的各个不同阶段。

最后,写代码时一定要及时保存代码.!!!


合影

1342942-20180417135601728-283677351.jpg

Coding地址 Ryanjie/Arithmetic
图片看不见的请看: Ryanjie/Arithmetic/ScreenShots/

转载于:https://www.cnblogs.com/ryanjan/p/8832384.html

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值