• 练习一


    四则运算

    随机生成0~99的两个数进行四则运算

    要注意除法分母为0的情况

    预计用时30分钟

    实际用时60分钟

    代码:

    #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("四则运算练习(结果取整数):(输入-10000结束程序) ");
     while(Answer!=-1000)
     {
      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("Right! ");
      }
      else
      {
       printf("Wrong! ");
      }
     }
    }

  • 相关阅读:
    支付宝支付
    django之contenttype
    vue 项目搭建 及基础介绍
    redis续
    1012
    1009
    灾后重建
    FLOYD判圈
    1007
    1006
  • 原文地址:https://www.cnblogs.com/wuzijian/p/4368534.html
Copyright © 2020-2023  润新知