zoukankan      html  css  js  c++  java
  • 实验一修改(输入非数字死循环,输入小数点进入死循环)

    #include<stdio.h>
    #include<stdlib.h>
    #include<time.h>
    #include<math.h>
    #define N 10 //题目数量
    main()
    {
    clock_t start = clock();
    char opChar;
    double a,b,system;
    double user,user1;
    int op;
    int i,rightType,wrongType;
    int rightNumber=0;
    int flag=0;
    int ret;
    srand(time(NULL));
    do{
    for(i=0;i<N;i++)
    {
    a=rand()%10+1;
    b=rand()%10+1;
    op=rand()%4+1;
    rightType=rand()%4+1;
    wrongType=rand()%4+1;
    switch(op)
    {
    case 1:
    opChar='+';
    system=a+b;
    break;
    case 2:
    opChar='-';
    system=a-b;
    break;
    case 3:
    opChar='*';
    system=a*b;
    break;
    case 4:
    opChar='/';
    if(fabs(b)<=1e-7)
    {
    printf(" division by zero ");
    }
    else{
    user=(double)a/b;
    }
    break;
    default:
    printf("Unknown operator!");
    }
    printf("%.0lf %c %.0lf= ? ",a,opChar,b);
    ret=scanf("%lf",&user1);
    if(ret!=1)
    {
    printf("输入错误,应输入数字 ");
    while(getchar()!=' ');
    printf("请重新输入 ");
    scanf("%lf",&user1);
    }
    if(user1==system || user1==user)
    {
    switch(rightType)
    {
    case 1:
    printf(" very good! ");
    break;
    case 2:
    printf(" excellent! ");
    break;
    case 3:
    printf(" nice work! ");
    break;
    case 4:
    printf(" keep up the good work! ");
    break;
    default:
    printf(" WrongType!");
    }
    rightNumber++;
    }
    else
    {
    printf(" wrong type! ");
    }

    }
    printf(" Total score is %d ",rightNumber*N);
    printf(" Rate of correnctness is %d %% ",rightNumber*N);
    if(rightNumber*N<75)
    {
    printf(" once again! ");
    rightNumber=0;
    flag=1;
    }
    }while(flag);
    clock_t finish = clock();
    double duration = (double)(finish - start) / CLOCKS_PER_SEC;
    printf(" 总共用时:");
    printf( " %f seconds ", duration );
    }

  • 相关阅读:
    dbutils关于连接维护的问题Q
    触发器
    mysql的full join的实现
    mysql exists 和 in的效率比较
    浏览器禁用Cookie后的session处理
    自定义org.apache.commons.beanutils的类型转换器
    Java中形参个数可变的方法
    递归方法的重要规定——递归一定要向己知方向递归
    抽象工厂模式——肯德基消费
    异常链
  • 原文地址:https://www.cnblogs.com/case1/p/4384164.html
Copyright © 2011-2022 走看看