首页 > 技术文章 > 四则运算3

littilsaber 2015-03-22 15:37 原文

一.要求:

    1.让程序能接受用户输入答案,并判定对错。最后给出总共对/错 的数量。

二.思路: 

   定义变量c1,c2并初始化,每出一道题就答一道题,如果正确c1++,如果错误c2++,再输出c1和c2的值

三.代码: 

#include <iostream>
using namespace std;
#include <math.h>
#include <time.h>

int c1=0;                    //初始化计数正误的个数
int c2=0;

int display1(int z, int x, int y, int i, int num) //有乘除 
{
    double jieguo; 
    if (i==0) 
    {
        cout << "(" << z<< ")" << x << " + " << y << " = ";
        cin >> jieguo;
        if (jieguo==x+y) //判断正误
        {
            cout << " 回答正确!" << " 正确题数为:" << c1 << " 错误题数为:" << c2 << endl;
            return c1++;

        }
        else
        {
            cout << " 回答错误!" << " 正确题数为:" << c1 << " 错误题数为:" << c2 << endl;
            return c2++;

        }
    }

    if (i== 1)
    {
        cout << "(" << z << ")" << x << " - " << y << " = ";
        cin >> jieguo;
        if (jieguo == x - y)
        {
            cout << " 回答正确!" << " 正确题数为:" << c1 << " 错误题数为:" << c2 << endl;
            return c1++;

        }
        else
        {
            cout << " 回答错误!" << " 正确题数为:" << c1 << " 错误题数为:" << c2 << endl;
            return c2++;

        }
    }
    if (i== 2)
    {
        cout << "(" << z << ")" << x << " x " << y << " = ";
        cin >> jieguo;
        if (jieguo == x*y)
        {
            cout << " 回答正确!" << " 正确题数为:" << c1 << " 错误题数为:" << c2 << endl;
            return c1++;

        }
        else
        {
            cout << " 回答错误!" << " 正确题数为:" << c1 << " 错误题数为:" << c2 << endl;
            return c2++;

        }
    }
    if (i == 3)
    {
        if (y == 0)
        {
            do
            {
                while (y != 0)
                {
                    y = rand() % (num + 1);
                }
            }
            while (y == 0);
        }
        else
        {
            cout << "(" << z << ")" << x << " / " << y << " = ";
            cin >> jieguo;
            if (jieguo == x*y)
            {
                cout << " 回答正确!" << " 正确题数为:" << c1 << " 错误题数为:" << c2 << endl;
                return c1++;

            }
            else
            {
                cout << " 回答错误!" << " 正确题数为:" << c1 << " 错误题数为:" << c2 << endl;
                return c2++;

            }
        }
    }
}

int display2(int z, int x, int y, int i, int num) //含有加减
{
    double jieguo;
    if (i == 0)
    {
        cout << "(" << z<< ")" << x << " + " << y << " = ";
        cin >> jieguo;
        if (jieguo == x + y)
        {
            cout << " 回答正确!" << " 正确题数为:" << c1 << " 错误题数为:" << c2 << endl;
            return c1++; 
        }
        else
        {
            cout << " 回答错误!" << " 正确题数为:" << c1 << " 错误题数为:" << c2 << endl;
            return c2++; 
        }
    }
    if (i == 1)
    {
        cout << "(" << z << ")" << x << " + " << y << " = ";
        cin >> jieguo;
        if (jieguo == x + y)
        {
            cout << " 回答正确!" << " 正确题数为:" << c1 << " 错误题数为:" << c2 << endl;
            return c1++;    
        }
        else
        {
            cout << " 回答错误!" << " 正确题数为:" << c1 << " 错误题数为:" << c2 << endl;
            return c2++;
        }
    }

}

