zoukankan      html  css  js  c++  java
  • 第三次软件工程作业

    组员:李专燕、邓婷

    题目:

    编写一个百数以内的小学生四则运算系统

    需求分析:
    系统自动出题,并对用户的答案判断对错

    扩张功能:题目中含有负数,且负数添加括号

    合作步骤:1、对题目进行分析研究

                  2、查找有关的资料

                  3、将上次作业模块化,将函数进行封装

                  4、修改、调试程序

    代码实现:

    #include<stdio.h>
    #include<stdlib.h>
    #include<time.h>
    void add();
    void sub();
    void mul();
    void divi();
    void print();
    
    
    #include<stdio.h>
    #include<stdlib.h>
    #include<time.h>
    
    int count=0;
    int right=0;
    int wrong=0;
    
    void add()
    {
        int a,b,sum,c,flag=1,key;
        do
        {
            srand((unsigned)time(NULL));
            a=rand()%201-100;
            b=rand()%201-100;
            sum=a+b;
            if(a<0&&b>0)
                printf("(%d)+%d=",a,b);
            else if(a>0&&b<0)
                printf("%d+(%d)=",a,b);
            else if(a<0&&b<0)
                printf("(%d)+(%d)=",a,b);
            else
                printf("%d+%d=",a,b);
            do
            {
                scanf("%d",&c);
                if(sum==c)
                {
                    printf("回答正确!");
                    count++;
                    right++;                 
                    break;
                }
                else
                {
                    printf("回答错误!");
                    count++;
                    wrong++;
                    break;
                }
            }while(1);
            fflush(stdin);
            printf("按任意键继续按'Q'键返回运算方式
    ");
            if((key=getchar())=='Q')
                flag=0;
        }while(flag);
    }
    void sub()
    {
        int a,b,sum,c,flag=1,key;
        do
        {
            srand(time(NULL));
            a=rand()%201-100;
            b=rand()%201-100;
            sum=a-b;
            if(a<0&&b>0)
                printf("(%d)-%d=",a,b);
            else if(a>0&&b<0)
                printf("%d-(%d)=",a,b);
            else if(a<0&&b<0)
                printf("(%d)-(%d)=",a,b);
            else
                printf("%d-%d=",a,b);
            do
            {
                scanf("%d",&c);
                if(sum==c)
                {
                    printf("回答正确!");
                    count++;
                    right++;
                    break;
                }
                else
                {
                    printf("回答错误!");
                    count++;
                    wrong++;
                    break;
                }
            }while(1);
            fflush(stdin);
            printf("按任意键继续按'Q'键返回运算方式
    ");
            if((key=getchar())=='Q')
                flag=0;
        }while(flag);
    }
    void mul()
    {
        int a,b,sum,c,flag=1,key;
        do
        {
            srand(time(NULL));
            a=rand()%201-100;
            b=rand()%201-100;
            sum=a*b;
            if(a<0&&b>0)
                printf("(%d)*%d=",a,b);
            else if(a>0&&b<0)
                printf("%d*(%d)=",a,b);
            else if(a<0&&b<0)
                printf("(%d)*(%d)=",a,b);
            else
                printf("%d*%d=",a,b);
            do
            {
                scanf("%d",&c);
                if(sum==c)
                {
                    printf("回答正确!");
                    count++;
                    right++;
                    break;
                }
                else
                {
                    printf("回答错误!");
                    count++;
                    wrong++;
                    break;
                }
            }while(1);
            fflush(stdin);
            printf("按任意键继续按'Q'键返回运算方式
    ");
            if((key=getchar())=='Q')
                flag=0;
        }while(flag);
    }
    void divi()
    {
        int a,b,sum,c,flag=1,key;
        do
        {
            srand(time(NULL));
            a=rand()%201-100;
            b=rand()%201-100;
            sum=a/b;
            if(a<0&&b>0)
                printf("(%d)/%d=",a,b);
            else if(a>0&&b<0)
                printf("%d/(%d)=",a,b);
            else if(a<0&&b<0)
                printf("(%d)/(%d)=",a,b);
            else
                printf("%d/%d=",a,b);
            do
            {
                scanf("%d",&c);
                if(sum==c)
                {
                    printf("回答正确!");
                    count++;
                    right++;
                    break;
                }
                else
                {
                    printf("回答错误!");
                    count++;
                    wrong++;
                    break;
                }
            }while(1);
            fflush(stdin);
            printf("按任意键继续
    ");
            if((key=getchar())=='Q')
                flag=0;
        }while(flag);
    }
    void print()
    {
        printf("共%d道题
    ",count);
        printf("正确%d
    ",right);
        printf("错误%d
    ",wrong);
    }
    
    
    #include<stdio.h>
    #include"test.h"
    
    void main()
    {
        int key;
        printf("		******欢迎进入小学生四则运算系统******
    ");
        printf("1、加法		2、减法		3、乘法		4、除法		5、退出
    ");
        do
        {
            scanf("%d",&key);
            switch(key)
            {
            case 1:add();
                break;
            case 2:sub();
                break;
            case 3:mul();
                break;
            case 4:divi();
                break;
            default:print();
                break;
            }
        }while(key!=5);
    }


    运行程序:

    总结:

    刚开始连题目都不知是什么意思,什么是封装、什么API接口都不知道,后来研究了很久才把之前的代码进行拆分,程序在封装过程中一直出现错误,也是弄了很久才调试出来。我觉得我们对计算机语言了解比较少,以至于理解题目都有很大的困难。

    耗时统计:

                           步骤                                                    耗时                                                    百分比                           
    查找资料 5 0.19
    需求分析 7 0.30
    设计 5 0.19
    封装函数 3 0.11
    修改、运行程序 7 0.30
  • 相关阅读:
    POJ2762 Going from u to v or from v to u?
    POJ3422或洛谷2045 Kaka's Matrix Travels
    LaTeX数学公式大全
    POJ1966 Cable TV Network
    转:Android中的Handler的机制与用法详解
    Httpservlet cannot be resolved to a type的原因与解决方法
    Textchangedlistener的用法
    contextMenu的使用
    转:android studio 一直卡在Gradle:Build Running的解决办法
    Sqlite操作的一些关键类的官方说明与Intent的startactivityforresult方法
  • 原文地址:https://www.cnblogs.com/lizhuanyan/p/4450686.html
Copyright © 2011-2022 走看看