学习日记---第8天(通讯录管理系统)

笔记复习

案例1


学校正在做毕设项目,每名老师带领3名学生,总共有2个老师,需求如下:
1.设计学生和老师的结构体,其中在老师的结构体中,有老师的姓名和一个存放3名学生的数组作为成员,学生的属性有学号、姓名、年龄、考试分数,创建数组存放3名老师
2.用函数实现打印功能,最终打印出老师数据以及老师所带的学生数据

完整代码如下:

#include<iostream>
using namespace std;

struct student {
	int sid;
	string name;
	int age;
	int score;
};

struct teacher {
	string name;
	student stu[3];
};

teacher tarray[2];
这里需要将tarray[2]放在全局中,否则下面printstruct函数中无法访问tarray
void printstruct(teacher *p, int length) {
	if (p == &tarray[0]) {
		for (int i = 0; i < 3; i++) {
			if (i == 0) {
				cout << "\t" << "导师:" << p->name << endl;
				cout << "   " << "学号" << " " << "姓名" << " " << "年龄" << " " << "分数" << endl;
			}
			cout << "    " << p->stu[i].sid << "   " << p->stu[i].name << "  " << p->stu[i].age << "  " << p->stu[i].score << endl;
		}
	}
	else {
		for (int i = 0; i < 3; i++) {
			if (i == 0) {
				cout << "\t" << "导师:" << p->name << endl;
				cout << "   " << "学号" << " " << "姓名" << " " << "年龄" << " " << "分数" << endl;
			}
			cout << "    " << p->stu[i].sid << "   " << p->stu[i].name << "  " << p->stu[i].age << "  " << p->stu[i].score << endl;
		}
	}
}
若想在函数中使用->运算符,需要用if语句,因为p并没有区分1,2,3...
int main() {
	tarray[0].name = "熊大";
	tarray[0].stu[0].sid = 1;
	tarray[0].stu[0].name = "熊二";
	tarray[0].stu[0].age = 19;
	tarray[0].stu[0].score = 60;
	tarray[0].stu[1].sid = 2;
	tarray[0].stu[1].name = "张三";
	tarray[0].stu[1].age = 20;
	tarray[0].stu[1].score = 61;
	tarray[0].stu[2].sid = 3;
	tarray[0].stu[2].name = "李四";
	tarray[0].stu[2].age = 21;
	tarray[0].stu[2].score = 62;
	tarray[1].name = "王五";
	tarray[1].stu[0].sid = 4;
	tarray[1].stu[0].name = "赵六";
	tarray[1].stu[0].age = 23;
	tarray[1].stu[0].score = 63;
	tarray[1].stu[1].sid = 5;
	tarray[1].stu[1].name = "柳七";
	tarray[1].stu[1].age = 24;
	tarray[1].stu[1].score = 64;
	tarray[1].stu[2].sid = 6;
	tarray[1].stu[2].name = "叶八";
	tarray[1].stu[2].age = 25;
	tarray[1].stu[2].score = 65;
	int length = sizeof(tarray[0].stu) / sizeof(tarray[0].stu[0]);
	for (int i = 0; i < 2; i++) {
		printstruct(&tarray[i], length);
	}
	return 0;
}

额外知识:

1. "\t"为水平制表符,可以生成八个空格

2.vs使用技巧

当我们将很多个相同的变量更换为其他时,可以选中其中一个需要被更换的变量按Ctrl+F,这时会弹出一个框,如下图:


 此时我们点击左边的小箭头展开得到:


 最后在第二条输入框中输入我们用于替换的内容即可

案例2

设计一个英雄的结构体,包括成员姓名,年龄,性别;创建结构体数组,数组中存放5名英雄。通过冒泡排序的算法,将数组中的英雄按照年龄进行升序排序,最终打印排序后的结果

完整代码如下:

#include<iostream>
using namespace std;

struct hero {
	string name;
	string gender;
	int age;
};

