本人新手求解答。《有关单项选择系统》
运行到答案输入后出错要用的文件在下楼
程序如下:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<time.h>
struct single_problem
{
char content[100];
char selectA[100];
char selectB[100];
char selectC[100];
char selectD[100];
}problem;
FILE *fp_read;
FILE *fp_write;
FILE *fp_answer;
FILE *fp_check;
int tishu=0;
int zongtishu=95;
void main()
{
void printquestion();
void answer();
int num=0;
int count=0;
printf("****************************************************\n");
printf("** **\n");
printf("** 1 题库维护 **\n");
printf("** **\n");
printf("** 2 答题 **\n");
printf("** **\n");
printf("** 3 查看全部题目 **\n");
printf("** **\n");
printf("** 4 退出 **\n");
printf("** **\n");
printf("** 5 帮助 **\n");
printf("** **\n");
printf("****************************************************\n");
if((fp_read=fopen("16-input_single.txt","r"))==NULL)
{
printf("文件16-input_single.txt打开错误!\n");
return;
}
if((fp_answer=fopen("16-input-answer.txt","r"))==NULL)
{
printf("文件16-input-answer.txt打开错误!\n");
return;
}
if((fp_check=fopen("答案.txt","w+"))==NULL)
{
printf("文件16-input-answer.txt打开错误!\n");
return;
}
if((fp_write=fopen("题库.txt","w+"))==NULL)
{
printf("文件题库.txt打开错误!\n");
return;
}
while(!feof(fp_read))
{
fscanf(fp_read,"%s",problem.content);
fscanf(fp_read,"%s",problem.selectA);
fscanf(fp_read,"%s",problem.selectB);
fscanf(fp_read,"%s",problem.selectC);
fscanf(fp_read,"%s",problem.selectD);
fwrite(&problem,sizeof(struct single_problem),1,fp_write);
}
while(!feof(fp_answer))
{
fscanf(fp_answer,"%s",problem.selectA);
fscanf(fp_answer,"%s",problem.selectB);
fscanf(fp_answer,"%s",problem.selectC);
fscanf(fp_answer,"%s",problem.selectD);
fwrite(&problem,sizeof(struct single_problem),1,fp_check);
}
if((fp_read=fopen("题库.txt","r"))==NULL)
{
printf("题库.txt打开错误!\n");
return;
}
if((fp_check=fopen("答案.txt","r"))==NULL)
{
printf("答案.txt打开错误!\n");
return;
}
scanf("%d",&num);
switch(num)
{
case 1:amend();break;
case 2:answer();break;
case 3:printquestion();break;
case 4:;break;
case 5:help();break;
}
fclose(fp_read);
fclose(fp_write);
fclose(fp_answer);
fclose(fp_check);
}
void printquestion()
{
while(!feof(fp_read))
{
fread(&problem,sizeof(struct single_problem),1,fp_read);
printf("%s\n",problem.content);
printf("%s\n",problem.selectA);
printf("%s\n",problem.selectB);
printf("%s\n",problem.selectC);
printf("%s\n",problem.selectD);
}
}
void answer()
{
void checkanswer(int,char);
struct temp_problem
{
char timu[100];
char A[100];
char B[100];
char C[100];
char D[100];
}problem1[200]={"0","0","0","0","0"};
int n;
int i,a[200];
char answer;
int t;
printf("请选择要答题的数量。(1至%d)\n",zongtishu);
scanf("%d",&n);
while(n>zongtishu||n<1)
{
printf("请重新输入1至%d的数量。\n",zongtishu);
scanf("%d",&n);
}
printf("\n");
srand(time(NULL));
for(i=0;i<n;i++)
a[i]=rand()%(zongtishu-1+1)+1;
for(i=0;i<n;i++)
{
fseek(fp_write,a[i]*sizeof(struct single_problem),0);
fread(&problem1[i],sizeof(struct single_problem),1,fp_write);
printf("%s\n%s\n%s\n%s\n%s\n",problem1[i].timu,problem1[i].A,problem1[i].B,problem1[i].C,problem1[i].D);
printf("请输入答案:");
for(t=0;t<1;t++)
{
getchar();
scanf("%c",&answer);
if((answer<64&&answer>69)||(answer<96&&answer>101))
{
t=0;
printf("请重新输入正确答案序号。\n");
}
}
checkanswer(a[i],answer);
}
}
void checkanswer(int b,char a)
{
struct ch
{
char an[20];
}che[200]={"\0"};
int i=0,j=0,n;
char bn[200]="\0";
char anw[200][20]={"0"};
rewind(fp_check);
while(!feof(fp_check))
{
fscanf(fp_check,"%s",che[i].an[20]);
fwrite(anw[i],sizeof(struct single_problem),1,fp_check);
i++;
}
n=i;
for(i=0;i<n;i++)
{
for(j=0;anw[i][j]!="\0";j++)
{
if(anw[i][j]==".")
bn[i]=che[i].an[j+1];
break;
}
}
// for(i=0;i<n;i++)
// printf("答案正确!\n");
if(a==bn[b])
printf("答案正确!\n");
else printf("答案错误!\n");
}
[ 本帖最后由 qq520520 于 2012-9-9 20:11 编辑 ]