int main()
{
    int number; //题的份数
    int xuanze; //做判断是否有乘除法
    int num; //数值范围
    int fushu; //减法结果是否存在负数的情况 
    int yushu; //表示运算结果有无余数
    int geshu; //表示每行打印的运算式的个数
    int a[30], b[30], m1[30], m2[30];

    srand((unsigned)time(NULL));
    cout << endl;
    cout << " 四则运算出题系统 " << endl;
    cout << endl;
    cout << " 请按要求输入控制条件!" << endl;
    cout << " 份数(tip:正整数):";
    cin >> number;
    cout << endl;
    cout << " 数值范围(tip:必须输入一个正整数,如:99表示范围为0~99):";
    cin >> num;
    cout << endl;
    cout << " 是否有乘除法(tip:必须输入0/1,0表示无,1表示有): ";
    cin >> xuanze;
    cout << endl;
    cout << " 结果是否有负数(tip:必须输入0/1,0表示无,1表示有):";
    cin >> fushu;
    cout << endl;
    cout << " 除法结果有无余数(tip:必须输入0/1,0表示无,1表示有0,):" ;
    cin >> yushu;
    cout << endl;
    for (int k = 1; k <= number; k++)
    {
        cout << "______________________________________________________________________________" << endl;
        cout << endl;
        cout << "" << k << "份题如下:" << endl;
        cout << "______________________________________________________________________________" << endl;
        cout << endl;
        if (xuanze == 1)
        {
            cout << "_____________________________________________________________________________" << endl;
            cout << endl;

            cout << "______________________________________________________________________________" << endl;
            cout << endl;
            for (int i = 1; i <= 30; i++)
            {
                a[i] = rand() % (num + 1);
                b[i] = rand() % (num + 1);
                m1[i] = rand() % 4;

                for (int j = 1; j <= i - 1; j++)
                {
                    if (a[i] == a[j] && b[i] == b[j] && m1[i] == m1[j])
                    {
                        do
                        {
                            a[i] = rand() % (num + 1);
                            b[i] = rand() % (num + 1);
                        } 
                        while (a[i] == a[j] && b[i] == b[j] && m1[i] == m1[j]);
                    }
                }
                if ((fushu == 1 && m1[i] == 1) || (yushu == 1 && m1[i] == 3))
                {
                    display1(i, a[i], b[i], m1[i], num);
                }
                else if (yushu == 0 && m1[i] == 3)
                {
                    if (a[i] % b[i] == 0)
                    {
                        display1(i, a[i], b[i], m1[i], num);
                    }
                    else
                    {
                        do
                        {
                            a[i] = rand() % (num + 1);
                            b[i] = rand() % (num + 1);
                            while (b[i] == 0)
                            {
                                b[i] = rand() % (num + 1);
                            }
                        } 
                        while (a[i] % b[i] != 0);
                            display1(i, a[i], b[i], m1[i], num);
                    }
                }
                else if (fushu == 0 && m1[i] == 1)
                {
                    if (a[i] >= b[i])
                    {
                        display1(i, a[i], b[i], m1[i], num);
                    }
                    else
                    {
                        do
                        {
                            a[i] = rand() % (num + 1);
                            b[i] = rand() % (num + 1);
                        } 
                        while (a[i]<b[i]);
                            display1(i, a[i], b[i], m1[i], num);
                    }
                }
                else
                {
                    display1(i, a[i], b[i], m1[i], num);
                }
                if (i%geshu == 0)
                {
                    cout << endl;
                }
                if (i == 30)
                {
                    cout << endl;
                }
            }
        }
        else if (xuanze == 0) //没有乘除法存在,只需要判断有无负数结果产生
        {
            cout << "___________________________________________________________________" << endl;
            cout << endl;

            for (int i = 1; i <= 30; i++)
            {
                a[i] = rand() % (num + 1);
                b[i] = rand() % (num + 1);
                m2[i] = rand() % 2;

                for (int j = 1; j <= i - 1; j++)
                {
                    if (a[i] == a[j] && b[i] == b[j] && m2[i] == m2[j])
                    {
                        do
                        {
                            a[i] = rand() % (num + 1);
                            b[i] = rand() % (num + 1);
                        } 
                        while (a[i] == a[j] && b[i] == b[j] && m2[i] == m2[j]);
                    }
                }
                if (fushu == 0)
                {
                    if (a[i] >= b[i])
                    {
                        display2(i, a[i], b[i], m2[i], num);
                    }
                else 
                {
                    do
                    {
                        a[i] = rand() % (num + 1);
                        b[i] = rand() % (num + 1);
                    } 
                    while (a[i]<b[i]);
                        display2(i, a[i], b[i], m2[i], num);
                }
            }
            else
            {
                display2(i, a[i], b[i], m2[i], num);
            }
            if (i%geshu == 0)
            {
                cout << endl;
            }
            if (i == 30)
            {
                cout << endl;
            }
        }
    }
    else
    {    
        cout << endl;
        cout << " Sorry! 输入错误,请按要求重输!" << endl;
        cout << endl;
        cout << endl;
        k = k - 1;
    }

  }
return 0;
}

 

 

 

