c++基础

目录

一、循环结构

1.while循环

2. do while语句

3. for 循环

4. 嵌套循环

二、跳转语句

1. break语句

2. continue语句

3. goto语句

三、数组

1. 一维数组

2.二维数组

四、函数

1.函数的作用

2. 函数的定义

3.函数的调用

4. 值传递

5. 函数的常见样式

6. 函数的声明

7. 函数的分文件编写

五、指针

1. 指针的定义和使用

2. 空指针和野指针

3.const 修饰指针

4. 指针和数组

5.指针和函数

7. 指针、数组、函数

六、结构体

1. 基本概念

2. 结构体定义和使用

3. 结构体数组

4. 结构体指针

5. 结构体嵌套结构体

6. 结构体做函数参数

7. 结构体中const使用场景

8. 结构体案例

七、通讯录管理系统

1.系统需求

2.创建项目

3.菜单功能

4. 退出功能

5. 添加联系人

6. 显示联系人

7.删除联系人

8. 查找联系人

9. 修改联系人

10.清空联系人


一、循环结构

1.while循环

()中填的是循环条件 避免死循环出现

#include<iostream>
using namespace std;
int main(){
	//输出0~9
	int num = 0;
	while (num<10) {
		cout << num << endl;
		num++;
	}
	system("pause");
    return 0;
}
//结果为0~9的一列

例子:猜数字

#include<iostream>
using namespace std;
//time系统头文件包含
#include<ctime>
int main(){
	//添加随机种子,作用:利用当前系统时间生成随机数,防止每次随机数都一样
	srand((unsigned int)time(NULL));
	int num=rand() % 100 + 1;//生成一个1~100的随机数
	//cout << num << endl;
	int val = 0;//玩家输入视频
	while (1) {
		cin >> val;
		if (val > num)
		{
			cout << "输入过大" << endl;
		}
		else if (val < num)
		{
			cout << "输入过小" << endl;
		}
		else {
			cout << "恭喜你猜对了" << endl;
			break;
		}
	}	
	system("pause");
    return 0;
}

2. do while语句

(1)do{循环语句}while(循环条件)

#include<iostream>
using namespace std;
//time系统头文件包含
#include<ctime>
int main(){  //输出0~9
	int num = 0;
	do {
		cout << num << endl;
		num++;
	} while (num < 10);
	system("pause");
    return 0;
}

(2) 例子:水仙花数(是一个三位数,每位数的三次方加起来就是那个三位数)

#include<iostream>
using namespace std;
int main() {
	//输入三位数
	int num = 100;
	do {
		//找出水仙花数
		int a = 0;//个位
		int b = 0;//十位
		int c = 0;//百位
		a = num % 10;
		b = num / 10 % 10;
		c = num / 100;
		if (a*a*a + b*b*b + c*c*c == num) {
			cout << num << endl;
		}	
		num++;
	}
	while (num < 1000);
	system("pause");
	return 0;
}

3. for 循环

(1)for(起始表达式;条件表达式;末尾循环体){循环语句;}

#include<iostream>
using namespace std;
int main() {
	//输入三位数
	for (int i = 0; i < 10; i++) {
		cout << i << endl;
	}
	system("pause");
	return 0;
}

(2)例:敲桌子(从1-100,包含7或是7的倍数,不输出)

#include<iostream>
using namespace std;
int main() {
	//输入三位数
	for (int i = 1; i <= 100; i++) {
		if (i%7==0||i%10==7||i/10==7) {   //7的倍数/各位含7/十位含7
			cout << "敲桌子" << endl;
		}
		else {
			cout << i << endl;
		}
	}
	system("pause");
	return 0;
}

4. 嵌套循环

(1)例子:打印星图

#include<iostream>
using namespace std;
int main() {
	//外层执行一次,内层执行一周
	for (int i = 0; i < 10; i++) {
		for (int j = 0; j < 10; j++) {
			cout << "* ";
		}
		cout << endl;
	}
	system("pause");
	return 0;
}

