c++学习笔记1

endl即(end line)和\n的区别
  1. \n 只负责换行
  2. endl 负责换行和打印缓冲区
HelloWorld解读
#include <iostream>

using namespace std;

int main()
{
    cout << "Hello world!" << endl;
    return 0;
}

  1. iostream 即(input output stream)输入输出流
  2. using namespace std; 使用std即(standard)标准 作为命名空间
  3. cout 输出流
  4. << 插入
  5. endl 换行
  6. return 0 表示程序正常结束 非0表示程序异常
C++编译和执行

1.编译(预处理->编译->生成目标文件)预处理相当于#include

.cpp———>.ii 源文件 转成 预处理文件

在这里插入图片描述
在这里插入图片描述
.ii———>.s 预处理文件 转成 汇编文件
在这里插入图片描述
在这里插入图片描述
.s———>.o 汇编文件 转成 二进制文件
在这里插入图片描述
在这里插入图片描述
.o———>.exe 二进制文件 转成 可执行文件
在这里插入图片描述
在这里插入图片描述

C++中的常量
  1. #define 来定义宏常量
  2. const修饰的变量为常量
#include<iostream>

using namespace std;
#define Day 7

int main() {
	
	cout << "一周:" << Day << "天" << endl;

	//const修饰的变量
	const int month = 12;
	cout << "一年有:" << month << "个月 " << endl;
	system("pause");

	return 0;

}
指针

1.本身所占的大小
在这里插入图片描述
2.空指针
指向编号为0的内存的指针
空指针用于给指针变量初始化
空指针是不能进行访问的 0~255之间的内存编号是系统占用的,因此不可以访问
在这里插入图片描述

#include<iostream>
using namespace std;

int main() {
	//空指针
	//1空指针用于给指针变量初始化
	int* p=NULL;

	//2空指针是不能进行访问的
	//0~255之间的内存编号是系统占用的,因此不可以访问
	//*p = 100;
	return 0;
}

3.野指针
直接指向一个非法内存编号

#include<iostream>
using namespace std;

int main() {

	//野指针
	int* p = (int *)0x1100;

	cout << *p << endl;
	system("pause");
	return 0;
}
const

1.const修饰指针----常量指针

int a;
int b;
const int *p=&a;
//常量指针特点
//指针的指向可以修改,但是指针指向的值不可以改变
*p=20;//错误,指针指向的值不可改变
p=&b;//正确,指针指向可以改变

2.const修饰常量----指针常量

int a;
int b;
int * const p=&a;//const后面跟变量
//特点:指针的指向不可以改变,指针指向的值可以改变
*p=20;//正确,指向的值可以改
p=&b;//错误,指针指向不可以改

3.const即修饰指针,又修饰常量

int a;
int b;
const int * const p=&a;
//特点:指针的指向和指针指向的值都不可以改
*p=10;//错误
p=&b;//错误
值传递与址传递

在这里插入图片描述

#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, b = 20;
	swap01(a, b);
	cout << "a=" << a << endl;
	cout << "b=" << b << endl;
	
	//2地址传递
	//如果是地址传递,可以修饰实参
	swap02(&a, &b);
	cout << "a=" << a << endl;
	cout << "b=" << b << endl;
	system("pause");
	return 0;
}

总结:如果不想修改实参,就用址传递。如果想修改实参,就用地址传递。

指针、数组、函数 配合案例
#include<iostream>
using namespace std;

//冒泡排序函数 参数1数组的首地址  参数2数组的长度
void bubbleSort(int * arr,int len) {
	for (int i = 0; i < len - 1; i++) {
		for (int j = 0; j < len - i - 1; 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] << " ";
	}
	cout << 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);//arr为址传递,实现实参的改变
	//3.打印排序后的数组
	printArray(arr,len);
	
	system("pause");
	return 0;
}
结构体

1.结构体数组

#include<iostream>
#include<string>
using namespace std;
//结构体数组

//1.定义结构体
struct Student {
	string name;
	int age;
	int score;
};

int main() {
//2.创建结构体数组
	struct Student stuArray[3] = {
		{"张三",18,100},
		{"李四",23,78},
		{"王五",27,89}
	};
//3.给结构体数组中元素赋值
	stuArray[2].name = "赵六";

//4.遍历结构体数组
	for (int i = 0; i < 3; i++) {
		cout << "姓名:" << stuArray[i].name
			<< "\t年龄:" << stuArray[i].age 
			<< "\t分数:" << stuArray[i].score
			<< endl;
	}
}

2.结构体指针
结构体指针用->来访问结构体成员

#include<iostream>
using namespace std;

//结构体指针
//定义学生结构体

struct Student {
	string name;
	int age;
	int score;
};

int main() {
	//1创建结构体变量
	struct Student s = { "张三",25,100 }; 
	//2通过指针指向结构体变量
	struct Student * p = &s;
	//3通过指针访问结构体变量中的数据
	cout << "姓名:" << p->name << "\t年龄:" << p->age << "\t分数:" << p->score << endl;

	system("pause");
	return 0;
}
结构体做函数参数

1.值传递与址传递

#include<iostream>
using namespace std;
//定义学生结构体
struct student {
	string name;
	int age;
	int score;
};

