#include "math.h"
#include "stdlib.h"
#include "stdio.h"
/* 0: +; 1:-; 2:*; 3: /; */
char cal_char_symbol[][5] =
{
"+",
"-",
"*",
"/"
};
int radomchar()
{
int i;
i=rand()%4;
return i;
}
int radomnum()
{
int num ;
num=rand()%101;
return num;
}
void leftShift(float *data, int srcPos, int shiftNum)
{
int i;
for(i = 0; i < shiftNum; i++)
{
data[srcPos + i] = data [srcPos + i +1];
}
return;
}
void leftShift1(int *data, int srcPos, int shiftNum)
{
int i;
for(i = 0; i < shiftNum; i++)
{
data[srcPos + i] = data [srcPos + i +1];
}
return;
}
void doplease(void)
{
float calNum[4],calNumBak[4],realanswer,answer;
int calSym[5],calSymBak[5];
int i,n,y=0,r=0,l;
srand((unsigned)time(NULL));
for(i=0;i<4;i++)
{
calNum[i] = radomnum();
calNumBak[i] =calNum[i];
}
for(i=0;i<3;i++)
{
calSym[i] = radomchar();
calSymBak[i] = calSym[i];
}
l = 0;
for(n=0;n<2;n++)
{
for(i=0;i<3 - l ;i++)
{
if(calSym[i]<2 && (n == 0))
continue;
switch(calSym[i])
{
case 0:
realanswer = calNum[i] + calNum[i+1];
break;
case 1:
realanswer = calNum[i] - calNum[i+1];
break;
case 2:
realanswer = calNum[i]*calNum[i+1];
break;
case 3:
realanswer =(float)calNum[i]/(float)calNum[i+1];
break;
default:
break;
}
l++;
calNum[i] = realanswer;
leftShift(calNum, i+1, 3 -(i + 1));
leftShift1(calSym, i, 2 - i);
i--;
if(l == 3)
break;
}
}
realanswer = calNum[0];
#if 1
printf("\nPlease calculate: %.2f%s%.2f%s%.2f%s%.2f = ",calNumBak[0], cal_char_symbol[calSymBak[0]],
calNumBak[1], cal_char_symbol[calSymBak[1]],
calNumBak[2], cal_char_symbol[calSymBak[2]],
calNumBak[3]);
scanf("\n%d",&answer);
if (realanswer==answer)
{
printf("congratulate\t");
y++;
getch();
}
else
{
printf("error\t");
r++;
printf("\nThe right answer is : %.2f",realanswer);
getch();
}
#endif
}
main()
{
int loop;
while(1)
{
printf("\nWell come to this system\n");
for(loop=0;loop<10;loop++)
{
doplease();
}
getch();
}
}