简单的学生管理系统
#include<iostream>
using namespace std;
#include<string>
struct Person{
string name;
int sex;
int age;
int phone;
string address;
};
#define MAX 100
struct Addressbooks{
Person personArray[MAX];
int 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(Addressbooks * abs){
if (abs->size==MAX){
cout<<"通讯录已满,无法添加"<<endl;
return ;
}
else{
string name;
cout<<"请输入姓名: "<<endl;
cin>>name;
abs->personArray[abs->size].name=name;
int sex;
cout<<"请输入性别,输入为1是男生,输入为2是女生"<<endl;
while(true){
cin>>sex;
if(sex==1||sex==2){
abs->personArray[abs->size].sex=sex;
break;
}
cout<<"输入有误,请重新输入"<<endl;
}
cout<<"请输入年龄"<<endl;
int age;
cin>>age;
abs->personArray[abs->size].age=age;
cout<<"请输入电话"<<endl;
int tele;
cin>>tele;
abs->personArray[abs->size].phone=tele;
cout<<"请输入住址"<<endl;
string address;
cin>>address;
abs->personArray[abs->size].address=address;
abs->size++;
cout<<"添加成功"<<endl;
system("pause");
system("cls");
}
}
void showPerson(Addressbooks * abs){
if (abs->size==0){
cout<<"当前的记录为空"<<endl;
}else{
for(int i=0;i<abs->size;i++){
cout<<"name= "<<abs->personArray[i].name<<"\t";
cout<<"sex= "<<abs->personArray[i].sex<<"\t";
cout<<"age= "<<abs->personArray[i].age<<"\t";
cout<<"phone= "<<abs->personArray[i].phone<<"\t";
cout<<"address= "<<abs->personArray[i].address<<endl;
}
}
system("pause");
system("cls");
}
int isExist(Addressbooks *abs,string name){
for(int i=0;i<abs->size;i++){
if(abs->personArray[i].name==name){
return i;
}
}
return -1;
}
void deletPerson(Addressbooks * abs){
cout<<"请输入你要删除的联系人的姓名"<<endl;
string name;
cin>>name;
int ret=isExist(abs,name);
if (ret==-1)
{
cout<<"联系人并不存在"<<endl;
}else{
for(int i=ret;i<abs->size;i++){
abs->personArray[i]=abs->personArray[i+1];
}
abs->size--;
cout<<"删除成功"<<endl;
}
if (abs->size==0){
cout<<"并没有存储着任何联系人"<<endl;
}
system("pasue");
system("cls");
}
void findPerson(Addressbooks * abs){
string name;
cout<<"请输入你要查找的联系人"<<endl;
cin>>name;
int ret=isExist(abs,name);
if(ret!=-1){
cout<<"name= "<<abs->personArray[ret].name<<"\t";
cout<<"sex= "<<abs->personArray[ret].sex<<"\t";
cout<<"age= "<<abs->personArray[ret].age<<"\t";
cout<<"phone= "<<abs->personArray[ret].phone<<"\t";
cout<<"address= "<<abs->personArray[ret].address<<endl;
}
else{
cout<<"未找到联系人"<<endl;
}
system("pasue");
system("cls");
}
void modfiyPerson(Addressbooks * abs){
cout<<"请输入你要修改的联系人"<<endl;
string name;
cin>>name;
int ret=isExist(abs,name);
if (ret!=-1){
cout<<"请输入你要修改的值"<<endl;
cout<<"name= "<<endl;
cin>>name;
abs->personArray[ret].name=name;
cout<<"sex= "<<endl;
int sex;
while(true){
cin>>sex;
if(sex==1||sex==2){
abs->personArray[abs->size].sex=sex;
break;
}
cout<<"输入有误,请重新输入"<<endl;
}
cout<<"age= "<<endl;
int age; cin>>age;
abs->personArray[ret].age=age;
cout<<"phone= "<<endl;
int phone; cin>>phone;
abs->personArray[ret].phone=phone;
cout<<"address= "<<endl;
int address; cin>>address;
abs->personArray[ret].address=address;
}
else{
cout<<"未找到联系人"<<endl;
}
system("pasue");
system("cls");
}
void cleanPerson(Addressbooks *abs){
abs->size=0;
cout<<"通讯录已经清空"<<endl;
system("pause");
system("cls");
}
int main(){
int select=0;
Addressbooks abs;
abs.size=0;
while(true){
showMenu();
cin>>select;
switch (select)
{
case 1:
addperson(&abs);
break;
case 2:
showPerson(&abs);
break;
case 3:
deletPerson(&abs);
break;
case 4:
findPerson(&abs);
break;
case 5:
modfiyPerson(&abs);
break;
case 6:
cleanPerson(&abs);
break;
case 0:
cout<<"欢迎下次使用"<<endl;
system("pause");
return 0;
break;
default:
break;
}
}
}