c/c++结构体通讯录

头文件

#pragma once
#include<iostream>
using namespace std;
#define MAX 20
#include<stdlib.h>
#include<string>
typedef struct p//每个人的详情
{
	char name[20];
	int age;
}p;
typedef struct d
{
	p pp[MAX];//创建 20人
	int i;//所含有的人数
}d;



//选择页面
void xuanze()
{
	cout << "╔══════════════════════╗" << endl;
	cout << "║════    通讯录    ════║" << endl;
	cout << "║═══                ═══║" << endl;
	cout << "║═══ 1.新建  2.输出 ═══║" << endl;
	cout << "║═══ 3.删除  4.寻找 ═══║" << endl;
	cout << "║═══ 5.修改  6.清除 ═══║" << endl;
	cout << "║═══     0.退出     ═══║" << endl;
	cout << "║═══                ═══║" << endl;
	cout << "╚══════════════════════╝" << endl;

}


//1.添加
void input(d *abs)
{
	if (abs->i > MAX)
	{
		cout << "输入已满" << endl;
		return;
	}
	cout << "请输入姓名,年龄" << endl;
	cin >> abs->pp[abs->i].name >> abs->pp[abs->i].age;
	cout << endl << "输入成功" << endl;
	(abs->i++);//下一个人
}


//2.输出
void output(d* abs)
{
	if (abs->i == 0)
	{
		cout << "当前没有数据\n";
	}
	for (int j = 0; j < abs->i; j++)
	{
		cout << "姓名为:" << abs->pp[j].name << "\t年龄为:" << abs->pp[j].age << endl;
	}
}



//3.删除联系人
void dele(d* abs)
{
	if (abs->i == 0)
	{
		cout << "当前没有数据\n";
		return;
	}
	cout << "请输入要删除人的姓名\n";
	char name[20];
	cin >> name;
	for (int i = 0;i < abs->i;i++)
	{
		if (!(strcmp(name, abs->pp[i].name)))
		{
			for (int j = i; j < abs->i;j++)
			{
				abs->pp[i] = abs->pp[i+1];
			}
			cout << "删除成功\n";
			while (i == abs->i)
			{
				abs->pp[i] = { 0 };
			}
			(abs->i)--;
		}
		else
		{
			cout << "没有该人员\n" << endl;
		}
	}
}



//4,寻找
void  find(d* abs)
{
	cout << "请输入要查找的人名";
	char name[20];
	cin >> name;
	for (int k = 0; k < abs->i; k++)
	{
		if (!(strcmp(name, abs->pp[k].name)))//找到为1
		{
			cout << "姓名为" << abs->pp[k].name << "  年龄为" << abs->pp[k].age;
		}
		else
			cout <<"查无此人";
	}
}


//5,修改
void  change(d* abs)
{
	cout << "请输入要修改的人名";
	char name[20];
	cin >> name;
	for (int k = 0; k < abs->i; k++)
	{
		if (!(strcmp(name, abs->pp[k].name)))//找到为1
		{
			cout << "姓名为" << abs->pp[k].name << "  年龄为" << abs->pp[k].age << endl;
			cout << "请输入更改的姓名,年龄" << endl;
			cin >> abs->pp[k].name >> abs->pp[k].age;
		}
		else
		{ 
			cout << "查无此人是否添加?  1/0" << endl;
			int z = 0;
			cin >> z;
			switch (z)
			{
			case 1:
				input(abs);
				break;
			default:
				break;
			}
		}
	}
}

//6.清除
void clean(d* abs)
{
	cout << "是否清空? 1/0" << endl;
	int a = 0;
	cin >> a;
	if (a == 1)
	{
		abs->i = 0;
		cout << "清空成功\n" << endl;
	}
	else
		cout<<"未被清空"<<endl;
}

函数体

#include<iostream>
using namespace std;
#pragma once
#include"ac.h"

//主函数
int main()
{
	//创建与初始化
	d data;
	data.i = 0;


	//选择界面
	int a = 0;
	do{
		xuanze();
		cin >> a;
		switch (a)
		{
		case 1://创建
			input(&data);
			break;
		case 2:
			output(&data);
			break;
		case 3:
			dele(&data);
			break;
		case 4:
			find(&data);
			break;
			case 5:
				change(&data);
				break;
			case 6:
				clean(&data);
				break;
		default:
			cout << "重新选择" << endl;
			break;
		}
		cout << endl << endl;
	} while (a);


	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是一个简单的C++代码,用于实现手机通讯录的链表: ```c++ #include <iostream> #include <string> using namespace std; // 联系人结构体 struct Contact { string name; string phone; Contact* next; }; // 添加联系人 void addContact(Contact* &head, string name, string phone) { Contact* newContact = new Contact; newContact->name = name; newContact->phone = phone; newContact->next = NULL; if (head == NULL) { head = newContact; } else { Contact* ptr = head; while (ptr->next != NULL) { ptr = ptr->next; } ptr->next = newContact; } } // 删除联系人 void deleteContact(Contact* &head, string name) { Contact* ptr = head; Contact* prev = NULL; while (ptr != NULL) { if (ptr->name == name) { if (prev == NULL) { head = ptr->next; } else { prev->next = ptr->next; } delete ptr; break; } prev = ptr; ptr = ptr->next; } } // 显示所有联系人 void showAllContacts(Contact* head) { Contact* ptr = head; cout << "所有联系人:" << endl; while (ptr != NULL) { cout << "姓名:" << ptr->name << " 电话:" << ptr->phone << endl; ptr = ptr->next; } } // 查找联系人 void searchContact(Contact* head, string name) { Contact* ptr = head; while (ptr != NULL) { if (ptr->name == name) { cout << "姓名:" << ptr->name << " 电话:" << ptr->phone << endl; break; } ptr = ptr->next; } if (ptr == NULL) { cout << "未找到联系人" << endl; } } int main() { Contact* head = NULL; // 添加联系人 addContact(head, "张三", "123456"); addContact(head, "李四", "456789"); addContact(head, "王五", "789123"); // 显示所有联系人 showAllContacts(head); // 删除联系人 deleteContact(head, "李四"); // 显示所有联系人 showAllContacts(head); // 查找联系人 searchContact(head, "张三"); return 0; } ``` 在这个示例代码中,我们定义了一个Contact结构体,其中包含了联系人的姓名、电话和下一个联系人的指针。我们使用addContact()函数向通讯录中添加联系人,使用deleteContact()函数删除联系人,使用showAllContacts()函数显示所有联系人,使用searchContact()函数查找指定联系人。我们使用一个指针head来指向通讯录的第一个联系人。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值