四.结果截图: 

 

五.总结: 

经过这次四则运算的实践,让我学到了更多知识,也增强了自己的动

手能力,同时也看到了自身的不足,在这次实践中虽然我只做了其中一

部分,但是体味到了其中的乐趣。我会在以后的学习和生活中努力思

考,善于发现和解决问题,开动脑筋,去发现更多新的东西。

六.PSPO过程文档:

 一.项目计划总结

                    周活动总结表

姓名:孔维春                                       日期:2015.3.22

日期       任务

听课

编写程序

阅读课本

准备考试

 

 

日总计

周日

 

 

 

 

 

 

 

周一

 

 

 

 

 

 

 

周二

 100

 30

 10

 

 

 

 140

周三

 

 20

 

 

 

 

20

周四

 

20

 15

 

 

 

35

周五

 100

 25

 15

 

 

 

 140

周六

 

 

 35

 

 

 

 35

周总结

 200

 95

 75

 

 

 

 375

阶段时间和效率                                            周数(上一次周活动表的周数+1):

不包括上一周在内的累计时间                                                                         

总计

 

 

 

 

 

 

 

平均

 

 

 

 

 

 

 

最大

 

 

 

 

 

 

 

最小

 

 

 

 

 

 

 

以前各周的累计时间                                                                                    

总计

 200

 201

  45

 

 

 

 446

平均

 200

 201

  45

 

 

 

 446

最大

 200

 201

  45

 

 

 

 446

最小

 200

 201

  45

 

 

 

 446

                  二、时间记录表:

学生        孔维春                                             日期   20153月22 

教师        王建民                                             课程           PSP       

日期

开始时间

结束时间

中断时间

净时间

活动

备注

 3.17

 20:00

 21:00

 

60

编程序 

作业 

 3.18

19:00

21:00

 

 120

 编程序

作业

 3.19

19:10

20:00

50

编程序

作业

3.20

15:00

16:00

60

编程序

作业

 3.21

 11:10

11:40

 

30

完善程序

 作业

 3.22

 10:00

 14:00

 无

 240

完善程序

 作业

 

                        三、缺陷记录日志:

学生     孔维春       

日期     20153月22 

教员       王建民   

程序号      2          

日期

编号

类型

引入阶段

排除阶段

修复时间

修复缺陷

 3.17

 1

 a2

编译

编译

1min

 

 在输入cin中,漏掉了一处地址符号&

 3.18

 2

a3

运行

 运行

10min

 

 m == 1漏写了一个=

 3.19

 3

B1

运行

运行之后

5min

 

 列行比较乱

 3.20

4

a1

编译

编译

1min

 

漏了一个retuen返回值

3.21

5

B2

运行

运行之后

45min

 

程序不够完善

推荐阅读