C++学习笔记(八)

在完成对C语言的学习后,我最近开始了对C++和Java的学习,目前跟着视频学习了一些语法,也跟着敲了一些代码,有了一定的掌握程度。现在将跟着视频做的笔记进行整理。本篇博客是整理C++知识点的第八篇博客。

本篇博客介绍了关于结构体的使用,并实现了一个通讯录管理系统。

本系列博客所有C++代码都在Visual Studio 2022环境下编译运行。程序为64位。

目录

结构体的使用

结构体嵌套

结构体作为函数参数

对结构体使用const

结构体案例

通讯录管理系统


结构体的使用

结构体嵌套

一个结构体可以是另一个结构体的成员。

#include<iostream>
#include<string>
using namespace std;
struct student {
	string name;
	int age;
	int score;
};

struct teacher {
	int id;
	string name;
	int age;
	struct student stu;
};
int main(void)
{
	struct teacher tea;
	tea.id = 10011;
	tea.name = "Gilbert";
	tea.age = 50;
	tea.stu.name = "Daniel";
	tea.stu.age = 20;
	tea.stu.score = 55;

	cout << "The teacher's id is " << tea.id << endl;
	cout << "The teacher's name is " << tea.name << endl;
	cout << "The teacher's age is " << tea.age << endl;

	cout << "The student's name is " << tea.stu.name << endl;
	cout << "The student's age is " << tea.stu.age << endl;
	cout << "The student's score is " << tea.stu.score << endl;
}

程序中定义了一种叫student的结构体,还有一种叫teacher的结构体。teacher结构体中,有两个int类型的成员id和age,一个string类型成员name,还有一个student类型的结构体成员stu。

程序的输出是:

The teacher's id is 10011
The teacher's name is Gilbert
The teacher's age is 50
The student's name is Daniel
The student's age is 20
The student's score is 55

结构体作为函数参数

结构体可以作为函数参数,可以进行值传递,也可以进行址传递。值传递无法修改原结构体的成员,但是址传递可以。

值传递占用空间较大,址传递占用空间小。

#include<iostream>
#include<string>
using namespace std;
struct student {
	string name;
	int age;
	int score;
};
void print1(struct student stu);
void print2(struct student* p);
int main(void)
{
	struct student stu = { "Betty", 18, 81 };
	print1(stu);
	print2(&stu);
	return 0;
}
void print1(struct student stu)
{
	cout << "The student's name is " << stu.name << endl;
	cout << "The student's age is " << stu.age << endl;
	cout << "The student's score is " << stu.score << endl;
}
void print2(struct student* p)
{
	cout << "The student's name is " << p -> name << endl;
	cout << "The student's age is " << p -> age << endl;
	cout << "The student's score is " << p -> score << endl;
}

函数print1接受一个student类型结构体,函数print2函数接受一个指向student类型结构体的指针。

程序的输出是:

The student's name is Betty
The student's age is 18
The student's score is 81
The student's name is Betty
The student's age is 18
The student's score is 81

对结构体使用const

结构体前加const防止被修改。向函数传递结构体指针时,形参用const修饰可以防止修改原数据。

#include<iostream>
#include<string>
using namespace std;
struct student {
	string name;
	int age;
	int score;
};
void print(const struct student* p);

int main(void)
{
	struct student stu = { "Violet", 20 , 90 };
	print(&stu);
	return 0;
}
void print(const struct student* p)
{
	cout << "The student's name is " << p->name << endl;
	cout << "The student's age is " << p->age << endl;
	cout << "The student's score is " << p->score << endl;
}

程序的输出是:

The student's name is Violet
The student's age is 20
The student's score is 90

结构体案例

假设有3个老师,每个老师辅导5位学生。要求输出学生的编号,分数,老师的编号。

#include<iostream>
#include<ctime>
#include<string>
using namespace std;
struct student {
	string name;
	int score;
};
struct teacher {
	string name;
	struct student stu[5];
};
void give(struct teacher* teachers, int length);
void print(struct teacher* teachers, int length);
int main(void)
{
	srand((unsigned int)time(NULL));
	struct teacher teachers[3];
	int length = sizeof(teachers) / sizeof(teachers[0]);
	give(teachers, length);
	print(teachers, length);
	return 0;
}

