• 四则运算


    #include<stdio.h>
    #include<stdlib.h>
    #include<time.h>
    #include<string.h>
    int scan()
    { 
    char s[100]; 
    int i,t,z=0; 
    do 
    { 
    z=0; 
    gets(s); 
    for(i=0;s[i]!='\0';i++) 
    if(s[i]<'0' || s[i]>'9') 
    break; 
    if(i>=strlen(s)) 
    for(t=0;s[t]!='\0';t++) 
    z=z*10+((int)s[t]-48); 
    else 
    printf("您的输入有误,请重新输入");
    } 
    while(i<strlen(s)); 
    return(z);
    }
    int x=1,y=1,choice=0,sum=0,right=0,z=0,answer;
    float precent;
    char fuhao;
    void suiji()
    { 
    srand(time(NULL)); 
    x=rand()%100+1; 
    y=rand()%100+1;
    }void choose()
    { 
    int yunsuan(int,int,int); 
    if(sum==0) 
    precent=0.0; 
    else 
    precent=(float)right/sum; 
    printf("\n如需进行加减乘除运算测试,请分别输入1、2、3、4\n"); 
    printf("如需退出请输入5\n"); choice=scan(); 
    switch(choice) 
    { 
    case 1:printf("欢迎来到加法运算测试系统,如需退出请输入00\n");
    fuhao='+';break; 
    case 2:printf("欢迎来到减法运算测试系统,如需退出请输入00\n");
    fuhao='-';break; 
    case 3:printf("欢迎来到乘法运算测试系统,如需退出请输入00\n");
    fuhao='*';break; 
    case 4:printf("欢迎来到除法运算测试系统,如需退出请输入00\n");
    fuhao='/';break; case 5:printf("本次测试结束,欢迎下次再来测试\n"); 
    printf("本次共做%d道题,其中%d道正确,正确率为%f\n",sum,right,precent); break; 
    default:printf("您的输入有误,请重新输入\n");choose(); 
    }
    }
    int ans(int x,int y,int choice)
    { 
    switch(choice) 
    { 
    case 1:z=x+y;break; 
    case 2:z=x-y;break; 
    case 3:z=x*y;break; 
    case 4:z=x/y;break; 
    } return(z);
    }
    void yunsuan()
    { 
    suiji(); 
    printf("%d%c%d=",x,fuhao,y); 
    z=ans(x,y,choice); 
    answer=scan(); 
    if(z==answer) 
    { 
    printf("正确\n"); 
    right++;
    sum++; 
    yunsuan(); 
    } else if(answer==00) 
    choose(); 
    else 
    { 
    printf("不正确, 正确答案为%5d\n",z); 
    sum++; 
    yunsuan(); 
    }
    }
    void main()
    { 
    printf("欢迎来到四则运算测试系统\n"); 
    choose(); 
    while(choice!=5) 
    yunsuan();
    }

  • 相关阅读:
    二 .数据库(Data)操作
    一. 数据库(Data)基础
    五种IO/模型
    并发编程 (协程)
    七.并发编程 (线程池,返回值,回调函数)
    六.并发编程 (线程对列)
    五.并发编程 (线程事件)
    四.并发编程 (线程信号量)
    三.并发编程 (线程锁)
    二.并发编程 (程序中线程操作)
  • 原文地址:https://www.cnblogs.com/ruishao13/p/4383964.html
Copyright © 2020-2023  润新知