1 Star 0 Fork 0

左一/spl

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
main.cpp 6.35 KB
一键复制 编辑 原始数据 按行查看 历史
p_ypsan 提交于 2021-07-22 14:14 . up
#include <iostream>
#include <string>
using namespace std;
// modidy 20210722
//define the const top of contact
#define MAX 1000
// contact struct
struct Person {
string m_fullName;
int m_Sex;
int m_Age;
string m_Phone;
string m_Addr;
};
//UIMS struct
struct Addressbooks {
struct Person personArray[MAX];
int m_Size;
};
// menu list
void showMenu() {
cout << "***********************************" <<endl;
cout << "******** 1. Add a Contact *******" <<endl;
cout << "******** 2. Show Contacts *******" <<endl;
cout << "******** 3. Delete a Contact ****" <<endl;
cout << "******** 4. Search a Contact ****" <<endl;
cout << "******** 5. Edit a Contact ******" <<endl;
cout << "******** 6. Clear Contacts ******" <<endl;
cout << "******** 0. Exit UIMS *******" <<endl;
cout << "***********************************" <<endl;
}
void addPerson (Addressbooks * abc) {
//Determine if the address book is full
if (abc->m_Size == MAX) {
cout << "Addressbook is full" << endl;
return;
} else {
string fullname;
cout << "Plese type your fullname:" << endl;
cin >> fullname;
abc->personArray[abc->m_Size].m_fullName = fullname;
cout << "Please type your gender:" << endl;
cout << "1 -- Male" << endl;
cout << "2 -- Female" << endl;
int sex = 0;
while (true) {
cin >> sex;
if (sex == 1 || sex == 2) {
abc->personArray[abc->m_Size].m_Sex = sex;
break;
}
cout << "type error" << endl;
}
//age
cout << "Please type your age:" << endl;
int age = 0;
cin >> age;
abc->personArray[abc->m_Size].m_Age = age;
//address
cout << "Please type your address:"<<endl;
string address;
cin >> address;
abc->personArray[abc->m_Size].m_Addr = address;
//家庭住址
cout <<"Please type your phone:"<<endl;
string phone = "";
cin >> phone;
abc->personArray[abc->m_Size].m_Phone = phone;
//update count for addressbooks
abc->m_Size++;
cout <<"Add Success"<<endl;
}
}
void showPerson(Addressbooks * abc){
if (abc->m_Size == 0){
cout <<"Contact is empty"<<endl;
} else{
for (int i = 0; i < abc->m_Size; i++){
cout<<"FullName:"<< abc->personArray[i].m_fullName <<"\t";
cout<<"Age:"<< abc->personArray[i].m_Age <<"\t";
cout<<"Sex:"<< (abc->personArray[i].m_Sex == 1 ? "Man":"Woman") <<"\t";
cout<<"Address:"<< abc->personArray[i].m_Addr <<"\t";
cout<<"Phone:"<< abc->personArray[i].m_Phone <<"\t\n";
}
}
}
//make sure your search name is in the contact
int isExist (Addressbooks * abc, string fullname){
for (int i = 0; i < abc->m_Size; i++){
if (abc->personArray[i].m_fullName == fullname){
return i;
}
}
return -1;
}
//delete person function
void delPerson (Addressbooks * abc){
cout <<"Please type delete person:" <<endl;
string fullname;
cin >> fullname;
int ret = isExist(abc, fullname);
if (ret != -1){
for (int i = ret; i<abc->m_Size; i++){
abc->personArray[i] = abc->personArray[i+1];
}
abc->m_Size--;
cout << "Delete Success" <<endl;
} else{
cout << "There is no such person:"<< fullname<<endl;
}
}
//search person
void searchPerson(Addressbooks * abc){
cout<<"Pleae type you want search person:" <<endl;
string fullname;
cin >> fullname;
int ret = isExist(abc, fullname);
if (ret != -1){
cout<<"FullName:"<< abc->personArray[ret].m_fullName <<"\t";
cout<<"Age:"<< abc->personArray[ret].m_Age <<"\t";
cout<<"Sex:"<< abc->personArray[ret].m_Sex <<"\t";
cout<<"Address:"<< abc->personArray[ret].m_Addr <<"\t";
cout<<"Phone:"<< abc->personArray[ret].m_Phone <<"\t\n";
} else {
cout <<"There is no such person:"<<fullname<<endl;
}
}
// edit person
void editPerson(Addressbooks * abc){
cout << "Pleae type your edit person:"<<endl;
string fullname;
cin >> fullname;
int ret = isExist(abc, fullname);
if (ret != -1){
string editfullname;
cout << "Plese type your fullname:" << endl;
cin >> editfullname;
abc->personArray[ret].m_fullName = editfullname;
cout << "Please type your gender:" << endl;
cout << "1 -- Male" << endl;
cout << "2 -- Female" << endl;
int sex = 0;
while (true) {
cin >> sex;
if (sex == 1 || sex == 2) {
abc->personArray[ret].m_Sex = sex;
break;
}
cout << "type error" << endl;
}
//age
cout << "Please type your age:" << endl;
int age = 0;
cin >> age;
abc->personArray[ret].m_Age = age;
//address
cout << "Please type your address:"<<endl;
string address;
cin >> address;
abc->personArray[ret].m_Addr = address;
//家庭住址
cout <<"Please type your phone:"<<endl;
string phone = "";
cin >> phone;
abc->personArray[ret].m_Phone = phone;
cout <<"Edit Success"<<endl;
}
}
//clear contact
void clearPerson(Addressbooks * abc){
abc->m_Size = 0;
cout <<"Contact cleared"<<endl;
}
int main() {
// Create Contacts
Addressbooks abc;
//init count
abc.m_Size = 0;
int select = 0;
while(true){
showMenu();
cin >> select;
switch (select) {
case 1:
addPerson(&abc);
break;
case 2:
showPerson(&abc);
break;
case 3:
delPerson(&abc);
break;
case 4:
searchPerson(&abc);
break;
case 5:
editPerson(&abc);
break;
case 6:
clearPerson(&abc);
break;
case 0:
cout <<"Welcome to you next time"<<endl;
//freeze screem in Windows
//system("pause")
return 0;
break;
default:
cout << "type error" << endl;
break;
}
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/hedysean/spl.git
git@gitee.com:hedysean/spl.git
hedysean
spl
spl
master

搜索帮助

0d507c66 1850385 C8b1a773 1850385