单项选择题,自己编写题目时未响应
#include <stdio.h>#include<windows.h>
#include <stdlib.h>
#define LEN sizeof(struct test)
struct test
{
char que[200];
char answer1[200];
char answer2[200];
char answer3[200];
char answer4[200];
char ture;
struct test *next;
};
void initializelist(struct test *head)
{
head=(struct test *)malloc(sizeof(struct test));
head->next=NULL;
}
int zts(struct test *head) //将所有题目导入内存
{
FILE *fp;
struct test *p1=NULL;
char ch1[100];
int n=0;
struct test *temp=head;
if((fp=fopen("test.txt","r"))==NULL)//只读方式打开文件
{
printf("文件打开失败请检查test.txt文件。\n");
exit(0);
}
for(n=0;!feof(fp);n++){
temp->next=(struct test *)malloc(sizeof(struct test));
temp=temp->next;
temp->next=NULL;
fscanf(fp,"%s%s%s%s%s%s",temp->que,temp->answer1,temp->answer2,temp->answer3,temp->answer4,ch1);
temp->ture=ch1[0];
}
fclose(fp);
return n;//返回题目总数n
}
void exe1(struct test *head)
{
void exe2();
int main();
int n,m=0,i,tm,t=0,f=0;
char ans;
tm=zts(head); //题目数量
struct test *temp=head;
struct test *p1=head->next;
system("cls");
printf("请输入要答题数:");
scanf("%d",&n);
while(n<0||n>tm){
printf("输入的答题超过题目数量,或则小于0\n");
Sleep(1200);
system("cls");
printf("请重新输入答题数:");
scanf("%d",&n);
}
for(i=0;i<n;i++)//n为用户要答题数
{
printf("\n第%d题:\n",i+1);
printf("%s\n%s\n%s\n%s\n%s\n请输入答案:",p1->que,p1->answer1,p1->answer2,p1->answer3,p1->answer4);
while((getchar())!='\n');
scanf("%c",&ans);
if(toupper(ans)==p1->ture)//判断用户输入答案是否正确
{
printf("恭喜你答对了!\n\n");
t++;//回答正确统计
}
else
{
printf("对不起,你答错了。。。\n正确答案是:%c\n\n",p1->ture);
f++;//回答错误统计
}
p1=p1->next;
}
p1=head->next;
do
{
head=p1->next;
free(p1);
p1=head;
}while(p1!=NULL);//释放结构体占用内存
printf("*******************************\n");
printf("\n已完成测试!\n共做%d道题:%d错误,%d正确。\n正确率:%5.2f%%\n\n",n,f,t,(float)t/n*100);
printf("*******************************\n");
printf("\n\n1.继续答题;2.添加题目;3.返回主菜单;\n请选择:");
i=0;
scanf("%d",&i);
if(i==1)
exe1(temp);//继续答题
else
if(i==2)
exe2();//添加题目
else
main();//返回主菜单
}
void exe2()//实现用户自主输入题目
{
int main();
system("cls");
int n;
FILE *fp;
if((fp=fopen("test.txt","a"))==NULL)
{
printf("文件打开失败请检查test.txt文件。\n");
exit(0);
}
struct test *p1=NULL;
p1=(struct test *)malloc(LEN);//开辟内存空间
do
{
printf("请输入题目:");
scanf("%s",&p1->que);
}while(p1->que[0]=='\0');
do
{
printf("请输入选项A:");
scanf("%s",&p1->answer1);
}while(p1->answer1=='\0');
do
{
printf("请输入选项B:");
scanf("%s",&p1->answer2);
}while(p1->answer2=='\0');
do
{
printf("请输入选项C:");
scanf("%s",&p1->answer3);
}while(p1->answer3=='\0');
do
{
printf("请输入选项D:");
scanf("%s",&p1->answer4);
}while(p1->answer4=='\0');
printf("请输入答案:");
do
{
scanf("%c",&p1->ture);
p1->ture=toupper(p1->ture);
}while(p1->ture!='A'&&p1->ture!='B'&&p1->ture!='C'&&p1->ture!='D');
printf("\n确认输入题目?\n1.是。 2.否。\n");
scanf("%d",&n);
if(n==1)
{
fputc('\n',fp);
fputs(p1->que,fp);
fputc(' ',fp);
fputs("A.",fp);
fputs(p1->answer1,fp);
fputc(' ',fp);
fputs("B.",fp);
fputs(p1->answer2,fp);
fputc(' ',fp);
fputs("C.",fp);
fputs(p1->answer3,fp);
fputc(' ',fp);
fputs("D.",fp);
fputs(p1->answer4,fp);
fputc(' ',fp);
fputc(p1->ture,fp);
fclose(fp);
printf("保存成功!");
system("cls");
free(p1);
printf("是否继续添加?\n1.继续添加 2.返回主菜单\n");
scanf("%d",&n);
if(n==1)
exe2();
else
main();
}
else
{
free(p1);
exe2();
}
}
int main()
{
struct test head;
system("cls");
initializelist(&head);
int n;
printf("********************************\n");
printf("欢迎使用单项选择题标准化考试系统\n");
printf("********************************\n");
printf("\t1.开始答题。\n");
printf("\t2.添加试题。\n");
printf("\t3.退出系统。\n");
printf("\n请选择:");
scanf("%d",&n);
switch(n)
{
case 1:exe1(&head);break;
case 2:exe2();break;
case 3:printf("\ngoodbye!\n");
}while(n!=3);
}
test.zip
(3.26 KB)