C++课程设计程序
更新时间:2023-08-24 10:12:01 阅读量: 教育文库 文档下载
#include <iostream>
#include <cstring>
using std::cout;
using std::endl;
class Course{
public:
Course():name(0),next(0){}
Course(char n[]);
~Course(){delete[] name;}
void setName(char n[]);
void setNextCourse(Course* c);
char *getName(){return name;}
Course* getNext(){return next;}
private:
char* name;
Course* next;
};
Course::Course(char n[]){
name=new char[strlen(n)+1];
strcpy(this->name,n);
}
void Course::setName(char n[]){
delete[] name;
this->name=new char[strlen(n)+1];
strcpy(this->name,n);
}
void Course::setNextCourse(Course* c){
this->next=c;
}
class Person{
public:
Person():id(0),name(0),courses(0){}
Person(char i[],char n[]);
~Person();
void setID(char i[]);
void setName(char n[]);
char *getID(){return id;}
char *getName(){return name;}
void addCourses(Course* c);
virtual void display();
protected:
char* id;
char* name;
Course* courses;
};
Person::Person(char i[],char n[]){
this->id=new char[strlen(i)+1];
strcpy(this->id,i);
this->name=new char[strlen(n)+1];
strcpy(this->name,n);
}
void Person::setID(char i[]){
delete[] id;
this->id=new char[strlen(i)+1];
strcpy(this->id,i);
}
Person::~Person(){
Course* c;
while(courses!=0){
c=courses->getNext();
delete courses;
courses=c;
}
delete[] id;
delete[] name;
}
void Person::setName(char n[]){
delete[] name;
this->name=new char[strlen(n)+1];
strcpy(this->name,n);
}
void Person::display(){
cout<<"ID: "<<id<<" Name: "<<name<<" Courses: ";
Course* c=courses;
while(c!=0){
cout<<c->getName()<<" ";
c=c->getNext();
}
cout<<endl;
}
void Person::addCourses(Course* c){
if(courses==0) courses=c;
else{
Course* t=courses;
while(t->getNext()!=0){
t=t->getNext();
}
t->setNextCourse(c);
}
}
class Teacher:public Person{
public:
Teacher():Person(),position(0){}
Teacher(char id[],char name[],char position[]);
Teacher(const Teacher& t);
~Teacher();
void display();
void setPosition(char p[]);
char *getPosition(){return position;}
private:
char* position;
};
Teacher::Teacher(char i[], char n[], char p[]):Person(i,n){
position=new char[strlen(position)+1];
strcpy(this->position,p);
}
Teacher::Teacher(const Teacher& t):Person(){
this->id=new char[strlen(t.id)+1];
strcpy(this->id,t.id);
this->name=new char[strlen(http://www.77cn.com.cn)+1];
strcpy(this->name,http://www.77cn.com.cn);
this->position=new char[strlen(t.position)+1];
strcpy(this->position,t.position);
}
Teacher::~Teacher(){
delete[] position;
}
void Teacher::display(){
cout<<"ID: "<<id<<" Name: "<<name<<" Position: "<<position<<" Courses to teacher: ";
Course* c=courses;
while(c!=0){
cout<<c->getName()<<" ";
c=c->getNext();
}
cout<<endl;
}
void Teacher::setPosition(char p[]){
delete[] position;
this->position=new char[strlen(p)+1];
strcpy(this->position,p);
}
class Postgraduate:public Person{
public:
Postgraduate():Person(){degree=new
char[strlen("Postgraduate")+1];strcpy(this->degree,"Postgraduate");} Postgraduate(char i[],char n[]);
Postgraduate(const Postgraduate& p);
~Postgraduate();
void display();
char *getDegree(){return degree;}
private:
char* degree;
};
Postgraduate::Postgraduate(char i[],char n[]):Person(i,n){
this->degree=new char[strlen("Postgraduate")+1];
strcpy(this->degree,"Postgraduate");
}
Postgraduate::Postgraduate(const Postgraduate& p):Person(){
this->id=new char[strlen(p.id)+1];
strcpy(this->id,p.id);
this->name=new char[strlen(http://www.77cn.com.cn)+1];
strcpy(this->name,http://www.77cn.com.cn);
this->degree=new char[strlen("Postgraduate")+1];
strcpy(this->degree,"Postgraduate");
}
Postgraduate::~Postgraduate(){
delete[] degree;
}
void Postgraduate::display(){
cout<<"ID: "<<id<<" Name: "<<name<<" Degree: "<<degree<<" Courses to study: ";
Course* c=courses;
while(c!=0){
cout<<c->getName()<<" ";
c=c->getNext();
}
cout<<endl;
}
class Undergraduate:public Person{
public:
Undergraduate():Person(){degree=new
char[strlen("Undergraduate")+1];strcpy(this->degree,"Undergraduate");} Undergraduate(char i[],char n[]);
Undergraduate(const Undergraduate& u);
~Undergraduate();
void display();
char *getDegree(){return degree;}
private:
char* degree;
};
Undergraduate::Undergraduate(char i[], char n[]):Person(i,n){
this->degree=new char[strlen("Undergraduate")+1];
strcpy(this->degree,"Undergraduate");
}
Undergraduate::Undergraduate(const Undergraduate& u):Person(){ this->id=new char[strlen(u.id)+1];
strcpy(this->id,u.id);
this->name=new char[strlen(http://www.77cn.com.cn)+1];
strcpy(this->name,http://www.77cn.com.cn);
this->degree=new char[strlen("Undergraduate")+1];
strcpy(this->degree,"Undergraduate");
}
Undergraduate::~Undergraduate(){
delete[] degree;
}
void Undergraduate::display(){
cout<<"ID: "<<id<<" Name: "<<name<<" Degree: "<<degree<<" Courses to study: ";
Course* c=courses;
while(c!=0){
cout<<c->getName()<<" ";
c=c->getNext();
}
cout<<endl;
}
class JuniorCollegeStudent:public Person{
public:
JuniorCollegeStudent():Person(){degree=new char[strlen("Junior college student")+1];strcpy(this->degree,"Junior college student");}
JuniorCollegeStudent(char i[],char n[]);
JuniorCollegeStudent(const JuniorCollegeStudent& j);
~JuniorCollegeStudent();
void display();
char *getDegree(){return degree;}
private:
char* degree;
};
JuniorCollegeStudent::JuniorCollegeStudent(char i[],char n[]):Person(i,n){ this->degree=new char[strlen("Junior college student")+1];
strcpy(this->degree,"Junior college student");
}
JuniorCollegeStudent::JuniorCollegeStudent(const JuniorCollegeStudent& j){ this->id=new char[strlen(j.id)+1];
strcpy(this->id,j.id);
this->name=new char[strlen(http://www.77cn.com.cn)+1];
strcpy(this->name,http://www.77cn.com.cn);
this->degree=new char[strlen("Junior college student")+1];
strcpy(this->degree,"Junior college student");
}
JuniorCollegeStudent::~JuniorCollegeStudent(){
delete[] degree;
}
void JuniorCollegeStudent::display(){
cout<<"ID: "<<id<<" Name: "<<name<<" Degree: "<<degree<<" Courses to study: ";
Course* c=courses;
while(c!=0){
cout<<c->getName()<<" ";
c=c->getNext();
}
cout<<endl;
}
#include<stdio.h>
#include<stdlib.h>
int N1,N2,kk1,kk2,kk3;
struct couse * head1;
struct student * head2;
struct couse//课程信息结构体
{
int num1;
char name1[20];
int score;
int nelepeo;//课程已选人数
int Melepeo;//课程人数上限
struct couse * next;
};
struct student//学生信息结构体
{
int num2;
char name2[20];
int nelenum[50];//已选课程编号
int nelen;//已选课程数量
struct student * next;
};
void Ms()
{
for(kk1=0;kk1<1100;kk1++)
for(kk2=0;kk2<1200;kk2++)
for(kk3=0;kk3<1200;kk3++);
}
void keyboardc()//录入课程子函数(从键盘录入)
{
struct couse *p1,*p2;
N1=0;
p1=p2=(struct couse*)malloc(sizeof(struct couse));
printf("课程编号\t课程名称\t学分\t课程人数上限\n");
scanf("%d%s%d%d",&p1->num1,p1->name1,&p1->score,&p1->Melepeo);
p1->nelepeo=0;
head1=NULL;
while(p1->num1!=0)
{
N1=N1+1;
if(N1==1)head1=p1;
else p2->next=p1;
p2=p1;
p1=(struct couse * )malloc(sizeof(struct couse));
scanf("%d%s%d%d",&p1->num1,p1->name1,&p1->score,&p1->Melepeo);
p1->nelepeo=0;
}
p2->next=NULL;
}
void filec()//录入键盘子函数(从文件录入)
{
FILE * fp;
char filepath[20];
struct couse *p1,*p2;
N1=0;
printf("输入要读入的文件路径:");
getchar();
gets(filepath);
if((fp=fopen(filepath,"r"))==NULL)
{
printf("找不到%s文件!\n",filepath);
exit(0);
p1=p2=(struct couse*)malloc(sizeof(struct couse));
fscanf(fp,"%d%s%d%d%d",&p1->num1,p1->name1,&p1->score,&p1->nelepeo,&p1->Melepeo); head1=NULL;
while(!feof(fp))
{
N1=N1+1;
if(N1==1)head1=p1;
else p2->next=p1;
p2=p1;
p1=(struct couse * )malloc(sizeof(struct couse));
fscanf(fp,"%d%s%d%d%d",&p1->num1,p1->name1,&p1->score,&p1->nelepeo,&p1->Melepeo); }
p2->next=NULL;
}
void inputc()//录入课程主函数
{
int i;
printf("\t\t\t录入课程信息\n");
printf("\n1.从键盘录入\n");
printf("2.从文件录入\n");
printf("3.返回主菜单\n");
printf("请选择(1~3):\n");
scanf("%d",&i);
switch(i)
{
case(1):keyboardc();break;
case(2):filec();break;
case(3):break;
}
}
void insertc(struct couse *incouse)//课程管理子函数(增加课程)
{
struct couse *p0,*p1,*p2;
p1=head1;
p0=incouse;
if(head1==NULL)
{
head1=p0;
p0->next=NULL;
}
else
while((p0->num1 > p1->num1) && (p1->next!=NULL))
{
p2=p1;
p1=p1->next;
}
if(p0->num1 <= p1->num1)
{
if(head1==p1) head1=p0;
else p2->next=p0;
p0->next=p1;
}
else
{
p1->next=p0;
p0->next=NULL;
}
}
N1=N1+1;
}
void delc(int num1)//课程管理子函数(删除课程)
{
struct couse *p1,*p2;
if(head1==NULL)
{
printf("\n没有课程,无法删除!\n");
goto end;
}
p1=head1;
while(num1!=p1->num1 && p1->next!=NULL)
{
p2=p1;
p1=p1->next;
}
if(num1==p1->num1)
{
if(p1==head1) head1=p1->next;
else p2->next=p1->next;
printf("已删除该编号课程!\n");
N1=N1-1;
}
else printf("无该编号的课程!\n");
end:;
}
void managementc()//课程管理主函数
{
struct couse * incouse;
int i,num1;
printf("\t\t\t课程管理\n");
printf("1.新增课程\n");
printf("2.删除课程\n");
printf("3.返回主菜单\n");
printf("请选择(1~3):\n");
scanf("%d",&i);
switch(i)
{
case(1):
{
incouse=(struct couse *)malloc(sizeof(struct couse));
printf("课程编号\t课程名称\t学分\t课程人数上限\n");
scanf("%d%s%d%d",&incouse->num1,incouse->name1,&incouse->score,&incouse->Melepeo); incouse->nelepeo=0;
insertc(incouse);
break;
}
case(2):
{
printf("请输入要删除课程的编号:\n");
scanf("%d",&num1);
delc(num1);
break;
}
case(3):break;
}
}
void keyboards()//录入学生信息子函数(从键盘录入)
{
int i;
struct student *p1,*p2;
N2=0;
p1=p2=(struct student *)malloc(sizeof(struct student));
printf("学生学号\t学生姓名\n");
scanf("%d%s",&p1->num2,p1->name2);
p1->nelen=0;
for(i=0;i<20;i++) p1->nelenum[i]=0;
head2=NULL;
while(p1->num2!=0)
{
N2=N2+1;
if(N2==1)head2=p1;
else p2->next=p1;
p2=p1;
p1=(struct student * )malloc(sizeof(struct student));
scanf("%d%s",&p1->num2,p1->name2);
p1->nelen=0;
for(i=0;i<20;i++) p1->nelenum[i]=0;
}
p2->next=NULL;
}
void files()//录入学生信息子函数(从文件录入)
{
int i=0;
FILE * fp;
char filepath[20];
struct student *p1,*p2;
N2=0;
printf("输入要读入的文件路径:");
getchar();
gets(filepath);
if((fp=fopen(filepath,"r"))==NULL)
{
printf("找不到%s文件!\n",filepath);
exit(0);
}
p1=p2=(struct student*)malloc(sizeof(struct student));
fread(p1,sizeof(struct student),1,fp);
head2=NULL;
while(!feof(fp))
{
i=0;
N2=N2+1;
if(N2==1)head2=p1;
else p2->next=p1;
p2=p1;
p1=(struct student * )malloc(sizeof(struct student));
fread(p1,sizeof(struct student),1,fp);
}
p2->next=NULL;
}
void inputs()//录入学生信息主函数
{
int i;
printf("\t\t\t录入学生信息\n");
printf("\n1.从键盘录入\n");
printf("2.从文件录入\n");
printf("3.返回主菜单\n");
printf("请选择(1~3):\n");
scanf("%d",&i);
switch(i)
{
case(1):keyboards();break;
case(2):files();break;
case(3):break;
}
}
void inserts(struct student * incouse)//学生信息管理子函数(填加学生信息)
{
struct student *p0,*p1,*p2;
p1=head2;
p0=incouse;
if(head2==NULL)
{
head2=p0;
p0->next=NULL;
}
else
{
while((p0->num2 > p1->num2) && (p1->next!=NULL))
{
p2=p1;
p1=p1->next;
}
if(p0->num2 <= p1->num2)
{
if(head2==p1) head2=p0;
else p2->next=p0;
p0->next=p1;
}
else
{
p1->next=p0;
p0->next=NULL;
}
}
N2=N2+1;
}
void dels(int num2)//学生信息管理子函数(删除学生信息)
{
struct student *p1,*p2;
if(head2==NULL)
{
printf("\n没有该学生信息,无法删除!\n");
goto end;
}
p1=head2;
while(num2!=p1->num2 && p1->next!=NULL)
{
p2=p1;
p1=p1->next;
}
if(num2==p1->num2)
{
if(p1==head2) head2=p1->next;
else p2->next=p1->next;
printf("已删除该学生信息!\n");
N2=N2-1;
}
else printf("无该学号的学生!\n");
end:;
}
void managements()//学生信息管理主函数
{
struct student * incouse;
int i,num2;
printf("\t\t\t学生信息管理\n");
printf("1.新增学生信息\n");
printf("2.删除学生信息\n");
printf("3.返回主菜单\n");
printf("请选择(1~3):\n");
scanf("%d",&i);
switch(i)
{
case(1):
{
incouse=(struct student *)malloc(sizeof(struct student));
incouse->nelen=0;
incouse->nelenum[0]=0;
printf("学生学号\t学生姓名\n");
scanf("%d%s",&incouse->num2,incouse->name2);
inserts(incouse);
break;
}
case(2):
{
printf("请输入要删除学生的学号:\n");
scanf("%d",&num2);
dels(num2);
break;
}
case(3):break;
}
}
void elect(struct student * s)//选课
{
struct couse * p;
int num1,i;
printf("请输入要选课的编号:\n");
scanf("%d",&num1);
for(i=0;s->nelenum[i]!=0;i++);
s->nelenum[i]=num1;
(s->nelen)++;
p=head1;
while(p->num1!=num1) p=p->next;
(p->nelepeo)++;
}
void cheak()//学生选课子函数(查询可选课程)
{
char e;
struct couse * c;
struct student * s;
int num2,i,j=0,t=0;
printf("请输入你的学号:");
scanf("%d",&num2);
s=head2;
while(s->num2!=num2 && s->next!=NULL) s=s->next;
if(s->num2!=num2)
{
printf("不存在你的信息,请进入主菜单录入你的信息!\n");
goto end;
}
c=head1;
printf("你的可选课程编号:\n");
while(c!=NULL)
{
for(t=0,i=0;s->nelenum[i]!=0;i++)
{
if(c->num1==s->nelenum[i]) t=1;
}
if(t==0 && (c->nelepeo!=c->Melepeo))
{
printf("%d\n",c->num1);
j++;
}
c=c->next;
}
if(j==0)
{
printf("你已选完所有课程,无法再多选!\n");
goto end;
}
printf("选课(y/n)?:\n");
getchar();
e=getchar();
i=0;
while(e=='y')
{
elect(s);
printf("继续选课(y/n)?:\n");
getchar();
e=getchar();
}
end:;
}
void back(struct student * p)//退课
{
struct couse * p1;
int num1,i,j;
printf("请输入你要退掉的课程编号:\n");
scanf("%d",&num1);
p1=head1;
while(p1->num1!=num1) p1=p1->next;
for(i=0;p->nelenum[i]!=num1;i++);
for(j=i;p->nelenum[j]!=0;j++) p->nelenum[j]=p->nelenum[j+1];
p->nelenum[--j]=0;
(p1->nelepeo)--;
printf("退课成功!\n");
}
void hcheak()//学生选课子函数(查询已选课程)
{
char c;
struct couse * p0;
struct student * p;
int num2,i,f=0;
printf("请输入学号:\n");
scanf("%d",&num2);
p=head2;
while(p->num2!=num2 && p!=NULL) p=p->next;
if(p==NULL)
{
printf("不存在你的信息,请回主菜单录入信息:\n");
goto end;
}
printf("已选课程编号:\n");
if(p->nelenum[0]==0)
{
printf("你还没选课!\n");
goto end;
}
for(i=0;p->nelenum[i]!=0;i++)
{
printf("%d\n",p->nelenum[i]);
p0=head1;
while(p0->num1!=p->nelenum[i]) p0=p0->next;
f=f+p0->score;
}
printf("总学分:%d\n",f);
printf("是否进行退课(y/n)?");
getchar();
c=getchar();
while(c=='y')
{
back(p);
printf("继续退课(y/n)?");
getchar();
c=getchar();
(p->nelen)--;
}
end:;
}
void elective()//学生选课主函数
{
int i;
printf("\t\t\t学生选课\n");
printf("1.查询可选课程\n");
printf("2.查询已选课程\n");
printf("3.返回主菜单\n");
printf("请输入(1~3):\n");
scanf("%d",&i);
switch(i)
{
case(1):cheak();break;
case(2):hcheak();break;
case(3):break;
}
}
void listc()//输出课程信息
{
struct couse * p;
p=head1;
printf("课程编号 课程名称 学分 课程已选人数 课程人数上限\n");
while(p!=NULL)
{
printf("%-8d%10s%6d%8d%12d\n",p->num1,p->name1,p->score,p->nelepeo,p->Melepeo); p=p->next;
}
}
void lists()//输出学生信息
{
struct student * p;
p=head2;
printf("学生学号 学生姓名 已选课程数量\n");
while(p!=NULL)
{
printf("%-4d %10s %6d\n",p->num2,p->name2,p->nelen);
p=p->next;
}
}
void intoc()//存储课程信息
{
FILE * fp;
struct couse * p;
char filepath[30];
printf("输入课程信息要保存的文件路径:");
getchar();
gets(filepath);
if((fp=fopen(filepath,"w"))==NULL)
{
printf("\n保存失败!");
exit(0);
}
p=head1;
while(p!=NULL)
{
fprintf(fp,"%d %s %d %d %d\n",p->num1,p->name1,p->score,p->nelepeo,p->Melepeo); p=p->next;
}
fclose(fp);
printf("课程信息已保存在%s中!\n",filepath);
}
void intos()//存储学生信息
{
FILE * fp;
struct student * p;
char filepath[30];
printf("输入学生信息要保存的文件路径:");
getchar();
gets(filepath);
if((fp=fopen(filepath,"w"))==NULL)
{
printf("\n保存失败!");
exit(0);
}
p=head2;
while(p!=NULL)
{
fwrite(p,sizeof(struct student),1,fp);
p=p->next;
}
fclose(fp);
printf("学生信息已保存在%s中!\n",filepath);
}
void into()//存储信息
{
int i;
printf("1.存储课程信息\n");
printf("2.存储学生信息\n");
printf("3.返回主菜单\n");
printf("请输入(1~3)\n");
正在阅读:
C++课程设计程序08-24
年新北师大版六年级数学上册第二单元测试题及答案 - 《分数混合03-21
A1 技术支持的学情分析作业1—学情分析方案(数学)08-01
泰山学院模拟电子考试试题及答案10-24
徐州市2009—2010学年度第二学期期末测试八年级语文试题03-28
PLC习题及大作业题目11-14
高一数学必修四公式大全10-19
小学数学教师基本功考试-试题及答案11-08
- exercise2
- 铅锌矿详查地质设计 - 图文
- 厨余垃圾、餐厨垃圾堆肥系统设计方案
- 陈明珠开题报告
- 化工原理精选例题
- 政府形象宣传册营销案例
- 小学一至三年级语文阅读专项练习题
- 2014.民诉 期末考试 复习题
- 巅峰智业 - 做好顶层设计对建设城市的重要意义
- (三起)冀教版三年级英语上册Unit4 Lesson24练习题及答案
- 2017年实心轮胎现状及发展趋势分析(目录)
- 基于GIS的农用地定级技术研究定稿
- 2017-2022年中国医疗保健市场调查与市场前景预测报告(目录) - 图文
- 作业
- OFDM技术仿真(MATLAB代码) - 图文
- Android工程师笔试题及答案
- 生命密码联合密码
- 空间地上权若干法律问题探究
- 江苏学业水平测试《机械基础》模拟试题
- 选课走班实施方案
- C++
- 课程
- 程序
- 设计
- 舌尖上的植物学期末考试答案
- 朋辈辅导在重点关注学生教育中的应用-2019年教育文档
- 14-16第五章隧道结构设计
- 车辆通行证使用须知
- unit2 Warming up & Speaking
- 第七章学习的联结-认知理论
- 再生纤维行业“一带一路”投资分析及发展战略研究报告2016-2021年
- 热力学基本关系式的记忆法
- 铰孔时如何正确选用切削液
- 2014-2019年中国吸尘器行业监测与发展趋势预测报告
- 2012春季学期五中作息时间表
- 民间工艺美术之苗绣
- 我国行政决策模式之转型_从管理主义模式到参与式治理模式
- 设备维护保养及润滑管理制度
- 检验大纲
- 公开课(二次函数中三角形面积问题)
- Could you please tell me
- 小区物业保安员岗位职责
- 第九讲 弗洛伊德的古典精神分析 第十讲 荣格的分析心理学和阿德勒的个体心理学
- GBJ97-87水泥混凝土路面施工及验收规范