• 结对编程 四则运算


    、需求分析:

          设计一个可以进行加减乘除运算法的一个小程序 产生0~10之间的随机整数  当用户输入+号表示选择加法、输入-号表示选择减法  、 输入*号表示选择乘法 、 输入/号表示选择除法 、输入00表示结束程序  结束时统计计算结果。

    二、设计思路:

    1、定义 Calculation_formula();产生随机生成运算式;

    2、定义 number是总的测试题数,accepted是正确题数;

    3、 定义 temp随机生成0-3表示相应运算符;

    三、具体实现

    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    #include <math.h>
    #include <string.h>
    
    int right;
    
    void Menu();//菜单说明
    void Calculation_formula();//随机生成运算式
    void swap(int *a,int *b);//交换数值
    void reverse(char str[],int n);//数组逆转
    
    int main()
    {
        int number=0,accepted=0;
        //number是总的测试题数,accepted是正确题数
        int i,length,true_result;
        char result[5];
        Menu();
        while(1){
            i=0;
            true_result=0;
            Calculation_formula();
            scanf("%s",result);//以数字字符数组形式保存输入的结果
            if(result[1]=='0'&&result[0]=='0')//结束标志
                break;
            length=strlen(result);
            reverse(result,length);
            while(result[i]!=''){//将数字字符串转换成对应数值
                true_result+=(result[i]-48)*pow(10,i);
                i++;
            }
            if(true_result==right){
                accepted++;
                printf("计算正确!
    ");
            }
            else{
                printf("计算错误!正确结果是:%d
    ",right);
            }
            number++;
            fflush(stdin);
        }
        printf("测试结束!
    共%d道题目,通过%d道!
    ",number,accepted);
        return 0;
    }
    
    void Menu()//菜单说明
    {
        printf("
    
    ");
        printf("	 ---------程序说明----------
    ");
        printf("	|    输入+号表示选择加法    |
    ");
        printf("	|    输入-号表示选择减法    |
    ");
        printf("	|    输入*号表示选择乘法    |
    ");
        printf("	|    输入/号表示选择除法    |
    ");
        printf("	|    输入00表示结束程序     |
    ");
        printf("	 ---------------------------
    ");
        printf("测试开始:
    ");
    }
    
    void Calculation_formula()//运算式生成
    {
        int temp1,temp2;//保存随机生成的运算数
        char c;//保存运算符
        int temp;//随机生成0-3表示相应运算符
        srand((unsigned)time(NULL));//将当前时间设为随机函数种子
        temp=rand()%4;
        temp1=rand()%10;
        temp2=rand()%10;
        switch (temp){
            case 0://加法
                c='+';
                right=temp1+temp2;
                break;
            case 1://减法
                c='-';
                if(temp1<temp2)//结果非负
                    swap(&temp1,&temp2);
                right=temp1-temp2;
                break;
            case 2://乘法
                c='*';
                right=temp1*temp2;
                break;
            case 3://除法
                c='/';
                if(temp1==0&&temp2==0){//两运算数不能都是0
                    do{
                        temp1=rand()%10;
                        temp2=rand()%10;
                    }while(temp1||temp2);
                }
                if(temp1<temp2)//被除数应不小于除数
                    swap(&temp1,&temp2);
                if(temp2==0||temp1%temp2){//除数不能是0且相除不能有余数
                    do{
                       temp2=rand()%10;
                    }while(!temp2||(temp1%temp2));
                }
                right=temp1/temp2;
                break;
        }
        printf("%d%c%d=",temp1,c,temp2);
    }
    
    void swap(int *a,int *b)//交换数值
    {
        int p=*a;
        *a=*b;
        *b=p;
    }
    
    void reverse(char str[],int n)//将数组逆转
    {
        char c;
        int i;
        for(i=0;i<n/2;i++){
            c=str[i];
            str[i]=str[n-i-1];
            str[n-i-1]=c;
        }
    }
    

     四:测试结果及截图

    五、psp耗时表:

    六、总结:在结对编程的过程中因为我和我的搭档王祥想法不一样,往往造成进行不下去,历经了一次一次失败最终结果自我认为还可以吧

    总之结对编程这次忘我懂得了一个道理 ;要学会倾听别人的想法;也学到一些事情。还是有收获的。

  • 相关阅读:
    Java 利用SWFUpload多文件上传 session 为空失效,不能验证的问题 swfUpload多文件上传
    对ExtJS4应用 性能优化的几点建议
    Extjs4中用combox做下拉带图片的下拉框
    当你的才华还撑不起你的野心时,就应该静下心来学习(转)
    占位符行为 PlaceHolderBehavior 的实现以及使用
    一个简单的TabItem样式。
    WPF实现Twitter按钮效果(转)
    模仿36。杀毒~button(转)
    WPF自适应可关闭的TabControl 类似浏览器的标签页(转)
    WPF绘制简单常用的Path(转)
  • 原文地址:https://www.cnblogs.com/L0610/p/4889370.html
Copyright © 2020-2023  润新知