代码改变世界

小学生四则运算

2015-03-27 13:50  35曾威  阅读(131)  评论(0编辑  收藏  举报
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
char OperatorChar()                               
{
 int op;
 op=rand()%4;
 switch(op)
 {
 case 0:
  return '+';
 case 1:
  return '-';
 case 2:
  return '*';
 case 3:
  return '/';
 }
}
int Calculation(int a,char op,int b)               
{
 switch(op)
 {
 case '+':
  return a+b;
 case '-':
  return a-b;
 case '*':
  return a*b;
 case '/':
  return (int)a/b;
 }
}
main()
{
 int a;
 int b;
 int Answer;
 char op;
 srand((unsigned)time(NULL));
 printf("小学生四则运算,输入0000结束程序\n");
 while(Answer!=0000)
 {
  op=OperatorChar();
  a=rand()%100;
  b=rand()%100;
  if((b==0)&&(op=='/'))
  {
   break;
  }
  printf("%d%c%d=",a,op,b);
  scanf("%d",&Answer);
  if(Answer==Calculation(a,op,b))
  {
   printf("NICE!\n");
  }
  else
  {
   printf("Sorry,wrong!\n");
  }
 }
}

感想、duang duang duang,day day up