程序设计分两部分:1、C语言结构体和链表学习内容及重点;2、成绩管理系统实现的功能、设计方法。
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<conio.h>
#include<windows.h>
#define LEN sizeof(struct Exam)
void subj();
void initquestion();
void show();
void cut0();
void cut1();
void save();
void change();
void exam();
void inlist();
void maketest();
void showgrade();
void passwords();
struct Exam
{
char ques[100]; //题目
char ans[4][15]; //备选答案
char right; //正确选项
char write;
struct Exam * next;
};
int sum;//题目总数
//int n=1;//链表长度
struct Exam *p1=NULL,*p2=NULL,*head=NULL;
FILE *fp;
int main()
{
inlist();
system("color 74");
exam();
return 0;
}
void subj()//**********************题目管理
{
int x;
system("cls"); //为了美观加入清屏
printf("-------------------------------------------------------------------------------\n");
printf("\t\t\t ##欢迎来到题目管理系统##\n");
printf("\n");
printf("* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\n");
printf(" ************\n");
printf(" *1.新增题目*\n");
printf(" ************\n");
printf(" ************ ************\n");
printf(" *2.删除题目* *3.修改题目*\n");
printf(" ************ ************\n");
printf(" ************ ************\n");
printf(" *4.查看题目* ***5.保存***\n");
printf(" ************ ************");
printf(" \t\t\t\t\t\t\t\t ************\n");
printf(" 6.返回上一级\n");
printf(" ************\n");
printf("请选择:");
scanf("%d",&x);
switch(x)
{
case 1:initquestion();subj();break;
case 2:if(sum==0){printf("题库中没有题目!\n按任意键继续!");getch();subj();}
else if(sum==1)cut0();
else cut1();subj();break;
case 3:change();subj();break;
case 4:show();subj();break;
case 5:save();subj();break;
case 6:;exam();break;
default :printf("输入有误!\n");break;
}
}
void begineExam()//******************开始考试
{
int i;
system("cls");
printf("\t\t\t欢迎来到考试系统!\n");
for(i=3;i>0;i--)
{
printf("\t\t\t%d秒后开始考试\n",i);
Sleep(1000);
system("cls");
}
printf("go,开始考试!");
Sleep(1000);
system("cls");
printf("祝同学们考试顺利!!\n");
system("cls");
maketest();
}
void inlist()//**********************创建链表
{
head=(struct Exam*)malloc(LEN); //开辟一个节点,让head指向.
p2=p1=head;//使p1,p2也是指向新开辟的节点
p1->next=NULL;//使节点的next指向空
if((fp=fopen("tt.txt","rb+"))==NULL)fp=fopen("tt.txt","wb+");
while(1)
{
p1=(struct Exam*)malloc(LEN); //给p1开辟新节点
if(fread(p1,LEN,1,fp)==0)break;//读不出内容就跳出循环
p2->next=p1;//与第一个节点连接
p2=p1;//p1与p2同时指向新开辟的节点
sum++;//总题目数加一
p2->next=NULL;
}
fclose(fp);
}
void exam()//************考试系统
{
int x;
system("cls"); //为了美观加入清屏
printf("* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\n");
printf(" *欢迎进入考试系统*\n");
printf("* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\n");
printf(" * ****************** * \n");
printf("\n");
printf(" * 1.题目管理 * \n");
printf("\n");
printf(" * 2.开始考试 * \n");
printf("\n");
printf(" * 3.退出 * \n");
printf("\n");
printf(" * ****************** * \n");
printf("* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\n");
printf("\n");
printf("请选择:");
scanf("%d",&x);
switch(x)
{
case 1:passwords();subj();break;
case 2:begineExam();break;
case 3:exit(0);break;
default :printf("输入有误!\n");break;
}
}
void passwords()//******************密码管理
{
int i,x;
char a,v;
system("cls");
printf("\t\t亲,请输入密码:");
v=getch();
printf("*");
if(v!=49)
{
for(i=0,x=3;i<11,x>0;i++,x--)
{
printf("\n密码错误,请重新输入(你还有%d次机会!):",x);
a=getch();
printf("*");
if(a==49)
{
printf("密码正确");
printf("\t>o<\n");
break;
}
}
if(x==0)exit(0);
}
}
void save()//***************** 保存
{
struct Exam *pt=NULL;
pt=head->next;
fp=fopen("tt.txt","wb+"); //以清空原链表方式打开,对链表操作后在填入
do
{
fwrite(pt,LEN,1,fp); //链表里一个单元一个单元的往里存
pt=pt->next;
}while(pt!=NULL);
fclose(fp);
printf("\n已保存\n按任意键继续");
getch();
}
void show()//********************显示文本内容
{
int c,i;//c,选项ABCD有关
struct Exam *p=NULL;
system("cls");
if(sum==0){printf("题库中没有题目!\n");printf("按任意键继续!");getch();}
else
{
p=head;
i=0;//i,题目序号。
do
{
p=p->next;//指向第一个存数据的节点
printf("%d.%s\n\n",i+1,p->ques);
c=0;
while(c<4)
{
printf("%c:",c+'A');
printf("%s\n",p->ans[c++] );
}
printf("正确答案:");
printf("%c\n",p->right);
i=i+1;
printf("-------------------------------------------------------------\n");
}while(p->next!=NULL);
p=NULL;
printf("按任意键继续!\n");
getch();
}
}
void initquestion()//******************添加系统.
{
int x,i;//x,判断是否继续添加题目。i,与ABCD有关
system("cls");
printf("\t\t\t###欢迎来到题目添加系统###\n");
do
{
p1=(struct Exam*)malloc(LEN); //链表指针往后挪开辟新单元
p2->next=p1;
p2=p1;
printf("请输入第%d个题目:",sum+1);
scanf("%s",p1->ques);
printf("请输入备选答案:\n");
for(i=0;i<4;)
{
printf("%c:",i+'A');
scanf("%s",p1->ans[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; //选择2跳出循环
break;
}
system("cls");
}while(x==1);
save();
}
void cut1()//************删除题目(有至少两题)
{
int i,num=0;
struct Exam *pi=NULL;
show();
printf("\n请输入要删除的题号\n");
scanf("%d",&num);
pi=head;
for(i=0;i<num-1;i++)
{
pi=pi->next;
}
pi->next=pi->next->next;
pi=NULL;
sum--;
save();
}
void cut0()//***********删除题目,剩一题
{
struct Exam *p=NULL;
system("cls");
show();//显示文本题目
p=head->next;//p指向最后一题的节点
head->next=NULL;//使head的next指向空(相当于扔掉最后一题)
p=NULL;//使p指向空
sum-=1;//题目数量减一
system("cls");
subj();
getch();
subj();
}
void change()//*********修改题目
{
int i,num;
struct Exam *p=NULL;
struct Exam *pt=NULL;
show();//显示题目
if(sum>=1)
{
p=(struct Exam *)malloc(LEN);//让p指向一个新开的节点
pt=head;//让pt指向head
printf("请选择修改的题目序号:");
scanf("%d",&num);
for(i=0;i<num-1;i++)
{
pt=pt->next;
} //此时pt指向num-1题,
printf("请输入题目:");
scanf("%s",p->ques);//给新节点输入修改后的内容
printf("请输入备选答案:\n");
for(i=0;i<4;)
{
printf("%c:",i+'A');
scanf("%s",p->ans[i++] ); //输入备选答案
}
printf("输入正确答案:");//输入正确答案
p->right=getchar();
scanf("%c",&p->right);
p->next=pt->next->next;//此时新节点将代替原节点(新题目代替原题目)
pt->next=p; //首尾连接
p=NULL; //最后让指针指向空
pt=NULL;
save();
}
}
void maketest()//************开始考试
{
int i,c;//i,与题目序号有关。c,与ABCD有关
struct Exam *p=NULL;
if(sum>0)
{
i=0;
p=head;
do
{
p=p->next;
printf("%d.%s\n\n",i+1,p->ques);
c=0;
while(c<4)
{
printf("%c:",c+'A');
printf("%s\n",p->ans[c++] );
}
printf("请输入你的答案:");
p->write=getchar();
scanf("%c",&p->write);
i=i+1;
printf("----------------------------------------------------------\n");
}while(p->next!=NULL);
printf("考试已完成!\n");//可以加入考试是否确认提交的功能
//save();
printf("按任意键继续!");
getch();
showgrade();
}
else printf("题库中没有题目!\n");
printf("按任意键继续!\n");
getch();
}
void showgrade()//*********计算成绩
{
int i,c,grade=0,x=0;
float e;
struct Exam *pt=NULL;
system("cls");
i=0;//i,题目序号。
pt=head;
do
{
pt=pt->next;//指向第一个存数据的节点
printf("%d.%s\n\n",i+1,pt->ques);
c=0;
while(c<4)
{
printf("%c:",c+'A');
printf("%s\n",pt->ans[c++] );
}
printf("你的答案是:%c",pt->write);
printf("\t\t\t正确答案:");
printf("%c\n",pt->right);
printf("----------------------------------------------------------\n");
if(pt->right==pt->write){grade+=5;x=x+1;}
i=i+1;
e=((float)x/sum)*100;
}while(pt->next!=NULL);
printf("总成绩:%4.2f 分\t\t总题数:%d 个\t正确题数:%d 个\n",e,sum,x);
pt=NULL;
}