成都创新互联网站制作重庆分公司

C++学习笔记六-创新互联

6.1demo1

描述:每名老师带领五位同学,总共三名老师,设计学生和老师的结构体,在老师的结构体中,有老师和一个存放五名学生的数组作为成员,学生的成员有姓名、考试分数、创建数组存放三名老师,通过函数给每名老师以及所带的学生赋值,最终打印出老师数据以及老师所带的学生数据

成都创新互联长期为近千家客户提供的网站建设服务,团队从业经验10年,关注不同地域、不同群体,并针对不同对象提供差异化的产品和服务;打造开放共赢平台,与合作伙伴共同营造健康的互联网生态环境。为东平企业提供专业的成都网站制作、成都网站设计,东平网站改版等技术服务。拥有10余年丰富建站经验和众多成功案例,为您定制开发。

示例:

#include#includeusing 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;
			tArray[i].sArray[j].score = random;
		}
	}
}
//打印所有信息
void printInio(struct Teacher tArray[],int len) {
	for (int i = 0; i< len; i++) {
		cout<< "老师姓名:"<< tArray[i].tName<< endl;
		for (int j = 0; j< 5; j++) {
			cout<< "\t学生姓名:"<< tArray[i].sArray[j].sName<< " 考试分数:"<< tArray[i].sArray[j].score<< endl;
		}
	}
}

int main()
{
	//随机数种子
	srand((unsigned int)time(NULL));

	struct Teacher tArray[3];
	int len = sizeof(tArray) / sizeof(tArray[0]);
	allocateSpace(tArray, len);
	printInio(tArray, len);
	system("pause");
	return 0;
}
6.2通讯录管理系统

功能:

添加联系人:向通讯录添加新人;

显示联系人:显示通讯录所有人的信息;

删除联系人:按照姓名删除指定联系人;

查找联系人:按照姓名查找制定联系人;

修改联系人:按照姓名重新修改指定联系人信息;

清空联系人:清空通讯录中的所有信息;

退出通讯录:退出当前使用的通讯录;

示例:

#includeusing namespace std;
#include#define MAX 1000

