zoukankan      html  css  js  c++  java
  • 练习1——四则运算

    源代码:

    #include<stdio.h>
    #include<stdlib.h>
    #include<time.h>
    main()
    {   
    int a,b,c,d,i,j=0,n;   printf("请输入题数:");   scanf("%d",&n); //n选择产生题数   srand(time(NULL));   for(i=1;i<=n;i++)   {     b=rand()%100; //产生一个随机数     c=rand()%100;     a=rand()%4;     switch(a)     {       case 0:
            printf(
    "%d+%d=",b,c);         scanf("%d",&d);         if(d==b+c){           printf("正确\n");           j++;} //判断对错,累计答对题数         else printf("错误\n");         break;       case 1:       if(b>=c){ //确保不出现负数         printf("%d-%d=",b,c);         scanf("%d",&d);         if(d==b-c){           printf("正确\n");           j++;}         else printf("错误\n");         break;}       else if(b<c){         printf("%d-%d=",c,b);         scanf("%d",&d);         if(d==c-b){           printf("正确\n");           j++;}         else printf("错误\n");        break;}       case 2:       printf("%d*%d=",b,c);       scanf("%d",&d);       if(d==b*c){         printf("正确\n");         j++;}       else printf("错误\n");       break;       case 3:       printf("(精确为整数)%d/%d=",b,c);       scanf("%d",&d);       if(d==b/c){         printf("正确\n");         j++;}       else printf("错误\n");       break;      }
        }    printf(
    "\n恭喜,%d题答对了%d题!",n,j);
    }

    本次练习是一个较为简单的小程序,花费40分钟完成。但是功能仍然很不完善,有很多不足,如除法不能取余数,只能是简单的2个整数的四则运算,没有括号等优先级问题等!

  • 相关阅读:
    A1151 LCA in a Binary Tree (30分)
    A1150 Travelling Salesman Problem (25分)
    A1069 The Black Hole of Numbers (20分)
    A1149 Dangerous Goods Packaging (25分)
    A1148 Werewolf
    A1147 Heaps (30分)
    Ubuntu下git,gitlab团队协作
    如何查看JDK_API 2019.2.23
    linux_day1 (腾老师)2019年3月25日18:11:43(CentOs)
    jpa_缓存
  • 原文地址:https://www.cnblogs.com/dbssb/p/4369464.html
Copyright © 2011-2022 走看看