(2)例:99乘法表

#include<iostream>
using namespace std;
int main() {
	for (int i = 1; i <= 9; i++) { //i为行
		//cout << i << endl;
		for (int j = 1; j <= i; j++) {//j为列
			cout << j << "*" << i << "=" << j*i << "  ";
		}
		cout << endl;
	}
	system("pause");
	return 0;
}

二、跳转语句

1. break语句

#include<iostream>
using namespace std;
int main() {
	//break的使用时机
	//1.出现在swich语句中
	cout << "请选择题目难度" << endl;
	cout << "1.普通" << endl;
	cout << "2.中等" << endl;
	cout << "3.困难" << endl;
	int select = 0;//创建选择结果的变量
	cin >> select;
	switch (select)
	{
	case 1:
		cout << "您选择的是普通难度" << endl;
		break;
	case 2:
		cout << "您选择的是中等难度" << endl;
		break;
	case 3:
		cout << "您选择的是困难难度" << endl;
		break;
	}
	//2.出现在循环语句中
	for (int i = 0; i < 10; i++)
	{
		if (i == 5) {//输到5就结束
			break;
		}
		cout << i << endl;
	}
	//3.出现在嵌套循环语句中
	for (int i = 0; i < 10; i++)
	{
		for (int j = 0; j < 10; j++)
		{
			if (j == 5)
			{
				break;
			}
			cout << "*  ";
		}
		cout << endl;
	}
	system("pause");
	return 0;
}

2. continue语句

(1)作用:到continue这一步,不执行下面语句,会重新开启下一次循环

#include<iostream>
using namespace std;
int main() {
	for (int i = 0; i <= 100; i++) {
		if (i % 2 == 0)//不输出偶数,只输出奇数
		{
			continue;
		}
		cout << i << endl;
	}
	system("pause");
	return 0;
}

3. goto语句

(1)可以无条件跳转语句,goto 标记  执行到标记时,会跳转到标记的位置

#include<iostream>
using namespace std;
int main() {
	cout << "1.*** "<< endl;
	cout << "2.*** " << endl;
	goto FLAG;
	cout << "3.*** " << endl;
	FLAG:
	cout << "4.*** " << endl;
	system("pause");
	return 0;
}

三、数组

1. 一维数组

(1)定义方式

数据类型 数组名[数组长度];

数据类型 数组名[数组长度]={值1,值2,...};

数据类型 数组名[ ]={值1,值2,...};

可通过下标来访问数组,下标从0开始  每个元素类型相同

#include<iostream>
using namespace std;
int main() {
	int arr[5];//数组下标从0开始
	arr[0] = 10;
	arr[1] = 20;
	arr[2] = 30;
	arr[3] = 40;
	arr[4] = 50;
	cout << arr[0] << endl;
	cout << arr[1] << endl;
	cout << arr[2] << endl;
	cout << arr[3] << endl;
	cout << arr[4] << endl;
	int arr2[5] = { 10,20,30 };//未写的自动用0来补充
	for (int i = 0; i < 5; i++) {
		cout << arr2[i] << endl;
	}	
	system("pause");
	return 0;
}

(2)一维数组数组名

可统计在内存中的长度;在内存中的首地址

#include<iostream>
using namespace std;
int main() {
	//计算占用内存大小
	int arr[10] = { 1,2,3,4,5,6,7,8,9,10 };
	cout << "整个数组占用内存空间为:" << sizeof(arr) << endl;
	cout << "每个元素占用内存空间为:" << sizeof(arr[0]) << endl;
	cout << "数组中元素长度为:" << sizeof(arr)/sizeof(arr[0]) << endl;
	cout << "数组的首地址为:" << (int)arr<< endl;
	cout << "数组的中第一个元素地址为:" << (int)&arr[0] << endl;
	//数组名是常量,不可以进行赋值操作
	system("pause");
	return 0;
}

(3)例:5只小猪称体重给,打印最重的

#include<iostream>
using namespace std;
int main() {
	//计算占用内存大小
	int arr[5] = {100,500,400,300,200};
	int max = 0;
	for (int i = 0; i < 5 ; i++)
	{
		if (arr[i] > max)
		{
			max = arr[i];
		}
	}
	cout << "最重的小猪体重为:" <<max<< endl;
	system("pause");
	return 0;
}

(4)例子:数组元素逆置

#include<iostream>
using namespace std;
int main() {
	//创建数组
	int arr[5] = { 4,5,2,3,1 };
	cout << "数组逆置前:" << endl;
	for (int i = 0; i < 5; i++)
	{
		cout << arr[i];
	}
	cout << endl;
	//逆置
	int start = 0;//起始位置
	int end = sizeof(arr) / sizeof(arr[0]) - 1;//结束下标
	while (start < end) {
		//互换
		int temp = arr[start];
		arr[start] = arr[end];
		arr[end] = temp;
		//下标更新
		start++;
		end--;
	}
	cout << "数组逆置后的结果:" << endl;
	for (int j = 0; j < 5; j++)
	{
		cout << arr[j] ;
	}
	cout << endl;
	system("pause");
	return 0;
}

(5)冒泡排序

#include<iostream>
using namespace std;
int main() {
	//创建数组
	int arr[9] = { 7,2,5,3,6,9,5,8,4};
	cout << "排序前:" << endl;
	for (int i = 0; i < 9; i++)
	{
		cout << arr[i];
	}
	cout << endl;
	for (int i = 0; i < 9 - 1; i++) {//i表示轮数
		for (int j = 0; j < 9 - i - 1; j++) {//j表示每轮中的排序次数
			if(arr[j] > arr[j + 1]){
				int temp = arr[j];
				arr[j ] = arr[j+1];
				arr[j +1] = temp;
			}
		}
	}
	cout << "排序后的结果:" << endl;
	for (int m = 0; m< 9; m++)
	{
		cout << arr[m] ;
	}
	cout << endl;
	system("pause");
	return 0;
}

2.二维数组

(1)定义方式

 (2)二维数组名称用途

#include<iostream>
using namespace std;
int main() {
	//1.查看占用内存空间大小
	int arr[2][3] = {
		{1,2,3},
		{4,5,6}
	};
	cout << "二维数组所占空间大小为:" << sizeof(arr) << endl;
	cout << "二维数组第一行占用的内存为:" << sizeof(arr[0]) << endl;
	cout << "二维数组第一个元素占用的内存为:" << sizeof(arr[0][0]) << endl;
	cout << "二维数组行数为:" << sizeof(arr)/sizeof(arr[0]) << endl;
	cout << "二维数组列数为:" << sizeof(arr[0]) / sizeof(arr[0][0]) << endl;
	//2.查看二维数组首地址
	cout << "二维数组的首地址为:" << (int)arr<< endl;
	cout << "二维数组的第一行首地址为:" << (int)arr[0] << endl;	
	cout << "二维数组的第二行首地址为:" << (int)arr[1] << endl;
	cout << "二维数组的第一个元素首地址为:" << (int)&arr[0][0] << endl;
	system("pause");
	return 0;
}

(3) 例:考试成绩统计

#include<iostream>
using namespace std;
#include<string>
int main() {
	//创建二维数组
	int scores[3][3]
	{
		{100,100,100},
		{90,50,100},
		{60,70,80}
	};
	string name[3] = { "张三","李四","王五" };
	//统计每个人的分数
	for (int i = 0; i < 3; i++) 
	{
		int sum = 0;
		for (int j = 0; j < 3; j++) 
		{
			sum += scores[i][j];  
			//cout << scores[i][j] << " ";
		}
		cout << name[i]<<"的总分为:"<<sum<<endl;
	}
	system("pause");
	return 0;
}

四、函数

1.函数的作用

将常用的代码封装起来,避免重复操作

2. 函数的定义

