学了c++基础,写了一个简易通讯录管理系统练习,在这里记录一下。
项目介绍:
系统完成了“1、添加联系人;2、显示联系人;3、删除联系人;4、查找联系人;5、修改联系人;6、清空联系人;”六个功能,顺带写了一个用户登陆注册的功能。
下面是代码:
项目结构:
cpp文件:
main.cpp:
/*
Author: Euan Cai
Creation time: 2024-01-27
*/
#include <iostream>
#include <string>
#include "user.h"
#include "add_bk.h"
using namespace std;
//菜单
void menu(struct Person* ad_list, double userID) {
cout << "------------------------------" << endl;
cout << "----欢迎使用通讯录管理系统----" << endl;
cout << "------------------------------" << endl;
int flag;
int Position;
int tag;
MENU:
cout << "============================" << endl;
cout << "====菜单====:\n";
cout << "1、添加联系人;\n2、显示联系人;\n3、删除联系人;\n4、查找联系人;\n5、修改联系人;\n6、清空联系人;\n0、退出通讯录。" << endl;
do {
cout << "-------------------------------------------------------------------" << endl;
cout << "请输入你要执行的操作(直接输入选项序号即可,刷新/显示菜单请输入9):" << endl;
cin >> flag;
switch (flag) {
case 1:
add_list(ad_list, userID);
break;
case 2:
show_list(ad_list);
break;
case 3:
tag = deleteOne(ad_list);
if (tag == -1) {
cout << "删除失败。" << endl;
}
else {
cout << "删除成功。" << endl;
}
break;
case 4:
Position = find_address(ad_list);
if (Position == -1) {
cout << "查找失败,请检查输入是否有误。" << endl;
break;
}
else {
cout << "查找到联系人信息如下:" << endl;
showOne(ad_list, Position);
}
break;
case 5:
edit_list(ad_list);
break;
case 6:
delete_list(ad_list);
break;
case 9:
goto MENU;
break;
case 0:
break;
default:
cout << "您的输入有误,请重新输入。" << endl;
}
} while (flag);
}
int main() {
Person person;
person.size = 0;
double userID = sys_gate();
menu(&person, userID);
cout << "已退出程序..." << endl;
system("pause");
return 0;
}
address_book.cpp:
/*
Author: Euan Cai
Creation time: 2024-01-28
*/
#include <iostream>
#include <string>
#include "user.h"
#include "add_bk.h"
using namespace std;
//显示联系人信息
void showOne(struct Person* ad_list, int i) {
cout << "\t姓名:" << ad_list->personInfo[i].name << endl;
cout << "\t性别:" << ad_list->personInfo[i].sex << endl;
cout << "\t年龄:" << ad_list->personInfo[i].age << endl;
cout << "\t电话号码:" << ad_list->personInfo[i].phone << endl;
cout << "\t家庭住址:" << ad_list->personInfo[i].address << endl;
}
//查找联系人
//通过电话查找
int find_by_phone(struct Person* ad_list, string phone) {
for (int i = 0; i < ad_list->size; i++) {
if (phone.compare(ad_list->personInfo[i].phone) == 0) return i; //返回下标
}
return -1; //查找失败
}
//通过名字查找
int find_by_name(struct Person* ad_list, string name) {
for (int i = 0; i < ad_list->size; i++) {
if (name.compare(ad_list->personInfo[i].name) == 0) return i; //返回下标
}
return -1; //查找失败
}
//查找
int find_address(struct Person* ad_list) {
int i;
string info;
cout << "=====查找联系人====" << endl;
F:
cout << "\t1、通过姓名查询;\n\t2、通过电话号码查询; " << endl;
cout << "请选择:" << endl;
cin >> i;
if (i == 1) {
cout << "请输入联系人姓名:" << endl;
cin >> info;
return find_by_name(ad_list,info);
}
else if (i == 2) {
cout << "请输入联系人电话:" << endl;
cin >> info;
return find_by_phone(ad_list, info);
}
else {
cout << "输入有误,请重新输入。" << endl;
goto F;
}
}
//修改联系人
int edit_list(struct Person* ad_list) {
cout << "=====修改联系人信息=====" << endl;
cout << "请提供需要修改的联系人:" << endl;
int posi = find_address(ad_list);
if (posi == -1) {
cout << "未找到该联系人信息,请核对后再试。" << endl;
return -1; //修改失败
}
else {
cout << "你要修改的联系人信息如下:" << endl;
showOne(ad_list, posi);
cout << "信息更新:" << endl;
cout << "请输入该联系人的姓名:" << endl;
cin >> ad_list->personInfo[posi].name;
cout << "更新联系人性别:" << endl;
cin >> ad_list->personInfo[posi].sex;
cout << "更新联系人年龄:" << endl;
cin >> ad_list->personInfo[posi].age;
cout << "更新联系人电话号码:" << endl;
cin >> ad_list->personInfo[posi].phone;
cout << "更新联系人家庭住址:" << endl;
cin >> ad_list->personInfo[posi].address;
cout << "修改后联系人信息如下:" << endl;
showOne(ad_list, posi);
return 1;
}
}
//清空联系人;
int delete_list(struct Person* ad_list) {
cout << "=====清空联系人=====" << endl;
cout << "真的要删除吗?" << endl;
cout << "1、确定删除\t2、输入其他数字取消" << endl;
int i;
cin >> i;
if (i == 1) {
ad_list->size = 0;
return 1;
}
else {
return 0;
}
}
//删除联系人
int deleteOne(struct Person* ad_list) {
cout << "=====删除联系人=====" << endl;
cout << "请提供需要删除的联系人:" << endl;
int posi = find_address(ad_list);
if (posi == -1) {
cout << "未找到该联系人信息,请核对后再试。" << endl;
return -1; //删除失败
}
else {
cout << "你要删除的联系人信息如下:" << endl;
showOne(ad_list, posi);
cout << "真的要删除吗?" << endl;
cout << "1、确定删除\t2、输入其他数字取消" << endl;
int i;
cin >> i;
if (i == 1) {
for (int j = posi; j < ad_list->size; j++) {
ad_list->personInfo[j].address = ad_list->personInfo[j+1].address;
ad_list->personInfo[j].age = ad_list->personInfo[j + 1].age;
ad_list->personInfo[j].name = ad_list->personInfo[j + 1].name;
ad_list->personInfo[j].phone = ad_list->personInfo[j + 1].phone;
ad_list->personInfo[j].sex = ad_list->personInfo[j + 1].sex;
ad_list->personInfo[j].uID = ad_list->personInfo[j + 1].uID;
}
ad_list->size--;
return 1; //删除成功
}
else {
cout << "删除取消" << endl;
return -1;
}
}
}
//添加联系人
int add_list(struct Person* ad_list, double uid) {
cout << "====新增联系人====:" << endl;
ad_list->personInfo[ad_list->size].uID = uid;
cout << "请输入联系人姓名:";
cin >> ad_list->personInfo[ad_list->size].name;
cout << "请输入联系人性别:";
cin >> ad_list->personInfo[ad_list->size].sex;
cout << "请输入联系人年龄:";
cin >> ad_list->personInfo[ad_list->size].age;
cout << "请输入联系人电话号码:";
cin >> ad_list->personInfo[ad_list->size].phone;
cout << "请输入联系人家庭地址:";
cin >> ad_list->personInfo[ad_list->size].address;
ad_list->size++;
return 1;
}
//显示联系人
void show_list(struct Person* ad_list) {
cout << "====显示联系人====" << endl;
cout << "当前共有" << ad_list->size << "个联系人。" << endl;
for (int i = 0; i < ad_list->size; i++) {
cout << "联系人" << i + 1 << ":\t" << endl;
showOne(ad_list, i);
}
}
user.cpp:
/*
Author: Euan Cai
Creation time: 2024-01-28
*/
#include<iostream>
#include<string>
#include<cmath>
#include "user.h"
using namespace std;
//通过账户查找用户
int finduser_for_acc(User* user_list, string user_account, int len) {
for (int i = 0; i < len; i++) {
if (user_account.compare(user_list[i].account) == 0) return i;
}
return -1;
}
//登录
double User_login(User* user_list, int len) {
cout << "=====用户登录=====" << endl;
cout << "请输入账户手机号或邮箱:" << endl;
string uacc;
cin >> uacc;
int position = finduser_for_acc(user_list, uacc, len);
if (position == -1) {
cout << "未查找到该用户,请检查后重新登录。" << endl;
return -2; //用户不存在
}
else {
Login:
cout << "您好" << user_list[position].account << ",请输入您的密码:" << endl;
string pass;
cin >> pass;
if (pass.compare(user_list[position].password) == 0) {
cout << "您好" << user_list[position].name << "(先生/女士),欢迎登录。" << endl;
return user_list[position].ID; //登录成功
}
else if (pass == "取消") {
cout << "登录取消。" << endl;
return -1; //取消登录
}
else {
cout << "密码错误,请检查后重新输入。" << endl;
goto Login;
}
}
}
//注册
int User_Register(User* user_list, int len) {
cout << "=====用户注册=====" << endl;
cout << "请输入用户名:" << endl;
cin >> user_list[len].name;
cout << "请输入账户手机号或邮箱:" << endl;
cin >> user_list[len].account;
cout << "请设置密码:" << endl;
cin >> user_list[len].password;
user_list[len].ID = 100000 + len;
cout << "您好" << user_list[len].name << ",您的账户注册成功,您的账户ID为系统自动分配:" << user_list[len].ID << "请牢记。" << endl;
return len + 1;
}
double sys_gate() {
cout << "------------------------------" << endl;
cout << "----欢迎使用通讯录管理系统----" << endl;
cout << "------------------------------" << endl;
struct User user_list[10];
int choice;
double userID;
int len = 0;
do {
cout << "--请选择你要执行的操作:\n\t1、登录;\n\t2、注册;\n\t0、退出系统。" << endl;
cin >> choice;
switch (choice)
{
case 1:
userID = User_login(user_list, len);
if (userID == -1) {
cout << "用户取消登录。" << endl;
}
else if (userID == -2) {
break;
}
else {
return userID;
}
break;
case 2:
len = User_Register(user_list, len);
break;
case 0:
break;
default:cout << "您的输入有误,请重新输入。" << endl; break;
}
} while (choice);
}
头文件:
user.h:
#include <iostream>
#include<string>
using namespace std;
//用户信息
struct User {
string name;
double ID;
string account;
string password;
};
double sys_gate();
add_bk.h:
#pragma once
#include <iostream>
#include<string>
using namespace std;
//联系人信息
struct Contact_person_info {
string name;//姓名
string sex; //性别
int age; //年龄
string phone; //电话
string address; //家庭住址
double uID; //用户ID
};
//联系人
struct Person {
Contact_person_info personInfo[1000];
int size;
};
void showOne(struct Person* ad_list, int i);
int find_by_phone(struct Person* ad_list,string phone);
int find_by_name(struct Person* ad_list, string name);
int find_address(struct Person* ad_list);
int edit_list(struct Person* ad_list);
int delete_list(struct Person* ad_list);
int deleteOne(struct Person* ad_list);
int add_list(struct Person* ad_list, double uid);
void show_list(struct Person* ad_list);
结果测试:
注册登录:
添加联系人:
修改联系人信息:
删除联系人:
清空联系人:
过程中遇到的问题:
编写时没有报错,但是运行时,提示"xxx不是Contact_person_info成员"的错误。如图:
(当时没截图,只有这张手机拍的了哈哈哈哈)
后面检查发现是:声明“Contact_person_info”结构体在头文件“add_bk.h”里,但是这个头文件没有加“using namespace std;”,加上后成功运行。
里面还有很多不完美的地方,通讯录也没有根据不同用户进行分开展示管理。代码可能也有很多不足,希望通过后面的学习能够有所进步(●ˇ∀ˇ●)。