程序功能部分有问题,目前3,4,5功能有问题,希望可以帮忙修改一下
#pragma once
#include
#include
#include
using namespace std;
class Student
{
private:
Student* next;
int student_ID;
string name;
string gender;
int age;
string tel;
string major;
int flag;
Student* first;
public:
Student();
void read( );//读取学生信息
void show_Menu();//展示菜单
void Exitsystem();//退出系统
void find( );//查找学生信息
int s_num;//记录学生人数
void searchbyname( );
void searchbyID( );
void searchbytel( );
void Delete( );
static int num_total;
static int num_sex;
static int num_age;
Student** s_stuArray;
~Student();
void Add_stu( );
friend void write(Student stu[], int n);
void save( );
void change( );
};
Student::Student()
{
this->s_num = 0;
this->s_stuArray = NULL;
student_ID = 0;
age = 5;
}
void Student::show_Menu()//目录
{
cout << "**************************************" << endl;
cout << "*******欢迎使用学生信息管理系统!*****" << endl;
cout << "**********0、录入学生信息*************" << endl;
cout << "**********1、显示学生信息*************" << endl;
cout << "**********2、查找学生信息*************" << endl;
cout << "**********3、删除学生信息*************" << endl;
cout << "**********4、添加学生信息*************" << endl;
cout << "**********5、修改学生信息*************" << endl;
cout << "**********6、退出系统*****************" << endl;
cout << "**************************************" << endl;
cout << endl;
}
void Student::Exitsystem()
{
cout << "欢迎下次使用" << endl;
exit(0);//退出程序
}
void Student::read( )//读取学生信息
{
ifstream infile;
infile.open("student.txt", ios::in);
if (!infile.is_open())
{
cout << "open error" << endl;
return;
}
char buf[1024] = { 0 };
while (infile >> buf)
{
cout << buf << endl;
}
infile.close();
}
void Student::find( )
{
Student* p = NULL;
int m = 0;
int flag = 0;
cout << "\n\t\t******************************\n";
cout << "\t\t*****1.按学生姓名查找*********\n";
cout << "\t\t*****2.按学生学号查找*********\n";
cout << "\t\t*****3.按学生电话号码查找*****\n";
cout << "请输入你的选择" << endl;
cin >> m;
switch (m)
{
case 1:
searchbyname();
break;
case 2:
searchbyID();
break;
case 3:
searchbytel();
break;
}
}
void write(Student stu[], int n)//录入学生信息
{
ofstream outfile("student.txt", ios::out);
if (!outfile)
{
cout << "open error!" << endl;
exit(1);
}
cout << "请输入" << n << "个学生信息" << endl;
cout << "学号 姓名 性别 年龄 电话号码 专业" << endl;
for (int i = 0; i < n; i++)
{
cin >> stu[i].student_ID >> stu[i].name >> stu[i].gender >> stu[i].age >> stu[i].tel >> stu[i].major;
outfile << stu[i].student_ID << " " << stu[i].name << " " << stu[i].age << " " << stu[i].tel << " " << stu[i].major << endl;
}
outfile.close();
}
void Student::searchbyname( )
{
system("cls");
cout << "请输入要查找的的学生姓名: ";
int flag = 0;
string _name;
cin >> _name;
cout <<"输入的姓名:"<< _name << " first节点 " << first->num_total << endl;
Student* p = first->next;
cout << _name << " " << p->name << endl;
while (p != NULL)
{
if (p->name == _name)
{
flag = 1;
cout << "下面是查找匹配结果" << endl;
cout << "学号: " << p->student_ID << " " << "姓名" << p->name << " "
<< "性别" << p->gender << " " << "年龄" << p->age << " " << "电话号码"
<< p->tel << " " << "专业" << endl;
}
p = p->next;
}
if (flag == 0)
{
cout << "未找到匹配项。。。" << endl;
}
system("cls");
}
void Student::searchbyID( )
{
system("cls");
int flag = 0;
int id;
cout << "请输入待查找学生的学号: ";
cin >> id;
Student* p = first->next;
while (p != NULL)
{
if (p->student_ID == id)
{
flag = 1;
cout << "下面是查找匹配结果" << endl;
cout << endl;
cout << "学号: " << p->student_ID << " " << "姓名" << p->name << " "
<< "性别" << p->gender << " " << "年龄" << p->age << " " << "电话号码"
<< p->tel << " " << "专业" << endl;
}
p = p->next;
}
if (flag == 0)
{
cout << "未找到匹配项。。。" << endl;
}
cout << endl; cout << endl; cout << endl;
cout << "按任意键继续" << endl;
system("cls");
}
void Student::searchbytel( )
{
system("cls");
int flag = 0;
string _tel;
cout << "请输入待查找学生的电话号码: ";
cin >> _tel;
Student* p = first->next;
while (p != NULL)
{
if (p->tel==_tel)
{
flag = 1;
cout << "下面是查找匹配结果" << endl;
cout << endl;
cout << "学号: " << p->student_ID << " " << "姓名" << p->name << " "
<< "性别" << p->gender << " " << "年龄" << p->age << " " << "电话号码"
<< p->tel << " " << "专业" << endl;
}
p = p->next;
}
if (flag == 0)
{
cout << "未找到匹配项。。。" << endl;
}
cout << endl; cout << endl; cout << endl;
cout << "按任意键继续" << endl;
system("cls");
}
void Student::Delete( )
{
string _name;
system("cls");
cout << "请输入需要删除的学生姓名:" << endl;
cin >> _name;
int k = 0;
Student* p = first;
Student* pre = first;
while (p->next)
{
pre = p->next;
if (pre->name == _name)
{
p->next = pre->next;
k = 1;
delete pre;
}
p = p->next;
}
if (k == 0 && p->name != _name)
cout << "记录为空!" << endl;
}
void Student::Add_stu()
{
int addNum = 0;
cout << "请输入添加学生的数量" << endl;
cin >> addNum;
if (addNum > 0)
{
int newSize = this->s_num + addNum;
Student** newSpace = new Student * [newSize];
if (this->s_stuArray != NULL)
{
for (int i = 0; i < this->s_num; i++)
{
newSpace[i] = this->s_stuArray[i];
}
}
for (int i = 0; i "请输入" << addNum << "个学生信息" << endl;
cout << "请输入学生的学号 姓名 性别 年龄 电话号码 专业" << endl;
cin >> student_ID >> name >> gender >> age >> tel >> major;
}
delete[]this-> s_stuArray;
this->s_stuArray = newSpace;
this->s_num = newSize;
this->save();//保存数据到文件中
cout << "成功添加" << addNum << "名学生" << endl;
}
else
{
cout << "输入数据有误" << endl;
}
system("pause");
system("cls");
}
void Student::save()
{
ofstream outfile;
outfile.open("f1.txt", ios::out);
for (int i = 0; i < this->s_num; i++)
{
outfile << this->s_stuArray[i]->student_ID << this->s_stuArray[i]->name << this->s_stuArray[i]->gender <<
this->s_stuArray[i]->age << this->s_stuArray[i]->tel <<
this->s_stuArray[i]->major << endl;
}
outfile.close();
}
Student::~Student()
{
if (this->s_stuArray != NULL)
{
delete[]s_stuArray;
this->s_stuArray = NULL;
}
}
void Student::change()
{
system("cls");
int flag = 0, temp;
int _id, _age,_name1,_gender,_tel,_major;
string _name;
cout << "请输入需要改动信息的学生姓名:" << endl;
cin >> _name;
Student* p = first->next;
while (p != NULL)
{
if (p->name == _name)
{
flag = 1;
cout << "该学生当前信息如下:" << endl;
cout << "学号:" << p->student_ID << "姓名:" << p->name << "性别:" << gender << "年龄:" << p->age << "电话号码" << p->tel << "专业" << major << endl;
}
cout << "需要修改哪一项" << endl;
cout << "1.学号" << endl << "2.姓名" << endl << "3.性别" << endl << "4.年龄" << endl << "5.电话号码" << endl << "6.专业" << endl;
cin >> temp;
switch (temp)
{
case 1:
{
cout << "请输入新的学号" << endl;
cin >> _id;
p->student_ID = _id;
}
break;
case 2:
{
cout << "请输入新的姓名" << endl;
cin >> _name1;
p->name = _name;
}
case 3:
{
cout << "请输入新的性别" << endl;
cin >> _gender;
p->gender = _gender;
}
break;
case 4:
{
cout << "请输入新的年龄" << endl;
cin >> _age;
p->age = _age;
}
break;
case 5:
{
cout << "请输入新的电话号码" << endl;
cin >> _tel;
p->tel = _tel;
}
break;
case 6:
{
cout << "请输入新的专业" << endl;
cin >> _major;
p->major = _major;
}
break;
cout << "修改后的信息如下" << endl;
cout << "学号: " << p->student_ID << " " << "姓名" << p->name << " "
<< "性别" << p->gender << " " << "年龄" << p->age << " " << "电话号码"
<< p->tel << " " << "专业" << endl;
default:
cout << "输入有误" << endl;
}
p = p->next;
}
if (flag == 0)
cout << "当前记录中没有此学生" << endl;
}
int main()
{
Student a[2];
int choice = 0;
while (true)
{
a[2].show_Menu();
cout << "请输入您的选择" << endl;
cin >> choice;
switch (choice)
{
case 0: //录入信息
write(a, 2);
break;
case 1:
a[2].read();//显示学生信息
break;
case 2:
a[2].find();//查询学生信息
break;
case 3:
a[2].Delete();//删除学生信息
break;
case 4:
a[2].Add_stu();//添加学生信息
break;
case 5:
a[2].change();
break;
case 6:a[2].Exitsystem();
break;
default:
system("cls");
break;
}
}
return 0;
}