STL 模板类学生成绩管理。可是不能运行
/**********************StudentMange.cpp************************/#include "stdafx.h"
#include<algorithm>
#include"Student.h"
#include"Course.h"
#include<iostream>
#include<map>
#include<string>
#include<fstream>
#include<iomanip>
#include<list>
using namespace std;
char chose;
int Num;
int CNum;
bool haveinfo = false;
list<Course> lc;
list<Student> ls;
list<Course>::iterator ic;
list<Student>::iterator is;
map<int,Student> ms; //按学号建立索引
void ShowMenu();
void findfailStudent();
void AddStudentInfo();
void AddScore();
void FindScore();
void statStudentScore();
void FindAllStudent();
void FindStudentByNo();
void findStudenScoreByArea();
int main(int argc, char* argv[])
{
ShowMenu();
return 0;
}
void ShowMenu()
{
cout<<"*******************************************"<<endl;
cout<<"*********欢迎使用学生成绩管理系统**********"<<endl;
cout<<"**** 录入学生基本信息(A) ******"<<endl;
cout<<"**** 添加课程 (G) ******"<<endl;
cout<<"**** 录入学生成绩 (B) ******"<<endl;
cout<<"**** 查询学生成绩 (C) ******"<<endl;
cout<<"**** 统计成绩 (T) ******"<<endl;
cout<<"**** 退出系统 (E) ******"<<endl;
cout<<"*******************************************"<<endl;
cout<<"*******************************************"<<endl;
cout<<endl;
cout<<"请输入所要进行的操作:";
cin>>chose;
switch(chose)
{
case'a':case'A': AddStudentInfo();break;
case'b':case'B': AddScore();break;
case'c':case'C': FindScore();break;
case't':case'T': statStudentScore();break;
case'e':case'E': system("PAUSE");break;
case'g':case'G': Student::AddCourseInfo();ShowMenu(); break;
default:system("cls");cout<<"Error:输入错误,请重新输入!"<<endl;ShowMenu();
}
}
//添加学生信息
void AddStudentInfo()
{
ofstream of("student.txt");
if(!of)
{
cerr<<"打开文件错误"<<endl;
exit(1);
}
cout<<"***************************"<<endl;
cout<<"请输入学生数量"<<endl;
cin>>Num;
//录入信息并保存到文本文件中
of<<setiosflags(ios::left)<<setw(10)<<"学号"<<setw(10)<<"姓名"<<setw(10)<<"性别"<<setw(10)<<"年龄"<<setw(10)<<"电话号码"<<endl;
for(int i=0; i<Num; i++)
{
cout<<"***************************"<<endl;
cout<<"请输入第"<<i+1<<"个学生的学号、姓名、性别、年龄、电话号码。"<<endl;
Student*s = new Student();
cin>>s->No>>s->Name>>s->Sex>>s->Age>>s->Phone;
s->MyCourse = Student::AllCourse;
//cout<<ms[1].No<<endl;
/*for(iter = Student::AllCourse.begin(); iter!=Student::AllCourse.end(); iter++)
{
cout<<(*iter).CourseName<<endl;
}*/
ls.push_back(*s);
of<<setiosflags(ios::left)<<setw(10)<<s->No<<setw(10)<<s->Name<<setw(10)<<s->Sex<<setw(10)<<s->Age<<setw(10)<<s->Phone<<endl;
}
of.close();
/*for(iter = ls.begin(); iter!=ls.end(); iter++)
{
cout<<(*iter).Name<<endl;
}*/
haveinfo = true;
cout<<endl;
cout<<"**************添加学生信息成功******************"<<endl;
system("cls");
ShowMenu();
}
//输入学生成绩
void AddScore()
{
if(!haveinfo)
{
cout<<endl;
cout<<"请首先完成学生基本信息输入"<<endl;
cout<<endl;
cout<<"输入学生基本信息请按(A)"<<" "<<"退出请按任意键"<<endl;
cout<<endl;
cin>>chose;
if(chose=='A'||chose=='a')
AddStudentInfo();
}
else
{
for(is = ls.begin(); is!=ls.end(); is++)
{
cout<<"***********************************"<<endl;
cout<<"请输入学号为:"<<(*is).No<<"学生姓名为:"<<(*is).Name<<"的各科成绩"<<endl;
(*is).MyCourse = Student::AllCourse;
for(ic = (*is).MyCourse.begin(); ic!=(*is).MyCourse.end(); ic++)
{
cout<<"["<<(*ic).CourseName<<"]"<<"的成绩为:"<<endl;
cin>>(*ic).CourseScore;
}
//按学号建立索引
ms.insert(pair<int,Student>(is->No,*is));
//课程与分数建立索引
is->CourseRScore();
/*for(ic = (*is).MyCourse.begin(); ic!=(*is).MyCourse.end(); ic++)
{
cout<<(*ic).CourseName<<"的成绩为:"<<endl;
cout<<(*ic).CourseScore;
}*/
}
}
system("cls");
cout<<"****成绩录入成功*****"<<endl;
ShowMenu();
}
//查询学生信息
void FindScore()
{
cout<<"**********************************"<<endl;
cout<<"**********查询学生成绩************"<<endl;
cout<<"***** 按学号查找 (N) ******"<<endl;
cout<<"***** 姓名查找 (M) ******"<<endl;
cout<<"***** 所有学生的成绩(F) ******"<<endl;
cout<<"***** 返回上一页 (U) ******"<<endl;
cout<<"**********************************"<<endl;
cout<<"**********************************"<<endl;
cout<<endl;
cout<<"请输入所要进行的操作:";
cin>>chose;
switch(chose)
{
case'f':case'F': FindAllStudent();break;
case'n':case'N': FindStudentByNo();break;
case'u':case'U': ShowMenu();break;
default:cout<<"输入错误,请重新输入!"<<endl;FindScore();
}
}
//按学号折半查找学生信息
void FindStudentByNo()
{
int sn;
cout<<endl;
cout<<"请输入要查找的学生学号:"<<endl;
cin>>sn;
Student fs = ms[sn];
//输出此学生的相关信息
cout<<"学号"<<setw(10)<<"姓名"<<endl;
cout<<fs.No<<setw(10)<<fs.Name<<endl;
for(ic = fs.MyCourse.begin(); ic!=fs.MyCourse.end(); ic++)
{
cout<<"课程名称:";
cout<<setw(5);
cout<<(*ic).CourseName;
cout<<setw(5);
cout<<"课程分数:";
cout<<setw(5);
cout<<(*ic).CourseScore;
}
cout<<endl;
FindScore();
}
//查询学生的所有成绩
void FindAllStudent()
{
cout<<"学号"<<setw(10)<<"姓名"<<setw(10);
for(ic = Student::AllCourse.begin(); ic!=Student::AllCourse.end(); ic++)
{
cout<<(*ic).CourseName<<" ";
}
cout<<endl;
for(is = ls.begin(); is!=ls.end(); is++)
{
cout<<(*is).No<<setw(10)<<(*is).Name<<setw(10);
for(ic = (*is).MyCourse.begin(); ic!=(*is).MyCourse.end(); ic++)
{
cout<<(*ic).CourseScore<<" ";
}
cout<<endl;
}
FindScore();
}
//统计成绩
void statStudentScore()
{
//具有不及格成绩的学生
cout<<"*************统计成绩**************"<<endl;
cout<<"***** 统计不及格学生 (S) *****"<<endl;
cout<<"***** 统计成绩区间情况(P) *****"<<endl;
cout<<"***********************************"<<endl;
cout<<endl;
cout<<"请输入所要进行的操作:";
cin>>chose;
switch(chose)
{
case's':case'S':findfailStudent();break;
case'p':case'P':findStudenScoreByArea();break;
default:cout<<"输入错误,请重新输入!"<<endl;FindScore();
}
}
//统计不及格学生
void findfailStudent()
{
for(is = ls.begin(); is!=ls.end(); is++)
{
for(ic = (*is).MyCourse.begin(); ic != (*is).MyCourse.end(); ic++)
{
if((*ic).CourseScore<60)
{
cout<<endl;
cout<<"学号"<<setw(10)<<"姓名"<<setw(10)<<"不及格科目"<<endl;
cout<<(*is).No<<setw(10)<<(*is).Name<<setw(10)<<(*ic).CourseName<<":"<<(*ic).CourseScore<<endl;
}
}
}
statStudentScore();
}
//按区间查询学生成绩
void findStudenScoreByArea()
{
string findCourse;
int f,s;
cout<<endl;
cout<<"请输入要查找的科目"<<endl;
cin>>findCourse;
cout<<"************输入区间参数(如,60 70)代表60至70************"<<endl;
cin>>f>>s;
cout<<"学号"<<setw(10)<<"姓名"<<setw(10)<<"分数"<<endl;
for(is = ls.begin(); is!=ls.end(); is++)
{
if(is->cm[findCourse]>=f&&is->cm[findCourse]<=s)
{
cout<<is->No<<setw(10)<<is->Name<<setw(10)<<is->cm[findCourse]<<endl;
}
else
{
}
}
cout<<endl;
//返回到成绩查找菜单
statStudentScore();
}
/**********************Student.h************************/
#if !defined(AFX_STUDENT_H__CC6FCD1E_97CA_46CA_BB7F_6674DEEF92D8__INCLUDED_)
#define AFX_STUDENT_H__CC6FCD1E_97CA_46CA_BB7F_6674DEEF92D8__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#include"Course.h"
#include<algorithm>
#include<string>
#include<list>
#include<map>
using namespace std;
class Student
{
public:
Student();
virtual ~Student();
int No; //学号
string Name; //姓名
string Sex; //性别
int Age; //年龄
string Phone; //电话号码
list<Course> MyCourse;
map<string,int> cm;
static list<Course> AllCourse;//课程信息
static void AddCourseInfo();//添加课程信息
void CourseRScore();//建立课程索引
};
#endif // !defined(AFX_STUDENT_H__CC6FCD1E_97CA_46CA_BB7F_6674DEEF92D8__INCLUDED_)
/****************************Student.cpp***************************************/
#include "stdafx.h"
#include<algorithm>
#include"Student.h"
#include"Course.h"
#include<iostream>
#include<map>
#include<string>
#include<fstream>
#include<iomanip>
#include<list>
using namespace std;
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
Student::Student()
{
}
Student::~Student()
{
}
list<Course> Student::AllCourse;
void Student:: AddCourseInfo(){
int CNum;
cout<<"*********************"<<endl;
cout<<"请输入课程数量"<<endl;
cin>>CNum;
for(int i=0; i<CNum; i++)
{
Course*c = new Course();
cout<<"请输入第"<<i+1<<"个课程信息:课程名称"<<endl;
c->CourseNo = i;
cin>>c->CourseName;
AllCourse.push_back(*c);
}
cout<<endl;
cout<<"*************添加课程信息成功***************"<<endl;
cout<<endl;
system("cls");
}
//建立课程与分数的索引
void Student::CourseRScore(){
list<Course>::iterator lc;
for(lc = MyCourse.begin(); lc != MyCourse.end(); lc++)
cm.insert(pair<string,int>((*lc).CourseName,(*lc).CourseScore));
}
/***********************************Course.h***********************************/
// Course.h: interface for the Course class.
//
//////////////////////////////////////////////////////////////////////
#if !defined(AFX_COURSE_H__86B85C3C_C0F8_4DBC_9A98_1E1004D33636__INCLUDED_)
#define AFX_COURSE_H__86B85C3C_C0F8_4DBC_9A98_1E1004D33636__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#include"Course.h"
#include<algorithm>
#include<iostream>
#include<string>
#include<list>
#include<map>
using namespace std;
class Course
{
public:
Course();
virtual ~Course();
int CourseNo;
string CourseName;
int CourseScore;
};
#endif // !defined(AFX_COURSE_H__86B85C3C_C0F8_4DBC_9A98_1E1004D33636__INCLUDED_)