返回值类型 函数名 参数表列  函数体语句  return表达式

返回值类型  函数名(参数表列){

函数体语句;

return表达式;

}

int add(int num1, int num2)
{
	int sum = num1 + num2;
	return sum;
}

3.函数的调用

函数名(参数)

#include<iostream>
using namespace std;
#include<string>
//定义加法函数
int add(int num1, int num2)//num1 num2 没有真实数据,只是形参
{
	int sum = num1 + num2;
	return sum;
}
int main() {
	int a = 10;
	int b = 20;
	//当调用函数时,实参的值会传递给形参
	int c = add(a, b);//a 和b为实际参数
	cout << "c="<< c << endl;
	system("pause");
	return 0;
}

4. 值传递

把实参的值传递给形参;值传递时,若形参发生改变,并不会影响实参

#include<iostream>
using namespace std;
#include<string>
//定义交换函数,不需要返回值void
void swap(int num1,int num2)
{
	cout << "交换前" << endl;
	cout << "num1=" << num1 << endl;
	cout << "num2=" << num2 << endl;
	int temp = num1;
	num1 = num2;
	num2 = temp;
	cout << "交换后" << endl;
	cout << "num1=" << num1 << endl;
	cout << "num2=" << num2 << endl;
}

int main() {
	int a = 10;
	int b = 20;
	//当调用函数时,实参的值会传递给形参
	swap(a, b);//a 和b为实际参数
	system("pause");
	return 0;
}

5. 函数的常见样式

无参无返;有参无返;无参有返;有参有返

#include<iostream>
using namespace std;
//无参无返
void test1()
{
	cout << "This is a test." << endl;
}
//有参无返
void test2(int a) {
	cout << "This is a test2 a=" <<a << endl;
}
//无参有返
int test3()
{
	cout << "This is a test3 ." << endl;
	return 1000;
}
//有参有返
int test4(int a)
{
	cout << "This is a test4.a=" << a << endl;
	return a;
}
int main()
{
	test1();//1
	test2(100);//2
	int num1 = test3();//3
	cout << "num1=" << num1 << endl;
	int num2 = test4(10000);//4
	cout << "num2=" << num2 << endl;

	system("pause");
	return 0;
}

6. 函数的声明

#include<iostream>
using namespace std;
//函数的声明
//比较函数,两个整形的进行比较,并返回较大值
//提前告诉函数的存在,用函数声明
//声明可以写多次,定义只能一次
int max(int a, int b);

int main()
{
	int a = 10;
	int b = 20;
	cout << max(a,b) << endl;

	system("pause");
	return 0;
}
//定义 
int max(int a, int b)
{
	return a > b ? a : b;
}

7. 函数的分文件编写

创建后缀名为.h的头文件    创建后缀名为.cpp的源文件    在头文件中写函数的声明 

在源文件中写函数的定义

(1)主函数

#include<iostream>
using namespace std;
#include "swap.h"
/*void swap(int a, int b);
void swap(int a, int b)
{
	int temp = a;
	a = b;
	b = temp;
	cout << "a=" << a << endl;
	cout << "b=" << b << endl;
}*/
int main()
{
	int a = 10;
	int b = 20;
	swap(a, b);
	system("pause");
	return 0;
}

(2)头文件

#include<iostream>
using namespace std;
//函数声明
void swap(int a, int b);

(3)函数定义源文件

#include "swap.h"
//函数定义
void swap(int a, int b)
{
	int temp = a;
	a = b;
	b = temp;
	cout << "a=" << a << endl;
	cout << "b=" << b << endl;
}

五、指针

1. 指针的定义和使用

#include<iostream>
using namespace std;
int main()
{
	//1.定义指针
	int a = 10;
	//指针定义的语法:数据类型 * 指针变量名
	int *p;
	//让指针记录变量a的地址
	p = &a;            //也可以直接int *p=&a;
	cout << "a的地址为:" << &a << endl;
	cout << "指针p为:" << p << endl;
	//2.使用指针 
	//可通过解引用的方式找到指针指向的内存
	//*p表示解引用,找到指针指向的内存中的数据
	*p = 1000;
	cout << a << endl;
	cout << *p << endl;
	system("pause");
	return 0;
}

2. 空指针和野指针

空指针:指向内存中编号为0的空间    初始化指针变量  空指针指向的内存是不可访问的

#include<iostream>
using namespace std;
int main()
{
	//空指针
	int *p = NULL;
	//0~255是系统占用的
	*p = 100;//错误
	system("pause");
	return 0;
}

野指针:指针变量指向非法的内存空间 

3.const 修饰指针

(1)const修饰指针    常量指针

const int *p=&a    特点:指针的指向可以修改,但指针指向的值不可更改

(2)const修饰常量     指针常量

int *const p=&a    特点:指针的指向不可以改,但指针指向的值可以改

*p=20;  正确

p=&b    错误

(3)const既修饰指针,又修饰常量

const int * const p  特点:指针的指向和指针所指向的值都不可以改

4. 指针和数组

利用指针访问数组中的元素

#include<iostream>
using namespace std;
int main()
{
	int arr[10] = { 1,2,3,4,5,6,7,8,9,10 };
	cout << "第一个元素为:" << arr[0] << endl;
	int *p = arr;//arr 就是首地址
	cout << "利用指针访问数组的第一个元素" << *p << endl;
	p++;//让指针向后偏移四个字节
	cout << "利用指针访问数组的第二个元素" << *p << endl;
	cout << "利用指针遍历数组" << endl;
	int *p2 = arr;
	for (int i = 0; i < 10; i++) {
		cout << *p2 << endl;
		p2++;
	}
	system("pause");
	return 0;
}

5.指针和函数

#include<iostream>
using namespace std;
//实现两个数字交换
void swap01(int a,int b)
{
	int temp = a;
	a = b;
	b = temp;
	cout << "swap01 a=" << a << endl;
	cout << "swap01 b=" << b << endl;
}
void swap02(int *p1,int *p2)
{
	int temp = *p1;
	*p1 = *p2;
	*p2= temp;
}
int main()
{
	//指针和函数
	//1.值传递
	int a = 10;
	int b = 20;
	//swap01(a, b);
	
	//2.地址传递  如果是地址传递,可以修改实参 
	swap02(&a, &b);
	cout << "a=" << a << endl;
	cout << "b=" << b << endl;
	system("pause");
	return 0;
}

7. 指针、数组、函数

封装一个函数,利用冒泡排序,实现对整型数组的升序排序

int arr[10]={4,3,6,9,1,2,10,8,7,5}

#include<iostream>
using namespace std;
//冒泡排序函数(参数一:数组的首地址;参数二:数组长度)
void bubbleSort(int *arr,int len)
{
	for (int i = 0; i < len - 1; i++)
	{
		for (int j = 0; j < len - 1 - i; j++)
		{
			if (arr[j] > arr[j + 1])
			{
				int temp = arr[j];
				arr[j] = arr[j + 1];
				arr[j + 1] = temp;
			}
		}
	}
}
void printArray(int *arr, int len)
{
	for (int i = 0; i < len; i++)
	{
		cout << arr[i] << endl;
	}
}
int main()
{
	//1.创建数组
	int arr[10] = { 4,3,6,9,1,2,10,8,7,5 };
	//数组长度
	int len = sizeof(arr) / sizeof(arr[0]);
	//2.构造函数,冒泡
	bubbleSort(arr, len);
	//3.打印排序后数组
	printArray(arr, len);
	system("pause");
	return 0;
}

把数组传进去要用地址

六、结构体

1. 基本概念

自定义的数据类型,允许用户存储不同的数据类型

2. 结构体定义和使用

struct 结构体名{结构体成员列表}

#include<iostream>
using namespace std;
#include<string>
//1.创建学生数据类型:姓名、年龄、分数
//自定义数据类型  //结构体定义时不可以省略
struct Student
{
	//成员列表
	//姓名
	string name;
	//年龄
	int age;
	//分数
	int score;
}s3;                                   //只有第三种的时候才加s3
//2.通过学生类型创建具体学生

int main()
{
	//2.1 struct Student s1
//struct 关键字可省略
	struct Student s1;
	//给s1属性赋值,通过.访问结构体变量中的属性
	s1.name = "张三";
	s1.age = 18;
	s1.score = 100;
	cout << "姓名:" << s1.name << "  年龄:" << s1.age << "   分数:" << s1.score << endl;
	//2.2 struct Student s2={....}
	struct Student s2={"李四",19,80};
	cout << "姓名:" << s2.name << "  年龄:" << s2.age << "   分数:" << s2.score << endl;

	//2.3 定义结构体时顺便定义结构体变量
	s3.name = "王五";
	s3.age = 20;
	s3.score = 60;
	cout << "姓名:" << s3.name << "  年龄:" << s3.age << "   分数:" << s3.score << endl;
	system("pause");
	return 0;
}

3. 结构体数组

struct 结构体名  数组名[元素个数]={{},{},....{}}

#include<iostream>
using namespace std;
#include<string>
//结构体数组
//1.定义结构体
struct Student
{
	//成员列表
	//姓名
	string name;
	//年龄
	int age;
	//分数
	int score;
};
int main()
{
	//2.创建结构体数组
	struct Student stuArray[3] =
	{
		{"张三",18,100},
		{ "李四",20,60 },
		{ "王五",21,80},
	};
	//3.给结构体数组中的元素赋值
	stuArray[2].name = "赵六";
	stuArray[2].age = 25;
	stuArray[2].score = 68;
	//4.遍历结构体数组
	for (int i = 0; i < 3; i++)
	{
		cout << "姓名:" << stuArray[i].name 
			 << "    年龄:" << stuArray[i].age 
			 << "   分数" << stuArray[i].score << endl;
	}
	system("pause");
	return 0;
}

4. 结构体指针

利用->可以通过结构体指针访问结构体属性

#include<iostream>
using namespace std;
#include<string>
//结构体指针
//1.定义结构体
struct Student
{
	//成员列表
	//姓名
	string name;
	//年龄
	int age;
	//分数
	int score;
};
int main()
{
	//2.创建学生结构体变量
	struct Student s = { "张三",18 , 80 };
	//通过指针指向结构体变量
	struct Student *p = &s;
	//通过指针访问结构体变量中的元素
	cout << "姓名:" << p->name << "  年龄:" << p->age << "  分数" << p->score << endl;	
	system ("pause");
	return 0;
}

5. 结构体嵌套结构体

每个老师辅导一个学员,一个老师的结构体中,包含一个学生的结构体

#include<iostream>
using namespace std;
#include<string>
//结构体嵌套结构体
//定义学生结构体
struct student
{
	string name;//姓名	
	int age;//年龄	
	int score;//分数
};
//1.定义老师结构体
struct teacher
{
	int id;//教师编号
	string name;//教师姓名
	int age;//教师年龄
	struct student stu;
};

int main()
{
	//2.创建老师
	struct teacher t;
	t.id = 10000;
	t.name = "老王";
	t.age = 50;
	t.stu.name = "小王";
	t.stu.age = 20;
	t.stu.score = 80;
	cout << "老师姓名:" << t.name << "  老师id:" <<t.id<< "  老师年龄:" << t.age 
		 <<"   学生姓名:"<<t.stu.name<< "   学生年龄:" << t.stu.age << "   学生分数:" << t.stu.score <<endl;
	system ("pause");
	return 0;
}

6. 结构体做函数参数