//联系人结构体
struct Person
{
	string m_Name;
	int m_Sex;
	int m_Age;
	string m_Phone;
	string m_Addr;
};
//通讯录结构体
struct Addressbooks
{
	struct Person personArray[MAX];//通讯录中保存的联系人数组
	int m_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;
}
//1.添加联系人
void addPerson(Addressbooks* abs) {
	//判断通讯录是否已满,如果满了就不在添加
	if (abs->m_Size == MAX) {
		cout<< "通讯录已满,无法添加!"<< endl;
		return;
	}
	else {
		//添加具体联系人
		//姓名
		string name;
		cout<< "请输入姓名:"<< endl;
		cin >>name;
		abs->personArray[abs->m_Size].m_Name = name;
		//性别
		cout<< "请输入性别:"<< endl;
		cout<< "1 --- 男"<< endl;
		cout<< "2 --- 女"<< endl;
		int sex = 0;
		while (true) {
			cin >>sex;
			if (sex == 1 || sex == 2) {
				abs->personArray[abs->m_Size].m_Sex = sex;
				break;
			}
			cout<< "输入有误,请重新输入"<< endl;
		}
		//年龄
		cout<< "请输入年龄:"<< endl;
		int age = 0;
		cin >>age;
		abs->personArray[abs->m_Size].m_Age = age;
		//电话
		cout<< "请输入联系电话:"<< endl;
		string phone;
		cin >>phone;
		abs->personArray[abs->m_Size].m_Phone = phone;
		//住址
		cout<< "请输入家庭住址:"<< endl;
		string address;
		cin >>address;
		abs->personArray[abs->m_Size].m_Addr = address;
		//更新通讯录人数
		abs->m_Size++;
		cout<< "添加成功"<< endl;
		system("pause");
		system("cls");//清屏操作

	}
}
//2.显示联系人
void showPerson(Addressbooks* abs) {
	if (abs->m_Size == 0) {
		cout<< "当前记录为空"<< endl;
	}
	else {
		for (int i = 0; i< abs->m_Size; i++) {
			cout<< "姓名:"<< abs->personArray[i].m_Name<< "\t";
			cout<< "性别:"<< (abs->personArray[i].m_Sex == 1 ? "男": "女")<< "\t";
			cout<< "年龄:"<< abs->personArray[i].m_Age<< "\t";
			cout<< "电话:"<< abs->personArray[i].m_Phone<< "\t";
			cout<< "住址:"<< abs->personArray[i].m_Addr<< endl;
		}
	}
	system("pause");
	system("cls");
}
//检测联系人是否存在
int isExist(Addressbooks* abs, string name) {
	for (int i = 0; i< abs->m_Size; i++) {
		if (abs->personArray[i].m_Name == name) {
			return i;
		}
	}
	return -1;
}
//3.删除指定联系人
void deletePerson(Addressbooks* abs) {
	cout<< "请输入您要删除的联系人:"<< endl;
	string name;
	cin >>name;
	//ret == -1 未查到
	//ret != -1 查到了
	int ret = isExist(abs, name);
	if (ret != -1) {
		for (int i = ret; i< abs->m_Size; i++) {
			//数据前移
			abs->personArray[i] = abs->personArray[i + 1];
		}
		abs->m_Size--;
		cout<< "删除成功"<< endl;
	}
	else {
		cout<< "查无此人"<< endl;
	}
	system("pause");
	system("cls");
}
//4.查找联系人
void findPerson(Addressbooks* abs) {
	cout<< "请输入您要查找的联系人:"<< endl;
	string name;
	cin >>name;
	int ret = isExist(abs, name);
	if (ret != -1) {
		cout<< "姓名:"<< abs->personArray[ret].m_Name<< "\t";
		cout<< "性别:"<< abs->personArray[ret].m_Sex<< "\t";
		cout<< "年龄:"<< abs->personArray[ret].m_Age<< "\t";
		cout<< "电话:"<< abs->personArray[ret].m_Phone<< "\t";
		cout<< "住址:"<< abs->personArray[ret].m_Addr<< endl;
	}
	else {
		cout<< "查无此人"<< endl;
	}
	system("pause");
	system("cls");
}
//5.修改指定联系人信息
void modifyPerson(Addressbooks* abs){
	cout<< "请输入您要修改的联系人:"<< endl;
	string name;
	cin >>name;
	int ret = isExist(abs, name);
	if(ret != -1) {
		//姓名
		string name;
		cout<< "请输入姓名:"<< endl;
		cin >>name;
		abs->personArray[ret].m_Name = name;
		//性别
		cout<< "请输入性别:"<< endl;
		cout<< "1 --- 男"<< endl;
		cout<< "2 --- 女"<< endl;
		int sex = 0;
		while (true) {
			cin >>sex;
			if (sex == 1 || sex == 2) {
				abs->personArray[ret].m_Sex = sex;
				break;
			}
			cout<< "输入有误,请重新输入"<< endl;
		}
		//年龄
		cout<< "请输入年龄:"<< endl;
		int age = 0;
		cin >>age;
		abs->personArray[ret].m_Age = age;
		//电话
		cout<< "请输入联系电话:"<< endl;
		string phone;
		cin >>phone;
		abs->personArray[ret].m_Phone = phone;
		//住址
		cout<< "请输入家庭住址:"<< endl;
		string address;
		cin >>address;
		abs->personArray[ret].m_Addr = address;
	}
	else {
		cout<< "查无此人"<< endl;
	}
	system("pause");
	system("cls");
}
//6.清空联系人
void cleanPerson(Addressbooks* abs) {
	abs->m_Size = 0;
	cout<< "通讯录已清空!"<< endl;
	system("pause");
	system("cls");
}

int main()
{
	Addressbooks abs;
	abs.m_Size = 0;
	int select = 0;
	while (true) {
		showMenu();
		cin >>select;
		switch (select)
		{
		case 1: //1.添加联系人
			addPerson(&abs);
			break;
		case 2: //2.显示联系人
			showPerson(&abs);
			break;
		case 3: //3.删除联系人
			deletePerson(&abs);
			break;
		case 4: //4.查找联系人
			findPerson(&abs);
			break;
		case 5: //5.修改联系人
			modifyPerson(&abs);
			break;
		case 6: //6.清空联系人
			cleanPerson(&abs);
			break;
		case 0: //0.退出通讯录
			cout<< "欢迎下次使用"<< endl;
			system("pause");
			return 0;
			break;
		default:
			break;
		}
	}
	system("pause");
	return 0;
}

你是否还在寻找稳定的海外服务器提供商?创新互联www.cdcxhl.cn海外机房具备T级流量清洗系统配攻击溯源,准确流量调度确保服务器高可用性,企业级服务器适合批量采购,新人活动首月15元起,快前往官网查看详情吧


网页题目:C++学习笔记六-创新互联
文章源于:http://cxhlcq.com/article/cedjhg.html

其他资讯

在线咨询

微信咨询

电话咨询

028-86922220(工作日)

18980820575(7×24)

提交需求

返回顶部