结对编程 四则运算

题目:
小学老师要每周给同学出300道四则运算练习题。
这个程序有很多种实现方式:
C/C++
C#/VB.net/Java
Excel
Unix Shell
Emacs/Powershell/Vbscript
Perl
Python
两个运算符,100 以内的数字,不需要写答案。
需要检查答案是否正确,并且保证答案在 0——1000 之间
我们采用的是c++来实现的
伙伴:2252310
分工:我负责前面加减乘除的不混合运算,伙伴负责加减乘除的混合运算
代码:

#include <stdlib.h>
#include <time.h>
int getChoice();
void Menu();
void doExercise(int);
int test(int);

int exnum=0;                    //题目数量
int main(void)
{
    int choice=0;
    do
    {
        Menu();                    //显示菜单
        
        while(1)
        {
            choice=getChoice();
            if(choice<0 || choice>5)
                printf("请重新输入");
            else
                break;
        }
        
        if(choice==0)
        {
            break; //用户选择0,退出循环(退出系统)
        }
        
        doExercise(choice); //测试系统
    }while(choice!=0);
    
    printf("练习结束,玩去吧!\n");
    return 0;
}

int getChoice()
{
    printf("请输入你的选择(0--5):");
    int choice;
    scanf("%d",&choice);
    return choice;
}
void Menu()
{
    
    printf("--------------------------------------------\n");
    printf("|             四则运算练习                 |\n");
    printf("|               1.加法练习                 |\n");
    printf("|               2.减法练习                 |\n");
    printf("|               3.乘法练习                 |\n");
    printf("|               4.除法练习                 |\n");
    printf("|               5.混合练习                 |\n");
    printf("|                0.退出                    |\n");
    printf("--------------------------------------------\n");
    
}
void doExercise(int n)
{
    int score=0; //练习得分,初始化为0
    int i=0; //练习题数的计数器
    printf("你想做多少道题?");
    
    scanf("%d",&exnum);
    for(i=0;i<exnum;i++)
    {
        score=score+test(n); //间接递归调用test(n)
    }
    printf("\n正确率:%d/%d\n\n\n",score,exnum);
}

int test(int n)
{
    int rightanswer=0,rightanswer1=0; //正确答案
    int useranswer=0; //用户输入的答案
    int t=0; //临时变量
    char operation,operation1,operation2; //运算类别
    int num1=0; //操作数1
    int num2=0; //操作数2
    int num3=0,num4=0,num5=0; 
    do{
    srand(time(NULL)); //使用当前时间进行随机数发生器的初始化。 
    num1=rand()%10; //取0—10之间的随机数
    num2=rand()%10;
    num3=rand()%10;
    num4=rand()%4;
    num5=rand()%4;
    
    
    
    
    
    switch(n)
{
    case 1:
    operation1='+';
    operation2='+';
    break;
    
    case 2:
    operation1='-';
    operation2='-';
    break;
    
    case 3:
    operation1='*';
    operation2='*';
    break;
    case 4:
    operation1='/';
    operation2='/';
    break;
    case 5:
    if(num4==0)
        operation1='+';
    else if (num4==1)
        operation1='-'; 
    else if (num4==2)
        operation1='*';
    else if (num4==3)
        operation1='/' ;
    
    if(num5==0)
        operation2='+';
    else if (num5==1)
        operation2='-' ;
    else if (num5==2)
        operation2='*' ;
    else if (num5==3)
        operation2='/';
    break;
    
    }
    
    
    
    if((operation=='-') )
    {   
        t=num1;
        num1=num2;
        num2=t;
    }
    
    if(operation2=='+'||operation2=='-'){
        
    switch(operation1)
    {
    case '+':
        rightanswer1=num1+num2;
        break;
        
    case '-':
    
        rightanswer1=num1-num2;
        break;
        
    case '*':
        rightanswer1=num1*num2;
        break;
    case '/':
        rightanswer1=num1/num2;
        break;
        
    }
    switch(operation2)
    {
    case '+':
        rightanswer=rightanswer1+num3;
        break;
        
    case '-':
        rightanswer=rightanswer1-num3;
        break;
        
    case '*':
        rightanswer=rightanswer1*num3;
        break;
    case '/':
        rightanswer=rightanswer1/num3;
        break;
        
    }
    }
    else if(operation2=='*'||(operation2=='/'&&(operation1=='+'||operation1=='-'))){
        switch(operation2)
        {
            
        case '*':
            rightanswer1=num2*num3;
            break;
        case '/':
            rightanswer1=num2/num3;
            break;
            
        }
        switch(operation1)
        {
        case '+':
            rightanswer=num1+rightanswer1;
            break;
            
        case '-':
            rightanswer=num1-rightanswer1;
            break;
            
        case '*':
            rightanswer=num1*rightanswer1;
            break;
        case '/':
            rightanswer=num1/rightanswer1;
            break;
            
        }
        
    }
    else if(operation2=='/'&&(operation1=='*'||operation1=='/'))
    {
        switch(operation1)
        {
            
            
        case '*':
            rightanswer1=num1*num2;
            break;
        case '/':
            rightanswer1=num1/num2;
            break;
            
        }
        switch(operation2)
        {
        case '/':
            rightanswer=rightanswer1/num3;
            break;
            
        }
    }
    }while(rightanswer<0);
//判断正误
    int errornum=0;
    printf("%d%c%d%c%d= ",num1,operation1,num2,operation2,num3);
    while(1){
        scanf("%d",&useranswer);
        
        if(useranswer==rightanswer)
        {
            if(errornum>0){
                printf("做对了!\n\n");
                return 0;
            }
            else{
                printf("做对了!\n\n");
                return 1;
            }
        }
        else 
        {
            errornum++;
            if(errornum==3){
                printf("错误3次,正确答案为:%d\n\n",rightanswer);return 0;
            }
            else{
                printf("做错了!请重新输入答案:");
            }
        }
    }
}`

运行:

体会:
结对编程是一种非常有效的合作方式,通过结对编程可以让团队成员之间更好地沟通和协作,共同解决问题。在结对编程过程中,两个程序员共同工作在同一个工作站上,一人编写代码,一人审查代码,相互交流讨论,共同思考解决方案。

通过结对编程,我们可以相互学习对方的编程技能和思维方式,共同提高编程能力。同时,结对编程也可以减少错误和bug的产生,因为两个人共同审查代码,可以及时发现问题并进行修复。

在结对编程过程中,需要相互尊重和信任对方,共同努力达成共同的目标。这种合作方式可以增强团队的凝聚力和合作精神,提高工作效率和质量。

总的来说,结对编程是一种非常有益的合作方式,可以带来更好的工作体验和更好的成果。希望我们可以继续保持这种合作方式,共同进步,共同成长。

posted @ 2024-04-21 13:04  luoqingyun  阅读(17)  评论(0)    收藏  举报