C++学习笔记(七)——学生信息管理系统
前言
大致功能概况
具体代码实现
登录学生系统模块
创建学生信息模块
显示学生信息模块
保存学生信息模块
读取学生信息模块
增添学生信息模块
删除学生信息模块
查找学生信息模块
修改学生信息模块
插入学生信息模块
学生成绩排序模块
前言
前面已经学了C++的类和对象,对C++已经有了初步了解,为了加深对其应用,下面和大家一起学习编写学生信息管理系统。
大致功能概况
学生信息管理系统功能函数:
- void menu();//主菜单
- void CreatStuInfo();//创建学生信息
- void GetStuFile();//将学生信息写入文件
- void ShowStu();//显示全部学生信息
- void PutStuFile();//将学生信息显示在终端上
- void AddStu();//增加学生信息
- void DeleteStu();//删除学生信息
- void FindStu();//查找学生信息
- void ModifyStu();//修改学生信息
- void InsertStu();//插入学生信息
- //void CountStu();//统计学生信息
- void SortStu();//按照学生成绩排序

单链表结点定义
struct Student_Node {int num; //学号int age; //年龄char name[20];//姓名char sex[10]; //性别char major[10];//专业char born[10]; //出生日期char address[20];//家庭住址int Math; //数学成绩struct Student_Node* next;//指针域
};
学生类
class Student
{
public:Student(){head = new Student_Node;head->next = NULL;}void menu();//主菜单void CreatStuInfo();//创建学生信息void GetStuFile();//将学生信息写入文件void ShowStu();//显示全部学生信息void PutStuFile();//将学生信息显示在终端上void AddStu();//增加学生信息void DeleteStu();//删除学生信息void FindStu();//查找学生信息void ModifyStu();//修改学生信息void InsertStu();//插入学生信息//void CountStu();//统计学生信息void SortStu();//按照学生成绩排序
private:struct Student_Node* head;
};
具体代码实现
登录学生系统模块
首先看运行结果:这里登录系统做的有点草率,大家可以限制登录次数或者已经有的账号,不允许再次注册等约束条件。

