软件工程(2018)结对编程第二次作业

目录

软件工程(2018)结对编程第二次作业


一、题目选择

我们在刚开始上课的时候介绍过一个小学四则运算自动生成程序的例子,请实现它,要求:

  • 能够自动生成四则运算练习题
  • 可以定制题目数量
  • 用户可以选择运算符
  • 用户设置最大数(如十以内、百以内等)
  • 用户选择是否有括号、是否有小数
  • 用户选择输出方式(如输出到文件、打印机等)
  • 最好能提供图形用户界面(根据自己能力选做,以完成上述功能为主)

二、任务分工

三、设计思路

1、生成随机整数和小数

  利用rand()函数生成随机数,具体细节参考随机数生成算法

  • 生成整数
//范围1~range
int random_0(int range)
{
    //srand(time(0));
    return rand() % range + 1;
}
  • 生成小数
float random_1(int range)
{
    //srand(time(0));
    return ((float)(rand() % 100) / 100) + rand() % range;
}

2、随机生成运算符

  利用随机数任意选择‘+’、‘-’、‘*’、‘/’符号中的一种。


char GetOperation(int x)
{
L1:switch (x)
{
case 1:return '+'; break;
case 2:return'-'; break;
case 3:return '*'; break;
case 4:return '/'; break;
case 5:x = random_0(4); goto L1; break;
default:return 0; break;
}
}

3、生成算术式

  随机选择数字和运算符组成算术式。

  • 无小数无括号算式
void display0(int max, int x)
{
    int i = random_0(5) + 1;
    int a[10], j;
    char b[10];
    for (j = 0; j < i - 1; j++)
    {
        a[j] = random_0(max);
        b[j] = GetOperation(x);
        printf("%d%c", a[j], b[j]);
        fprintf(fp, "%d%c", a[j], b[j]);
    }
    a[j] = random_0(max);
    printf("%d=\n", a[j]);
    fprintf(fp, "%d=\n", a[j]);
}
  • 有小数无括号算式
void display1(int max, int x)
{
    int i = random_0(5) + 1, j;
    float a[10];
    char b[10];
    for (j = 0; j < i - 1; j++)
    {
        a[j] = random_1(max);
        b[j] = GetOperation(x);
        printf("%.2f%c", a[j], b[j]);
        fprintf(fp, "%.2f%c", a[j], b[j]);

    }
    a[j] = random_1(max);
    printf("%.2f=\n", a[j]);
    fprintf(fp, "%.2f=\n", a[j]);

}
  • 有括号算式
void kuohao_3(int max, char x2, int x)
{
    int i,j,a[10];
    float b[10];
    for (j = 0; j < 3; j++)
    {
        a[j] = random_0(max);
        b[j] = random_1(max);
    }
    if (x2 == 'N')//没有小数
    {
        i = random_0(2);
        if (i == 1)
        {
            printf("%d%c(%d%c%d)=\n",a[0], GetOperation(x), a[1], GetOperation(x),a[2]);
            fprintf(fp, "%d%c(%d%c%d)=\n", a[0], GetOperation(x),a[1], GetOperation(x), a[2]);
        }
        else
        {
            printf("(%d%c%d)%c%d=\n", a[0], GetOperation(x), a[1], GetOperation(x), a[2]);
            fprintf(fp, "(%d%c%d)%c%d=\n", a[0], GetOperation(x), a[1], GetOperation(x), a[2]);
        }
    }
    else       //有小数
    {
        i = random_0(2);
        if (i == 1)
        {
            //cout << random_1(max) << GetOperation(x) << "(" << random_1(max) << GetOperation(x) << random_1(max) << ")" << endl;
            printf("%.2f%c(%.2f%c%.2f)=\n", b[0], GetOperation(x), b[1], GetOperation(x), b[2]);
            fprintf(fp, "%.2f%c(%.2f%c%.2f)=\n", b[0], GetOperation(x), b[1], GetOperation(x), b[2]);
        }
        else
        {
            //cout << "(" << random_1(max) << GetOperation(x) << random_1(max) << ")" << GetOperation(x) << random_1(max) << endl;
            printf("(%.2f%c%.2f)%c%.2f=\n", b[0], GetOperation(x), b[1], GetOperation(x), b[2]);
            fprintf(fp, "(%.2f%c%.2f)%c%.2f=\n", b[0], GetOperation(x), b[1], GetOperation(x), b[2]);
        }
    }
}

四、运行结果

  1. 整数无括号算式

  2. 小数无括号算式

  3. 整数有括号算式
  4. 小数有括号算式

五、感想与收获

  第二次与刘雨霏同学合作,让我体会到了与同学合作的益处,它远远比一个人单独编写程序好的多,很容易发现自己的错误。在开发任何一个项目中能够及时发现错误并改正都是非常重要的,这更让我体会到了合作的重要性。
  同时我也认识到自己的不足,例如这次的小程序,我在编写时,虽然大体实现了老师题目中的要求,但是没有接触过打印机输出,所以选择了相对熟悉的文件输出,也没有实现图形化界面,这让我觉得非常遗憾。程序里也还有很多毛病,像刘雨霏同学提出的随机小数的精度不够;对于减法和除法在做整数运算时可能会出现小数或者负数,这些情况都没有考虑,对于小学生来说这些都已经是超出能力范围的题目;还有带括号的算术题,我写的程序不够灵活(可以说非常草率)等等……
  但是收获也很多,第一次正式意义上与同学合作编程,认识到自己的不足,也可以学习同学身上的优点。以后一定会更严格的要求自己,fighting!

猜你喜欢

转载自www.cnblogs.com/wnscwt/p/10807079.html