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.

493 lines
11 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<malloc.h>
void prin1();
void choose();
typedef struct subjects //定义课程信息结构体
{
int num; //课程编号
char name[20]; //课程名称
char kind[10];//课程性质
int stime;//总学时
int ttime;//授课学时
int etime;//实验或上机学时
int score;//学分
int term;//开课学期
struct subjects *next;
} SUB;
SUB *head=NULL;
//下面定义链表模块
SUB *create_form()
{
SUB *head,*tail,*p;//定义头节点,尾节点,中间节点。
int num,stime,ttime;
int etime,score,term;
char name[20],kind[10];
int size=sizeof(SUB);
head=tail=NULL;
printf("输入选修课程信息:\n");
scanf("%d%s%s%d%d%d%d%d",&num,name,kind,&stime,&ttime,&etime,&score,&term);
while(num!=0)
{
p=(SUB *)malloc(size);
p->num=num;
strcpy(p->name,name);
strcpy(p->kind,kind);
p->stime=stime;
p->ttime=ttime;
p->etime=etime;
p->score=score;
p->term=term;
if(head==NULL)
head=p;
else
tail->next=p;
tail=p;
scanf("%d%s%s%d%d%d%d%d",&num,name,kind,&stime,&ttime,&etime,&score,&term);
}
tail->next=NULL;
return head;
}
//保存文件模块
void savefile() //保存文件
{
SUB *p;
FILE *fp;fp=fopen("2.txt","w");
if(fp==NULL)exit(0);
printf("课程编号 课程名称 课程性质 总学时 授课学时 实践或上级学时 学分 开课学期\n");
for(p=head;p;p=p->next)
fprintf(fp,"%5d%12s%9s%9d%9d%11d%11d%7d\n",p->num,p->name,p->kind,p->stime,p->ttime,p->etime,p->score,p->term);
fclose(fp);
printf("创建后信息已放入'2.txt'文件中\n");
system("pause");}
void savefile1()
{
SUB *p;
FILE *fp;fp=fopen("3.txt","w");
if(fp==NULL)exit(0);
for(p=head;p;p=p->next)
fprintf(fp,"%5d%12s%9s%9d%9d%11d%11d%7d\n",p->num,p->name,p->kind,p->stime,p->ttime,p->etime,p->score,p->term);
fclose(fp);
printf("创建后信息已放入'3.txt'文件中\n");
system("pause");
}
//读取文件模块
void readfile()//阅读文件
{
void *myInsert(SUB*);
SUB *newSub;//新课程
int num,stime,ttime,etime;
int score,term;
char c,name[20],kind[10],fname[20];
FILE *fp;
fp=fopen("2.txt","r");
while(!feof(fp))
{
newSub=(SUB*)malloc(sizeof(SUB));
fscanf(fp,"%d%s%s%d%d%d%d%d\n",&newSub->num,newSub->name,newSub->kind,&newSub->stime,&newSub->ttime,&newSub->etime,&newSub->score,&newSub->term);
myInsert(newSub);
}
fclose(fp);
}
void read()
{
void *myInsert(SUB*);
SUB *newSub;//新课程
int num,stime,ttime,etime;
int score,term;
char c,name[20],kind[10],fname[20];
FILE *fp;
fp=fopen("3.txt","r");
while(!feof(fp))
{
newSub=(SUB*)malloc(sizeof(SUB));
fscanf(fp,"%d%s%s%d%d%d%d%d\n",&newSub->num,newSub->name,newSub->kind,&newSub->stime,&newSub->ttime,&newSub->etime,&newSub->score,&newSub->term);
myInsert(newSub);
}
fclose(fp);
}
//下面定义浏览模块
void prin()
{
SUB *ptr;
head=NULL;
readfile();
if(head==NULL)
{
printf("\n\n\t**********NO RECORDS!**********\n");
return;
}
printf("课程编号 课程名称 课程性质 总学时 授课学时 实践或上级学时 学分 开课学期\n");
for(ptr=head;ptr;ptr=ptr->next)
{
printf("%5d%12s%9s%9d%9d%11d%11d%7d\n",ptr->num,ptr->name,ptr->kind,ptr->stime,ptr->ttime,ptr->etime,ptr->score,ptr->term);
}
system("pause");
}
//下面定义浏览已选修课程模块
void prin1()
{
SUB *ptr;
head=NULL;
read();
if(head==NULL)
{
printf("\n\n\t**********NO RECORDS!**********\n");
return;
}
printf("课程编号 课程名称 课程性质 总学时 授课学时 实践或上级学时 学分 开课学期\n");
for(ptr=head;ptr;ptr=ptr->next)
{
printf("%5d%12s%9s%9d%9d%11d%11d%7d\n",ptr->num,ptr->name,ptr->kind,ptr->stime,ptr->ttime,ptr->etime,ptr->score,ptr->term);
}
system("pause");
}
//插入链表模块
void *myInsert(SUB *subj)//链表插入操作
{
SUB *ptr,*ptr2;
ptr=subj;
if(head==NULL)
{
head=ptr;
head->next=NULL;
}
else
{
for(ptr2=head;ptr2;ptr2=ptr2->next)
if(ptr2->next==NULL)
{
ptr2->next=subj;
subj->next=NULL;
break;
}
}
return head;
}
//添加模块
void *insert()
{
SUB *ptr,*subj;
int size=sizeof(SUB);
char ch,ch1;
while(ch!='0')
{
subj=(SUB *)malloc(size);
ptr=subj;
printf("请输入要插入的课程信息:\n");
printf("\n\t\t 请输入课程编号:");
scanf("%d",&subj->num);
printf("\n\t\t 请输入课程名称:");
scanf("%s",&subj->name);
printf("\n\t\t 请输入课程性质:");
scanf("%s",&subj->kind);
printf("\n\t\t 请输入总学时:");
scanf("%d",&subj->stime);
printf("\n\t\t 请输入授课学时:");
scanf("%d",&subj->ttime);
printf("\n\t\t 请输入实践或上机学时:");
scanf("%d",&subj->etime);
printf("\n\t\t 请输入学分:");
scanf("%d",&subj->score);
printf("\n\t\t 请输入开课学期:");
scanf("%d",&subj->term);
myInsert(subj);
printf("\n添加完毕,新信息存入文件夹\n");
printf("\n\n 继续插入请按回车\n");
printf("\n结束添加请按 0:[ ]\b\b");
ch1=getchar();
ch=getchar();
}
return head;
}
//删除模块
void *del()
{
SUB *p1,*p2;
char ch,ch1;
int num;
while(ch!='0')
{
printf("请输入想删除的课程编号:[ ]\b\b\b\b");
scanf("%d",&num);
if(head->num==num);
{
p2=head;
head=head->next;
free(p2);
}
if(head==NULL)
return NULL;
p1=head;
p2=head->next;
while(p2)
{
if(p2->num==num)
{
p1->next=p2->next;
free(p2);
}
else
p1=p2;
p2=p1->next;
}
printf("\n继续删除按回车\n");
printf("\n结束删除按 0:[]\b\b");
ch1=getchar();
ch=getchar();
}
return head;
system("pause");
}
//选修课程模块
void choose()
{
SUB *p,*q;
int a[5];
int num,total=0,i=0,j;
printf("输入想要选修的课程编号,编号之间用空格分开\n");
scanf("%d",&num);
printf("如果确认输入完要选的编号,请输入 0:[]\n\n");
while(num!=0)
{
for(p=head;p;p=p->next)
if(p->num==num)
{
total=total+p->score;
a[i]=num;
i++;
}
scanf("%d",&num);
}
if(total<60)
{
printf("选修总学分为%d,未达到60选修失败",total);
system("pause");
}
else
{
FILE*fp;
fp=fopen("3.txt","w");
for(j=0;j<i;j++)
for(q=head;q;q=q->next)
if(q->num==a[j])
fprintf(fp,"%5d%12s%9s%9d%9d%11d%11d%7d\n",q->num,q->name,q->kind,q->stime,q->ttime,q->etime,q->score,q->term);
fclose(fp);
printf("\t\t\n*****选修成功!*******\n");
printf("\n 您选修的课程总学分为%d,课程分别为:\n",total);
printf("课程编号 课程名称 课程性质 总学时 授课学时 实践或上级学时 学分 开课学期\n");
for(j=0;j<i;j++)
for(q=head;q;q=q->next)
if(q->num==a[j])
printf("%5d%12s%9s%9d%9d%11d%11d%7d\n",q->num,q->name,q->kind,q->stime,q->ttime,q->etime,q->score,q->term);
printf("\n以上信息全部保存在'3.txt'中\n");
}
system("pause");
}
//下面定义查询板块
void search()
{
int a,num;
int t=1;
char type[10],min[10];
SUB *ptr;
L1:system("cls");
printf("\n\n\t\t***********请选择查询方式***********\n");
printf("\n\t\t\t1--按课程名称查找\n");
printf("\n\t\t\t2--按课程性质查找\n");
printf("\n\t\t\t3--按学分查找\n");
printf("\n\t\t\t4-退出查找\n");
printf("\n\n\t\t************************************\n");
printf("\n\nChoose your numeber(1-4):[]\b\b");
scanf("%d",&a);
switch(a)
{
case 1:printf("输入查找课程名:");
scanf("%s",min);
printf("课程编号 课程名称 课程性质 总学时 授课学时 实践或上机学时 学分 开课学时\n");
for(ptr=head;ptr;ptr=ptr->next)
if(strcmp(min,ptr->name)==0)
{
printf("%5d%12s%9s%9d%9d%11d%11d%7d\n",ptr->num,ptr->name,ptr->kind,ptr->stime,ptr->ttime,ptr->etime,ptr->score,ptr->term);
t=0;
}
if(t)
printf("\t\n 未找到!\n");
t=1;
system("pause");
goto L1;
case 2:printf("请输入查找课程性质:");
scanf("%s",type);
printf("课程编号 课程名称 课程性质 总学时 授课学时 实践或上机学时 学分 开课学时\n");
for(ptr=head;ptr;ptr=ptr->next)
if(strcmp(type,ptr->kind)==0)
{
printf("%5d%12s%9s%9d%9d%11d%11d%7d\n",ptr->num,ptr->name,ptr->kind,ptr->stime,ptr->ttime,ptr->etime,ptr->score,ptr->term);
t=0;
}
if(t)
printf("\t\n 未找到!\n");
t=1;
system("pause");
goto L1;
case 3:printf("输入要查找的课程的学分:");
scanf("%d",&num);
printf("课程编号 课程名称 课程性质 总学时 授课学时 实践或上机学时 学分 开课学时\n");
for(ptr=head;ptr;ptr=ptr->next)
if(ptr->score==num)
{
printf("%5d%12s%9s%9d%9d%11d%11d%7d\n",ptr->num,ptr->name,ptr->kind,ptr->stime,ptr->ttime,ptr->etime,ptr->score,ptr->term);
t=0;
}
if(t)
printf("\n\t未找到!\n");
t=1;
system("pause");
goto L1;
case 4:break;
}
}
//下面定义管理员板块
void Mangers()
{
int n,w=1,flag=0,i=3;
char s[8];
char password[7]="1";//密码为1
do
{
printf("\n\nEnter password:");//输入密码
scanf("%s",s);
if(!strcmp(s,password))//密码匹配验证
{
flag=1;
break;//输入正确
}
else
{
printf("\n\nError!You only have %d times!Pleasa enter again:\n",i-1);
i--;
}
}
while(i>0);
if(!flag)
{
printf("you have already entered 3 times!");//输入密码超过3次退出
exit(0);
}
do
{
system("cls");//清屏
puts("\n\n\t\t*************管 理 员 菜 单**************\n\n");
puts("\t\t\t\t1--浏览课程\n");
puts("\t\t\t\t2--查询课程\n");
puts("\t\t\t\t3--添加课程\n");
puts("\t\t\t\t4--删除课程\n");
puts("\t\t\t\t5--返回主菜单");
puts("\n\n\t\t******************************************\n");
printf("choose your number(1-5):[]\b\b");
scanf("%d",&n);
switch(n)
{
case 1:prin();break;//浏览
case 2:search();break;//查询
case 3:insert();savefile();break;//添加
case 4:del();savefile();break;//删除
case 5:return;
default:;
}
}while(w==1);
}
//下面定义学生板块
void Students()
{
int n,w=1;
do
{
system("cls");
puts("\n\n\t\t**********************************\n\n");
puts("\t\t\t\t1--浏览所有课程\n");
puts("\t\t\t\t2--查询课程信息\n");
puts("\t\t\t\t3--选择选修课程\n");
puts("\t\t\t\t4--浏览我选修的课程\n");
puts("\t\t\t\t5--删除错选课程\n");
puts("\t\t\t\t6--返回");
puts("\n\n\t\t****************************************\n");
printf("choose your number(1-6):[]\b\b");
scanf("%d",&n);
switch(n)
{
case 1:prin();break;//浏览
case 2:search();break;//查询
case 3:choose();break;//选择
case 4:prin1();break;//浏览我选修的
case 5:del();savefile();break;//删除错选
case 6:return;//返回
default:;
}
}
while(w==1);
}
//下面编辑主页面
int main()
{
int n,w=1;
do
{
system("cls");//清除屏幕
puts("\n\n\t\t****************MENU****************\n\n");
puts("\t\t\t\t1.以管理员身份登录\n");
puts("\t\t\t\t2.以学生身份登录\n");
puts("\t\t\t\t3.退出");
puts("\n\n\t\t***********************************\n");
printf("Choose your number(1--3):[ ]\b\b");
scanf("%d",&n);
switch(n)
{
case 1:Mangers();break;//选1进入管理员系统
case 2:Students();break;//选2进入学生系统
case 3:w=0;break;//选3退出
default:;
}
}
while(w==1);
return 0;
}