📄 course.cpp
字号:
// course.cpp: implementation of the course class.
//
//////////////////////////////////////////////////////////////////////
#include "color.h"
#include "course.h"
//////////////////////////////////////////////////////////////////////
#include<iostream.h>
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
//////////////////////////////////////////////////////////////////////
extern int choice;
extern color c;
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
void course::input()
{
c.setcolor(10);
cout<<"请输入编号:";
cin.getline(sernum,80);
cout<<"请输入课程名称:";
cin.getline(coursename,80);
cout<<"请输入讲授学时:";
cin.getline(teach_period,80);
cout<<"请输入实验学时:";
cin.getline(experiment_period,80);
cout<<"请输入学分:";
cin.getline(credit_hour,80);
}
void course::output()
{
c.setcolor(10);
cout<<endl;
cout<<"课程编号为:"<<sernum<<endl;
cout<<"学分为 :"<<credit_hour<<endl;
cout<<"课程名称为:"<<coursename<<endl;
cout<<"讲授学时为:"<<teach_period<<endl;
cout<<"实验学时为:"<<experiment_period<<endl;
}
//////////////////////////////////////////////////////////////////////
// fun2 Class
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
//课程信息维护
void course_fun::maintenance()
{
void menu2();
c.setcolor(4);
cout<<"----------------------------------------------------"<<endl;
cout<<"欢迎进入学生课程信息的维护系统"<<endl;
cout<<"****************************************************"<<endl;
int type;//switch选择开关
char str[2];//用于吃回车
while(1)
{
menu2();
cin>>type;
cin.getline(str,2);
switch(type)
{
case 1:
file();//课程基本信息录入
break;
case 2:
display();//课程基本信息的显示
break;
case 3:
modify();
break;
case 4:
del();
break;
case 5:
break;
case 0:
exit(0);
break;
}if(type==5)break;
}
}
//课程基本信息录入
void course_fun::file()
{
system("cls");
if(choice==1)
{
c.setcolor(8);
course cour;
FILE *fp;
if((fp=fopen("course.dat","ab+"))==NULL)
{
cout<<"can't open file course.dat";
exit(0);
}
char ch;//控制循环
do
{
cour.input();
fwrite(&amt;cour,sizeof(cour),1,fp);
cout<<"继续输入请按 y,否则请按其他键: ";
cin>>ch;
char str[2];//吃回车
cin.getline(str,2);//吃回车
}while(ch=='y');
fclose(fp);
if((fp=fopen("course.dat","rb"))==NULL)
{
cout<<"can't open file course.dat";
exit(0);
}
while(fread(&amt;cour,sizeof(course),1,fp)==1)
cour.output();
fclose(fp);
}
else
{
c.setcolor(7);
cout<<"***********************你不是管理员,无权录入信息。**************************"<<endl;
}
}
//课程基本信息的显示
void course_fun::display()
{
system("cls");
FILE *fp;
course cour;
if((fp=fopen("course.dat","rb"))==NULL)
{
cout<<"can't open file course.dat";
exit(0);
}
while(fread(&amt;cour,sizeof(course),1,fp)==1)
cour.output();
fclose(fp);
}
//菜单
void menu2()
{
c.setcolor(3);
cout<<"***********************************************************"<<endl;
cout<<"1:课程基本信息录入 2:课程基本信息显示"<<endl;
cout<<"3:课程基本信息修改 4:课程基本信息删除"<<endl;
cout<<"5:返回上级菜单 0:退出系统"<<endl;
cout<<"***********************************************************"<<endl;
cout<<"请选择(0-5): ";
}
//课程基本信息修改
void course_fun::modify()
{
system("cls");
if(choice==1)
{
c.setcolor(6);
cout<<endl;
cout<<"----------------------------------------------------"<<endl;
cout<<"课程基本信息修改"<<endl;
cout<<"****************************************************"<<endl;
course cour;
FILE *fp;
if((fp=fopen("course.dat","rb+"))==NULL)//打开文件
{
cout<<"can't open file course.dat";
exit(0);
}
char sernum[8]; //要修改的课程编号
int flag=0;//控制循环
c.setcolor(4);
cout<<"请输入要修改的课程编号:";
cin>>sernum;
char str[2];//吃回车
cin.getline(str,2);//吃回车
while(fread(&amt;cour,sizeof(course),1,fp)==1)//显示要修改课程的信息
if(strcmp(sernum,cour.getsernum())==0)
cour.output();
fclose(fp);//关闭文件
int wide=sizeof(cour);
if((fp=fopen("course.dat","rb+"))==NULL)//打开文件
{
cout<<"can't open file course.dat";
exit(0);
}
while(fread(&amt;cour,sizeof(course),1,fp)==1)
{
if(strcmp(sernum,cour.getsernum())==0)
{
fseek(fp,-wide,1);
cour.input();
fwrite(&amt;cour,sizeof(course),1,fp);
flag=1;
}
if(flag==1)break;
}
if(flag==0)
{
c.setcolor(7);
cout<<"不存在此学生的信息!"<<endl;
}
fclose(fp);//关闭文件
}
else
{
c.setcolor(9);
cout<<"**************************你不是管理员,无权进行修改。**************************"<<endl;
}
}
//课程基本信息删除
void course_fun::del()
{
system("cls");
if(choice==1)
{
c.setcolor(11);
cout<<"----------------------------------------------------"<<endl;
cout<<"课程基本信息删除"<<endl;
cout<<"****************************************************"<<endl;
int i;
course cour[100];
long filesize,rscount;
FILE *fp;
if((fp=fopen("course.dat","rb"))==NULL)//打开文件
{
cout<<"can't open file course.dat";
exit(0);
}
fseek(fp,0,2);
filesize=ftell(fp);
rewind(fp);
rscount=filesize/sizeof(course);
for(i=0;i<rscount;i++)
fread(&amt;cour[i],sizeof(course),1,fp);
fclose(fp);
c.setcolor(13);
char sernum[20];
cout<<"请输入你要删除的课程编号:";
cin>>sernum;
fp=fopen("course.dat","wb");
for(i=0;i<rscount;i++)
if(strcmp(sernum,cour[i].getsernum())!=0)
fwrite(&amt;cour[i],sizeof(course),1,fp);
fclose(fp);
cout<<"删除成功!"<<endl;
cout<<"继续删除请按 Y否则请按其他键: ";
char a;//判断是否继续删除
cin>>a;
if(a=='y'||a=='Y')
del();
}
else
{
c.setcolor(14);
cout<<"***********************你不是管理员,无权删除信息。************************"<<endl;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -