// 简易学生管理系统
#include<bits/stdc++.h>
using namespace std;
class Student{
private:
int people; // 记录学生人数
string name[100];
int age[100];
int xuehao[100];
public:
Student(){
people = 0;
}
void Add(int n);
void Fix();
int Delete();
void Show();
};
void Menu(){
cout << "******************" << endl
<< " 学生管理系统" << endl
<< "******************" << endl
<< "请选择功能:" << endl
<< "1.添加学生信息" << endl
<< "2.修改学生信息" << endl
<< "3.删除学生信息" << endl
<< "4.显示学生信息" << endl
<< "5.退出" << endl;
}
void Student::Add(int n){
for(int i = people; i < people + n; i++){
cout << "请添加学生的姓名:";
cin >> name[i];
cout << "请添加学生的年龄:";
cin >> age[i];
cout <<"请添加学生的学号(8位):";
cin >> xuehao[i];
}
people = people + n;
}
void Student::Fix(){
string old_name;
int n_1 = 0;
cout << "请输入你要修改的学生的姓名:";
cin >> old_name;
for(int i = 0; i < people; i ++){
if(old_name == name[i]){
cout << "请输入修改后学生的姓名:";
cin >> name[i];
cout << "请输入修改后学生的年龄:";
cin >> age[i];
cout <<"请输入修改后学生的学号(8位):";
cin >> xuehao[i];
}
else{
n_1 ++;
}
}
if(n_1 >= people) cout << "需要修改的名字不存在." << endl;
}
int Student::Delete(){
string old_name;
int n_2; // 记录要删除的学生的位置
cin >> old_name;
for(int i = 0; i < people; i ++){
if(old_name == name[i]){
n_2 = i;
}
}
for(int i = n_2; i < people-1; i ++){
name[i] = name[i+1];
age[i] = age[i+1];
xuehao[i] = xuehao[i+1];
}
people --;
}
void Student::Show(){
for(int i = 0; i < people; i ++){
cout << "姓名:" << name[i] << endl
<< "年龄:" << age[i] << endl
<< "学号:" << xuehao[i] << endl;
}
}
int main(){
char num;
Student stu;
Menu();
while(cin>> num){
if(num == '1'){
stu.Add(1);
Menu();
}
else if (num == '2'){
stu.Fix();
Menu();
}
else if(num == '3'){
cout << "请输入你要删除的学生的姓名:";
stu.Delete();
Menu();
}
else if(num == '4'){
stu.Show();
Menu();
}
else if(num == '5'){
cout << "You have return.";
break;
}
else{
cout << "Change your number in 1~5.";
Menu();
}
}
}
每日一练——线性表的顺序表示(数据结构复习)
最新推荐文章于 2025-05-14 13:28:31 发布