我又改了一下,我就是想做个,四个数的加减乘除运算,想随机出十道题可总是做六道就出来了
#include "math.h"
#include "stdlib.h"
/* 0: +; 1:-; 2:*; 3: /; */
char cal_char_symbol[][5] =
{
"+",
"-",
"*",
"/"
};
void mydelay()
{
unsigned long i, n;
for(i = 0; i < 50000000; i++)
{
n = n+i;
}
}
int radomchar()
{
int i;
mydelay();
srand((unsigned)time(NULL));
i=rand()%4;
return i;
}
int radomnum()
{
int i,num = 0;
/*sleep(1);*/
mydelay();
srand((unsigned)time(NULL));
while(num == 0 )
num=rand()%100;
return num;
}
void leftShift(int *data, int srcPos, int shiftNum)
{
int i;
for(i = 0; i < shiftNum; i++)
{
data[srcPos + i] = data [srcPos + i +1];
}
return;
}
main()
{
int calNum[4], calSym[3];
int calNumBak[4],calSymBak[3];
int i,n,l,k,yes,no,realanswer,answer,loop;
while(1)
{
for(loop=0;loop<10;loop++)
{
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 = calNum[i]/calNum[i+1];
break;
default:
break;
}
l++;
calNum[i] = realanswer;
leftShift(calNum, i+1, 3 -(i + 1));
leftShift(calSym, i, 2 - i);
i--;
if(l == 3)
break;
}
}
realanswer = calNum[0];
#if 1
printf("\nPlease calculate: %d%s%d%s%d%s%d = ",calNumBak[0], cal_char_symbol[calSymBak[0]],
calNumBak[1], cal_char_symbol[calSymBak[1]],
calNumBak[2], cal_char_symbol[calSymBak[2]],
calNumBak[3]);
scanf("%d",&answer);
if (realanswer==answer)
printf("congratulate");
else
printf("error");
#endif
}
printf("\nContinue,yes or no!=");
scanf("%d",&k);
if(k=yes)
printf("\nPlease calculate: %d%s%d%s%d%s%d = ",calNumBak[0], cal_char_symbol[calSymBak[0]],
calNumBak[1], cal_char_symbol[calSymBak[1]],
calNumBak[2], cal_char_symbol[calSymBak[2]],
calNumBak[3]);
scanf("%d",&answer);
if (realanswer==answer)
printf("congratulate");
else
printf("error");
getch();
getch();
}
}
----------------解决方案--------------------------------------------------------