void bubblesort(hero*p,int len) {
	for (int i = 0; i < len - 1; i++) {
		for (int j = 0; j < len - i - 1; j++) {
			//if (p->h[j].age > p->h[j + 1].age) {//这个操作有问题,跟前面的区别就是,p所指向的结构体并没有h这个属性
			if (p[j].age > p[j + 1].age) {
				hero temp = p[j];
				p[j] = p[j + 1];
				p[j + 1] = temp;
			}
		}
	}

}

void printarray(hero* h, int len) {
	for (int i = 0; i < len; i++) {
		cout << h[i].name << h[i].gender << h[i].age << endl;
	}
}

int main() {
	hero h[5];
	h[0].name="刘备";
	h[0].gender="男";
	h[0].age=55;
	h[1].name = "关羽";
	h[1].gender = "男";
	h[1].age = 50;
	h[2].name = "钢铁侠";
	h[2].gender = "男";
	h[2].age = 60;
	h[3].name = "神奇女侠";
	h[3].gender = "女";
	h[3].age = 500000;
	h[4].name = "绿巨人";
	h[4].gender = "不明";
	h[4].age = 10;
	bubblesort(h, 5);//地址传递传递数组时要传递数组的首地址,直接在参数列表里面写数组名即可
	printarray(h, 5);
	return 0;
}

注意:交换两个指针交换的是这两个指针的指向,但指针指向的值并没有发生改变

通讯录管理系统


系统中需要实现的功能如下:
添加联系人:向通讯录中添加新人,信息包括(姓名、性别、年龄、联系电话、家庭住址)最多记录1000人
显示联系人:显示通讯录中所有联系人信息
删除联系人:按照姓名进行删除指定联系人
查找联系人:按照姓名查看指定联系人信息
修改联系人:按照姓名重新修改指定联系人
清空联系人:清空通讯录中所有信息
退出通讯录:退出当前所使用的通讯录

显然,根据这个系统的要求,我们需要利用函数来分模块实现上面的功能,总共需要写七个函数,分别是菜单展示函数,添加,显示,删除,查找,修改,清空函数,最后退出利用return即可。上面的功能我们可以使用前面学到的switch语句实现

故编程思路为:1.先定义结构体和结构体变量

2.编写菜单展示函数

3.先写出switch语句的大致框架,在case 7中加入return语句以退出程序

4.编写添加函数

5.编写显示函数

6.编写删除函数

7.编写查找函数

8.编写修改函数

9.编写清空函数

并且每编写完一个函数都需要测试这个函数是否能够如期运行

下面为项目的具体实现过程(不展示测试过程和结果):

1.

struct contracts {
	string name;
	string gender;
	int age;
	int phonr;
	string location;
};

contracts c[1000];

2.

void printcaidan() {
	cout << "************************************" << endl;
	cout << "************" <<"1.添加联系人" << "************" << endl;
	cout << "************" <<"2.显示联系人"<< "************" << endl;
	cout << "************" <<"3.删除联系人"<< "************" << endl;
	cout << "************" <<"4.查找联系人"<< "************" << endl;
	cout << "************" <<"5.修改联系人" <<"************" << endl;
	cout << "************" <<"6.清空联系人"<< "************" << endl;
	cout << "************" <<"7.退出通讯录"<< "************" << endl;
	cout << "************************************" << endl;
}

3.

nt main() {
	printcaidan();
	while (true) {
		int xuanze = 0;
		cin >> xuanze;
		switch (xuanze) {
		case 1:
			break;
		case 2:
			break;
		case 3:
			break;
		case 4:
			break;
		case 5:
			break;
		case 6:
			break;
		case 7:
			return 0;
		default:
			break;
		}
	}
	return 0;
}

到这里大概的框架我们已经搭建好了,接下来就是去实现具体得功能,这也是编程的四思路之一,先构建整体的框架,再向框架中填充代码实现具体的功能。

4.

在实现添加联系人函数功能时,会出现一个问题,那就是我们只能将联系人添加到结构体数组的具体元素上,而不能将它添加到数组的末尾,那么如何解决这个问题呢?这时候我们可以引入一个计数器,用来计算通讯录中目前所存储的联系人的个数。

