四则运算

#include <stdio.h>

#include <stdlib.h>

#include <time.h>

#include <assert.h>

int comp(int);

int init(void);

int main(int argc, char* argv[])

{

int in;

do

{

init();

scanf("%d",&in);

assert(in>=0&&in<=4);

system("cls");

if (in!=0) comp(in);

}

while (in!=0);

return 0;

}

int init(void)

{

 system("cls");

 printf("1. 加法\n2.减法\n3. 乘法\n4.除法\n0. EXIT\n");

 return 0;

}

int comp(int op)

{

int a,b,c; float cfd;

printf("Input \'00\' to exit!\n\n");

srand(time(NULL));

cfd=(float)(a/b);

do

{

a=rand()%100+1;

b=rand()%100+1;

switch(op)

{

case 1:printf("%d+%d=",a,b);

b=a+b; break;

case 2:while (a-b<0)

{

a=rand()%100+1;

b=rand()%100+1;

}

printf("%d-%d=",a,b);

b=a-b;

break;

case 3:printf("%d*%d=",a,b);

b=a*b;

break;

case 4:while (a<b||cfd*b!=a)

{

a=rand()%100+1;

b=rand()%100+1;

cfd=a/b;

}

printf("%d/%d=",a,b);

b=a/b;

break;

}

scanf("%d",&c);

b==c?printf("\nRight!\n"):printf("\nWrong! It's %d\n",b);

}

while (c!=-1);

return 0;

}

posted @ 2015-04-01 16:14  06刘泽豪  阅读(129)  评论(1编辑  收藏  举报