void give(struct teacher* teachers, int length)
{
	int i, j;
	for (i = 0; i < length; i += 1) {
		(*(teachers + i)).name = "Teacher";
		(*(teachers + i)).name += (char)(i + 65);
		for (j = 0; j < 5; j += 1) {
			(*(teachers + i)).stu[j].name = "Student";
			(*(teachers + i)).stu[j].name += (char)(j + 65);
			(*(teachers + i)).stu[j].score = rand() % 51 + 50;
		}
	}
}
void print(struct teacher* teachers, int length)
{
	int i, j;
	for (i = 0; i < length; i += 1) {
		cout << "The teacher's name is " << (*(teachers + i)).name << endl;
		for (j = 0; j < 5; j += 1) {
			cout << "The student's name is " << (*(teachers + i)).stu[j].name << endl;
			cout << "The score is " << (*(teachers + i)).stu[j].score << endl;
		}
	}
}

程序定义了student结构体和teacher结构体,teacher类结构体有一个有5个成员的student类型结构体数组成员。在main函数中定义了有3个成员的teacher类型结构体数组。随后将此数组和长度作为参数传递给give函数和print函数。

give函数用循环的方式对数组进行赋值。老师和学生的名字用拼接的方式生成,学生分数随机产生。print函数对数组遍历输出。

由于输出过长,此处不再显示。

通讯录管理系统

截至目前所学的知识和C语言大同小异,这一部分也涵盖了C语言的大部分语法。利用目前掌握的知识,可以简单地写一个管理系统。比如通讯录管理系统,学生管理系统等。下面是一个简单的通讯录管理系统,可以实现添加联系人,显示联系人,删除联系人,查找联系人,修改联系人,清空联系人功能。

system("pause")使得屏幕上输出按任意键继续,按任意键后继续向后执行。

system("cls")清空屏幕。

下面是一个实现代码,考虑到功能比较简单,也没有分文件编写,所有功能都在一个源文件上(写得差勿喷)。

#include<iostream>
#include<string>
using namespace std;
#define MAX 1000
struct people {
	string name;
	int sex;
	int age;
	string phone;
	string address;
};
struct book {
	struct people People[MAX];
	int number;
};
void showmenu(void);
void add(struct book* peoplebook);
void print(struct book* peoplebook);
void deletemember(struct book* peoplebook);
int search(struct book* peoplebook, string name);
void find(struct book* peoplebook);
void revise(struct book* peoplebook);
void blank(struct book* peoplebook);

int main(void)
{
	struct book peoplebook;
	peoplebook.number = 0;
	int choice;
	while (true) {
		showmenu();
		cout << "Please enter your choice: ";
		cin >> choice;
		switch (choice) {
		case 1:
			add(&peoplebook);
			break;
		case 2:
			print(&peoplebook);
			break;

		case 3:
			deletemember(&peoplebook);
			break;
		case 4:
			find(&peoplebook);
			break;
		case 5:
			revise(&peoplebook);
			break;
		case 6:
			blank(&peoplebook);
			break;
		case 0:
			cout << "Exit the system" << endl;
			break;

		default:
			cout << "Input error" << endl;
			break;
		}
		if (choice == 0) {
			break;
		}
	}
	return 0;
}
void showmenu(void)
{
	cout << "********************" << endl;
	cout << "***** 1. add *******" << endl;
	cout << "***** 2. show ******" << endl;
	cout << "***** 3. delete ****" << endl;
	cout << "***** 4. search ****" << endl;
	cout << "***** 5. revise ****" << endl;
	cout << "***** 6. blank *****" << endl;
	cout << "***** 0. exit ******" << endl;
	cout << "********************" << endl;
}
void add(struct book* peoplebook)
{
	if (peoplebook->number == MAX) {
		cout << "It is full" << endl;
		system("pause");
		system("cls");
		return;
	}

	cout << "Please enter the name: " << endl;
	cin >> peoplebook->People[peoplebook->number].name;
	cout << "Please enter the sex(1: male  2:female): " << endl;
	int sex = 0;

	while (true) {
		cin >> sex;
		if (sex == 1 || sex == 2) {
			peoplebook->People[peoplebook->number].sex;
			break;
		}
		cout << "Please enter 1(male) or 2(female): " << endl;
	}

	cout << "Please enter the age: " << endl;
	cin >> peoplebook->People[peoplebook->number].age;
	cout << "Please enter the phone: " << endl;
	cin >> peoplebook->People[peoplebook->number].phone;
	cout << "Please enter the address: " << endl;
	cin >> peoplebook->People[peoplebook->number].address;

	peoplebook->number += 1;
	system("pause");
	system("cls");
}

