给小朋友做加减运算的程序

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main()
{
 char ch;
 while(1)
 {
  printf("---------\n");
  printf("做题请按g\n");
  printf("退出请按x\n");
  printf("---------\n");
  scanf("%c",&ch);
  getchar();
  if(ch=='x')
  {break; }
  else if(ch=='g')
  { 
   int x,y,op,r,input; 
   x=rand()%10;
   y=rand()%10; 
   switch(rand()%2)
   {   case 0:  
   op='+'; 
   r=x+y; 
   break; 
   default:  
    op='-'; 
    if(x<y)    {//交换  
     r=x;  
     x=y;  
     y=r;  
    }
    r=x-y;  
    break;  
   }
   printf("%d%c%d=",x,op,y);
   scanf("%d",&input);
   if(input==r)
   {   
    printf("真聪明,做对啦!\n");
 
   }
   else
   {  
    printf("好笨啊,做错啦!\n");
   } 
   printf("\n");
  }
 }
 return 0;
}

你可能感兴趣的:(程序)