| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 814 人关注过本帖
标题:加减乘除程序改进,只有在第一遍输入难度和题数时可以判断输入是否正确,( ...
只看楼主 加入收藏
sunqi311247
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2020-12-17
结帖率:0
收藏
已结贴  问题点数:20 回复次数:1 
加减乘除程序改进,只有在第一遍输入难度和题数时可以判断输入是否正确,(请直接看主函数)
#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
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9029
专家分:54050
注 册:2011-1-18
收藏
得分:20 
乱七八糟的

程序代码:
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <time.h>

int random( int level )
{
    return rand()%level+1;
}

int get_int_from_stdin( void )
{
    int result;
    int n = scanf("%d",&result);
    scanf( "%*[^\n]" );
    if( n == 1 )
        return result;
    if( feof(stdin) )
        exit( 1 );
    return -1;
}

bool add( int level )
{
    int a = random( level-1 );
    int b = random( level-a );
    printf( "%d + %d = ", a, b );

    int answer = get_int_from_stdin();
    if( answer == a+b )
        return true;
    printf( " ------ 正确答案为:%d\n", a+b );
    return false;
}

bool minus( int level )
{
    int a = random( level );
    int b = random( a );
    printf( "%d - %d = ", a, b );

    int answer = get_int_from_stdin();
    if( answer == a-b )
        return true;
    printf( " ------ 正确答案为:%d\n", a-b );
    return false;
}

bool multiplication( int level )
{
    int a = random( level/2 );
    int b = random( level/a );
    printf( "%d * %d = ", a, b );

    int answer = get_int_from_stdin();
    if( answer == a*b )
        return true;
    printf( " ------ 正确答案为:%d\n", a*b );
    return false;
}

bool division( int level )
{
    int a = random( level );
    int b = random( level/a );
    int c = a*b;
    printf( "%d / %d = ", c, a );

    int answer = get_int_from_stdin();
    if( answer == c/b )
        return true;
    printf( " ------ 正确答案为:%d\n", c/b );
    return false;
}

////////////////////////////////////////////

char get_char_from_stdin_condition( const char* tip, const char* condition )
{
    char input;
    for( ; ; )
    {
        printf( "%s", tip );
        int n = scanf(" %c",&input);
        scanf( "%*[^\n]" );
        if( n != 1 )
            exit( 1 );
        if( strchr(condition,input) )
            break;
        printf( "输入错误," );
    }
    return input;
}

char get_int_from_stdin_condition( const char* tip, int low, int upper )
{
    int input;
    for( ; ; )
    {
        printf( "%s", tip );
        int n = scanf( "%d", &input );
        scanf( "%*[^\n]" );
        if( n!=1 && feof(stdin) )
            exit( 1 );
        if( n==1 && input>=low && input<=upper )
            break;
        printf( "输入错误," );
    }
    return input;
}

int main( void )
{
    srand( (unsigned)time(NULL) );

    int total_correct=0, total_wrong=0;
    for( ; ; )
    {
        char internal = get_char_from_stdin_condition( "请输入你需要进行的运算(+、-、*、/): ", "+-*/" );
        int level = get_char_from_stdin_condition( "请输入你需要的难度(1=简单,2=难): ", "12" )=='1' ? 100:1000;
        int count = get_int_from_stdin_condition( "请输入需要练习的题数(100以内): ", 1, 99 );

        int correct = 0;
        for( int i=0; i!=count; ++i )
        {
            switch( internal )
            {
            case '+':
                correct += add(level);
                break;
            case '-':
                correct += minus(level);
                break;
            case '*':
                correct += multiplication(level);
                break;
            case '/':
                correct += division(level);
                break;
            }
        }
        printf( "做对题数:%d, 做错题数:%d\n", correct, count-correct );
        total_correct += correct;
        total_wrong += count-correct;

        printf( "是否继续?(是输入1): " );
        char bcontinue;
        if( scanf(" %c",&bcontinue)!=1 || bcontinue!='1' )
            break;
        scanf( "%*[^\n]" );
    }
    printf( "此次总共对了%d道, 做错了%d道\n", total_correct, total_wrong );
}
2020-12-17 10:08
快速回复:加减乘除程序改进,只有在第一遍输入难度和题数时可以判断输入是否正确 ...
数据加载中...
 
   



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

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