⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 course.cpp

📁 该程序为本人初学者编的
💻 CPP
字号:
#include <iostream.h>
#include <string.h>
#include <fstream.h>
#include <iomanip.h>
#include <stdlib.h>
#include "head.h"

Course::Course(){	//构造函数
	char str[20];
	cout<<"请输入课程名称:";
	cin>>str;
	name = new char[strlen( str )+1];
	strcpy(name,str);

	cout<<"请输入上课地点:";
	cin>>str;
	classroom = new char [strlen( str )+1];
	strcpy ( classroom,str );

	cout<<"请输入教师姓名:";
	cin>>str;
	teacher = new char [strlen( str )+1];
	strcpy ( teacher,str );

	cout<<"请输入课程起始周和结束周:";
	cin>>week.first>>week.last;

	cout<<"星期几上课(1-7分别代表星期一到星期日):";
	cin>>time.weekday; 

	cout<<"第几节到第几节上课:";
	cin>>time.start>>time.end;
	cout<<endl;
}

Course::~Course (){
	delete [] name;
	delete [] classroom;
	delete [] teacher;
}

Week Course::GetWeek () const{	//返回开课周数
	Week w;
	w.first = week.first;
	w.last = week.last;
	return w;
}

int Course::GetWeekday () const{//返回星期几上课
	return time.weekday;
}
int Course::Getstart () const{//返回起始节数
	return time.start;
}
int Course::Getend () const{//返回结束节数
	return time.end;
}

char* Course::Getname () const{//返回课程名称
	return name;
}

ostream &operator<<(ostream &os,Course& cour) //course输出运算符重载
{
	os<<endl;
	os<<setw(25)<<cour.name
	  <<setw(10)<<cour.classroom
	  <<setw(16)<<cour.teacher
	  <<setw(3)<<cour.week.first
	  <<setw(3)<<cour.week.last
	  <<setw(3)<<cour.time.weekday
	  <<setw(3)<<cour.time.start
	  <<setw(3)<<cour.time.end;	 
	 return os;
}

istream &operator>>(istream &is,Course &cour) //course输入运算符重载
{
	char str[20];
	is>>str;
	cour.name=new char[strlen(str)+1];
	if (!cour.name)
	{
		cout<<" memory allocation error\n";
		exit(1);
	}
	strcpy(cour.name,str);
	is>>str;
	cour.classroom=new char[strlen(str)+1];
	if (!cour.classroom)
	{
		cout<<" memory allocation error\n";
		exit(1);
	}	
	strcpy(cour.classroom,str);
	is>>str;
	cour.teacher=new char[strlen(str)+1];
	if (!cour.teacher)
	{
		cout<<" memory allocation error\n";
		exit(1);
	}
	strcpy(cour.teacher,str);
	is>>cour.week.first;
	is>>cour.week.last;
	is>>cour.time.weekday;
	is>>cour.time.start;
	is>>cour.time.end;
	return is;
}

void Interface()		//用于显示用户界面
{
	cout<<"\t\t************************************************"<<endl;
	cout<<"\t\t\t\t  课程表管理"<<endl;
	cout<<"\t\t************************************************"<<endl;
    cout<<"\t\t\t\t1.课程信息输入"<<endl;
	cout<<"\t\t\t\t2.显示当天课程"<<endl;
	cout<<"\t\t\t\t3.显示本周课程"<<endl;
    cout<<"\t\t\t\t4.课程信息修改"<<endl;
	cout<<"\t\t\t\t0.退出"<<endl;
}

void InforInput (){	//输入课程信息并写入文档 
	int choose;
	cout<<"开始输入?(1,是 0,否)";
	cin>>choose;
		  while(choose){			  		      	
			  Course cour;
			  ofstream fout("course.txt",ios::app);
			  if(!fout){
				  cout<<"不能打开输入文件!"<<endl;
				  exit(1);
			  }
			  else{
				  fout<<cour;
				  cout<<cour;
			  }
			  cout<<endl;
			  cout<<"继续输入下一门课程?(1,是 0,否)";
			  cin>>choose;
			  
		  } 
}  

void DisplayToday (){	//显示当天课表(用户输入第几周,星期几)
	int currentweek;	//当前周
	int currentweekday; //当前星期几
	bool noclass = 0;

	cout<<"今天是第几周星期几 :";
	cin>>currentweek;
	cin>>currentweekday;
	Course temp(1);
	Week w;

	ifstream fin("Course.txt");
		if(!fin)
		{
			cout<<"文件不能打开!\n";
			exit(1);
		}
		//开始查询
		while(!fin.eof()){
			fin>>temp;
			w=temp.GetWeek();
			if ( (currentweek >=w.first)&&(currentweek <= w.last)){
			    if(temp.GetWeekday()==currentweekday){				
				Display ( temp );
				DisplayTime ( temp.Getstart (),temp.Getend () );
				cout<<endl;
				noclass = 1;
				}
				
			}
		}	
		if( !noclass ){
					cout<<"今天没有课!"<<endl;
			}
		fin.close();
} 

void DisplayWeek (){	//显示本周课表(用户输入第几周)
	int currentweek;	//当前周
	
	cout<<"请输入要显示第几周课程:";
	cin>>currentweek;
	cout<<endl;
	Course temp(1);
    Week w;	
	ifstream fin("Course.txt");
		if(!fin)
		{
			cout<<"文件不能打开!\n";
			exit(1);
		}
		//开始查询
		while( !fin.eof()){
				fin>>temp;
				w=temp.GetWeek();
			if ( (currentweek >= w.first)&&(currentweek <=w.last)){
				Display ( temp );
				DisplayTime ( temp.Getstart (),temp.Getend () );
				cout<<endl;				
			}			
		}
		
		fin.close();
}
void Modify (){		//课程信息修改
}

void DisplayTime ( int start,int end ){	//显示上课时间 (根据课程节数)
	int temp[2] = { start,end };
	cout<<"上课时间为:"<<endl;
	for ( int i = 0; i<2; i ++){
		switch (temp[i]){
		case 1:cout<<"第一节:8:00 - 8:45"<<endl;break;
		case 2:cout<<"第二节:8:50 - 9:35"<<endl;break;
		case 3:cout<<"第三节:9:50 - 10:35"<<endl;break;
		case 4:cout<<"第四节:10:40 - 11:25"<<endl;break;
		case 5:cout<<"第五节:11:30 - 12:15"<<endl;break;
		case 6:cout<<"第六节:13:30 - 14:15"<<endl;break;
		case 7:cout<<"第七节:14:20 - 15:05"<<endl;break;
		case 8:cout<<"第八节:15:20 - 16:05"<<endl;break;
		case 9:cout<<"第九节:16:10 - 16:55"<<endl;break;
		case 10:cout<<"第十节:17:30 - 17:45"<<endl;break;
		case 11:cout<<"第十一节:19:00 - 19:45"<<endl;break;
		case 12:cout<<"第十二节:19:50 - 20:35"<<endl;break;
		case 13:cout<<"第十三节:20:40 - 21:25"<<endl;break;
		default:cout<<"错误!"<<endl;break;
		}	
	}
}

void Display ( Course& c ){
	cout<<c.name<<endl;
	cout<<"教室:"<<c.classroom<<"  ";
	cout<<"教师:"<<c.teacher<<endl;
} 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -