C++学生信息管理(用类实现输入保存、查询、显示)
实现:
(1)从键盘输入N个学生的信息,保存到自定义命名的文件中(比如, Broccoli.txt/dat)。
(2)设计表示学生信息的类Student,其中包括但不限于学生的编号、姓名、电话等数据成员(访问权限均为私有),以及若干成员函数。
(3)从文件(自定义命名)中读取所有学生的信息并显示到屏幕上,对文件的读写分别以ifstream和ofstream实现。
(4)分别定义方法void seek1(int seeknumber)、void seek2(char seekname[])实现按编号和姓名查找学生的信息。
运行截图
代码实现
/*
练习:
(1)从键盘输入N个学生的信息,保存到自定义命名的文件中(比如, Broccoli.txt/dat)。
(2)设计表示学生信息的类Student,其中包括但不限于学生的编号、姓名、电话等数据成员(访问权限均为私有),以及若干成员函数。
(3)从文件(自定义命名)中读取所有学生的信息并显示到屏幕上,对文件的读写分别以ifstream和ofstream实现。
(4)分别定义方法void seek1(int seeknumber)、void seek2(char seekname[])实现按编号和姓名查找学生的信息。
*/
#include"iostream"
#include"fstream"
using namespace std;
#define N 2 //宏定义学生的数量
class Student //创建学生信息的类Student
{
private:
int number;
char name[10];
char phone[13];
public:
void input();
void output();
void seek1(int seeknumber);
void seek2(char seekname[10]);
};
void Student::input() //输入学生信息
{
//创建Broccoli.txt文件并写入信息
ofstream file1;
file1.open("Broccoli.txt", ios::out);
for (int i = 0; i < N; i++)
{
cout << "【请输入第" << i + 1 << "位学生的学生信息】" << endl;
cout << "编号:";
cin >> number;
file1 << number << endl;
cout << "姓名:";
cin >> name;
file1 << name << endl;
cout << "电话:";
cin >> phone;
file1 << phone << endl;
cout << endl;
}
file1.close();
}
void Student::output() //显示所有学生信息
{
//调用Broccoli.txt文件中的信息并显示
ifstream file2;
file2.open("Broccoli.txt", ios::in);
for (int i = 0; i < N; i++)
{
cout << endl;
cout << "【第" << i + 1 << "位学生】" << endl;
file2 >> number;
cout << "编号:" << number << endl;
file2 >> name;
cout << "姓名:" << name << endl;
file2 >> phone;
cout << "电话:" << phone << endl;
}
file2.close();
}
void Student::seek1(int seeknumber) //按编号查询学生信息
{
int time = 0;
ifstream file3;
file3.open("Broccoli.txt", ios::in);
for (int i = 0; i < N; i++)
{
file3 >> number;
file3 >> name;
file3 >> phone;
if (seeknumber == number)
{
time++;
cout << endl;
cout << "【查询结果如下】" << endl;
cout << "编号:" << number << endl;
cout << "姓名:" << name << endl;
cout << "电话:" << phone << endl;
cout << endl;
break;
}
}
if (time == 0)
{
cout << endl;
cout << "查无此人!" << endl;
cout << "请查看是否输入正确的学生编号" << endl;
cout << endl;
}
file3.close();
}
void Student::seek2(char seekname[10]) //按姓名查找学生信息
{
int time = 0;
ifstream file4;
file4.open("Broccoli.txt", ios::in);
for (int i = 0; i < N; i++)
{
file4 >> number;
file4 >> name;
file4 >> phone;
if (strcmp(seekname, name) == 0) //比较字符串是否相同
{
time++;
cout << endl;
cout << "【查询结果如下】" << endl;
cout << "编号:" << number << endl;
cout << "姓名:" << name << endl;
cout << "电话:" << phone << endl;
cout << endl;
break;
}
}
if (time == 0)
{
cout << "查无此人!!!" << endl;
cout << "请查看是否输入正确的学生姓名" << endl;
cout << endl;
}
file4.close();
}
int main()
{
Student Eder, *p; //定义类的对象Eder和指针变量p
p = &Eder; //指针变量指向Eder
p->input();
//Eder.input();
cout << " ----------------------" << endl;
cout << "| 1.按编号查找学生信息 |" << endl;
cout << "| 2.按姓名查找学生信息 |" << endl;
cout << "| 3.显示所有学生信息 |" << endl;
cout << "| 4.保存信息并退出 |" << endl;
cout << " ----------------------" << endl;
int choice;
int seeknumber;
char seekname[10];
while (1)
{
cout << "请输入你的选择:";
cin >> choice;
while (choice != 1 && choice != 2 && choice != 3 && choice != 4)
{
cout << "输入错误!" << endl;
cout << "请重新输入你的选择:";
cin >> choice;
}
switch (choice)
{
case 1:
cout << "请输入要查找的学生编号:";
cin >> seeknumber;
p->seek1(seeknumber);
//Eder.seek1(seeknumber);
break;
case 2:
cout << "请输入要查找的姓名:";
cin >> seekname;
p->seek2(seekname);
//Eder.seek2(seekname);
break;
case 3:
p->output();
//Eder.output();
break;
case 4:
break;
}
}
return 0;
}
代码小白,仅作学习记录📝