环境:ide:Mac+clion
视频链接:
https://www.bilibili.com/video/BV1Hb411Y7E5?p=5
指针操作结构体
struct Student{
string name;
int age;
int number;
};
Student student = {"张三",20,1};
Student * p = &student;
cout << p->name << "," << p->age << ","<<p->number<<endl;
结构体中有其他的结构体
struct Student{
string name;
int age;
int number;
};
struct Teacher{
string name;
Student student;
};
Student student = {"张三",20,1};
Student * p = &student;
cout << p->name << "," << p->age << ","<<p->number<<endl;
Teacher teacher = {"张老师",student};
Teacher * teacherP = & teacher;
cout << teacherP->name << "的学生信息是:" << teacherP->student.name << ","<<teacherP->student.age
<<teacherP->student.number<<endl;
结构体变量成为形参的时候,建议使用指针,节约内存占用。内存仅有4个字节或者8个字节。结构体中的数据如果copy 一份的话,会多占用内存。使用地址(指针)传递的时候,建议用const修饰。
srand种子的时候,需要引入#include<ctime>
通讯录练习题
#include <iostream>
#include <stdlib.h>
//#include <unistd.h>
//#include "contacts.h"
using namespace std;
#include <string>
#define MAX_PERSON 1000
//联系人结构体
struct Person {
string name;//姓名
int sex;//性别1男 2 女
int age;//年龄
string phoneNumber; //联系电话
string address;//家庭住址
};
//通讯录结构体
struct Contacts {
struct Person perssonArray[MAX_PERSON];
int size;//当前通讯录联系人个数
};
/**
* 显示菜单
*/
void showMenu() {
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;
}
//添加联系人
void addPersion(Contacts *contact) {
if (contact->size == MAX_PERSON) {
cout << "联系人已满,无法添加新的联系人。" << endl;
return;
} else {
string name;//姓名
cout << "请输入联系人姓名:" << endl;
cin >> name;
contact->perssonArray[contact->size].name = name;
cout << "请输入联系人性别:" << endl;
cout << "1----->男" << endl;
cout << "2----->女" << endl;
int sex = 0;
while (true) {
cin >> sex;
if (sex == 1 || sex == 2) {
contact->perssonArray[contact->size].sex = sex;
break;
}
cout << "输入错误,请重新输入。" << endl;
}
cout << "请输入联系人年龄:" << endl;
int age;//年龄
cin >> age;
contact->perssonArray[contact->size].age = age;
cout << "请输入联系人电话:" << endl;
string phoneNumber; //联系电话
cin >> phoneNumber;
contact->perssonArray[contact->size].phoneNumber = phoneNumber;
cout << "请输入联系人家庭住址:" << endl;
string address;//家庭住址
cin >> address;
contact->perssonArray[contact->size].address = address;
contact->size++;
cout << "联系人添加成功。" << endl;
system("clear");
}
}
//显示联系人:
void displayPerson(const Contacts *contacts) {
if (contacts->size <= 0) {
cout << "没有联系人。" << endl;
return;
} else {
for (int i = 0; i < contacts->size; i++) {
string sex = (contacts->perssonArray[i].sex == 1) ? "男" : "女";
cout << "姓名:" << contacts->perssonArray[i].name << "\t性别:" << sex << "\t年龄:"
<< contacts->perssonArray[i].age
<< "\t电话:" << contacts->perssonArray[i].phoneNumber
<< "\t家庭住址:" << contacts->perssonArray[i].address
<< endl;
}
}
system("clear");
}
//联系人是否存在。如果存在,返回数组的index,不存在返回-1
int exit(const Contacts const *contacts, string name) {
int index = -1;
for (int i = 0; i < contacts->size; i++) {
if (contacts->perssonArray[i].name == name) {
index = i;
}
}
return index;
}
//删除联系人
void deletePerson(Contacts *contacts) {
if (contacts->size <= 0) {
cout << "没有联系人。" << endl;
return;
} else {
cout << "请输入删除联系人姓名:" << endl;
string name;
cin >> name;
int value = exit(contacts, name);
if (value == -1) {
cout << "没有此联系人。" << endl;
} else {
for (int i = value; i < contacts->size; ++i) {
contacts->perssonArray[i] = contacts->perssonArray[i++];
}
contacts->size--;
}
}
cout << "删除成功" << endl;
system("clear");
}
//找到对应的联系人。
void findPerson(const Contacts *contacts) {
if (contacts->size <= 0) {
cout << "没有联系人。" << endl;
return;
}
cout << "输入查找联系人:" << endl;
string name;
cin >> name;
int value = exit(contacts, name);
if (value == -1) {
cout << "查无此人";
} else {
cout << "姓名:" << contacts->perssonArray[value].name << "\t";
cout << contacts->perssonArray[value].age << "\t";
cout << contacts->perssonArray[value].phoneNumber << "\t";
cout << contacts->perssonArray[value].address << "\t";
cout << endl;
}
system("clear");
}
void modifyPerson(Contacts *contacts) {
if (contacts->size <= 0) {
cout << "没有联系人。" << endl;
return;
}
cout << "输入查找联系人:" << endl;
string name;
cin >> name;
int value = exit(contacts, name);
if (value == -1) {
cout << "查无此人";
} else {
cout << "请输入联系人姓名:"<<endl;
string name1;
cin >> name1;
contacts->perssonArray[value].name = name1;
cout << "请输入联系人年龄:"<<endl;
int age;
cin >> age;
contacts->perssonArray[value].age = age;
cout << "请输入联系人性别:"<<endl;
cout <<"1----->男"<<endl;
cout <<"2----->女"<<endl;
int sex = 0;
while (true) {
cin >> sex;
if (sex==1 || sex ==2){
contacts->perssonArray[value].sex = sex;
break;
}else{
cout << "输入有误,请重新输入!"<<endl;
}
}
cout << "请输入联系人地址:"<<endl;
string address;
cin >> address;
contacts->perssonArray[value].address = address;
cout << "修改联系人完成。"<<endl;
}
system("clear");
}
void cleanContacts(Contacts * contacts){
contacts->size == 0;
cout << "清除联系人完成"<<endl;
system("clear");
}
int main() {
int select = 0;
struct Contacts contacts;//定义通讯录的结构体
contacts.size = 0;//初始化这个通讯录的size大小
while (true) {
showMenu(); //显示菜单
cin >> select;
switch (select) {
case 0://0.退出联系人
cout << "欢迎下次使用" << endl;
return 0;
break;
case 1: //1.新建联系人
addPersion(&contacts);
break;
case 2://2.显示联系人
displayPerson(&contacts);
break;
case 3://3.删除联系人
deletePerson(&contacts);
break;
case 4://4.查找联系人
findPerson(&contacts);
break;
case 5://5.修改联系人
modifyPerson(&contacts);
break;
case 6://6.清空联系人
cleanContacts(&contacts);
break;
default:
break;
}
}
return 0;
}
网友评论