一直都觉得以项目为导向学习编程是一种很高效的学习编程的方法,我这段时期的主要学习任务是复习一下c++的基本语法,比起再一章一章的去学,写一个增删改查的小项目或许更能帮助我复习之前的知识点,同时也能增强自己的开发能力。我对这个小项目的目标是实现最基本的增删改查功能然后再用MFC做一个简单的UI界面。接下来我会将已经完成的功能模块代码展示出来。
文章目录- 菜单功能
- 增加联系人功能
- 显示联系人功能
- 搜索联系人功能
- 修改联系人功能
`void menu()
{
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;
}
增加联系人功能
//This funcation is designed to add contacts
void add_contacts( contact_book *abs)
{
if(abs->book_size == MAX)
{
cout << "通讯录已满,无法添加!" << endl;
return;
}
else//add a contact
{
//add his name
string name;
cout << "请输入姓名:" << endl;
cin >> name;
abs->book[abs->book_size].name = name;//问题①:这一语句的逻辑是什么?
//add his sex
cout << "请输入性别: " << endl;
cout << "1 --- 男" << endl;
cout << "0 --- 女" << endl;
int sex = 0;
while(true)//这个while(1)有很强的鲁棒性
{
//如果输入的是0或者1可以退出循环,因为输入的是正确值
//如果输入有误,重新输入
cin >> sex;
if(sex == 0 || sex == 1)
{
abs->book[abs->book_size].sex = sex;
break;
}
cout << "输入有误,请重新输入" << endl;
}
//add his age
cout << "请输入年龄: " << endl;
int age = 0;
cin >> age;
abs->book[abs->book_size].age = age;
//add his address
cout << "请输入地址:" << endl;
string address = string();//string类型的数据赋初值的方法
cin >> address;
abs->book[abs->book_size].address = address;
//add his phone
cout << "请输入电话:" << endl;
string phone = string();
cin >> phone;
abs->book[abs->book_size].phone = phone;
abs->book_size++;
cout << "添加成功" << endl;
system("pause");
system("cls");
}
}
显示联系人功能
//This function is used to show all the contacts in the address book.
void show_contacts(contact_book *abs)
{
//If there aren't any contacts in the address book,it will show the sentence below.
if(abs->book_size == 0)
{
cout << "该通讯录中没有联系人!" << endl;
}
//This for loop can show all the contacts in the address book.
for(int i = 0;i < abs->book_size;i++)
{
cout << "姓名为:" << abs->book[i].name << "\t";
cout << "性别为:" << abs->book[i].sex << "\t";
cout << "年龄为:" << abs->book[i].age << "\t";
cout << "地址为:" << abs->book[i].address << "\t";
cout << "电话为:" << abs->book[i].phone << endl;
}
system("pause");//Press any keys to continue.
system("cls");//All the contents before this sentece will disappear.
}
搜索联系人功能
//Try to search a particular contact in the address book.
void search_contact(contact_book *abs)
{
string name;
if(abs->book_size == 0)
{
cout << "The address book is empty so you can't delete any contacts!" << endl;
system("pause");
system("cls");
return;//the return should be put in the end.
}
//input the name you want to search.
cout << "Please input the name you want to search: " << endl;
cin >> name;
//Search this guy in the address book
for(int i = 0;i < abs->book_size;i++)
{
if(name == abs->book[i].name)
{
cout << "name:" << abs->book[i].name << "\t";
cout << "sex:" << abs->book[i].sex << "\t";
cout << "age:" << abs->book[i].age << "\t";
cout << "address:" << abs->book[i].address << "\t";
cout << "phone:" << abs->book[i].phone << endl;//endl is different from above "\t".But without endl your UI would be in a mess.
}
else
{
cout << "No such a contact in this address book!" << endl;
cout << "pause" << endl;//Press any keys to continue.
cout << "cls" << endl;
}
}
}
修改联系人功能
//revise the information of some contact.
void revise_contact(contact_book *abs)
{
string name;
if(abs->book_size == 0)
{
cout << "The address book is empty so you can't revise any contacts!" << endl;
system("pause");
system("cls");
return;//the return should be put in the end.
}
cout << "Input the name you want to revise: " << endl;
cin >> name;
for(int i = 0;i < abs->book_size;i++)
{
if(name == abs->book[i].name)
{
string new_name,new_sex,new_age,new_address,new_phone;
cout << "请输入新的名字:" << endl;
cin >> new_name;
abs->book[i].name = new_name;
cout << "请输入新的性别:" << endl;
cin >> new_sex;
abs->book[i].sex = new_sex;
cout << "请输入新的年龄:" << endl;
cin >> new_age;
abs->book[i].age = new_age;
cout << "请输入新的地址:" << endl;
cin >> new_address;
abs->book[i].address = new_address;
cout << "请输入新的电话号码:" << endl;
cin >> new_phone;
abs->book[i].phone = new_phone;
system("pause");//Press any keys to continue.
system("cls");
}
else
{
cout << "No such a contact in this address book!" << endl;
system("pause");//Press any keys to continue.
system("cls");
}
}
}
待更新。。。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)