void print(struct book* peoplebook)
{
	if (peoplebook->number == 0) {
		cout << "It is blank" << endl;
		system("pause");
		system("cls");
		return;
	}

	int i;
	for (i = 0; i < peoplebook->number; i += 1) {
		cout << "The name is " << peoplebook->People[i].name << "\t";
		if (peoplebook->People[i].sex == 1) {
			cout << "The sex is male\t";
		}
		else if (peoplebook->People[i].sex == 2) {
			cout << "The sex is female\t";
		}

		cout << "The age is " << peoplebook->People[i].age << "\t";
		cout << "The phone is " << peoplebook->People[i].phone << "\t";
		cout << "The address is " << peoplebook->People[i].address << "\t";
		cout << endl;
	}

	system("pause");
	system("cls");
}

void deletemember(struct book* peoplebook)
{
	string name;
	cout << "Please enter the member" << endl;
	cin >> name;
	int i = search(peoplebook, name);

	if (i == -1) {
		cout << "It doesn't exist" << endl;
		system("pause");
		system("cls");
		return;
	}

	for (; i < peoplebook->number - 1; i += 1) {
		peoplebook->People[i] = peoplebook->People[i + 1];
	}
	peoplebook->number -= 1;
	cout << "Done" << endl;
	system("pause");
	system("cls");
}
int search(struct book* peoplebook, string name)
{
	int i;
	for (i = 0; i < peoplebook->number; i += 1) {
		if (peoplebook->People[i].name == name) {
			return i;
		}
	}
	return -1;
}
void find(struct book* peoplebook) 
{
	string name;
	cout << "Please enter the name" << endl;
	cin >> name;

	int i;
	i = search(peoplebook, name);
	if (i == -1) {
		cout << "It doesn't exist" << endl;
		system("pause");
		system("cls");
		return;
	}

	cout << "The name is " << peoplebook->People[i].name << "\t";
	if (peoplebook->People[i].sex == 1) {
		cout << "The sex is male\t";
	}
	else if (peoplebook->People[i].sex == 2) {
		cout << "The sex is female\t";
	}

	cout << "The age is " << peoplebook->People[i].age << "\t";
	cout << "The phone is " << peoplebook->People[i].phone << "\t";
	cout << "The address is " << peoplebook->People[i].address << "\t";
	cout << endl;

	system("pause");
	system("cls");
	return;
}
void revise(struct book* peoplebook)
{
	string name;
	cout << "Please enter the name" << endl;
	cin >> name;

	int i;
	i = search(peoplebook, name);
	if (i == -1) {
		cout << "It doesn't exist" << endl;
		system("pause");
		system("cls");
		return;
	}

	cout << "Please enter the name: " << endl;
	cin >> peoplebook->People[i].name;
	cout << "Please enter the sex(1: male  2:female): " << endl;
	int sex = 0;
	while (true) {
		cin >> sex;
		if (sex == 1 || sex == 2) {
			peoplebook->People[i].sex;
			break;
		}
		cout << "Please enter 1(male) or 2(female): " << endl;
	}

	cout << "Please enter the age: " << endl;
	cin >> peoplebook->People[i].age;
	cout << "Please enter the phone: " << endl;
	cin >> peoplebook->People[i].phone;
	cout << "Please enter the address: " << endl;
	cin >> peoplebook->People[i].address;
	system("pause");
	system("cls");
	return;
}

void blank(struct book* peoplebook)
{
	int flag = 10;
	while (true) {
		cout << "Do you want to clear the information?" << endl;
		cout << "1.yes 0.no";
		cin >> flag;

		if (flag == 0 || flag == 1) {
			break;
		}
		cout << "Please enter 1 to yes 2 to no" << endl;
	}
	if (flag == 0) {
		peoplebook->number = 0;
		cout << "Done" << endl;
	}
	system("pause");
	system("cls");
	return;
}

此代码相对比较简单,这里不再解释。

目前前面C++的基础部分(这部分是面向过程,也可以说是C语言的复习)结束了。

接下来将接触C++面向对象的部分,并将接触到类。这部分是C++的核心。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值