|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
#include<stdio.h>
#include<stdlib.h>
#define LEN sizeof(struct Test)
void getInput(struct Test *test);
int put_test(int m);
void testing();
int total(void);
void remain();
void main();
struct Test
{
char que[100];
char answer1[100];
char answer2[100];
char answer3[100];
char answer4[100];
char ture[100];
struct Test *next;
};
struct Test *head;
int put_test(int m)
{
struct Test *test=NULL;
FILE *fp;
if((fp=fopen("test1.txt","r"))==NULL)
{
printf("打开文件失败!\n");
exit(0);
}
char ch,ture_answer[5];
int i,n=0,now,py;
do
{
py=1;
ch=fgetc(fp);
for(i=0;ch!='\n'&&ch!=-1;i++)
{
py++;
ch=fgetc(fp);
}
n++;
if(n==m)
{
if(test==NULL)
{
test=(struct Test *)malloc(LEN);
head=test;
test->next=NULL;
}
else
{
test->next=test;
test=(struct Test *)malloc(LEN);
test->next=NULL;
}
now=ftell(fp);
fseek(fp,(now-py-1),0);
fscanf(fp,"%s%s%s%s%s%s",&test->que,&test->answer1,&test->answer2,&test->answer3,&test->answer4,&ture_answer);
fseek(fp,now+1,0);
*test->ture=ture_answer[0];
break;
}
}while(!feof(fp));
fclose(fp);
return 0;
}
void testing()
{
int n,o_n,m,i,yes=0,no=0;
char user_answer[2];
struct Test *test;
printf("请输入您想答题的数目:\n");
scanf("%d",&n);
o_n=total();
if(n>o_n||n<0)
{
printf("输入错误!");
testing();
}
for(i=0;i<n;i++)
{
do
{
m=rand();
}while(m<=0&&m>o_n);
put_test(m);
test=head;
printf("第%d题:\n",i+1);
printf("%s\n%s\n%s\n%s\n%s\n请输入您的答案:",*test->que,*test->answer1,*test->answer2,*test->answer3,*test->answer4);
scanf("%s",&user_answer);
if(user_answer[0]==*test->ture)
{
printf("答案正确!");
yes++;
}
else
{
printf("答案错误!\n正确答案是:%s\n",test->ture);
no++;
}
}
test=head;
do
{
head=test->next;
free(test);
test=head;
}while(test!=NULL);
printf("测试结束!\n您的得分是:%d\n正确率:%.2f%%\n",yes,(float)yes/n*100);
remain();
}
int total(void)
{
FILE *fp;
if((fp=fopen("test1.txt","r"))==NULL)
{
printf("打开文件失败!");
exit(0);
}
int n=0;
char c;
c=fgetc(fp);
do
{
if(c=='\n')
n++;
c=fgetc(fp);
}while(!feof(fp));
return n;
}
void getInput(struct Test *test)
{
int i,n;
printf("你将输入多少道试题?\n");
scanf("%d",&n);
FILE *fp;
if((fp=fopen("test1.txt","a"))==NULL)
{
printf("打开文件错误!\n");
exit(0);
}
test=(struct Test *)malloc(LEN);
for(i=1;i<=n;i++)
{
printf("题目:");
scanf("%s",test->que);
printf("A.");
scanf("%s",test->answer1);
printf("B.");
scanf("%s",test->answer2);
printf("C.");
scanf("%s",test->answer3);
printf("D.");
scanf("%s",test->answer4);
printf("正确答案:");
scanf("%s",test->ture);
fputs(test->que,fp);
fputs(" A.",fp);
fputs(test->answer1,fp);
fputs(" B.",fp);
fputs(test->answer2,fp);
fputs(" C.",fp);
fputs(test->answer3,fp);
fputs(" D.",fp);
fputs(test->answer4,fp);
fputs(" 正确答案:",fp);
fputs(test->ture,fp);
fputc('\n',fp);
}
fclose(fp);
free(test);
remain();
}
void remain()
{
int flag;
printf("1.返回主菜单\n");
printf("2.退出程序\n");
printf("请输入你的选择:\n");
scanf("%d",&flag);
switch(flag)
{
case 1: main(); break;
case 2: exit(0);break;
default:printf("输入错误!");
}
}
void main()
{
struct Test test;
int flag;
printf("--------单项选择题标准化考试系统--------\n");
printf("1.录入试题\n");
printf("2.自主测试\n");
printf("3.退出程序\n");
printf("请输入你的选择:\n");
scanf("%d",&flag);
switch(flag)
{
case 1: getInput(&test); break;
case 2: testing(); break;
case 3: exit(0); break;
default:printf("输入错误!\n");
}
}
如果你的编译器什么提示也没有的话,一定要换一个好点的 main.c:5:22: warning: ‘struct Test’ declared inside parameter list will not be visible outside of this definition or declaration
5 | void getInput(struct Test *test);
| ^~~~
main.c: In function ‘put_test’:
main.c:61:29: warning: format ‘%s’ expects argument of type ‘char *’, but argument 3 has type ‘char (*)[100]’ [-Wformat=]
61 | fscanf(fp,"%s%s%s%s%s%s",&test->que,&test->answer1,&test->answer2,&test->answer3,&test->answer4,&ture_answer);
| ~^ ~~~~~~~~~~
| | |
| char * char (*)[100]
main.c:61:31: warning: format ‘%s’ expects argument of type ‘char *’, but argument 4 has type ‘char (*)[100]’ [-Wformat=]
61 | fscanf(fp,"%s%s%s%s%s%s",&test->que,&test->answer1,&test->answer2,&test->answer3,&test->answer4,&ture_answer);
| ~^ ~~~~~~~~~~~~~~
| | |
| char * char (*)[100]
main.c:61:33: warning: format ‘%s’ expects argument of type ‘char *’, but argument 5 has type ‘char (*)[100]’ [-Wformat=]
61 | fscanf(fp,"%s%s%s%s%s%s",&test->que,&test->answer1,&test->answer2,&test->answer3,&test->answer4,&ture_answer);
| ~^ ~~~~~~~~~~~~~~
| | |
| char * char (*)[100]
main.c:61:35: warning: format ‘%s’ expects argument of type ‘char *’, but argument 6 has type ‘char (*)[100]’ [-Wformat=]
61 | fscanf(fp,"%s%s%s%s%s%s",&test->que,&test->answer1,&test->answer2,&test->answer3,&test->answer4,&ture_answer);
| ~^ ~~~~~~~~~~~~~~
| | |
| char * char (*)[100]
main.c:61:37: warning: format ‘%s’ expects argument of type ‘char *’, but argument 7 has type ‘char (*)[100]’ [-Wformat=]
61 | fscanf(fp,"%s%s%s%s%s%s",&test->que,&test->answer1,&test->answer2,&test->answer3,&test->answer4,&ture_answer);
| ~^ ~~~~~~~~~~~~~~
| | |
| char * char (*)[100]
main.c:61:39: warning: format ‘%s’ expects argument of type ‘char *’, but argument 8 has type ‘char (*)[5]’ [-Wformat=]
61 | fscanf(fp,"%s%s%s%s%s%s",&test->que,&test->answer1,&test->answer2,&test->answer3,&test->answer4,&ture_answer);
| ~^ ~~~~~~~~~~~~
| | |
| char * char (*)[5]
main.c: In function ‘testing’:
main.c:93:26: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘int’ [-Wformat=]
93 | printf("%s\n%s\n%s\n%s\n%s\n请输入您的答案:",*test->que,*test->answer1,*test->answer2,*test->answer3,*test->answer4);
| ~^ ~~~~~~~~~~
| | |
| char * int
| %d
main.c:93:30: warning: format ‘%s’ expects argument of type ‘char *’, but argument 3 has type ‘int’ [-Wformat=]
93 | printf("%s\n%s\n%s\n%s\n%s\n请输入您的答案:",*test->que,*test->answer1,*test->answer2,*test->answer3,*test->answer4);
| ~^ ~~~~~~~~~~~~~~
| | |
| char * int
| %d
main.c:93:34: warning: format ‘%s’ expects argument of type ‘char *’, but argument 4 has type ‘int’ [-Wformat=]
93 | printf("%s\n%s\n%s\n%s\n%s\n请输入您的答案:",*test->que,*test->answer1,*test->answer2,*test->answer3,*test->answer4);
| ~^ ~~~~~~~~~~~~~~
| | |
| char * int
| %d
main.c:93:38: warning: format ‘%s’ expects argument of type ‘char *’, but argument 5 has type ‘int’ [-Wformat=]
93 | printf("%s\n%s\n%s\n%s\n%s\n请输入您的答案:",*test->que,*test->answer1,*test->answer2,*test->answer3,*test->answer4);
| ~^ ~~~~~~~~~~~~~~
| | |
| char * int
| %d
main.c:93:42: warning: format ‘%s’ expects argument of type ‘char *’, but argument 6 has type ‘int’ [-Wformat=]
93 | printf("%s\n%s\n%s\n%s\n%s\n请输入您的答案:",*test->que,*test->answer1,*test->answer2,*test->answer3,*test->answer4);
| ~^ ~~~~~~~~~~~~~~
| | |
| char * int
| %d
main.c:94:25: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘char (*)[2]’ [-Wformat=]
94 | scanf("%s",&user_answer);
| ~^ ~~~~~~~~~~~~
| | |
| | char (*)[2]
| char *
main.c: At top level:
main.c:137:6: error: conflicting types for ‘getInput’; have ‘void(struct Test *)’
137 | void getInput(struct Test *test)
| ^~~~~~~~
main.c:5:6: note: previous declaration of ‘getInput’ with type ‘void(struct Test *)’
5 | void getInput(struct Test *test);
| ^~~~~~~~
main.c:197:6: warning: return type of ‘main’ is not ‘int’ [-Wmain]
197 | void main()
| ^~~~
|
|