1.结构体案例1
每名老师带领5个学生,总共有3个老师。设计学生和老师的结构体,其中在老师的结构体中,有老师姓名和一个存放5名学生的数组作为成员。学生的成员有姓名、考试分数,创建数组存放3名老师,通过函数给每个老师及所带的学生赋值。最终打印出老师数据以及老师所带学生数据。
#include <iostream>
#include <string>
#include <ctime>
using namespace std;
struct Student
{
string sname;
int score;
};
struct Teacher
{
string tname;
struct Student sArray[5]; // 学生数组
};
void allocateSpace(struct Teacher tArray[], int len)
{
string nameSeed = "ABCDE";
for (int i = 0; i < len; i++)
{
tArray[i].tname = "Teacher_";
tArray[i].tname += nameSeed[i];
for (int j = 0; j < 5; j++)
{
tArray[i].sArray[j].sname = "Student_";
tArray[i].sArray[j].sname += nameSeed[j];
int random = rand() % 61 + 40; // 40-100
tArray[i].sArray[j].score = random;
}
}
}
void printArray(struct Teacher tArray[], int len)
{
for (int k = 0; k < len; k++)
{
cout << "老师姓名:" << tArray[k].tname << endl;
for (int m = 0; m < 5; m++)
{
cout << "学生姓名:" << tArray[k].sArray[m].sname << "score:"
<< tArray[k].sArray[m].score << endl;
}
}
}
int main()
{
// 随机数种子
srand((unsigned int)time(NULL));
struct Teacher tArray[3];
int len = sizeof(tArray) / sizeof(tArray[0]);
allocateSpace(tArray, len);
printArray(tArray, len);
system("pause");
return 0;
}
2.结构体案例2
设计一个英雄的结构体,包括成员姓名,年龄,性别创建结构体数组,数组中存放5名英雄,通过冒泡排序的算法,将数组中的英雄按照年龄进行升序排序,最终打印排序后的结果。
#include <iostream>
#include <string>
using namespace std;
struct hero
{
string name;
int age;
string gender;
};
void Sort(struct hero hArray[], int len)
{
for (int i=0;i<len-1;i++)
{
for (int j=0;j<len-1-i;j++)
{
if (hArray[j].age > hArray[j+1].age)
{
struct hero temp = hArray[j];
hArray[j] = hArray[j+1];
hArray[j + 1] = temp;
}
}
}
}
int main()
{
struct hero hArray[5] =
{
{"刘备", 23, "男"},
{"关羽", 22, "男"},
{"张飞", 20, "男"},
{"赵云", 21, "男"},
{"貂蝉", 19, "女"}
};
int len = sizeof(hArray) / sizeof(hArray[0]);
Sort(hArray, len);
for (int k=0;k<len;k++)
{
cout << "name: " << hArray[k].name
<< "age: " << hArray[k].age
<<"gender: " << hArray[k].gender
<< endl;
}
system("pause");
return 0;
}
3.通讯录管理系统
用C++实现一个通讯录管理系统
- 添加联系人:向通讯录中添加信任,信息包括姓名、性别、年龄、联系电话、家庭住址,最多记录1000人
- 显示联系人:显示通讯录中所有联系人信息
- 删除联系人:按照姓名进行删除指定联系人
- 查找联系人:按照姓名查看指定联系人信息
- 修改联系人:按照姓名重新修改指定联系人
- 清空联系人:清空通讯录中所有信息
- 退出通讯录:退出当前使用的通讯录
①菜单功能
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;
}
②退出
case 0:
cout << "欢迎下次使用" << endl;
system("pause");
return 0;
break;
③添加
// 联系人结构体
struct person
{
string name;
int gender; // 1-男 2-女
int age;
string phone;
string address;
};
// 通讯录结构体
struct Addressbook
{
struct person personArray[MAX];
// 记录当前联系人个数
int size;
};
void addPerson(struct Addressbook* abs)
{
// 判断通讯录是否满员
if(abs->size == MAX)
{
cout << "通讯录已满,无法添加!" << endl;
}
else
{
string name;
cout << "请输入姓名: "<< endl;
cin >> name;
abs->personArray[abs->size].name = name;
int gender = 0;
cout << "请输入性别(1-男,2-女): "<< endl;
while(true)
{
cin >> gender;
if (gender == 1 || gender == 2)
{
abs->personArray[abs->size].gender = gender;
break; // 输入合法可以退出循环
}
cout << "输入有误,请重新输入" << endl;
}
int age;
cout << "请输入年龄: "<< endl;
cin >> age;
abs->personArray[abs->size].age = age;
string number;
cout << "请输入电话: "<< endl;
cin >> number;
abs->personArray[abs->size].phone = number;
string address;
cout << "请输入地址: "<< endl;
cin >> address;
abs->personArray[abs->size].address = address;
abs->size++;
cout << "添加成功" << endl;
system("pause"); // 按任意键继续
system("cls"); // 清屏
}
}
④显示联系人
void showPerson(struct Addressbook* abs)
{
if (abs->size == 0)
{
cout << "当前通讯录为空" << endl;
}
else
{
for (int i=0;i<abs->size;i++)
{
cout << " 姓名:"<< abs->personArray[i].name
<< " 性别:" << (abs->personArray[i].gender == 1 ? "男" : "女")
<< " 年龄:" << abs->personArray[i].age
<< " 电话:" << abs->personArray[i].phone
<< " 地址:" << abs->personArray[i].address << endl;
}
}
system("pause"); // 按任意键继续
system("cls"); // 清屏
}
⑤删除联系人
int existPerson(struct Addressbook* abs, string target_name)
{
for (int i=0; i<abs->size;i++)
{
if (abs->personArray[i].name == target_name)
{
return i;
}
}
return -1; // 没找到这个人
}
void deletePerson(struct Addressbook* abs)
{
string target_name;
cout << "请您输入要删除的联系人: " << endl;
cin >> target_name;
int ret = existPerson(abs, target_name); // 为什么是abs 不是*abs
if (ret != -1)
{
for (int i =ret;i<abs->size;i++)
{
abs->personArray[i] = abs->personArray[i+1];
}
abs->size--;
cout << "删除成功" << endl;
}
else
{
cout << "查无此人" << endl;
}
system("pause"); // 按任意键继续
system("cls"); // 清屏
}
⑥查找联系人
void findPerson(struct Addressbook* abs)
{
string target_name;
cout << "请您输入要查找的联系人: " << endl;
cin >> target_name;
int ret = existPerson(abs, target_name); // 为什么是abs 不是*abs
if (ret != -1)
{
cout << " 姓名:"<< abs->personArray[ret].name
<< " 性别:" << (abs->personArray[ret].gender == 1 ? "男" : "女")
<< " 年龄:" << abs->personArray[ret].age
<< " 电话:" << abs->personArray[ret].phone
<< " 地址:" << abs->personArray[ret].address << endl;
}
else
{
cout << "查无此人" << endl;
}
system("pause"); // 按任意键继续
system("cls"); // 清屏
}
⑦修改联系人
void changePerson(struct Addressbook* abs)
{
string target_name;
cout << "请您输入要修改的联系人: " << endl;
cin >> target_name;
int ret = existPerson(abs, target_name); // 为什么是abs 不是*abs
if (ret != -1)
{
string name;
cout << "请输入姓名: "<< endl;
cin >> name;
abs->personArray[ret].name = name;
int gender = 0;
cout << "请输入性别(1-男,2-女): "<< endl;
while(true)
{
cin >> gender;
if (gender == 1 || gender == 2)
{
abs->personArray[ret].gender = gender;
break; // 输入合法可以退出循环
}
cout << "输入有误,请重新输入" << endl;
}
int age;
cout << "请输入年龄: "<< endl;
cin >> age;
abs->personArray[ret].age = age;
string number;
cout << "请输入电话: "<< endl;
cin >> number;
abs->personArray[ret].phone = number;
string address;
cout << "请输入地址: "<< endl;
cin >> address;
abs->personArray[ret].address = address;
cout << "修改成功!" << endl;
}
else
{
cout << "查无此人" << endl;
}
system("pause"); // 按任意键继续
system("cls"); // 清屏
}
⑧清空联系人
void clearPerson(struct Addressbook* abs)
{
abs->size = 0; // 将当前记录的联系人置为0,做一个逻辑上的清空
cout << "通讯录已清空" << endl;
system("pause"); // 按任意键继续
system("cls"); // 清屏
}
完整代码:
#include <iostream>
#include <string>
using namespace std;
#define MAX 1000
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;
}
// 联系人结构体
struct person
{
string name;
int gender; // 1-男 2-女
int age;
string phone;
string address;
};
// 通讯录结构体
struct Addressbook
{
struct person personArray[MAX];
// 记录当前联系人个数
int size;
};
void addPerson(struct Addressbook* abs)
{
// 判断通讯录是否满员
if(abs->size == MAX)
{
cout << "通讯录已满,无法添加!" << endl;
}
else
{
string name;
cout << "请输入姓名: "<< endl;
cin >> name;
abs->personArray[abs->size].name = name;
int gender = 0;
cout << "请输入性别(1-男,2-女): "<< endl;
while(true)
{
cin >> gender;
if (gender == 1 || gender == 2)
{
abs->personArray[abs->size].gender = gender;
break; // 输入合法可以退出循环
}
cout << "输入有误,请重新输入" << endl;
}
int age;
cout << "请输入年龄: "<< endl;
cin >> age;
abs->personArray[abs->size].age = age;
string number;
cout << "请输入电话: "<< endl;
cin >> number;
abs->personArray[abs->size].phone = number;
string address;
cout << "请输入地址: "<< endl;
cin >> address;
abs->personArray[abs->size].address = address;
abs->size++;
cout << "添加成功" << endl;
system("pause"); // 按任意键继续
system("cls"); // 清屏
}
}
void showPerson(struct Addressbook* abs)
{
if (abs->size == NULL)
{
cout << "当前通讯录为空" << endl;
}
else
{
for (int i=0;i<abs->size;i++)
{
cout << " 姓名:"<< abs->personArray[i].name
<< " 性别:" << (abs->personArray[i].gender == 1 ? "男" : "女")
<< " 年龄:" << abs->personArray[i].age
<< " 电话:" << abs->personArray[i].phone
<< " 地址:" << abs->personArray[i].address << endl;
}
}
system("pause"); // 按任意键继续
system("cls"); // 清屏
}
int existPerson(struct Addressbook* abs, string target_name)
{
for (int i=0; i<abs->size;i++)
{
if (abs->personArray[i].name == target_name)
{
return i;
}
}
return -1; // 没找到这个人
}
void deletePerson(struct Addressbook* abs)
{
string target_name;
cout << "请您输入要删除的联系人: " << endl;
cin >> target_name;
int ret = existPerson(abs, target_name); // 为什么是abs 不是*abs
if (ret != -1)
{
for (int i =ret;i<abs->size;i++)
{
abs->personArray[i] = abs->personArray[i+1]; // 数据前移
}
abs->size--;
cout << "删除成功" << endl;
}
else
{
cout << "查无此人" << endl;
}
system("pause"); // 按任意键继续
system("cls"); // 清屏
}
void findPerson(struct Addressbook* abs)
{
string target_name;
cout << "请您输入要查找的联系人: " << endl;
cin >> target_name;
int ret = existPerson(abs, target_name); // abs(指针本身) 不是abs*(指针指向的值)
if (ret != -1)
{
cout << " 姓名:"<< abs->personArray[ret].name
<< " 性别:" << (abs->personArray[ret].gender == 1 ? "男" : "女")
<< " 年龄:" << abs->personArray[ret].age
<< " 电话:" << abs->personArray[ret].phone
<< " 地址:" << abs->personArray[ret].address << endl;
}
else
{
cout << "查无此人" << endl;
}
system("pause"); // 按任意键继续
system("cls"); // 清屏
}
void changePerson(struct Addressbook* abs)
{
string target_name;
cout << "请您输入要修改的联系人: " << endl;
cin >> target_name;
int ret = existPerson(abs, target_name); // 为什么是abs 不是*abs
if (ret != -1)
{
string name;
cout << "请输入姓名: "<< endl;
cin >> name;
abs->personArray[ret].name = name;
int gender = 0;
cout << "请输入性别(1-男,2-女): "<< endl;
while(true)
{
cin >> gender;
if (gender == 1 || gender == 2)
{
abs->personArray[ret].gender = gender;
break; // 输入合法可以退出循环
}
cout << "输入有误,请重新输入" << endl;
}
int age;
cout << "请输入年龄: "<< endl;
cin >> age;
abs->personArray[ret].age = age;
string number;
cout << "请输入电话: "<< endl;
cin >> number;
abs->personArray[ret].phone = number;
string address;
cout << "请输入地址: "<< endl;
cin >> address;
abs->personArray[ret].address = address;
cout << "修改成功!" << endl;
}
else
{
cout << "查无此人" << endl;
}
system("pause"); // 按任意键继续
system("cls"); // 清屏
}
void clearPerson(struct Addressbook* abs)
{
abs->size = 0; // 将当前记录的联系人置为0,做一个逻辑上的清空
cout << "通讯录已清空" << endl;
system("pause"); // 按任意键继续
system("cls"); // 清屏
}
int main()
{
Addressbook abs;
abs.size = 0;
int select = 0;
while(true)
{
showMenu();
cin >> select;
switch(select)
{
case 1:
addPerson(&abs); // 地址传递才能改变实参
break;
case 2:
showPerson(&abs);
break;
case 3:
deletePerson(&abs);
break;
case 4:
findPerson(&abs);
break;
case 5:
changePerson(&abs);
break;
case 6:
clearPerson(&abs);
break;
default:
break;
case 0:
cout << "欢迎下次使用" << endl;
system("pause");
return 0;
break;
}
}
system("pause");
return 0;
}
标签:cout,gender,C++,ret,abs,通讯录,personArray,打卡,name From: https://blog.csdn.net/weixin_57139639/article/details/137479595