代码示例:
//首页
void start_page()
{cout << "\t\t\t\t|------------------------------------|" << endl;cout << "\t\t\t\t|------------------------------------|" << endl;cout << "\t\t\t\t|------------------------------------|" << endl;cout << "\t\t\t\t|------------------------------------|" << endl;cout << "\t\t\t\t|------欢迎使用学生信息管理系统--------|" << endl;cout << "\t\t\t\t|------------------------------------|" << endl;cout << "\t\t\t\t|------------------------------------|" << endl;cout << "\t\t\t\t|------------------------------------|" << endl;cout << "\t\t\t\t|------------------------------------|" << endl;cout << "\t\t\t\t请按Enter进行下一步!!!" << endl << "\t\t\t\t";getchar();//停顿便于观察system("cls");//刷新界面
}//登录注册界面
void login()
{char username[20], password[10];cout << "\t\t\t\t欢迎您进入学生基本信息管理软件的注册界面!" << endl << endl;cout << "\t\t\t\t【0】我已经注册过了!" << endl << endl;cout << "\t\t\t\t【1】未注册,现在注册!" << endl << endl;cout << "\t\t\t\t请按Entet键继续......";getchar();int i;cout << "\t\t\t\t" << "请输入......";cin >> i;if (i == 1){cout << "\t\t\t\t" << "欢迎新用户注册!" << endl << endl;cout << "\t\t\t\t" << "请输入您的用户名:";cin >> username;cout << "\t\t\t\t" << "请输入您的密码:";cin >> password;system("pause");system("cls");ofstream regist(".login.txt");regist << username;regist << password;regist.close();cout << "注册成功!" << endl;getchar();system("cls");}cout << "\t\t\t\t" << "请输入您的用户名:";cin >> username;cout << "\t\t\t\t" << "请输入您的密码:";cin >> password;char str[20];FILE* fp = fopen(".login.txt", "r");if (!fp){cout << "打开失败" << endl;return;}while (fgets(str, 20, fp)){}fclose(fp);if (strcmp(strcat(username, password), str) == 0){cout << "登录成功";//登录成功后,在这里添加主菜单//main_function();}else {cout << "\t\t\t\t账号或密码错误,请输入正确的账号和密码!!!" << endl;}getchar();//system("cls");
}
创建学生信息模块
运行结果示例:很明显发现年龄与出生日期不符合等与实际不符合问题,大家可以根据需求去添加一些必要的约束条件。

代码如下:
//创建学生信息
void Student::CreatStuInfo()
{cout << "\t\t\t\t请输入您要创建的学生的数量:" << endl << "\t\t\t\t";int i;cin >> i;struct Student_Node* p1 = head;struct Student_Node* p2;for (int j = 1; j < i + 1; j++){p2 = new Student_Node;cout << "\t\t\t\t请输入第" << j << "个学生的学号:" << endl << "\t\t\t\t";cin >> p2->num;cout << "\t\t\t\t请输入第" << j << "个学生的年龄:" << endl << "\t\t\t\t";cin >> p2->age;cout << "\t\t\t\t请输入第" << j << "个学生的姓名:" << endl << "\t\t\t\t";cin >> p2->name;cout << "\t\t\t\t请输入第" << j << "个学生的性别:" << endl << "\t\t\t\t";cin >> p2->sex;cout << "\t\t\t\t请输入第" << j << "个学生的专业:" << endl << "\t\t\t\t";cin >> p2->major;cout << "\t\t\t\t请输入第" << j << "个学生的出生日期:" << endl << "\t\t\t\t";cin >> p2->born;cout << "\t\t\t\t请输入第" << j << "个学生的家庭地址:" << endl << "\t\t\t\t";cin >> p2->address;cout << "\t\t\t\t请输入第" << j << "个学生的数学成绩:" << endl << "\t\t\t\t";cin >> p2->Math;p2->next = NULL;p1->next = p2;p1 = p2;}getchar();
}//主菜单
void Student::menu()
{cout << "\t\t\t\t------------------------------------" << endl;cout << "\t\t\t\t| 主菜单 |" << endl;cout << "\t\t\t\t| 【1】创建学生的信息 |" << endl;cout << "\t\t\t\t| 【2】显示学生的信息 |" << endl;cout << "\t\t\t\t| 【3】将录入的学生信息进行保存 |" << endl;cout << "\t\t\t\t| 【4】将所保存的学生的信息进行读取 |" << endl;cout << "\t\t\t\t| 【5】增添学生的信息 |" << endl;cout << "\t\t\t\t| 【6】删除学生的信息 |" << endl;cout << "\t\t\t\t| 【7】查找学生的信息 |" << endl;cout << "\t\t\t\t| 【8】修改学生的信息 |" << endl;cout << "\t\t\t\t| 【9】插入学生的信息 |" << endl;cout << "\t\t\t\t| 【10】统计学生的信息 |" << endl;cout << "\t\t\t\t| 【11】退出学生基本信息管理软件 |" << endl;cout << "\t\t\t\t| 【12】 按照学生的数学成绩进行排序 |" << endl;cout << "\t\t\t\t------------------------------------|" << endl;cout << "\t\t\t\t请输入您的选择:" << endl << "\t\t\t\t";
}
显示学生信息模块
运行结果示例

代码如下:
//显示全部学生成绩
void Student::ShowStu()
{struct Student_Node* p1 = head->next;int i = 1;if (p1 == nullptr){i = 0;}if (i == 0){cout << "\t\t\t\t没有找到学生的信息!!!" << endl;}if (i == 1){while (p1){cout << endl;cout << "学生的学号:\t\t\t" << p1->num << endl;cout << "学生的年龄:\t\t\t" << p1->age << endl;cout << "学生的姓名:\t\t\t" << p1->name << endl;cout << "学生的性别:\t\t\t" << p1->sex << endl;cout << "学生的专业:\t\t\t" << p1->major << endl;cout << "学生的出生日期:\t\t\t" << p1->born << endl;cout << "学生的家庭住址:\t\t\t" << p1->address << endl;cout << "学生的英语成绩:\t\t\t" << p1->Math;p1 = p1->next;}}getchar();
}
保存学生信息模块
运行结果示例:

在对应的路径下,可以找到创建的记事本

代码如下:
//将学生信息写入文件中
void Student::GetStuFile()
{ofstream outfile(".studentlist.txt");if (!outfile){cout << "打开文件失败!\n" << endl;}struct Student_Node* p1 = head->next;while (p1){outfile << "学号:" << p1->num << endl;outfile << "年龄:" << p1->age << endl;outfile << "姓名:" << p1->name << endl;outfile << "性别:" << p1->sex << endl;outfile << "专业:" << p1->major << endl;outfile << "出生日期:" << p1->born << endl;outfile << "家庭地址:" << p1->address << endl;outfile << "英语成绩:" << p1->Math << endl;p1 = p1->next;}outfile.close();cout << "保存成功!" << endl;getchar();
}
读取学生信息模块
运行结果示例:可以直接从文件中读取学生信息

代码如下:
//将学生信息写到终端
void Student::PutStuFile()
{int count = 0;char str[1024];FILE* fp = fopen(".studentlist.txt", "r");if (!fp){return;}while (fgets(str, 1024, fp)){if (count % 8 == 0){cout << endl;}cout << str;count++;}fclose(fp);system("pause");
}
增添学生信息模块
//增加学生信息
void Student::AddStu()
{struct Student_Node* p1 = head->next;struct Student_Node* p2;int a = 1, b = 1;int c; //学生的学号 while (p1->next != NULL){p1 = p1->next;a++;}cout << "\t\t\t\t请输入学生的学号:" << endl << "\t\t\t\t";cin >> c;p1 = head->next;for (int i = 1; i <= a; i++)//判断学生的学号是否重复 {if (p1->num == c){cout << "\t\t\t\t出错:学号重复!!!" << endl;b = 0;break;}p1 = p1->next;}if (b != 0)//判断 {p1 = head;while (p1->next != NULL){p1 = p1->next;}p2 = new Student_Node;p2->num = c;cout << "\t\t\t\t请输入学生的年龄:" << endl << "\t\t\t\t"; cin >> p2->age;cout << "\t\t\t\t请输入学生的姓名:" << endl << "\t\t\t\t"; cin >> p2->name;cout << "\t\t\t\t请输入学生的性别:" << endl << "\t\t\t\t"; cin >> p2->sex;cout << "\t\t\t\t请输入学生的专业:" << endl << "\t\t\t\t"; cin >> p2->major;cout << "\t\t\t\t请输入学生的出生日期:" << endl << "\t\t\t\t"; cin >> p2->born;cout << "\t\t\t\t请输入学生的家庭住址:" << endl << "\t\t\t\t"; cin >> p2->address;cout << "\t\t\t\t请输入学生的数学成绩:" << endl << "\t\t\t\t"; cin >> p2->Math;p2->next = NULL;p1->next = p2;p1 = p2;}getchar();
}
删除学生信息模块
void Student::DeleteStu()
{struct Student_Node* p1;p1 = head;struct Student_Node* p2;int i = 1;if (p1->next == NULL){cout << "\t\t\t\t删除失败!!!" << endl;cout << "\t\t\t\t无可用的数据!!!" << endl;i = 0;}if (i == 1){int j;cout << "\t\t\t\t请输入您想要删除的学生的学号:" << endl << "\t\t\t\t";cin >> j;while (p1->next != NULL){if (p1->next->num == j){cout << "\t\t\t\t******删除成功********" << endl;getchar();p2 = p1->next;p1->next = p2->next;delete p2;return;}p1 = p1->next;}cout << "\t\t\t\t删除错误!!!无法找到此学生的信息!!!" << endl;}getchar();
}
查找学生信息模块
void Student::FindStu()
{int i = 0;struct Student_Node* p1 = head->next;int j;cout << "\t\t\t\t|--------------------------|" << endl;cout << "\t\t\t\t| |" << endl;cout << "\t\t\t\t|【1】请输入学生的学号查询 |" << endl;cout << "\t\t\t\t|【2】请输入学生的姓名查询 |" << endl;cout << "\t\t\t\t| |" << endl;cout << "\t\t\t\t|--------------------------|" << endl;cout << "\t\t\t\t";cin >> j;switch (j){case 1:int a;cout << "\t\t\t\t请输入学生的学号:" << endl << "\t\t\t\t";cin >> a;while (p1 != NULL){if (p1->num == a){cout << "\t\t\t\t该学生的学号:\t\t\t\t";cout << p1->num << endl;cout << "\t\t\t\t该学生的年龄:\t\t\t\t";cout << p1->age << endl;cout << "\t\t\t\t该学生的姓名:\t\t\t\t";cout << p1->name << endl;cout << "\t\t\t\t该学生的性别:\t\t\t\t";cout << p1->sex << endl;cout << "\t\t\t\t该学生的专业:\t\t\t\t";cout << p1->major << endl;cout << "\t\t\t\t该学生的出生日期:\t\t\t";cout << p1->born << endl;cout << "\t\t\t\t该学生的家庭住址:\t\t\t";cout << p1->address << endl;cout << "\t\t\t\t该学生的数学成绩:\t\t\t";cout << p1->Math << endl;i++;}p1 = p1->next;}if (i == 0){cout << "\t\t\t\t查询错误!!!" << endl;cout << "\t\t\t\t没有此学生的数据!!!" << endl;}break;case 2:char xingming[20];cout << "\t\t\t\t请输入学生的姓名:" << endl;cout << "\t\t\t\t";cin >> xingming;while (p1 != NULL){if (strcmp(p1->name, xingming) == 0){cout << "\t\t\t\t该学生的学号:\t\t\t\t";cout << p1->num << endl;cout << "\t\t\t\t该学生的年龄:\t\t\t\t";cout << p1->age << endl;cout << "\t\t\t\t该学生的姓名:\t\t\t\t";cout << p1->name << endl;cout << "\t\t\t\t该学生的性别:\t\t\t\t";cout << p1->sex << endl;cout << "\t\t\t\t该学生的专业:\t\t\t\t";cout << p1->major << endl;cout << "\t\t\t\t该学生的出生日期:\t\t\t";cout << p1->born << endl;cout << "\t\t\t\t该学生的家庭住址:\t\t\t";cout << p1->address << endl;cout << "\t\t\t\t该学生的数学成绩:\t\t\t";cout << p1->Math;i++;}p1 = p1->next;}if (i == 0){cout << "\t\t\t\t查询错误!!!" << endl;cout << "\t\t\t\t没有此学生的数据!!!" << endl;}break;default:cout << "\t\t\t\t信息错误!!!" << endl;}getchar();}
修改学生信息模块
void Student::ModifyStu()
{struct Student_Node* p1 = head->next;struct Student_Node* p2 = head->next;cout << "\t\t\t\t请输入需要修改的学生的学号:" << endl;int i;int j = 0;int k = 1;cout << "\t\t\t\t";cin >> i;while (p1){if (p1->num == i)//对是否有该生的信息进行判断 {j = 1;}p1 = p1->next;}if (j == 0){cout << "\t\t\t\t错误!!!" << endl;cout << "\t\t\t\t没有该生的信息!!!" << endl;}if (j == 1){p1 = head->next;while (p1){if (p1->num == i){cout << "\t\t\t\t-----------------------------" << endl;cout << "\t\t\t\t|---【1】修改学生的学号------" << endl;cout << "\t\t\t\t|---【2】修改学生的姓名------" << endl;cout << "\t\t\t\t|---【3】修改学生的性别------" << endl;cout << "\t\t\t\t|---【4】修改学生的专业------" << endl;cout << "\t\t\t\t|---【5】修改学生的出生日期--" << endl;cout << "\t\t\t\t|---【6】修改学生的家庭住址--" << endl;cout << "\t\t\t\t|---【7】修改学生的数学成绩--" << endl;cout << "\t\t\t\t|---【8】修改学生的年龄------" << endl;cout << "\t\t\t\t|----------------------------" << endl;int x;cout << "\t\t\t\t";cin >> x;switch (x){case 1:cout << "\t\t\t\t请输入修改后的学号:" << endl;int y;cout << "\t\t\t\t";cin >> y;while (p2){if (p2->num == y){cout << "\t\t\t\t出错!!!" << endl;cout << "\t\t\t\t学号重复!!!" << endl;break;k = 0;}p2 = p2->next;}if (k != 0){p1->num = y;}break;case 2:cout << "\t\t\t\t请输入姓名:" << endl << "\t\t\t\t";cin >> p1->name;break;case 3:cout << "\t\t\t\t请输入性别:" << endl << "\t\t\t\t";cin >> p1->sex;break;case 4:cout << "\t\t\t\t请输入专业:" << endl << "\t\t\t\t";cin >> p1->major;break;case 5:cout << "\t\t\t\t请输入出生日期:" << endl << "\t\t\t\t";cin >> p1->born;break;case 6:cout << "\t\t\t\t请输入家庭住址:" << endl << "\t\t\t\t";cin >> p1->address;break;case 7:cout << "\t\t\t\t请输入数学成绩:" << endl << "\t\t\t\t";cin >> p1->Math;break;case 8:cout << "\t\t\t\t请输入年龄:" << endl << "\t\t\t\t";cin >> p1->age;break;}break;}}}getchar();}
插入学生信息模块
void Student::InsertStu()
{struct Student_Node* p1, * p2;struct Student_Node* p3;int i;cout << "\t\t\t\t请输入要在哪个学号之后插入学生信息:" << endl << "\t\t\t\t";cin >> i;p1 = new Student_Node;cout << "\t\t\t\t请输入待插入学生的学号:" << endl << "\t\t\t\t";cin >> p1->num;cout << "\t\t\t\t请输入待插入学生的年龄:" << endl << "\t\t\t\t";cin >> p1->age;cout << "\t\t\t\t请输入待插入学生的姓名:" << endl << "\t\t\t\t";cin >> p1->name;cout << "\t\t\t\t请输入待插入学生的性别:" << endl << "\t\t\t\t";cin >> p1->sex;cout << "\t\t\t\t请输入待插入学生的专业:" << endl << "\t\t\t\t";cin >> p1->major;cout << "\t\t\t\t请输入待插入学生的出生日期:" << endl << "\t\t\t\t";cin >> p1->born;cout << "\t\t\t\t请输入待插入学生的数学成绩:" << endl << "\t\t\t\t";cin >> p1->Math;cout << "\t\t\t\t请输入待插入学生的家庭住址:" << endl << "\t\t\t\t";cin >> p1->address;p2 = head->next;p3 = head;while (p2){p3 = p2;if (p3->num == i){break;}p2 = p2->next;}p1->next = p3->next;p3->next = p1;getchar();
}
学生成绩排序模块
void Student::SortStu()
{//由高分到低分排列 {int i = 1;if (head->next == NULL){i = 0;}if (i == 0){cout << "\t\t\t\t出错!!!" << endl;cout << "\t\t\t\t没有找到学生的数据!!!" << endl;}if (i == 1){struct Student_Node* p1, * p2;int num_1;int age_1;char name_1[20];char sex_1[10];char major_1[10];char born_1[10];char address_1[10];int Math_1;int num_2;int age_2;char name_2[20];char sex_2[10];char major_2[10];char born_2[10];char address_2[10];int Math_2;for (p1 = head->next; p1 != NULL; p1 = p1->next){for (p2 = p1->next; p2 != NULL; p2 = p2->next){if (p1->Math < p2->Math){num_1 = p1->num;age_1 = p1->age;strcpy(name_1, p1->name);strcpy(sex_1, p1->sex);strcpy(major_1, p1->major);strcpy(born_1, p1->born);strcpy(address_1, p1->address);Math_1 = p1->Math;num_2 = p2->num;age_2 = p2->age;strcpy(name_2, p2->name);strcpy(sex_2, p2->sex);strcpy(major_2, p2->major);strcpy(born_2, p2->born);strcpy(address_2, p2->address);Math_2 = p2->Math;p1->num = num_2;p1->age = age_2;strcpy(p1->name, name_2);strcpy(p1->sex, sex_2);strcpy(p1->major, major_2);strcpy(p1->born, born_2);strcpy(p1->address, address_2);p1->Math = Math_2;p2->num = num_1;p2->age = age_1;strcpy(p2->name, name_1);strcpy(p2->sex, sex_1);strcpy(p2->major, major_1);strcpy(p2->born, born_1);strcpy(p2->address, address_1);p2->Math = Math_1;}}}cout << "\t\t\t\tWonderful*****排序成功*****" << endl;}getchar();}
}
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!