int counter = 0;

void addc() {
	cin >> c[counter].name >> c[counter].gender >> c[counter].age >> c[counter].phone >> c[counter].location;
	counter += 1;
}

5.

在显示函数中,我们首先要判断数组是否为空,这时候就用到了前面的计数器

void showc() {
	if (counter==0) {
		cout << "联系人为空" << endl;
	}
	else {
		for (int i = 0; i < counter; i++) {
			cout << c[i].name << c[i].gender << c[i].age << c[i].phone << c[i].location << endl;
		}
	}
}

6.

接下来我们实现删除函数,在c++中,删除数组中的元素实际上是将该元素后面的元素提前一位,将该元素覆盖,从而达到删除的效果,例如:

int arr[] = { 1,2,3,4 };
int shuzi_to_delete;
cin >> shuzi_to_delete;
for (int i = 0; i < sizeof(arr) / sizeof(arr[0]); i++) {
	if (arr[i] == shuzi_to_delete) {
		for (int j = i; j < sizeof(arr) / sizeof(arr[0]) - 1; j++) {
			arr[j] = arr[j + 1];
		}
	}
}

我们删除函数代码就是在此基础上增加一个判断,判断是否存在该联系人

void deletec() {
	string name_to_delete;
	cin >> name_to_delete;
	bool found = false;
	for (int i = 0; i < counter; i++) {
		if (c[i].name == name_to_delete) {
			for (int j = i; j < counter - 1; j++) {
				c[j] = c[j + 1];
			}
			found = true;
			counter -= 1;
		}
	}
	if (found) {
		cout << "删除成功" << endl;
	}
	else {
		cout << "未找到该联系人" << endl;
	}
}

7.

查找函数中我们依旧跟前面一样用bool found作为辅助判断是否查找到联系人

void seekc() {
	string name_to_seek;
	cin >> name_to_seek;
	bool found = false;
	for (int i = 0; i < counter; i++) {
		if (c[i].name == name_to_seek) {
			cout << c[i].name << c[i].gender << c[i].age << c[i].phone << c[i].location << endl;
			found = true;
			break;
		}
	}
	if (not found) {
		cout << "未找到该联系人" << endl;
	}
}

8.

修改联系人只需要重新输入一遍即可

void modifyc() {
	string name_to_modify;
	cin >> name_to_modify;
	bool found = false;
	for (int i = 0; i < counter; i++) {
		if (c[i].name == name_to_modify) {
			cin >> c[i].name >> c[i].gender >> c[i].age >>c[i].phone >> c[i].location;
			found = true;
			break;
		}
	}
	if (found) {
		cout << "修改成功" << endl;
	}
	else {
		cout << "未找到该联系人" << endl;
	}
}

9.

实现清空函数时还需要加入一个if确认语句,确认用户是否确实要清空通讯录

void clearc() {
	string confirm;
	cout << "是否确认清空?(yes or no)" << endl;
	cin >> confirm;
	if (confirm == "yes") {
		for (int i = 0; i < 1000; i++) {
			c[i] = {};
		}
		counter = 0;
		cout << "清空成功" << endl;
	}
	else {
	}
}

到此,我们整个通讯录管理系统就大致完成了,接下来我们需要完善细节,在case中我们每次调用完函数之后都要重新显示一遍功能菜单,并且为了不显得输入框复杂,我们需要写一个清屏功能,具体代码如下:

case 1:
	addc();
	system("pause");//请按任意键继续
	system("cls");//清屏功能
	break;

最后是加上提示语句,最终完整代码如下:

#include<iostream>
#include<string>
using namespace std;

struct contracts {
	string name;
	string gender;
	int age;
	int phone;
	string location;
};

contracts c[1000];

int counter = 0;

