提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
提示:这里可以添加本文要记录的大概内容:
例如:随着人工智能的不断发展,机器学习这门技术也越来越重要,很多人都开启了学习机器学习,本文就介绍了机器学习的基础内容。
提示:以下是本篇文章正文内容,下面案例可供参考
本文是通过结构体的定义完成的一个学生管理系统,在此我们定义了两个结构体,bj是类似于班级,里面包含了学生和班级容量的元素。而学生则包含了姓名、性别、年龄、电话、住址等元素。
#include
#include
#define MAX 1000
using namespace std;
struct Person{
string m_Name;
int m_Sex;
int m_Age;
string m_Num;
string m_Address;
};
struct bj{
struct Person personArray[MAX];
int m_Size;
};
主界面的展示:
void showMenu(){
cout<<"-----------------------------------"<<endl;
cout<<"-----------1.输入联系人------------"<<endl;
cout<<"-----------2.查找联系人------------"<<endl;
cout<<"-----------3.修改联系人------------"<<endl;
cout<<"-----------4.删除联系人------------"<<endl;
cout<<"-----------5.清空联系人------------"<<endl;
cout<<"-----------6.显示联系人------------"<<endl;
cout<<"-----------0.退出系统--------------"<<endl;
cout<<"-----------------------------------"<<endl;
}
添加学生信息:
首先我们先对班级进行一个测试,如果班级已满,就不进行操作,
反之,进行信息的添加。
void addPerson(bj *abs){
if(abs->m_Size==MAX){ //判断通讯塔是否已满
cout<<"班级已满,不能再添加任何信息!"<<endl;
return; }
else{
string name;
cout<<"请输入姓名:"<<endl;
cin>>name;
abs->personArray[abs->m_Size].m_Name=name;
//利用m_Size作为当前下标将输入的姓名赋值给当前下标的人
cout<<"请输入性别:"<<endl;
cout<<"1---男"<<endl;
cout<<"2---女"<<endl;
int sex=0;
while(true){
cin>>sex; //如果是1或2,可以退出循环
if(sex==1||sex==2){
abs->personArray[abs->m_Size].m_Sex=sex;
break;}
cout<<"输入有误,请重新输入"<<endl; }
cout<<"请输入年龄:"<<endl;
int age=0;
cin>>age;
while(age>120){
cout<<"请重新输入年龄:"<<endl;
cin>>age;
}
abs->personArray[abs->m_Size].m_Age=age;
cout<<"请输入联系电话:"<<endl;
string num;
cin>>num;
abs->personArray[abs->m_Size].m_Num=num;
cout<<"请输入家庭地址:"<<endl;
string addr;
cin>>addr;
abs->personArray[abs->m_Size].m_Address=addr;
}
abs->m_Size++;
cout<<"信息更新成功"<<endl;
system("pause");
system("cls"); //清屏操作
}
信息全展示代码:
void showPerson( const bj* abs){ //const说明显示功能只能显示,不能进行修改
if(abs->m_Size==0){ //先判断班级是否为空
cout<<"通讯录为空,无法显示!"<<endl;
}
else{
for(int i=0;i<abs->m_Size;i++){
cout<<"姓名:"<<abs->personArray[i].m_Name<<'\t';
cout<<"性别:"<<(abs->personArray[i].m_Sex==1?"男":"女")<<'\t';
cout<<"年龄:"<<abs->personArray[i].m_Age<<'\t';
cout<<"联系电话:"<<abs->personArray[i].m_Num<<'\t';
cout<<"家庭住址:"<<abs->personArray[i].m_Address<<endl;
}
}
system("pause");
system("cls"); //清屏操作
}
对姓名查找功能的判断:返回值传递给需要的功能
int isExist(bj *abs,string name){
for(int i=0;i<abs->m_Size;i++){
if(abs->personArray[i].m_Name==name){
return i;
}
}
return -1;
}
删除功能:
void deletePerson(bj* abs){
cout<<"请输入您要删除人的姓名:"<<endl;
string name;
cin>>name;
int ret=isExist(abs,name);
if(ret!=-1){
for(int i=ret;i<abs->m_Size;i++)
{
abs->personArray[i]=abs->personArray[i+1];
}
abs->m_Size--;//更新班级人数
cout<<"删除成功!"<<endl;
}
else{
cout<<"查无此人!"<<endl;
}
system("pause");
system("cls");
}
查找功能:
void findPerson(bj* abs){
cout<<"请输入您要查找的学生名:"<<endl;
string name;
cin>>name;
int ret=isExist(abs,name);
if(ret!=-1){ //ret记录的是查询的下标
cout<<"姓名:"<<abs->personArray[ret].m_Name<<'\t';
cout<<"性别:"<<abs->personArray[ret].m_Sex<<'\t';
cout<<"年龄:"<<abs->personArray[ret].m_Age<<'\t';
cout<<"联系电话:"<<abs->personArray[ret].m_Num<<'\t';
cout<<"家庭住址:"<<abs->personArray[ret].m_Address<<'\t'; }
else{
cout<<"查无此人!"<<endl;
}
system("pause");
system("cls");
}
修改功能:
void changePerson(bj* abs){
cout<<"请输入您要修改的联系人:"<<endl;
string name;
cin>>name;
int ret=isExist(abs,name);
if(ret!=-1){
cout<<"请输入姓名:"<<endl;
string name;
cin>>name;
abs->personArray[ret].m_Name=name;
cout<<"请输入性别:"<<endl;
int sex=0;
while(true){
cin>>sex;
if(sex==1||sex==2){
abs->personArray[ret].m_Sex=sex;break;
}
cout<<"输入错误,请重新输入!"<<endl;
}
cout<<"请输入年龄:"<<endl;
int age=0;
cin>>age;
while(age>120){
cout<<"请输入正确数字:"<<endl;
cin>>age;
}
abs->personArray[ret].m_Age=age;
cout<<"请输入联系电话:"<<endl;
string num;
cin>>num;
abs->personArray[ret].m_Num=num;
cout<<"请输入家庭住址:"<<endl;
string addr;
cin>>addr;
abs->personArray[ret].m_Address=addr;
}
else{
cout<<"查无此人!"<<endl;
}
system("pause");
system("cls");
}
清空功能:
void cleanPerson(bj* abs){
cout<<"是否真到要清空,是(1)/否(2)"<<endl;
int a;
cin>>a;
if(a==1) {
abs->m_Size=0;
cout<<"班级已清空"<<endl;
}
if(a==2);
system("pause");
system("cls");
}
主界面:
int main(){
bj abs;
abs.m_Size=0;
int a=0;
while(true){
showMenu();
cout<<"请输入你想进行的操作:"<<endl;
cin>>a;
switch(a){
case 1: addPerson(&abs); //1、添加信息 利用地址传递修饰实参
break;
case 2: findPerson(&abs); //2、 查询学生信息
break;
case 3: changePerson(&abs); //3、修改学生信息
break;
case 4: deletePerson(&abs); //4、删除学生信息
break;
case 5: cleanPerson(&abs); //5、清空学生信息
break;
case 6: showPerson(&abs); //6、展示学生信息
break;
case 0: cout<<"欢迎下次使用"<<endl;
return 0;
break;
}
}
return 0;
}
#include
#include
#define MAX 1000
using namespace std;
struct Person{
string m_Name;
int m_Sex;
int m_Age;
string m_Num;
string m_Address;
};
struct bj{
struct Person personArray[MAX];//通讯录里的人的数组
int m_Size; //通讯录的最大容量
};
void showMenu(){
cout<<"-----------------------------------"<<endl;
cout<<"-----------1.输入联系人------------"<<endl;
cout<<"-----------2.查找联系人------------"<<endl;
cout<<"-----------3.修改联系人------------"<<endl;
cout<<"-----------4.删除联系人------------"<<endl;
cout<<"-----------5.清空联系人------------"<<endl;
cout<<"-----------6.显示联系人------------"<<endl;
cout<<"-----------0.退出系统--------------"<<endl;
cout<<"-----------------------------------"<<endl;
}
void addPerson(bj *abs){
if(abs->m_Size==MAX){ //判断通讯塔是否已满
cout<<"通讯录已满,不能再添加任何信息!"<<endl;
return; }
else{
string name;
cout<<"请输入姓名:"<<endl;
cin>>name;
abs->personArray[abs->m_Size].m_Name=name;
//利用m_Size作为当前下标将输入的姓名赋值给当前下标的人
cout<<"请输入性别:"<<endl;
cout<<"1---男"<<endl;
cout<<"2---女"<<endl;
int sex=0;
while(true){
cin>>sex; //如果是1或2,可以退出循环
if(sex==1||sex==2){
abs->personArray[abs->m_Size].m_Sex=sex;
break;}
cout<<"输入有误,请重新输入"<<endl; }
cout<<"请输入年龄:"<<endl;
int age=0;
cin>>age;
while(age>120){
cout<<"请重新输入年龄:"<<endl;
cin>>age;
}
abs->personArray[abs->m_Size].m_Age=age;
cout<<"请输入联系电话:"<<endl;
string num;
cin>>num;
abs->personArray[abs->m_Size].m_Num=num;
cout<<"请输入家庭地址:"<<endl;
string addr;
cin>>addr;
abs->personArray[abs->m_Size].m_Address=addr;
}
abs->m_Size++;
cout<<"信息更新成功"<<endl;
system("pause");
system("cls"); //清屏操作
}
void showPerson( const bj* abs){ //const说明显示功能只能显示,不能进行修改
if(abs->m_Size==0){ //先判断通讯录是否为空
cout<<"通讯录为空,无法显示!"<<endl;
}
else{
for(int i=0;i<abs->m_Size;i++){
cout<<"姓名:"<<abs->personArray[i].m_Name<<'\t';
cout<<"性别:"<<(abs->personArray[i].m_Sex==1?"男":"女")<<'\t';
cout<<"年龄:"<<abs->personArray[i].m_Age<<'\t';
cout<<"联系电话:"<<abs->personArray[i].m_Num<<'\t';
cout<<"家庭住址:"<<abs->personArray[i].m_Address<<endl;
}
}
system("pause");
system("cls");
}
int isExist(bj *abs,string name){
for(int i=0;i<abs->m_Size;i++){
if(abs->personArray[i].m_Name==name){
return i;
}
}
return -1;
}
void deletePerson(bj* abs){
cout<<"请输入您要删除人的姓名:"<<endl;
string name;
cin>>name;
int ret=isExist(abs,name);
if(ret!=-1){
for(int i=ret;i<abs->m_Size;i++)
{
abs->personArray[i]=abs->personArray[i+1];
}
abs->m_Size--;//更新通讯录人数
cout<<"删除成功!"<<endl;
}
else{
cout<<"查无此人!"<<endl;
}
system("pause");
system("cls");
}
void findPerson(bj* abs){
cout<<"请输入您要查找的联系人:"<<endl;
string name;
cin>>name;
int ret=isExist(abs,name);
if(ret!=-1){ //ret记录的是查询的下标
cout<<"姓名:"<<abs->personArray[ret].m_Name<<'\t';
cout<<"性别:"<<abs->personArray[ret].m_Sex<<'\t';
cout<<"年龄:"<<abs->personArray[ret].m_Age<<'\t';
cout<<"联系电话:"<<abs->personArray[ret].m_Num<<'\t';
cout<<"家庭住址:"<<abs->personArray[ret].m_Address<<'\t'; }
else{
cout<<"查无此人!"<<endl;
}
system("pause");
system("cls");
}
void changePerson(bj* abs){
cout<<"请输入您要修改的联系人:"<<endl;
string name;
cin>>name;
int ret=isExist(abs,name);
if(ret!=-1){
cout<<"请输入姓名:"<<endl;
string name;
cin>>name;
abs->personArray[ret].m_Name=name;
cout<<"请输入性别:"<<endl;
int sex=0;
while(true){
cin>>sex;
if(sex==1||sex==2){
abs->personArray[ret].m_Sex=sex;break;
}
cout<<"输入错误,请重新输入!"<<endl;
}
cout<<"请输入年龄:"<<endl;
int age=0;
cin>>age;
while(age>120){
cout<<"请输入正确数字:"<<endl;
cin>>age;
}
abs->personArray[ret].m_Age=age;
cout<<"请输入联系电话:"<<endl;
string num;
cin>>num;
abs->personArray[ret].m_Num=num;
cout<<"请输入家庭住址:"<<endl;
string addr;
cin>>addr;
abs->personArray[ret].m_Address=addr;
}
else{
cout<<"查无此人!"<<endl;
}
system("pause");
system("cls");
}
void cleanPerson(bj* abs){
cout<<"是否真到要清空,是(1)/否(2)"<<endl;
int a;
cin>>a;
if(a==1) {
abs->m_Size=0;
cout<<"通讯录已清空"<<endl;
}
if(a==2);
system("pause");
system("cls");
}
int main(){
bj abs;
abs.m_Size=0;
int a=0;
while(true){
showMenu();
cout<<"请输入你想进行的操作:"<<endl;
cin>>a;
switch(a){
case 1: addPerson(&abs); //1、添加联系人 利用地址传递修饰实参
break;
case 2: findPerson(&abs);
break;
case 3: changePerson(&abs);
break;
case 4: deletePerson(&abs);
break;
case 5: cleanPerson(&abs);
break;
case 6: showPerson(&abs);
break;
case 0: cout<<"欢迎下次使用"<<endl;
return 0;
break;
}
}
return 0;
}