| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 806 人关注过本帖
标题:加减乘除程序改进,只有在第一遍输入难度和题数时可以判断输入是否正确,( ...
取消只看楼主 加入收藏
sunqi311247
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2020-12-17
结帖率:0
收藏
已结贴  问题点数:20 回复次数:0 
加减乘除程序改进,只有在第一遍输入难度和题数时可以判断输入是否正确,(请直接看主函数)
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
int all=0,all0=0;
int random(int level) //random随机产生数 ,level为难度输入 1(简单)或2(难)
{
    int result;
    int k;
    if (level==1) k=100;
    if (level==2) k=1000;
    result=rand()%k+1;
    //printf("%d",result);
    return result;
}
int add(int c,int k)  //加法
{
              int i,num1,num2,answer,op,count=0;
              for (i=1;i<=k;i++)
                  {
                    if(c==1) op=100;
                    if(c==2) op=1000;
                    again1:
                    num1=random(c);
                    num2=random(c);
                    if(num1+num2>op) goto again1;
                    printf("%d+%d=",num1,num2);
                    scanf("%d",&answer);
                    if(answer==num1+num2)
                    {
                        printf("答案是正确的\n");
                        count++ ;
                        all++;
                    }
                    else
                    {
                        printf("errer!正确答案为:%d\n",num1+num2);
                        all0++;
                    }

                  }

                  return count;
}
int minus(int c,int k)   //减法
{             int i,num1,num2,answer,temp,count=0;
              for (i=1;i<=k;i++)
                  {
                     
                    num1=random(c);
                    num2=random(c);
                    if(num1<num2)
                    {
                        temp=num1;
                        num1=num2;
                        num2=temp;
                    }
                    printf("%d-%d=",num1,num2);
                    scanf("%d",&answer);
                    if(answer==num1-num2)
                    {
                        printf("答案是正确的\n");
                        count++;
                        all++;
                    }
                    else
                    {
                        printf("errer!正确答案为:%d\n",num1-num2);
                        all0++;
                    }
                  }

                  return count;
}
int multiplication(int c,int k)  //乘法
{              int i,num1,num2,answer,op,count=0;
               for (i=1;i<=k;i++)
                  { if(c==1) op=100;
                    if(c==2) op=1000;
                    again2:
                    num1=random(c);
                    num2=random(c);
                    if(num1*num2>op) goto again2;
                    printf("%d*%d=",num1,num2);
                    scanf("%d",&answer);
                    if(answer==num1*num2)
                    {
                        printf("答案是正确的\n");
                        count++;
                        all++;
                    }
                    else
                    {
                        printf("errer!正确答案为:%d\n",num1*num2);
                        all0++;
                    }
                  }

                  return count;
}
int  division(int c,int k)       //除法
{            int i,num1,num2,answer,count=0;
             for (i=1;i<=k;i++)
                  {
                      loop1:
                    num1=random(c);
                    num2=random(c);
                    if(num1%num2==0)
                    {
                       printf("%d/%d=",num1,num2);
                       scanf("%d",&answer);
                       if(answer==num1/num2)
                       {
                           printf("答案是正确的\n");
                           count++;
                           all++;
                       }
                       else
                       {
                           printf("errer!正确答案为:%d\n",num1/num2);
                           all0++;
                       }
                    }
                    else goto loop1;
                  }

                  return count;
}
int main(void)
{
    int P,k,num1,num2,answer,c,count;  //k表示题数
    int i;
    char internal;  //internal辨别运算种类
    loop:
    printf("请输入你需要进行的运算(+、-、*、/):");
    scanf("%c",&internal);
    getchar();
    if(internal!='+'&&internal!='-'&&internal!='*'&&internal!='/')
    {
          system("cls");
          printf("请输入正确的运算符!\n");
          //internal=getchar();
          goto loop;
    }
    loop5:
    printf("请输入你需要难度输入 1(简单)或2(难):");
    scanf(" %d",&c);
    getchar();
    if(c!=1&&c!=2)
    {
        printf("请输入正确的难度!\n");
        //getchar();
        goto loop5;
    }
    loop6:
    printf("请输入需要练习的题数:");
    scanf(" %d", &k);
    //getchar();
    if(k>0&&k<100)
    {
        //k=k;
        getchar();
    }
    else
    {

        printf("请输入正确的题数!\n");
        getchar();
        goto loop6;
    }
    //printf("%c\n",internal);
    srand(time(NULL));
    switch (internal)
    {
         case '+':
                  count=add(c,k);
                  printf("做对题数:%d\t做错题数:%d\n",count,k-count);
                  break;
         case '-':
                  count=minus(c,k);
                  printf("做对题数:%d\t做错题数:%d\n",count,k-count);
                  break;
         case '*':
                  count=multiplication(c,k);
                  printf("做对题数:%d\t做错题数:%d\n",count,k-count);
                  break;
         case '/':
                  count=division(c,k);
                  printf("做对题数:%d\t做错题数:%d\n",count,k-count);
                  break;
         default:
                 printf("请输入正确的运算符!\n");
                 internal=getchar();
                 goto loop;      //判断是不是加减乘除,若不是返回至errer函数
    }
    printf("是否继续?(是输入1,否输入0):");
    scanf("%d",&P);
    fflush(stdin);
    //internal=getchar();
    if(P==1)
    {
           system("cls");
           goto loop;
    }
    printf("此次总共对了%d,做错了%d\n",all,all0);
    return 0;
}

        
搜索更多相关主题的帖子: int answer printf count 输入 
2020-12-17 08:26
快速回复:加减乘除程序改进,只有在第一遍输入难度和题数时可以判断输入是否正确 ...
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.017865 second(s), 11 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved