C语言:一个能自动生成小学四则运算题目的程序

完成这个程序,半个小时内完成了,这个程序,可以自动生成小学简易的四则运算,提供菜单让用户选择,然后判断加减乘除,判断答对答错的题目个数,用户同时也可以重新选择继续答题或重新选择或退出程序。

源程序:

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

原文地址:https://www.cnblogs.com/ys1101/p/4368103.html