void printcaidan() {
	cout << "************************************" << endl;
	cout << "************" <<"1.添加联系人" << "************" << endl;
	cout << "************" <<"2.显示联系人"<< "************" << endl;
	cout << "************" <<"3.删除联系人"<< "************" << endl;
	cout << "************" <<"4.查找联系人"<< "************" << endl;
	cout << "************" <<"5.修改联系人" <<"************" << endl;
	cout << "************" <<"6.清空联系人"<< "************" << endl;
	cout << "************" <<"7.退出通讯录"<< "************" << endl;
	cout << "************************************" << endl;
}

void addc() {
	cout << "请分别输入姓名,性别,年龄,电话号码,家庭地址(用空格隔开)" << endl;
	cin >> c[counter].name >> c[counter].gender >> c[counter].age >> c[counter].phone >> c[counter].location;
	cout << "添加成功" << endl;
	counter += 1;
}

void showc() {
	if (counter==0) {
		cout << "联系人为空" << endl;
	}
	else {
		for (int i = 0; i < counter; i++) {
			cout<<i+1<<"  " << "姓名:"<<c[i].name<<" 性别:" << c[i].gender << " 年龄:"<<c[i].age <<" 电话号码:"<< c[i].phone <<" 家庭地址:" <<c[i].location << endl;
		}
	}
}

void deletec() {
	string name_to_delete;
	cout << "请输入要删除的联系人姓名:";
	cin >> name_to_delete;
	bool found = false;
	for (int i = 0; i < counter; i++) {
		if (c[i].name == name_to_delete) {
			for (int j = i; j < counter - 1; j++) {
				c[j] = c[j + 1];
			}
			found = true;
			counter -= 1;
		}
	}
	if (found) {
		cout << "删除成功" << endl;
	}
	else {
		cout << "未找到该联系人" << endl;
	}
}

void seekc() {
	string name_to_seek;
	cout << "请输入要查找的联系人姓名:";
	cin >> name_to_seek;
	bool found = false;
	for (int i = 0; i < counter; i++) {
		if (c[i].name == name_to_seek) {
			cout << i + 1 << "  " << "姓名:" << c[i].name << " 性别:" << c[i].gender << " 年龄:" << c[i].age << " 电话号码:" << c[i].phone << " 家庭地址:" << c[i].location << endl;
			found = true;
			break;
		}
	}
	if (not found) {
		cout << "未找到该联系人" << endl;
	}
}

void modifyc() {
	string name_to_modify;
	cout << "请输入要修改的联系人姓名:";
	cin >> name_to_modify;
	bool found = false;
	for (int i = 0; i < counter; i++) {
		if (c[i].name == name_to_modify) {
			cout << "请分别输入姓名,性别,年龄,电话号码,家庭地址(用空格隔开)" << endl;
			cin >> c[i].name >> c[i].gender >> c[i].age >>c[i].phone >> c[i].location;
			found = true;
			break;
		}
	}
	if (found) {
		cout << "修改成功" << endl;
	}
	else {
		cout << "未找到该联系人" << endl;
	}
}

void clearc() {
	string confirm;
	cout << "是否确认清空?(yes or no)" << endl;
	cin >> confirm;
	if (confirm == "yes") {
		for (int i = 0; i < 1000; i++) {
			c[i] = {};
		}
		counter = 0;
		cout << "清空成功" << endl;
	}
	else {
	}
}

int main() {
	while (true) {
		printcaidan();
		int xuanze = 0;
		cin >> xuanze;
		switch (xuanze) {
		case 1:
			addc();
			system("pause");
			system("cls");
			break;
		case 2:
			showc();
			system("pause");
			system("cls");
			break;
		case 3:
			deletec();
			system("pause");
			system("cls");
			break;
		case 4:
			seekc();
			system("pause");
			system("cls");
			break;
		case 5:
			modifyc();
			system("pause");
			system("cls");
			break;
		case 6:
			clearc();
			system("pause");
			system("cls");
			break;
		case 7:
			return 0;
		default:
			cout << "输入有误,请重新输入" << endl;
			system("pause");
			system("cls");
			break;
		}
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值