//打印学生
//值传递
void printStudent1(struct student s) {
	s.age = 100;
	cout << "子函数1中 姓名:" << s.name << "\t年龄:" << s.age << "\t分数:" << s.score << endl;
}
//址传递
void printStudent2(struct student* p) {
	p->age = 200;
	cout << "子函数2中 姓名:" << p->name << "\t年龄:" << p->age << "\t分数:" << p->score << endl;
}
int main() {
	//将结构体做函数参数
	//将学生传入到一个参数中,打印学生信息

	//创建结构体变量
	struct student s;
	s.name = "张三";
	s.age = 25;
	s.score = 100;
	printStudent1(s);
	printStudent2(&s);
	cout << "main中打印 姓名:" << s.name << "\t年龄:" << s.age << "\t分数:" << s.score << endl;
	system("pause");
	return 0;
}
结构体中const的使用

作用:加const防止修改结构体里面的值

#include<iostream>
using namespace std;
//const的使用场景

struct student {
	string name;
	int age;
	int score;
};
//将函数中的形参改为指针,可以减少内存空间,而且不会复制新的副本出来
void printStudent(const student *s) {//加const防止修改结构体里面的值
	//s->age = 150;//操作失败,因为加了const修饰
	cout << "姓名:" << s->name << "\t年龄:" << s->age << "\t分数:" << s->score << endl;
}
int main() {
	//创建结构体变量
	struct student s = { "张三",15,85 };
	//通过函数打印结构体变量信息
	printStudent(&s);
	cout << "main函数中 年龄:" << s.age << endl;


}
对结构体数组进行冒泡排序
#include<iostream>

using namespace std;
//1设计英雄结构体
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 - i - 1; 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 << "\t" << heroArray[i].age << "\t" << heroArray[i].sex << endl;
	}
}
int main() {
	

	//2创建数组存放5名英雄
	struct Hero heroArray[5] = { {"关羽",23,"男"},{"刘备",22,"男"} ,{"张飞",20,"男"} ,{"赵云",21,"男"} ,{"貂蝉",19,"女"} };
	//3对数组进行排序,按照年龄进行升序排序
	int len = sizeof(heroArray) / sizeof(heroArray[0]);
	//排序前的打印
	cout << "排序前的打印" << endl;
	for (int i = 0; i < len; i++) {
		cout << heroArray[i].name << "\t" << heroArray[i].age << "\t" << heroArray[i].sex << endl;
	}
	bubbleSort(heroArray,len);
	//4将排序后的结果打印输出
	cout << "排序后的打印" << endl;
	printHero(heroArray, len);

	system("pause");
	return 0;
}
通讯录管理系统
//封装函数显示菜单 void showMenu()
//在main中调用封装好的函数
#include<iostream>
#define MAX 1000//最大人数
using namespace std;

//1.菜单界面
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 m_Name;
	int m_Sex;//性别 1男 2女
	int	m_Age;
	string m_Phone;
	string m_Addr;//住址
};
//通讯录结构体
struct Addressbooks {
	struct Person personArray[MAX];//通讯录中保存的联系人数组
	int m_Size;//通讯录中人员个数
};

//2.添加联系人
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;
		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");//清屏操作
	}
}

//3.显示所有的联系人
void showPerson(Addressbooks * abs) {
	//判断通讯录中认数是否为0,如果为0,提示记录为空
	//如果不为0,显示记录的联系人信息
	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\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");//清屏
}

//4.检测联系人是否存在,如果存在,返回联系人所在数组中的具体位置,不存在返回-1
//参数1 通讯录 参数2 对比姓名
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;//如果遍历结束都没有找到,返回-1
}

//5.删除指定联系人
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");
}

//6.查找联系人
void findPersion(Addressbooks * abs){
	cout << "请输入您要查找的联系人" << endl;
	string name;
	cin >> name;
	//判断要查找的人是否存在
	int ret = isExist(abs, name);
	if ( ret== -1) {
		cout << "查无此人" << endl;

	}
	else {
		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;
		
	}

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

//7.修改联系人
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;
		//性别
		int sex=0;
		cout << "请输入姓别:" << endl;
		cout << "1---男" << endl;
		cout << "2---女" << endl;
		while (true) {
			cin >> sex;
			if (sex == 1 || sex == 2) {
				//输入正确退出循环
				abs->personArray[ret].m_Sex = sex;
				break;
			}
			cout << "输入有误,请重新输入" << endl;
		}
		//年龄
		cout << "请输入年龄:" << endl;
		int age;
		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;

		cout << "修改成功" << endl;

	}
	else {//未找到
		cout << "查无此人" << endl;
	}

	//按任意键后清屏
	system("pause");
	system("cls");
}

//8.清空所有联系人
void cleanPerson(Addressbooks * abs) {
	cout << "是否清空所有联系人?" << endl;
	cout << "1---确认清空" << endl;
	cout << "2---放弃清空" << endl;
	int selectResult = 0;
	cin >> selectResult;
	if (selectResult == 1) {
		abs->m_Size = 0;//将当前记录联系人数量置为0,做逻辑上清空操作
		cout << "通讯录已经清空" << endl;
	}
	else {
		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);
		/*{
			cout << "请输入要删除的人的姓名:" << endl;
			string name;
			cin >> name;
			if (isExist(&abs, name) == -1) {
				cout << "查无此人" << endl;
			}
			else {
				cout << "找到此人" << endl;

			}
		}*/
			break;
		case 4://4.查找联系人
			findPersion(&abs);
			break;
		case 5://5.修改联系人
			modifyPerson(&abs);
			break;
		case 6://6.清空联系人
			cleanPerson(&abs);
			break;
		case 0://退出
			cout << "欢迎下次使用" << endl;
			system("pause");
			return 0;
			break;
		}
	}
	

	
	system("pause");
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值