|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
#include<iostream>
#include<fstream>
#include<cstring>
#include <stdlib.h>
#include <conio.h>
#include<vector>
using namespace std;
int n;
class Student
{public:
char sname[20];
char snum[20];
char sclass[20];
void input();
};
/*--------------------------学生登陆-----------------------------*/
void Student::input()
{
cout<<"请输入您的姓名:"; cin>>sname;
cout<<"请输入您的学号:"; cin>>snum;
cout<<"请输入您的班级:"; cin>>sclass;
cout<<"登陆成功!输入任意字符继续";
getchar();
}
/*--------------------------学生,教师类模板-----------------------------*/
template<typename T1,typename T2,typename T3,typename T4,typename T5,typename T6,typename T7>
class Lesson
{
public:
Lesson(){};
~Lesson(){};
T1 tname[100];//教师姓名
T2 tlesson[100];//班级名称
T3 grade;//评教分数
T4 tnumber[100];//教师编号
T5 tele[100]; //电话
T6 snumber[50];//班级编号
T7 syard[100];//所在院系
void display();
void input();
void show();
};
/*--------------------------输出函数-----------------------------*/
template<typename T1,typename T2,typename T3,typename T4,typename T5,typename T6,typename T7>
void Lesson<T1,T2,T3,T4,T5,T6,T7>::show()
{
cout<<"教师姓名:"<<tname<<endl;
cout<<"教师编号:"<<tnumber<<endl;
cout<<"电话:"<<tele<<endl;
cout<<"班级名称:"<<tlesson<<endl;
cout<<"班级编号 :"<<snumber<<endl;
cout<<"所在院系 :"<<syard<<endl;
cout<<"评教分数:"<<grade<<endl;
}
/*--------------------------评学类-----------------------------*/
class System
{public:
System(){};
~System(){};
void add();
int lname(char *x);
void find();
void change();
void dele();
void menu();
void display();//显示
void save();
void read();
};
vector<Lesson<char,char,char,char,char,char,char> >v;
/*--------------------------输入函数-----------------------------*/
template<typename T1,typename T2,typename T3,typename T4,typename T5,typename T6,typename T7>
void Lesson<T1,T2,T3,T4,T5,T6,T7>::input()
{
cout<<"请输入教师姓名:"; cin>>tname;
cout<<"请输入教师编号:"; cin>>tnumber;
cout<<"请输入电话:"; cin>>tele;
cout<<"请输入班级名称:"; cin>>tlesson;
cout<<"请输入班级编号 :"; cin>>snumber;
cout<<"请输入所在院系 :"; cin>>syard;
cout<<"请输入打分(0-100):"; cin>>grade;
try{ //异常处理
if(grade<0||grade>100) throw 0;
else
cout<<endl;}
catch(int)
{cout<<"输入数据错误,请重新输入";}
cout<<endl;
}
/*--------------------------读取函数-----------------------------*/
void System::read()
{
Lesson<char,char,char,char,char,char,char> ch;
ifstream infile("sys.txt",ios::binary);
if(!infile){
cout<<"文件打不开"<<endl;
}
infile.read((char*)&ch,sizeof(ch));
while(!infile.eof())
{
v.push_back(ch);
infile.read((char*)&ch,sizeof(ch));
cout<<"教师姓名:"<<ch.tname<<endl;
cout<<"教师编号:"<<ch.tnumber<<endl;
cout<<"电话:"<<ch.tele<<endl;
cout<<"班级名称:"<<ch.tlesson<<endl;
cout<<"班级编号 :"<<ch.snumber<<endl;
cout<<"所在院系 :"<<ch.syard<<endl;
cout<<"评教分数:"<<ch.grade<<endl;
cout<<endl;
}
infile.close();
cout<<"读取成功"<<endl;
getchar();
}
/*--------------------------保存函数-----------------------------*/
void System::save()
{
ofstream outfile("sys.txt",ios::binary);
Lesson<char,char,char,char,char,char,char>t;
vector<Lesson<char,char,char,char,char,char,char> >::iterator ap;
if(!outfile)
cout<<"文件不能被打开"<<endl;
else
{
for(ap=v.begin();ap!=v.end();ap++)
{
t=*ap;
outfile.write((char*)&t,sizeof(t));
cout<<"保存成功"<<endl;
}
outfile.close();
getchar();
}
}
void System::display()
{
for(int i=0;i<n;i++)
{v[i].show();}
cout<<"输入任意字符继续"; getchar();
}
/*--------------------------主菜单-----------------------------*/
void System::menu()
{
cout<<"--------------*评 教 管 理 系 统*-------------"<<endl;
cout<<"--------------*$1. 学生登陆界面*--------------"<<endl;
cout<<"--------------*$2. 增加评教信息*--------------"<<endl;
cout<<"--------------*$3. 更改评教信息*--------------"<<endl;
cout<<"--------------*$4. 查找评教信息*--------------"<<endl;
cout<<"--------------*$5. 显示评教信息*--------------"<<endl;
cout<<"--------------*$6. 删除评教信息*--------------"<<endl;
cout<<"--------------*$7. 保存相关数据*--------------"<<endl;
cout<<"--------------*$8. 读取相关数据*--------------"<<endl;
cout<<"--------------*$9. 退出评教系统*--------------"<<endl;
}
/*--------------------------添加数据-----------------------------*/
void System::add()
{
string h="y";
do{
Lesson<char,char,char,char,char,char,char>l;
l.input();
n++;
v.push_back(l);
cout<<"是否继续添加(y/n)?";
cin>>h;
}while(h=="y");
cout<<"添加成功!输入任意字符继续";
getchar();
}
/*--------------------------查找课程-----------------------------*/
int System::lname(char *x)
{
int i;
for(i=0;i<n;i++)
{
if(strcmp(v[i].tlesson,x)==0)
{ return i;}
}
return -1;
}
void System::find()
{
char x[20];
int a;
cout<<"请输入要查找的教师名称:"; cin>>x;
a=lname(x);
if(a!=-1)
v[a].show();
else
cout<<"没有找到相关教师信息"<<endl;
cout<<"输入任意字符继续";
getchar();
}
/*--------------------------更改数据-----------------------------*/
void System::change()
{
char b[20];
int k;
cout<<"请输入需要修改的教师名称:"; cin>>b;
k=lname(b);
if(k!=-1){
cout<<"已找到,请输入新的信息:"<<endl;
v[k].input();}
cout<<"输入任意字符继续:";
getchar();
}
/*--------------------------删除数据-----------------------------*/
void System::dele()
{
char c[20];
int d;
cout<<"请输入要删除的教师名称:"; cin>>c;
d=lname(c);
if(d!=-1)
{for(;d<n-1;d++)
{strcpy(v[d].tname,v[d+1].tname);
strcpy(v[d].tlesson,v[d+1].tlesson);
v[d].grade==v[d+1].grade;
}
n--;
}
else
cout<<"输入错误,找不到该教师信息"<<endl;
cout<<"输入任意字符继续";
getchar();
}
/*--------------------------主函数-----------------------------*/
int main()
{
Student x;
System y;
int a;
string ss="y";
do{
system("cls");
cout<<"欢迎进入评教管理系统"<<endl;
y.menu() ;
cout<<"请输入相应阿拉伯数字:";
cin>>a;
switch(a)
{
case 1:x.input();break;
case 2:y.add();break;
case 3:y.change();break;
case 4:y.find();break;
case 5:y.display();break;
case 6:y.dele();break;
case 7:y.save();break;
case 8:y.read();break;
case 9:ss="n";break;
}
}while(ss=="y");
return 0;
}
输入4查找,并没有效果
实在不明白是咋回事
你都不调试程序的吗?
基础很重要的,先打基础吧
#include <cstring>
#include <fstream>
#include <iostream>
#include <cstdlib>
#include <vector>
using namespace std;
int n;
class Student {
public:
char sname[20];
char snum[20];
char sclass[20];
void input();
};
/*--------------------------学生登陆-----------------------------*/
void Student::input() {
cout << "请输入您的姓名:"; cin >> this->sname;
cout << "请输入您的学号:"; cin >> this->snum;
cout << "请输入您的班级:"; cin >> this->sclass;
cout << "登陆成功!输入任意字符继续"; getchar();
}
/*--------------------------学生,教师类模板-----------------------------*/
template <typename T> class Lesson {
public:
Lesson(){};
~Lesson(){};
T tname[100]; //教师姓名
T tlesson[100]; //班级名称
T grade; //评教分数
T tnumber[100]; //教师编号
T tele[100]; //电话
T snumber[50]; //班级编号
T syard[100]; //所在院系
void display();
void input();
void show();
};
/*--------------------------输出函数-----------------------------*/
template <typename T>
void Lesson<T>::show() {
cout << "教师姓名:" << tname << endl;
cout << "教师编号:" << tnumber << endl;
cout << "电话:" << tele << endl;
cout << "班级名称:" << tlesson << endl;
cout << "班级编号 :" << snumber << endl;
cout << "所在院系 :" << syard << endl;
cout << "评教分数:" << grade << endl;
}
/*--------------------------评学类-----------------------------*/
class System {
public:
System(){};
~System(){};
void add();
int lname(char *x);
void find();
void change();
void dele();
void menu();
void display(); //显示
void save();
void read();
};
vector<Lesson<char>> v;
/*--------------------------输入函数-----------------------------*/
template <typename T>
void Lesson<T>::input() {
cout << "请输入教师姓名:"; cin >> tname;
cout << "请输入教师编号:"; cin >> tnumber;
cout << "请输入电话:"; cin >> tele;
cout << "请输入班级名称:"; cin >> tlesson;
cout << "请输入班级编号 :"; cin >> snumber;
cout << "请输入所在院系 :"; cin >> syard;
cout << "请输入打分(0-100):"; cin >> grade;
try {
if(grade < 0 || grade > 100) throw 0;
else cout << endl;
} catch(int) {
cout << "输入数据错误,请重新输入";
}
cout << endl;
}
/*--------------------------读取函数-----------------------------*/
void System::read() {
Lesson<char> ch;
ifstream infile("sys.txt", ios::binary);
if(!infile) {
cout << "文件打不开" << endl;
}
infile.read((char *)&ch, sizeof(ch));
while(!infile.eof()) {
v.push_back(ch);
infile.read((char *)&ch, sizeof(ch));
cout << "教师姓名:" << ch.tname << endl;
cout << "教师编号:" << ch.tnumber << endl;
cout << "电话:" << ch.tele << endl;
cout << "班级名称:" << ch.tlesson << endl;
cout << "班级编号 :" << ch.snumber << endl;
cout << "所在院系 :" << ch.syard << endl;
cout << "评教分数:" << ch.grade << endl;
cout << endl;
}
infile.close();
cout << "读取成功" << endl;
getchar();
}
/*--------------------------保存函数-----------------------------*/
void System::save() {
ofstream outfile("sys.txt", ios::binary);
Lesson<char> t;
vector<Lesson<char>>::iterator ap;
if(!outfile) cout << "文件不能被打开" << endl;
else {
for(ap = v.begin(); ap != v.end(); ap++) {
t = *ap;
outfile.write((char *)&t, sizeof(t));
cout << "保存成功" << endl;
}
outfile.close();
getchar();
}
}
void System::display() {
for(int i = 0; i < n; i++) {
v[i].show();
}
cout << "输入任意字符继续";
getchar(); getchar();
}
/*--------------------------主菜单-----------------------------*/
void System::menu() {
cout << "--------------*评 教 管 理 系 统*-------------" << endl;
cout << "--------------*$1. 学生登陆界面*--------------" << endl;
cout << "--------------*$2. 增加评教信息*--------------" << endl;
cout << "--------------*$3. 更改评教信息*--------------" << endl;
cout << "--------------*$4. 查找评教信息*--------------" << endl;
cout << "--------------*$5. 显示评教信息*--------------" << endl;
cout << "--------------*$6. 删除评教信息*--------------" << endl;
cout << "--------------*$7. 保存相关数据*--------------" << endl;
cout << "--------------*$8. 读取相关数据*--------------" << endl;
cout << "--------------*$9. 退出评教系统*--------------" << endl;
}
/*--------------------------添加数据-----------------------------*/
void System::add() {
string h = "y";
do {
Lesson<char> l;
l.input();
n++;
v.push_back(l);
cout << "是否继续添加(y/n)?";
cin >> h;
} while(h == "y");
cout << "添加成功!输入任意字符继续";
getchar();
}
/*--------------------------查找课程-----------------------------*/
int System::lname(char *x) {
int i;
for(i = 0; i < n; i++) {
if(strcmp(v[i].tlesson, x) == 0) {
return i;
}
}
return -1;
}
void System::find() {
char x[20];
int a = -1;
cout << "请输入要查找的教师名称:";
cin >> x;
for(int i = 0; i < n; ++i) {
if(strcmp(v[i].tname, x) == 0) {
a = i; break;
}
}
if(a != -1) v[a].show();
else cout << "没有找到相关教师信息" << endl;
cout << "输入任意字符继续";
getchar(); getchar();
}
/*--------------------------更改数据-----------------------------*/
void System::change() {
char b[20];
int k;
cout << "请输入需要修改的教师名称:";
cin >> b;
k = lname(b);
if(k != -1) {
cout << "已找到,请输入新的信息:" << endl;
v[k].input();
}
cout << "输入任意字符继续:";
getchar();
}
/*--------------------------删除数据-----------------------------*/
void System::dele() {
char c[20];
int d = -1;
cout << "请输入要删除的教师名称:";
cin >> c;
for(int i = 0; i < n; ++i) {
if(strcmp(v[i].tname, c) == 0) {
d = i; break;
}
}
if(d != -1) {
for(; d < n - 1; d++) {
strcpy(v[d].tname, v[d + 1].tname);
strcpy(v[d].tlesson, v[d + 1].tlesson);
v[d].grade = v[d + 1].grade;
}
n--;
} else cout << "输入错误,找不到该教师信息" << endl;
cout << "输入任意字符继续";
getchar(); getchar();
}
/*--------------------------主函数-----------------------------*/
int main() {
Student x;
System y;
int a;
string ss = "y";
do {
system("clear");
cout << "欢迎进入评教管理系统" << endl;
y.menu();
cout << "请输入相应阿拉伯数字:";
cin >> a;
switch(a) {
case 1: x.input(); break;
case 2: y.add(); break;
case 3: y.change(); break;
case 4: y.find(); break;
case 5: y.display(); break;
case 6: y.dele(); break;
case 7: y.save(); break;
case 8: y.read(); break;
case 9: ss = "n"; break;
}
} while(ss == "y");
return 0;
}
|
|