自动生成简单四则运算

我这篇代码来自:http://www.cnblogs.com/ys1101/p/4368103.html。是在windows下,用c语言在vs2017下编译的,没有bug,它可以实现由用户自己选择生成题目的类型,并判断对错。

#include<stdio.h>
#include<math.h>
#include<windows.h>
int right=0;
int wrong=0;
void add()
{
 int a,b,c;
 a=rand()%100;
 b=rand()%100;
 printf("请回答:\n\t\t %d + %d = ",a,b);
 scanf("%d",&c);
 if(a+b==c)
 {
  printf("回答正确!\n");
  right++;
 }
 else
 {
  printf("回答错误!\n");
  wrong++;
 }
}
void minu()
{
 int a,b,c;
 a=rand()%100;
 b=rand()%100;
 printf("请回答:\n\t\t %d - %d = ",a,b);
 scanf("%d",&c);
 if(a-b==c)
 {
  printf("回答正确!\n");
  right++;
 }
 else
 {
  printf("回答错误!\n");
  wrong++;
 }
}
void mul()
{
 int a,b,c;
 a=rand()%100;
 b=rand()%100;
 printf("请回答:\n\t\t %d * %d = ",a,b);
 scanf("%d",&c);
 if(a*b==c)
 {
  printf("回答正确!\n");
  right++;
 }
 else
 {
  printf("回答错误!\n");
  wrong++;
 }
}
void di()
{
 int a,b,c;
 a=rand()%100;
 b=rand()%100;
 printf("请回答:\n\t\t %d / %d = ",a,b);
 scanf("%d",&c);
 if(a/b==c)
 {
  printf("回答正确!\n");
  right++;
 }
 else
 {
  printf("回答错误!\n");
  wrong++;
 }
}
void main()
{
 int choise;
 int con=0;
 printf("\n\t\t\t欢迎进入小学简易四则运算\n\n");
 while(1)
 {
  printf("请选择:\n");
  printf("\t\t\t  加法运算(请输入1)\n");
  printf("\t\t\t  减法运算(请输入2)\n");
  printf("\t\t\t  乘法运算(请输入3)\n");
  printf("\t\t\t  除法运算(请输入4)\n");
  printf("\t\t\t  退出运算(请输入5)\n");
  if(con==0)
   scanf("%d",&choise);
  switch(choise)
  {
  case 1:
   add();
   break;
  case 2:
   minu();
   break;
  case 3:
   mul();
   break;
  case 4:
   di();
   break;
  case 5:
   return;
  }
  printf("\n\t\t\t继续运算?(请输入1)\n");
  printf("\n\t\t\t重新选择?(请输入2)\n");
  printf("\n\t\t\t退出运算?(请输入3)\n");
  scanf("%d",&con);
  if(con==1)
   con=1;
  else if(con==2)
   con=0;
  else if(con==3)
   break;
  else
   printf("抱歉!,你输入的指令有误!请重新输入!\n");
 }
 printf("您总共完成了 %d 道题\n正确 %d 道\n错误 %d 道\n",right+wrong,right,wrong);
}

转载于:https://www.cnblogs.com/Ylove/p/7550910.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值