#include<iostream>
using namespace std;
#include<string>
//结构体做函数参数
//定义学生结构体
struct student
{
	string name;//姓名	
	int age;//年龄	
	int score;//分数
};
//打印学生信息函数
//1.值传递
void printStudent1(struct student s)
{
	s.age = 100;//不会改变实参的值
	cout << "在子函数中打印  姓名" << s.name << "   年龄" << s.age << "   分数" << s.score << endl;
}
//2.地址传递
void printStudent2(struct student *p)
{
	p->age = 20;//会改变实参的值
	cout << "在子函数2中打印  姓名" << p->name << "   年龄" << p->age << "   分数" << p->score << endl;
}
int main()
{
	//将学生传入一个参数中,打印学生信息
	//创建结构体变量
	struct student s;
	s.name = "张三";
	s.age = 20;
	s.score = 85;

	printStudent1(s);
	//printStudent2(&s);
	cout << "在main函数中打印  姓名" << s.name << "   年龄" << s.age << "   分数" << s.score << endl;
	system ("pause");
	return 0;
}

7. 结构体中const使用场景

防止误操作

#include<iostream>
using namespace std;
#include<string>
//const 使用场景
struct student
{
	string name;
	int age;
	int score;
};
//将函数中的形参改为指针,可以减小内存空间,且不会复制新的副本出来
void printstu(const struct student *s)
{
	//s->age = 150;//加入const后,不可修改
	cout << "姓名:" << s->name << "   年龄:" << s->age << "    分数:" << s->score << endl;
}
int main()
{
	//创建结构体变量
	struct student s = { "张三",15,70 };
	//利用函数打印结构体
	printstu(&s);
	cout << "main中姓名:" << s.name << "   年龄:" << s.age << "    分数:" << s.score << endl;
	system ("pause");
	return 0;
}

8. 结构体案例

(1)每个老师带5个学生,共3个老师   老师结构体中,有老师姓名和一个存放5名学生的数组作为成员

学生成员有姓名、考试分数, 创建数组存放3明老师,通过函数给每个老师及所带学生赋值

#include<iostream>
using namespace std;
#include<string>
#include<ctime>
//学生的结构体定义
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;//41~100间的随机数
			tArray[i].sArray[j].score = random;
		}
		
	}
}
//打印函数
void printinfo(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
				<< "\t学生的成绩:" << tArray[i].sArray[j].score<<endl;
		}
	}
}
int main()
{
	//随机数种子
	srand((unsigned int)time(NULL));
	//创建3名老师的数组
	struct teacher tArray[3];
	//通过函数赋值
	int len = sizeof(tArray) / sizeof(tArray[0]);
	allocateSpace(tArray, len);
	//打印信息
	printinfo(tArray, len);
	system ("pause");
	return 0;
}

(2)设计一个英雄的结构体,包括姓名、年龄、性别,创建结构体数组,存放5名英雄,通过冒牌排序,将数组中的英雄按年龄升序排列,打印

#include<iostream>
using namespace std;
#include<string>
//英雄的结构体
struct hero
{
	string name;
	int age;
	string sex;
};
void bubblesort(struct hero heroArray[], int len)
{
	for (int i = 0; i < len - 1; i++)
	{
		for (int j = 0; j < len - 1 - i; j++)
		{
			if (heroArray[j].age > heroArray[j + 1].age)
			{
				struct hero temp = heroArray[j];
				heroArray[j] = heroArray[j + 1];
				heroArray[j + 1] = temp;
			}
		}
	}
}
void printhero(struct hero heroArray[], int len)
{
	for (int i = 0; i < len; i++)
	{
	cout << "姓名:" << heroArray[i].name << "  年龄:" << heroArray[i].age << "  性别:" << heroArray[i].sex << endl;
	}
}
int main()
{
	//设计英雄的结构体
	//创建数组
	struct hero heroArray[5] =
	{
		{"刘备",23,"男"},
		{ "关羽",22,"男" },
		{ "张飞",20,"男" },
		{ "赵云",21,"男" },
		{ "貂蝉",19,"女" },
	};
	int len = sizeof(heroArray) / sizeof(heroArray[0]);
	/*for (int i = 0; i < len; i++)
	{
		cout << "姓名:" << heroArray[i].name << "  年龄:" << heroArray[i].age << "  性别:" << heroArray[i].sex << endl;
	}*/
	//对数组排序,年龄升序
	bubblesort(heroArray, len);
	//打印出来
	printhero(heroArray, len);
	system ("pause");
	return 0;
}

