求教此程序不稳定原因
c语言吧
全部回复
仅看楼主
level 1
h604364120 楼主
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<conio.h>
#include<time.h>
#define FILENAME "record.txt"
#define true 1 char ID[7];
void InputID() /*检查ID是否符合2个字母4个数字的要求,循环至条件符合*/
{
int cou_n,cou_l,i;
while(true)
{
cou_n=0;
cou_l=0;
printf("Please enter your ID number:\n");
scanf("%s",ID);
for(i=0;ID[i]!='\0';i++)
{
if(ID[i]>='a' && ID[i]<='z' || ID[i]>='A' && ID[i]<='Z')
cou_l++;
else if(ID[i]>='0' && ID[i]<='9')
cou_n++;
}
if(cou_n==4 && cou_l==2)
break;
else
{
printf("The ID you entered is not valid.\n");
continue;
}
}
} void WriteIn(int Sco,int Time)/*写入ID试后的分数和所用时间*/
{
FILE *fp;
if(!(fp=fopen(FILENAME,"a")))
{
printf("Error");
exit(0);
}
fprintf(fp,"%s %d %d\n",ID,Sco,Time);
fclose(fp);
} void Test() /*随机产生随机数及运算的测试函数*/
{
int i,temp,First,Second,Std,RightAns=0,WrongAns=0,Qus[10][3],Ans[10],Trans[10];
char Op;
time_t StartTime,EndTime;
srand((unsigned)time(NULL));
for(i=0;i<10;i++)
{
switch(rand()%4)/*随机产生运算符*/
{
case 0:Op='+';break;
case 1:Op='-';break;
case 2:Op='*';break;
case 3:Op='/';break;
}
while(true)
{
First=rand()%100;/*产生随机数*/
Second=rand()%100;
if(Second>First)
{
temp=First;
First=Second;
Second=temp;
}
switch(Op)
{
case'+':Std=First+Second;break;
case'-':Std=First-Second;break;
case'*':Std=First*Second;break;
case'/':Std=First/Second;break;
}
if( Op=='/' && (First%Second!=0||Second==0))
continue;
if(Std>=100)
continue;

2012年06月07日 14点06分 1
level 1
h604364120 楼主
break;
}
Qus[i][0]=First;
Qus[i][1]=Op;
Qus[i][2]=Second;
Trans[i]=Std;
}
printf("Press Any Key to Begin");
getche();
system("cls");
StartTime=time(NULL);
for(i=0;i<10;i++)
{
while(true)
{
printf("%d%c%d=",Qus[i][0],Qus[i][1],Qus[i][2]);
scanf("%d",&Ans[i]);
if(Ans[i]>=100)
{
printf("Invalid value.\n");
continue;
}
if(Ans[i]==Trans[i])
RightAns++;
else
WrongAns++;
break;
}
}
EndTime=time(NULL);
system("cls");
printf("Prob Uransw Correct Answ\n");/*产生表格*/
for(i=0;i<10;i++)
printf("%2d%c%2d= %2d %2d\n",Qus[i][0],Qus[i][1],Qus[i][2],Ans[i],Trans[i]);
printf("You get %d , with %d right and %d wrong answers ,in %d second(s)\n",10*RightAns,RightAns,WrongAns,EndTime-StartTime);
WriteIn(10*RightAns,EndTime-StartTime);
system("pause");
}
void Check()
{
FILE *fp;
char str[7];
int Sco,Time,i=0,r;
fp=fopen("record.txt","r");
printf("Your previous records:\n");
while( !feof(fp) )
{
fgets(str,7,fp);
r=strcmp(ID,str);
if(r==0)
{
i=1;
fscanf(fp,"%d",&Sco);
fscanf(fp,"%d",&Time);
printf(" %s ",ID);
printf(" %d\t%d s\n",Sco,Time);
}
}
if(!i)
printf("No Messenge");
}
int main()
{
FILE *fp;
if(!(fp=fopen(FILENAME,"r")))
{
if(!(fp=fopen(FILENAME,"w")))
{ printf("error");
exit(0);}
fprintf(fp,"records:\n");
fclose(fp);
}
else
fclose(fp);
InputID();
while(true)
{
system("cls");
printf("You have 3 choices,please enter a select code:\n");
printf("\n 1 for Start a test");
printf("\n 2 for Check scores");
printf("\n 3 for Exit\n");
switch(getch())
{
case '1':
system("cls");
Test();
break;
case '2':
system("cls");
Check();
getche();
break;
case '3':
exit(0);
}
}
}

2012年06月07日 14点06分 2
level 8
你确定发的是完整的??????????????
2012年06月07日 16点06分 3
level 8
各种凌乱 我帮不了你了
2012年06月07日 17点06分 4
1