You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
tanrunti_001/课程设计(final).cpp

285 lines
7.4 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<windows.h>
#include<conio.h>
typedef struct exam_system //定义链表
{
char problem[150];
char answer[4][20];
char right;
char input;
struct exam_system *next;
}EXAM; //类型名自定义
void management(); //题目管理函数
void add(); //添加题目函数
void show(); //遍历题库函数
void preservation(); //保存文档函数
void change(); //修改题目函数
void exam(); //考试初始界面函数
void link(); //创建链表函数
void Answer(); //答题函数
int sum; //题目总数
struct exam_system *p1,*p2,*head;
FILE *fp;
int main()
{
link();
system("color 75"); //自定义background
exam();
return 0;
}
void link() //创建链表
{
head=(EXAM *)malloc(sizeof(EXAM)); //创建头节点,并给予一个动态内存
p2=p1=head; //连续赋值
p1->next=NULL; //令p1指向末尾
if((fp=fopen("D:\\exam.txt","r+"))==NULL)
{
printf("文件打开失败!!");
exit(0);
}
while(1)
{
p1=(EXAM*)malloc(sizeof(EXAM));
if(fread(p1,sizeof(EXAM),1,fp)==0) //读取信息
break; //否则退出
p2->next=p1; //与第一个节点连接
p2=p1; //p1与p2同时指向新开辟的节点
sum++;
p2->next=NULL; //另p2指向末尾
}
fclose(fp);
}
void exam() //考试界面函数
{
int x;
system("cls");
printf("\t\t*********欢迎来到单项选择题考试系统*********\n");
printf("\t\t* *\n");
printf("\t\t* 1.题目管理 *\n");
printf("\t\t* *\n");
printf("\t\t* 2.开始考试 *\n");
printf("\t\t* *\n");
printf("\t\t* 3.退出 *\n");
printf("\t\t* *\n");
printf("\t\t********************************************\n");
printf("\n");
printf("请选择:");
scanf("%d",&x);
switch(x)
{
case 1:management();break;
case 2:Answer();break;
case 3:exit(0);break;
default :printf("输入有误!\n");break;
}
}
void management() //题目管理函数
{
int x;
system("cls");
printf("\t\t************欢迎来到题目管理系统************\n");
printf("\t\t* *\n");
printf("\t\t* 1.新增题目 *\n");
printf("\t\t* *\n");
printf("\t\t* 2.遍历题库 *\n");
printf("\t\t* *\n");
printf("\t\t* 3.修改题目 *\n");
printf("\t\t* *\n");
printf("\t\t* 4.返回上一级 *\n");
printf("\t\t* *\n");
printf("\t\t********************************************\n");
printf("\n");
printf("请选择:");
scanf("%d",&x);
switch(x)
{
case 1:add();management();break;
case 2:show();management();break;
case 3:change();management();break;
case 4:;exam();break;
default :printf("输入有误!\n");break;
}
}
void preservation() //保存文档函数
{
EXAM *current=NULL;
current=head->next;
fp=fopen("D:\\exam.txt","w+");
do
{
fwrite(current,sizeof(EXAM),1,fp); //逐个存储
current=current->next; //当前指针指向下一个
}while(current!=NULL); //直到指向末尾
fclose(fp);
printf("\n保存成功!!");
getch();
}
void show() //遍历题库函数
{
int c,i;
EXAM *p=NULL;
system("cls");
if(sum==0)
{
printf("题库中没有题目!\n");
printf("按任意键继续!");
getch();
}
else
{
p=head;
i=0;
do
{
p=p->next;
printf("%d.%s\n",i+1,p->problem);
c=0;
while(c<4)
{
printf("%c:",c+'A');
printf("%s\n",p->answer[c++] );
}
printf("正确答案:");
printf("%c\n",p->right);
i=i+1;
printf("\n-------------------------------------------------------------\n");
}while(p->next!=NULL);
p=NULL;
printf("按任意键继续!\n");
getch();
}
}
void add() //题目添加函数
{
int x,i;
system("cls");
printf("\t\t**************欢迎来到题目管理系统****************\n");
do
{
p1=(EXAM *)malloc(sizeof(EXAM));
p2->next=p1; //相连p1、p2
p2=p1; //指向相同
printf("请输入第%d个题目",sum+1);
scanf("%s",p1->problem);
printf("请输入备选答案:\n");
for(i=0;i<4;i)
{
printf("%c)",i+'A');
scanf("%s",p1->answer[i++] );
}
printf("输入正确答案:");
p1->right =getchar();
scanf("%c",&p1->right );
sum+=1;
printf("\n");
printf("\t是否继续添加\n\t1.继续\n\t2.退出\n请选择:");
scanf("%d",&x);
if(x==2)
{
p2->next=NULL;
break;
}
system("cls");
}while(x==1);
preservation();
}
void change() //修改题目函数
{
int i,num;
EXAM *p1=NULL;
EXAM *p2=NULL;
show();
if(sum>=1)
{
p1=(EXAM *)malloc(sizeof(EXAM)); //为P1分配一个动态内存
p2=head;
printf("请选择修改的题目序号:");
scanf("%d",&num);
for(i=0;i<num-1;i++)
{
p2=p2->next;
} //此时p2终于依次挪到num-1处
printf("请输入题目:");
scanf("%s",p1->problem);
printf("请输入备选答案:\n");
for(i=0;i<4;)
{
printf("%c:",i+'A');
scanf("%s",p1->answer[i++] );
}
printf("输入正确答案:");
p1->right=getchar();
scanf("%c",&p1->right);
p1->next=p2->next->next; //此时新节点将代替原节点
p2->next=p1; //p1、p2连接
p1=NULL;
p2=NULL; //最后都置为末尾
preservation();
}
}
void Answer() //答题函数
{
int i,j,c;
int n;
int score=0;
EXAM *p=NULL;
printf("祝你考试顺利!!\n");
system("cls");
if(sum>0)
{
i=0;
p=head;
printf("请输入你需要的题量:");
scanf("%d",&n);
do
{
p=p->next; //更新指针,输出下一个结构体的数据
printf("%d.%s\n",i+1,p->problem);
c=0;
while(c<4)
{
printf("%c:",c+'A');
printf("%s\n",p->answer[c++] );
}
printf("请输入你的答案:");
p->input=getchar();
scanf("%c",&p->input);
if(p->right==p->input)
{
printf("\n恭喜!!你的答案是 %c ,正确答案是 %c ",p->input,p->right);
score+=1;
}
else
{
printf("\n太可惜了~你的答案是 %c ,正确答案是 %c ",p->input,p->right);
}
i=i+1;
printf("\n----------------------------------------------------------\n");
if(i==n)
break;
}while(p->next!=NULL);
printf("考试结束!\n");
printf("总题数:%d\n正确数:%d\n",n,score);
getch();
;
}
else
printf("题库中没有题目!\n");
printf("按任意键继续!\n");
getchar();
}