七、通讯录管理系统

1.系统需求

添加联系人、显示、删除、查找、修改、清空、退出通讯录

2.创建项目

创建新项目、添加文件

3.菜单功能

封装菜单函数   在main中用

4. 退出功能

用switch语句

5. 添加联系人

联系人上限为1000,姓名、性别、年龄、电话

设计联系人结构体

设计通讯录结构体

main函数中创建通讯录

封装添加联系人函数

测试添加联系人功能

6. 显示联系人

封装函数、测试功能

判断有没有人员,没有,提示记录为空,

7.删除联系人

看是否存在 删除 测试

8. 查找联系人

查找联系人函数  测试

9. 修改联系人

函数、测试

10.清空联系人

函数、测试

#include<iostream>
using namespace std;
#include<string>
#define MAX 1000
//设计联系人结构体
struct person
{
	string m_name;
	int m_sex;//1男
	int m_age;
	string m_phone;
	string m_addr;
};
struct Addressbooks
{
	//通讯录中保存的联系人数组
	struct person personArray[MAX];
	//通讯录中当前记录联系人个数
	int m_size;
};
//1.添加联系人
void addperson(struct 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;
		while (true)
		{
			cin >> age;
			if (age > 0 && age < 150)
			{
				abs->personArray[abs->m_size].m_age = age;
				break;
			}
			cout << "输入有误,请重新输入!" << endl;
		}
		//电话
		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(struct 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
				<< "        性别:" <<(abs->personArray[i].m_sex==1? "男":"女")
				<< "        年龄:" << abs->personArray[i].m_age
				<< "        联系电话:" << abs->personArray[i].m_phone
				<< "        家庭住址:" << abs->personArray[i].m_addr << endl;
		}
	}
	system("pause");//请按任意键结束
	system("cls");//清屏
}
//检测联系人是否存在,存在,返回位置,不存在,返回-1
int isexit(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(struct Addressbooks *abs)
{
	cout << "请输入您要删除的联系人:" << endl;
	string name;
	cin >> name;
	int ret = isexit(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(struct Addressbooks *abs)
{
	cout << "请输入您要查找的联系人:" << endl;
	string name;
	cin >> name;
	int ret = isexit(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(struct Addressbooks *abs)
{
	cout << "请输入您要修改的联系人:" << endl;
	string name;
	cin >> name;
	int ret = isexit(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;
		while (true)
		{
			cin >> age;
			if (age > 0 && age < 150)
			{
				abs->personArray[ret].m_age = age;
				break;
			}
			cout << "输入有误,请重新输入!" << endl;
		}
		//电话
		cout << "请输入联系电话:" << endl;
		string phone;
		cin >> phone;
		abs->personArray[ret].m_phone = phone;
		//住址
		cout << "请输入家庭地址:" << endl;
		string address;
		cin >> address;
		abs->personArray[ret].m_addr = address;
		cout << "修改成功!" << endl;
	}
	else
	{
		cout << "查无此人!" << endl;
	}
	system("pause");
	system("cls");
}
//6.清空联系人
void cleanperson(Addressbooks *abs)
{
	abs->m_size = 0;
	cout << "通讯录已清空!" << endl;
	system("pause");
	system("cls");
}
//菜单界面
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;
}

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.删除联系人
		/*{
			cout << "请输入删除联系人的姓名:" << endl;
			string name;
			cin >> name;
			if (isexit(&abs, name) == -1)
			{
				cout << "查无此人!" << endl;
			}
			else
			{
				cout << "找到此人!" << endl;
			}
		}*/
			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;
}

  • 8
    点赞
  • 43
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值