c programming 1

floating point exception the reason is divided by zero.

after a afternoon's hard working, i get it ,so be aware of the variables that has different affect in different areas.

#include<stdio.h>
#include<time.h>
#include<math.h>
#include<stdlib.h>
int genQuestion(int);
int answerQuestion(int);
void response(int);
int main(){
         int i;
         int num;
         int dif;
         int ans;
        int count;
         srand(time(NULL));
         while(num<1||num>20)
        {
        printf("How many questions do you want : from (1-20)\n");
        scanf("%d", &num);
        }
        while(dif<1||dif>4)
        {
        printf("How many difficulty do you want: from (1-4)\n");
        scanf("%d", &dif);
        }
        for (i=0;i<num;i++)
        {
        int flag=0;
        printf("%s%d\n","Question",i+1);
        ans=genQuestion(dif);
        flag=answerQuestion(ans);
        response(flag);
        if(flag==1)
        count++;
        }
        printf("%s%d/%d\n","Your score is",count,num);
        return 0;
}

int genQuestion(int dif)
{
        int t=0;
        int opr1=0;
        int opr2=0;
        char a;
        int ans=0;
        t=rand()%4;
        if(t==0)a='+';
        if(t==1)a='-';
        if(t==2)a='*';
        if(t==3)a='/';
         switch (dif){
         case 1: opr1=rand()%10;
          opr2=rand()%10;
          break;
          case 2: opr1=rand()%50;
          opr2=rand()%50;
           break;
           case 3:opr1=rand()%100;
            opr2=rand()%100+1;
            break;
            case 4:opr1=rand()%200-100;
            opr2=rand()%200-100;
            break;
            }
  switch(t){
  case 0:ans=opr1+opr2;
  break;
  case 1:ans=opr1-opr2;
  break;
  case 2:ans=opr1*opr2;
  break;
  case 3:ans=opr1/opr2;
  break;
  }
printf("%d%c%d%c%c\n",opr1,a,opr2,'=','?');
return ans;
}

int answerQuestion(int ans){
        int flag=0;
        int out=0;
        printf("%s\n","Enter answer");
        scanf("%d",&out);
        if(ans==out)
        flag=1;
        else
        printf("%s%d\n","The correct answer is",ans);
        return flag;
}

void response(int flag){
        char *repg[]={"nice","great job","well done"};
        char *repb[]={"try again","oh,no","you can do it"};
        if (flag==1)
        printf("%s\n",repg[rand()%3]);
        else
        printf("%s\n",repb[rand()%3]);
}

srand()can't be included in the for loop


Executable files: Green
* Normal file : Normal
* Directory: Blue
* Symbolic link : Cyan
* Pipe: Yellow
* Socket: Magenta
* Block device driver: Bold yellow foreground, with black background
* Character device driver: Bold yellow foreground, with black background
* Orphaned syminks : Blinking Bold white with red background
* Missing links ( … and the files they point to) : Blinking Bold white
with red background

* Archives or compressed : Red (.tar, .gz, .zip, .rpm)
* Image files : Magenta (.jpg, gif, bmp, png, tif)



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值