C++ 实现班级成员管理系统
班级成员管理系统:要求:
1.Console程序;
2.对象管理每个学生(属性包括姓名、年龄、性别);
3.支持添加输入,能记录到文件;
4.支持从文件读入;
5.支持写入文件。
追问:谢谢啊,不胜感激哦,嘻嘻
追问:谢谢啊,不胜感激哦,嘻嘻
答案:
#include <iostream>
#include <fstream>
#include <string>
#include <conio.h>
using namespace std;
//学生信息
struct Student
{
int num;
string name;
string 易做图;
int age;
Student *next;
};
//重载 << 运算符
ostream &operator << (ostream &output, Student *&s)
{
output << s->num << '\t' << s->name << '\t' << s->易做图 << '\t' << s->age << endl;
return output;
}
//重载 >> 运算符
istream &operator >> (istream &input, Student *&s)
{
input >> s->num >> s->name >> s->易做图 >> s->age;
return input;
}
//初始化链表
void InitList (Student *&L)
{
L = new Student;
L->next = NULL;
}
//保存学生信息
void SaveData (Student *L)
{
Student *p = L->next;
ofstream outfile ("student.txt", ios::out);
if (!outfile)
{
cerr << "文件打开失败!" << endl;
exit (1);
}
outfile << "学号\t姓名\t性别\t年龄" << endl;
while (p != NULL)
{
outfile << p;
p = p->next;
}
outfile.close ();
}
//读取学生信息
void ReadData (Student *&L)
{
Student *r = L, *s;
ifstream infile ("student.txt", ios::in);
if (!infile)
{
cerr << "文件打开失败!" << endl;
exit (1);
}
infile.seekg (sizeof ("学号\t姓名\t性别\t年龄"));
while (!infile.eof ())
{
s = new Student;
infile >> s;
if (!infile.eof ())
{
r->next = s;
r = s;
}
}
r->next = NULL;
infile.close ();
}
//按学号查找
void SearchByNum ()
{
system ("cls");
Student *L;
InitList (L);
ofstream outfile ("student.txt", ios::app);
if (!outfile)
{
outfile.close ();
ofstream outfile ("student.txt", ios::trunc);
}
outfile.close ();
ReadData (L);
cout << "请输入学号:";
int n;
cin >> n;
Student *p = L->next;
while ((p != NULL) && (p->num != n))
{
p = p->next;
}
if (p == NULL)
cout << "查无此人!" << endl << endl;
else
cout << p << endl;
system ("pause");
}
//增加成员
void AddData ()
{
system ("cls");
Student *L, *s;
InitList (L);
//如果打开文件失败,则新建一个文件
ofstream outfile ("student.txt", ios::app);
if (!outfile)
{
outfile.close ();
ofstream outfile ("student.txt", ios::trunc);
outfile << "学号\t姓名\t性别\t年龄" << endl;
}
outfile.close ();
ReadData (L);
//输入新成员资料,并且学号不能重复
cout << "请输入以下资料:" << endl;
cout << "学号\t姓名\t性别\t年龄" << endl;
bool flag = true;
while (flag)
{
s = new Student;
cin >> s;
Student *q = L->next;
while ((q != NULL) && (q->num != s->num))
{
q = q->next;
}
if (q == NULL)
{
flag = false;
}
else
{
cout << "已存在该学号的学生,请重新输入……" << endl;
flag = true;
}
}
cout << "是否保存该学生信息?\n(1)是\n(2)否" << endl;
switch (getch ())
{
case '1':
s->next = L->next;
L->next = s;
SaveData (L);
cout << "保存成功!" << endl;
break;
case '2':
break;
default:
break;
}
system ("pause");
}
//主界面
void Menu ()
{
while (1)
{
system ("cls");
cout << endl << endl << endl;
cout << "\t\t**********************************************" << endl;
cout << "\t\t** **" << endl;
cout << "\t\t** 欢迎使用班级管理系统 **" << endl;
cout << "\t\t** (1) 添加成员 **" << endl;
cout << "\t\t** (2) 查询成员 **" << endl;
cout << "\t\t** (0) 退出 **" << endl;
cout << "\t\t** **" << endl;
cout << "\t\t**********************************************" << endl;
switch (getch ())
{
case '1':
AddData ();
cout << "添加成功,已保存到文件!" << endl;
break;
case '2':
SearchByNum ();
break;
case '0':
cout << "\n\n\t\t\t\t谢谢使用!" << endl << endl;
exit (0);
default:
break;
}
}
}
int main ()
{
Menu ();
return 0;
}
上一个:怎么学习C和C++语言
下一个:c++ 重载<<>>运算符