1 Star 0 Fork 0

李文威 / 20199450.txt

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
代码.cpp 4.09 KB
一键复制 编辑 原始数据 按行查看 历史
李文威 提交于 2020-04-26 19:46 . C语言II01
#include<string>
#include<fstream>
#include<iostream>
using namespace std;
int number = 0;
typedef struct News
{
int num;
string name;
int age;
string tel;
string address;
string hooby;
}News;
int Choose() //主界面
{
int choice;
cout << "***************手机通信录管理系统************" << endl;
cout << " 1:根据编号查询" << endl;
cout << " 2:添加" << endl;
cout << " 3:删除" << endl;
cout << " 4:修改" << endl;
cout << " 5:查找" << endl;
cout << " 6:退出程序" << endl;
cout << "**********************************************" << endl;
cout << "请输入选项(1-6):";
cin >> choice; //通过输入获得返回值,选择说要进入的系统
system("cls"); //清屏
return choice;
} // 选择所要进入系统的函数
int SearchByNum(News *a) //查找系统
{
int i,x;
int SearchNum;
cout << "请输入联系人编号";
cin >> SearchNum; //输入要查找的联系人编号
for (i = 0; i < number; i++)
{
if (a[i].num == SearchNum)
{
cout << "编号:" << a[i].num <<" "<< "姓名:" << a[i].name <<" "<< "年龄:" << a[i].age <<" "<< "电话:" << a[i].tel <<" "<< "地址:" << a[i].address <<" "<< "爱好:" << a[i].hooby << endl;
cout<<"查找结束,按任意键结束!";
cin>>x;
system("cls");
return i;//如果找到该联系人,返回该联系人在数组中的对应标号
}
}
cout << "抱歉,查无此人!" << endl;
cout<<"查找结束,按任意键结束!";
cin>>x;
system("cls");
return -1;//如果没有找到,就返回-1
}
void AddMembers(News *a)//添加联系人数据
{
int x;
int i = number; //在数组的末尾插入一个新的联系人。num既是人数,也代表数组的末尾
//输入新联系人的各项参数
cout << "编号:";
cin >> a[i].num;
cout << "姓名:";
cin >> a[i].name;
cout << "年龄:";
cin >> a[i].age;
cout << "电话:";
cin >> a[i].tel;
cout << "地址:";
cin >> a[i].address;
cout << "爱好:";
cin >> a[i].hooby;
//将新建联系人的各项参数放到对象中去
number++;//学生人数加一
system("cls");
cout<<"信息已保存,按任意键结束!";
cin>>x;
system("cls");
}
void DeleteMember(News *a)//删除联系人数据
{
int i, j,x;
i = SearchByNum(a); //调用查找函数,查找要删除的学生是否存在。如果存在,i表示学生在数组中对应的下标;如果不存在,i的值为 - 1
if (i > 0) // 如果学生存在,删除该学生
{
for (j = i; j < number - 1; j++) // 删除时,数组中第i个元素后面的每一个元素依次往前移动,覆盖掉原有的数据
{
a[j] = a[j + 1]; // 将数组中的第j+1个元素赋值给第j个元素,相当于向前移动,即用第j+1个元素覆盖掉了第j个元素
}
number--; //学生人数减1
cout << "该联系人已成功被移出您的通信录,按任意键结束!" << endl;
cin>>x;
system("cls");
}
}
void EditName(News *a)//修改联系人姓名
{
int i,x;
string newname, oldname;//定义原始名字,修改后的名字
cout << "原名字:";
cin >> oldname;
cout << "修改后的名字:";
cin >> newname;
for (i = 0; i < number; i++)
{
if (a[i].name == oldname)
{
a[i].name=newname;//调用成员函数setName,把该联系人的名字改为新名字newName
cout << "编号:" << a[i].num <<" "<< "姓名:" << a[i].name <<" "<< "年龄:" << a[i].age <<" "<< "电话:" << a[i].tel <<" "<< "地址:" << a[i].address <<" "<< "爱好:" << a[i].hooby << endl;
}
}
cout<<"修改完毕,按任意键结束!";
cin>>x;
system("cls");
}
void DisplayAll(News *a) //展示
{
int x;
for (int i = 0; i < number; i++) //用for循环逐个输出通讯录中所有成员数据
{
cout << "第" << i + 1 << "个人:" << endl;//数组下标从0开始,为符合平常使用习惯,下标+1,代表第几个学生
cout << "编号:" << a[i].num <<" "<< "姓名:" << a[i].name <<" "<< "年龄:" << a[i].age <<" "<< "电话:" << a[i].tel <<" "<< "地址:" << a[i].address <<" "<< "爱好:" << a[i].hooby << endl;
}
cout << "展示完毕,按任意键结束!" << endl;
cin>>x;
system("cls");
}
exit(News *a)
{
printf("感谢使用,正在退出程序.....");
}
int main()
{
int x=0;
News a[50];
while(x!=6)
{
x=Choose();
switch(x)
{
case 1:SearchByNum(a);break;
case 2:AddMembers(a);break;
case 3:DeleteMember(a);break;
case 4:EditName(a);break;
case 5:DisplayAll(a);break;
case 6:exit(a);break;
default: { cout << " Error!!! 选项为1-6,请重输!" << endl; }
}
}
return 0;
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C
1
https://gitee.com/wen_wei_li/x20199450.git
git@gitee.com:wen_wei_li/x20199450.git
wen_wei_li
x20199450
20199450.txt
master

搜索帮助

344bd9b3 5694891 D2dac590 5694891