• 四则运算


    一、作业信息

    博客班级 班级连接
    作业要求 作业连接
    作业目标 培养提高个人的编码能力
    学号 3180701139
    二、作业要求
    写一个能自动生成小学四则运算题目的程序,然后在此基础上扩展:
    1)除了整数以外,还要支持真分数的四则运算,例如:1/6+1/8=7/24
    2)程序要求能处理用户的输入,判断对错,累积分数
    3)程序支持可以由用户自行选择加、减、乘、除运算
    4)使用-n参数控制生成题目的个数,例如Myapp.exe -n 10,将生成10个题目
    三、代码提交
    1.设计思路
    功能设计:
    

    基本功能:实现简单的四则运算出题(包含分数以及负数)
    扩展功能:验证对错,如果错误显示正确答案并统计正确率
    2.源代码
    `#include <stdio.h>

    include

    include <stdlib.h>

    include <time.h>

    include <string.h>

    using namespace std;
    typedef struct //定义分数结构体
    {
    int up, down;
    }fraction;
    int gcd(int a, int b) //最大公约数
    {
    if (b == 0) return a;
    return gcd(b, a % b);
    }
    void yuefen(int &a, int &b,int flag) //分数约分,并自动进行输出,用flag标志答案,答案不输出
    {
    int d = gcd(abs(a), abs(b));
    a /= d;
    b /= d;
    if (flag == 0) {
    if (b == 1)printf("%d", a);
    else printf("%d/%d", a, b);
    }
    }
    void add(int num,fraction fraans[],int ans[]) //加法题目生成及运算
    {
    int a, b, up1, down1, up2, down2,flag=0,i;
    for (i = 0; i <=num / 2; i++) //整数部分
    {
    unsigned int times = (unsigned int)time(0); //生成随机数
    srand(times * (i + 1));
    a = rand() % 10 + 1;
    b = rand() % 10 + 1;
    ans[i] = a + b;
    printf("%d.%d+%d ", i+1, a, b); //输出整数题目
    }
    for (i = num/2+1; i < num; i++) //分数部分
    {
    unsigned int times = (unsigned int)time(0);
    srand(times * (i + 1));
    up1 = rand() % 10 + 1;
    down1 = rand() % 10 + 1;
    up2 = rand() % 10 + 1;
    down2 = rand() % 10 + 1;
    printf("%d.", i + 1);
    yuefen(up1, down1,flag);
    printf("+");
    yuefen(up2, down2,flag);
    printf(" "); //将随机生成的分数进行约分后再输出
    fraans[i].up = up1 * down2 + up2 * down1;
    fraans[i].down = down1 * down2;
    flag = 1; //用flag标志答案,对答案不进行输出
    yuefen(fraans[i].up, fraans[i].down,flag);
    flag = 0;
    }
    }

    void subtract(int num, fraction fraans[], int ans[]) //减法题目生成及运算
    {
    int a, b, up1, down1, up2, down2,flag=0,i;
    for (i = 0; i <= num / 2; i++)
    {
    unsigned int times = (unsigned int)time(0);
    srand(times * (i + 1));
    a = rand() % 10 + 1;
    b = rand() % 10 + 1;
    ans[i] = a - b;
    printf("%d.%d-%d ", i, a, b);
    }
    for (i = num / 2 + 1; i < num; i++)
    {
    unsigned int times = (unsigned int)time(0);
    srand(times * (i + 1));
    up1 = rand() % 10 + 1;
    down1 = rand() % 10 + 1;
    up2 = rand() % 10 + 1;
    down2 = rand() % 10 + 1;
    printf("%d.", i + 1);
    yuefen(up1, down1, flag);
    printf("-");
    yuefen(up2, down2, flag);
    printf(" ");
    fraans[i].up = up1 * down2 - up2 * down1;
    fraans[i].down = down1 * down2;
    flag = 1;
    yuefen(fraans[i].up, fraans[i].down, flag);
    flag = 0;
    }
    }

    void multiply(int num, fraction fraans[], int ans[]) //乘法题目生成及运算
    {
    int a, b, up1, down1, up2, down2,flag = 0,i;
    for (i = 0; i <= num / 2; i++)
    {
    unsigned int times = (unsigned int)time(0);
    srand(times * (i + 1));
    a = rand() % 10 + 1;
    b = rand() % 10 + 1;
    ans[i] = a * b;
    printf("%d.%d%d ", i+1, a, b);
    }
    for (i = num / 2 + 1; i < num; i++)
    {
    unsigned int times = (unsigned int)time(0);
    srand(times * (i + 1));
    up1 = rand() % 10 + 1;
    down1 = rand() % 10 + 1;
    up2 = rand() % 10 + 1;
    down2 = rand() % 10 + 1;
    printf("%d.", i + 1);
    yuefen(up1, down1, flag);
    printf("
    ");
    yuefen(up2, down2, flag);
    printf(" ");
    fraans[i].up = up1 * up2;
    fraans[i].down = down1 * down2;
    flag = 1;
    yuefen(fraans[i].up, fraans[i].down, flag);
    flag = 0;
    }
    }

    void divide(int num, fraction fraans[]) //除法题目生成及运算
    {
    int up1, down1, up2, down2, flag = 0;
    for (int i = 0; i < num; i++)
    {
    unsigned int times = (unsigned int)time(0);
    srand(times * (i + 1));
    up1 = rand() % 10 + 1;
    down1 = rand() % 10 + 1;
    up2 = rand() % 10 + 1;
    down2 = rand() % 10 + 1;
    printf("%d. ", i + 1);
    yuefen(up1, down1, flag);
    printf(" / ");
    yuefen(up2, down2, flag);
    printf(" ");
    fraans[i].up = up1 * down2;
    fraans[i].down = down1 * up2;
    flag = 1;
    yuefen(fraans[i].up, fraans[i].down, flag);
    flag = 0;
    }
    }

    int main()
    {

    int m=1;
    fraction fraans[400], userfraans[400];
    int ans[100] = { 0 }, userans[100] = { 0 },i;
    int num, symbol, grade;
    printf("
    
    
    			*****欢迎使用四则运算生成程序*****
    ");
    printf("				   1.加法运算");
    printf("
    				   2.减法运算");
    printf("
    				   3.乘法运算");
    printf("
    				   4.除法运算");
    printf("
    				   0.退出");
    while(m)
    {
    

    label: printf(" 请选择符号0、1、2、3、4:");
    scanf("%d", &symbol);
    grade=0;//用symbol标志用户选择的符号,1234对应加减乘除;
    switch (symbol) //对应选择不同的算法
    {
    case 0:m=0;break;
    case 1:
    printf("请输入题目数量(最少为2,最大100):");
    scanf("%d", &num);add(num, fraans, ans); break;
    case 2:
    printf("请输入题目数量(最少为2,最大100):");
    scanf("%d", &num);subtract(num, fraans, ans); break;
    case 3:
    printf("请输入题目数量(最少为2,最大100):");
    scanf("%d", &num);multiply(num, fraans, ans); break;
    case 4:
    printf("请输入题目数量(最少为2,最大100):");
    scanf("%d", &num);divide(num, fraans); break;
    default:
    printf("请重新输入:");
    goto label;
    }
    if(m!=0)
    {

    		if (symbol != 4)													//当用户选择的运算不为除法时,分别对整数和分数答案进行保存
    		{
    
    			for ( i = 0; i <= num / 2; i++)
    			{
    				printf("第%d道题的答案为:", i + 1);
    				scanf("%d", &userans[i]);
    			}
    			for ( i = num / 2 + 1; i < num; i++)
    			{
    				printf("第%d道题的答案为:", i + 1);
    				if (fraans[i].down == 1)
    				{
    					scanf("%d", &userfraans[i].up);
    					userfraans[i].down = 1;
    				}
    				else
    				{
    					scanf("%d/%d", &userfraans[i].up, &userfraans[i].down);
    				}
    			}
    		}
    	else																//当用户选择为出发时,对用户输入的答案进行保存
    	{
    		for ( i = 0; i < num; i++)
    		{
    			printf("第%d道题的答案为:", i + 1);
    			if (fraans[i].down == 1)
    			{
    				scanf("%d", &userfraans[i].up);
    				userfraans[i].down = 1;
    			}
    			else
    			{
    				scanf("%d/%d", &userfraans[i].up, &userfraans[i].down);
    			}
    		}
    	}
    
    	for ( i = 0; i <= num / 2; i++)									//对用户输入的答案和答案进行对比,如果一致,得分+1
    	{
    		if (userans[i] == ans[i])grade++;
    	}
    	for ( i = num / 2 + 1; i < num; i++)
    	{
    		if (userfraans[i].up == fraans[i].up && userfraans[i].down == fraans[i].down)grade++;
    	}
    	printf("grade=%d
    ", grade);
    	}
    	
    }
    

    }
    `
    3.运行截图

    四、个人小结

    psp2.1 任务内容 计算完成需要的时间(min) 实际完成需要的时间(min)
    Planning 计划 5 10
    Estimate 估计这个任务需要多少时间,并规划大致工作步骤 10 14
    Development 开发 120 150
    Analysis 需求分析 12 15
    Design Spec 生成设计文档 4 4
    Design Review 设计复审 5 10
    Coding Standard 代码规范 3 5
    Design 具体设计 10 15
    Coding 代码 30 50
    Code Review 代码复审 5 8
    Test 测试 6 10
    Reporting 报告 6 9
    Test Report 测试报告 3 3
    Size Measurement 计算工作量 2 2
    Postmortem & Process Improvement Plan 事后总结,并提出过程改进计划 5 5
    小结:1.对于第一次使用博客园来发表作业的我,对于其操作不是很熟练,但却很有趣,通过编辑加预览后的效果感觉发现了新大陆。
    2.对于自己的编程开发能力,我想更有待加强,因为与预估时间相比,我实际的时间常常超过了预估时间,这是缺乏能力的重大表现,也说明了我还有大量可进步可学习的地方。
  • 相关阅读:
    poj 1050
    poj 2479 Maximum sum
    Trie树结构(AC自动机前奏)(转)
    poj 3624(zeroonepack)
    poj 3630(Phone List )
    KMP算法(AC自动机前奏)(转)
    AC自动机(转)
    AC自动机模板(hdu2222)
    Running CMD.EXE as Local System
    什么是WPF(Avalon)
  • 原文地址:https://www.cnblogs.com/gmh6115/p/13940646.html
Copyright © 2020-2023  润新知