函数运行不完整
#include<iostream>#include<fstream>
#include<cstring>
#include <stdlib.h>
#include <conio.h>
#include<vector>
using namespace std;
int n;
class Student
{public:
char sname;
char snum;
char sclass;
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;//教师姓名
T2 tlesson;//班级名称
T3 grade;//评教分数
T4 tnumber;//教师编号
T5 tele; //电话
T6 snumber;//班级编号
T7 syard;//所在院系
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.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.tlesson,x)==0)
{return i;}
}
return -1;
}
void System::find()
{
char x;
int a;
cout<<"请输入要查找的教师名称:"; cin>>x;
a=lname(x);
if(a!=-1)
v.show();
else
cout<<"没有找到相关教师信息"<<endl;
cout<<"输入任意字符继续";
getchar();
}
/*--------------------------更改数据-----------------------------*/
void System::change()
{
char b;
int k;
cout<<"请输入需要修改的教师名称:"; cin>>b;
k=lname(b);
if(k!=-1){
cout<<"已找到,请输入新的信息:"<<endl;
v.input();}
cout<<"输入任意字符继续:";
getchar();
}
/*--------------------------删除数据-----------------------------*/
void System::dele()
{
char c;
int d;
cout<<"请输入要删除的教师名称:"; cin>>c;
d=lname(c);
if(d!=-1)
{for(;d<n-1;d++)
{strcpy(v.tname,v.tname);
strcpy(v.tlesson,v.tlesson);
v.grade==v.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;
char snum;
char sclass;
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; //教师姓名
T tlesson; //班级名称
T grade; //评教分数
T tnumber; //教师编号
T tele; //电话
T snumber;//班级编号
T syard; //所在院系
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.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> 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.tlesson, x) == 0) {
return i;
}
}
return -1;
}
void System::find() {
char x;
int a = -1;
cout << "请输入要查找的教师名称:";
cin >> x;
for(int i = 0; i < n; ++i) {
if(strcmp(v.tname, x) == 0) {
a = i; break;
}
}
if(a != -1) v.show();
else cout << "没有找到相关教师信息" << endl;
cout << "输入任意字符继续";
getchar(); getchar();
}
/*--------------------------更改数据-----------------------------*/
void System::change() {
char b;
int k;
cout << "请输入需要修改的教师名称:";
cin >> b;
k = lname(b);
if(k != -1) {
cout << "已找到,请输入新的信息:" << endl;
v.input();
}
cout << "输入任意字符继续:";
getchar();
}
/*--------------------------删除数据-----------------------------*/
void System::dele() {
char c;
int d;
cout << "请输入要删除的教师名称:";
cin >> c;
d = lname(c);
if(d != -1) {
for(; d < n - 1; d++) {
strcpy(v.tname, v.tname);
strcpy(v.tlesson, v.tlesson);
v.grade = v.grade;
}
n--;
} else cout << "输入错误,找不到该教师信息" << endl;
cout << "输入任意字符继续";
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;
}
人造人 发表于 2021-9-20 12:41
稍微修改了一点
有错误啊C:\Users\22482\Desktop\新建文件夹\新建文件夹 人造人 发表于 2021-9-20 12:41
稍微修改了一点
现在删除功能又出现问题了
呜呜
woshijunjun 发表于 2021-9-20 15:16
现在删除功能又出现问题了
呜呜
你都不调试程序的吗?
基础很重要的,先打基础吧
#include <cstring>
#include <fstream>
#include <iostream>
#include <cstdlib>
#include <vector>
using namespace std;
int n;
class Student {
public:
char sname;
char snum;
char sclass;
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; //教师姓名
T tlesson; //班级名称
T grade; //评教分数
T tnumber; //教师编号
T tele; //电话
T snumber;//班级编号
T syard; //所在院系
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.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.tlesson, x) == 0) {
return i;
}
}
return -1;
}
void System::find() {
char x;
int a = -1;
cout << "请输入要查找的教师名称:";
cin >> x;
for(int i = 0; i < n; ++i) {
if(strcmp(v.tname, x) == 0) {
a = i; break;
}
}
if(a != -1) v.show();
else cout << "没有找到相关教师信息" << endl;
cout << "输入任意字符继续";
getchar(); getchar();
}
/*--------------------------更改数据-----------------------------*/
void System::change() {
char b;
int k;
cout << "请输入需要修改的教师名称:";
cin >> b;
k = lname(b);
if(k != -1) {
cout << "已找到,请输入新的信息:" << endl;
v.input();
}
cout << "输入任意字符继续:";
getchar();
}
/*--------------------------删除数据-----------------------------*/
void System::dele() {
char c;
int d = -1;
cout << "请输入要删除的教师名称:";
cin >> c;
for(int i = 0; i < n; ++i) {
if(strcmp(v.tname, c) == 0) {
d = i; break;
}
}
if(d != -1) {
for(; d < n - 1; d++) {
strcpy(v.tname, v.tname);
strcpy(v.tlesson, v.tlesson);
v.grade = v.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;
}
人造人 发表于 2021-9-20 15:50
你都不调试程序的吗?
基础很重要的,先打基础吧
好的,哥
页:
[1]