【C++】学习笔记(包含代码和部分程序的思路)

#include <iostream>
#include <string>
#include <ctime>
//time系统时间头文件
using namespace std;

int main()
{
	srand((unsigned int)time(NULL));//添加随机数种子,利用当前系统时间生成随机数
		int num = rand() % 100 + 1;//rand()系统生成随机数%后面数字代表0~一个数,设定范围
		int ave = 0;
		while (ave != num)
		{
			cout << "请输入一个数字进行猜测" << endl;
			cin >> ave;
			if (ave>num)
			{
				cout << "猜测过大" << endl;
			}
			else if(ave<num)
			{
				cout << "猜测过小" << endl;
			}
			else
			{
				cout << "恭喜你猜对了" << endl;
				break;
			}
		}
	system("pause");
	return 0;
}

猜数字

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

int main()
{
	for (int i = 1; i <= 9; i++)
	{
		for (int j = 1; j <= i; j++)
		{
			cout << j << "*" << i <<"="<< i * j<<"\t";
		}
		cout << endl;
	}
	system("pause");
	return 0;
}

乘法口诀

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

int main()
{
	for (int i = 1; i <=100; i++)
	{
		if(i%2==0)
		{
			continue;
		}
		cout << i << endl;
	}
	system("pause");
	return 0;
}

输出奇数(continue的用法

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

int main()
{
	int arr[5] = { -12,-3,-15,-64,-4 };
	int max = arr[0];
	for (int i = 1; i < 5; i++)
	{
		if (arr[i] > max)
		{
			max = arr[i];
		}
	}
	cout << "最大值为" << max << endl;
	system("pause");
	return 0;
}

通过一维数组来找最大值

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

int main()
{
	int arr[5] = { 12,3,15,64,4 };
	cout << "前" << endl;
	for (int i = 0; i < 5; i++)
	{
		cout << arr[i] << endl;
	}
	int sta = 0;
	int end = sizeof(arr) / sizeof(arr[0]) - 1;
	int tem = 0;
	while (sta < end)
	{
		tem = arr[sta];
		arr[sta] = arr[end];
		arr[end] = tem;
		sta++; end--;
	}
	cout << "后" << endl;
	for (int i = 0; i < 5; i++)
	{
		cout << arr[i] << endl;
	}
	
	system("pause");
	return 0;
}

数组内元素逆置

int main()
{
	int arr[5] = { 12,3,15,64,4 };
	cout << "前" << endl;
	for (int i = 0; i < 5; i++)
	{
		cout << arr[i] << endl;
	}
	int tem = 0;
	for (int i = 0; i < 5-1; i++)
	{
		if (arr[i] > arr[i + 1])
		{
			tem = arr[i];
			arr[i] = arr[i + 1];
			arr[i + 1] = tem;
		}
	}
	cout << "后" << endl;
	for (int i = 0; i < 5; i++)
	{
		cout << arr[i] << endl;
	}
	system("pause");
	return 0;
}

完成一次冒泡,从第一个值[0]开始和后者比较,结果最大值去到最后一位 ;

第二次冒泡,不再需要比较最后一位,那么i<5-1(这里为啥要-1,第一次冒泡的最后一次比较是[3]和【4】比较,【5】并不存在,【i+1]不能赋予该值),此时应该变为i<5-1-1

同理

第三次,i< 5-2-1

第四次,i<5-3-1,【0】和【1】比较

第五次,没有第五次,此时只剩下【0】,无需冒泡

很明显在外部嵌套一个for循环即可限制条件变为通用式子i<5-j-1

for (j=0;j<5-1;j++)

成功!!

 

以上是老师的思路 

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


void swap(int num1, int num2)
{
	cout << "交换前" << endl;
	cout << "num1=" << num1 << endl;
	cout << "num2=" << num2 << endl;
	int tem = num1;
	num1 = num2;
	num2 = tem;
	cout << "交换后" << endl;
	cout << "num1=" << num1 << endl;
	cout << "num2" << num2 << endl;
}


int main()
{
	int a = 12;
	int b = 29;
	swap(a, b);
	cout << "a=" << a << endl;
	cout << "b=" << b << endl;
	system("pause");
	return 0;
}

 做值传递时,函数的形参发生改变,并不会影响实参

num1、num2仅仅是形参,函数定义过程中int过,但实际函数体语句里并不能直接用 

int main()
{
	int a = 12;
	int b = 29;

	const int* p = &a;//常量指针
	p = &b;
	cout << "*p=" << *p << endl;

int a = 12;
	int b = 29;

	int* const p = &a;//指针常量
	*p=2;
	cout << "*p=" << *p << endl;

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




int main()
{
	int arr[] = { 1,2,3,4,5,6,7,8,9,10 };
	int* p = arr;
	for (int i = 0; i < 10; i++)
	{
		cout << arr[i] << endl;
		cout << *p << endl;
		p++;
	}
	system("pause");
	return 0;
}

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

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

void swap(int* p1, int* p2)
{
	int tem = *p1;
	*p1 = *p2;
	*p2 = tem;
	cout << "*p1=" << *p1 << endl;
	cout << "p1=" << p1 << endl;
	cout << "*p2=" << *p2 << endl;
	cout << "p2=" << p2<< endl;
}


int main()
{
	int a = 19;
	int b = 28;
	swap(&a, &b);
	cout << "a=" << a << endl;
	cout << "b=" << b << endl;
	system("pause");
	return 0;
}

地址传递(改变实参 

拿选中的这一块来学习一下p++

"定义了一个整型指针变量p,把数组arr的地址赋给了p,

因为数组名就可以表示这个数组的首元素的地址,所以不用使用取地址符号&"

同理p1确实存的是地址,p1++后p1地址会加四个字符,但不代表*p也会加一,因为新地址并未放内容所以啥也不是,负的

结构体(函数、嵌套)

#include <iostream>
#include <string>
#include<ctime>//利用随机数种子时需要的头文件
#include<string>//调用string时需要这个头文件
using namespace std;

struct student//先定义学生防止嵌套时报未知
{
	string name;
	int sco;
};

struct teacher
{
	string tname;
	struct student sarray[5];//成员结构可以是其他结构体以实现嵌套,在这里也完成了学生数组创建
};

void fuzhi(struct teacher tarray[], int len1)//在这个位置等于变量新定义,不要忘记写数据类型,以及数组要加[]
{
	string nameseed = "ABCDE";
	for (int i = 0; i < len1; i++)//i属于新定义的,要写数据类型int
	{
		tarray[i].tname = "teacher_";
		tarray[i].tname += nameseed[i];//+=等于追加
		for (int j = 0; j < 5; j++)
		{
			tarray[i].sarray[j].name= "student_";
			tarray[i].sarray[j].name += nameseed[j];
			int random = rand() % 40 + 60;
			tarray[i].sarray[j].sco = random;
		}

	}
}

void dayin(struct teacher tarray[], int len1)
{
	for (int i = 0; i < len1; i++)
	{
		cout << "老师的姓名:" << tarray[i].tname << endl;
		for (int j = 0; j < 5; j++)
		{
			cout << "\t学生的姓名:" << tarray[i].sarray[j].name << endl;
			     cout << "\t学生的成绩:" << tarray[i].sarray[j].sco<< endl;
		}
	}
}
int main()
{
	srand((unsigned  int)time(NULL));//随机数种子,srand后跟一个包含后面东西的大()
	struct teacher tarray[3];//创建3名老师的数组
	int len1 = sizeof(tarray) / sizeof(tarray[0]);//数字长度
	fuzhi(tarray, len1);//调用函数,实现赋值
	dayin(tarray, len1);//调用函数,实现打印
	system("pause");
	return 0;
}

代码实现

 核心

 结构体案例二(冒泡排序)

代码

#include <iostream>
#include <string>

using namespace std;

struct hero
{
	string name;
	int age;
	string sex;
};//这个位置要加引号

void paxu(struct hero harray[], int len)
{
	for (int i = 0; i < len - 1; i++)
	{
		for (int j = 0; j < len - i - 1; j++)
		{
			if (harray[j].age > harray[j + 1].age)
			{
				struct hero tem = harray[j];//这里tem用的数据类型不再是int,因为要暂存harray[]英雄数组中的全部信息,不止整形数字,
				harray[j] = harray[j + 1];//以及harray定义过了,不需要在带数据类型,后面再用到tem也是
				harray[j + 1] = tem;//第二行j和j+1的位置不能错,第一行运行后j里面空了,把j+1的拿过来放到j,这时j+1空出,第三行实现把在tem暂存的原来j中的数据拿到j+1
			}
		}
	}
}

void dayin(struct hero harray[], int len)
{
	for (int i = 0; i < len; i++)
	{
		cout << "姓名:" << harray[i].name << "年龄:" << harray[i].age << "性别:" << harray[i].sex << endl;
	}
}

int main()
{
	struct hero harray[5]
	{
		{"刘备",23,"男"},//都是逗号
		{"张飞",25,"男"},
		{"貂蝉",16,"女"},
		{"关羽",34,"男"},
		{"西施",19,"女"},

	};//记得加引号
	int len = sizeof(harray)/sizeof(harray[0]);
	paxu(harray, len);
	dayin(harray, len);
	system("pause");
	return 0;
}

引用传递

#include <iostream>
#include <string>

using namespace std;

void swap01(int a, int b)//值传递
{
	int tem = a;
	a = b;
	b = tem;
}
void swap02(int* a, int* b)//地址传递
{
	int tem = *a;
	*a = *b;
	*b = tem;
}
void swap03(int& a, int& b)//引用传递
{
	int tem = a;
	a = b;
	b = tem;
	cout << "形参a=" << a << endl;
	cout << "形参b=" << b << endl;
}
int main()
{
	int a = 10;
	int b = 20;
	//swap01(a, b);//值传递,形参不会修饰实参,实参不会随着改变
	//swap02(&a, &b);//地址传递,形参会修饰实参,实参会随着改变
	swap03(a, b);//引用传递,形参会修饰实参,实参会随着改变

	cout <<"a="<< a << endl;
	cout <<"b="<< b << endl;
	
	system("pause");
	return 0;
}

封装1

#include <iostream>
#include <string>

using namespace std;

//封装的意义:在设计类的时候,属性和行为写在一起来表现事物

double Pi = 3.14;

class yuan//class代表设计一个类,类后面跟类名称
{//属性和行为都放在这个大括号下
	//访问权限
	//公共权限
public :
	double r;//属性——数据类型 变量
	double zhouchang()//行为——函数(因为返回值为double*double所以返回值类型还得是double
	{
		return 2 * Pi * r;
	}
};
int main()
{
	yuan c1;//实例化:通过圆类创建具体的圆(对象)——类名称 对象名称;
	c1.r = 2.1;//给对象的属性赋值
	cout << "圆1的周长为" << c1.zhouchang() << endl;//通过对象的行为(因为行为是函数,记得跟括号即使是空括号

	system("pause");
	return 0;
}

全局函数和局部函数,bool

#include <iostream>
#include <string>

using namespace std;

class Cube
{
public:
	void setl(int l)//设置长,赋值
	{
		m_l=l;
	}
	int getl()//获取长,调用
	{
		return m_l;
	}
	void seth(int h)
	{
		m_h=h;
	}
	int geth()
	{
		return m_h;
	}
	void setw(int w)
	{
		m_w=w;
	}
	int getw()
	{
		return m_w;
	}
	int tiji()
	{
		return m_l*m_w*m_h;
	}
	bool issamenei(Cube c)
	{
	if(m_l==c.getl()&& m_w == c.getw()&& m_h == c.geth())
	{
		return true;
	}
	return false;
	}
private:
	int m_l;
	int m_h;
	int m_w;

};

bool isSame(Cube& c1,Cube& c2)
{
	if (c1.getl() == c2.getl() && c1.geth() == c2.geth() && c1.getw() == c2.getw())
	{
		return true;
	}
		return false;
}
int main()
{
	Cube c1;
	c1.setl(10);
	c1.setw(10);
	c1.seth(10);
	cout << "体积" << c1.tiji() << endl;
	Cube c2;
	c2.setl(10);
	c2.setw(10);
	c2.seth(12);
	bool res = isSame(c1, c2);
	if (res)
	{
		cout << "二者相等" << endl;
	}
	else
	{
		cout << "二者不等" << endl;
	}
	bool res1 = c1.issamenei(c2);
	if (res1)
	{
		cout << "类内二者相等" << endl;
	}
	else
	{
		cout << "类内二者不等" << endl;
	}
	system("pause");
	return 0;
}

类的嵌套

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

class Point
{
public:
	void setx(int x)
	{
		m_x = x;
	}
	int gety()
	{
		return m_y;
	}
	void sety(int y)
	{
		m_y = y;
	}
	int getx()
	{
		return m_x;
	}
private:
	int m_x;
	int m_y;
};
class Circle
{
public:
	void setr(int r)
	{
		m_r = r;
	}
	int getr()
	{
		return m_r;
	}
	void setcenter(Point p)//它是点传入的也是点
	{
		m_center = p;
	}
	Point getcenter()
	{
		return m_center;
	}
private:
	int m_r;
	Point m_center;//一个类作为另一个类的成员属性
};

void where(Point &p, Circle &c)
{
	int dis1 =
		(c.getcenter().getx() - p.getx()) * (c.getcenter().getx() - p.getx()) + (c.getcenter().gety() - p.gety()) * (c.getcenter().gety() - p.gety());
	//c.getcenter().getx()精彩,get()可以.连用,但set要传入的值放在()内不能用.也不能连用
    int dis2 =
			c.getr() * c.getr();
	if (dis1 == dis2)//判断用==
	{
		cout << "点在圆上" << endl;
		}
	else if (dis1 < dis2)
	{
		cout << "点在圆内" << endl;
	}
	else
	{
		cout << "点在圆外" << endl;
	}

}

int main()
{
	Point p1;
	Point p2;
	Circle c1;
	p1.setx(10);
	p1.sety(6);
	p2.setx(10);
	p2.sety(0);
	c1.setr(10);
	c1.setcenter(p2);//传入的是点所以p2的xy属性提前赋值

	where(p1, c1);//顺序不能错
	system("pause");
	return 0;
}

分文件

构造函数和析构函数

#include <iostream>
#include <string>

using namespace std;

class Person
{
public://以便外部调用
	Person()//构造函数
	{
		cout << "构造函数的调用" << endl;
	}
	~Person()//析构函数
	{
		cout << "析构函数的调用" << endl;
	}
};
void test()
{
	Person p;//创建一个对象,并未调用,但系统自动调用
	        //因为这个对象创建在栈区(局部变量)test执行完毕后会释放这个对象(执行完毕划重点),对象在销毁前会自动调用析构函数
}


int main()
{
	test();//调用test
	system("pause");
	return 0;
}

 函数调用

1括号法

2显示法

 3隐式转换法

#include <iostream>
#include <string>

using namespace std;

class Person
{
public://以便外部调用
	
	Person()//构造函数
	{
		cout << "默认构造函数的调用" << endl;
	}
	Person(int a)//构造函数
	{
		age = a;
		cout << "有参构造函数的调用" << endl;
	}
	Person(const Person& p)
	{
		age = p.age;//将传入的对象的属性拷贝到自己身上
		cout << "拷贝构造函数的调用" << endl;
	}

	~Person()//析构函数
	{
		cout << "析构函数的调用" << endl;
	}

	int age;
};
void test()
{
	//调用
	//括号法
	//Person p1;//默认构造函数的调用,没有括号
	//          //Person p1();//编辑器还认为是一个函数的声明,不会报错但也不会调用我们的默认构造函数
	//Person p2(10);//有参函数的调用
	//Person p3(p2);//拷贝函数的调用,划重点
	//显示法
	Person p1;
	Person p2 = Person(10);//有参构造
	Person p3 = Person(p2);//拷贝构造
	//隐式转换法
	Person p4 = 10;//相当于写了 Person p4 = Person(10); 有参构造
	Person p5 = p4;//拷贝构造
}


int main()
{
	test();//调用test
	system("pause");
	return 0;
}

this

#include <iostream>
#include <string>

using namespace std;

class Person
{
public:
	
	Person(int age)
	{
		this->age = age;//解决名称冲突,因为传入的和成员属性都是age,编译器默认是同一个,this指向p1,因为p1在调用成员函数Person
	}
    Person& addPerson(Person &p)
	{
		this->age += p.age;//p.age传入的p
		return *this;//返回对象本身用*this
	}

	int age;//做好区分,成员属性就叫m_age
};
void test()
{
	Person p(12);
	cout << p.age << endl;
	
}
void test1()
{
	Person p1(1);
	Person p2(2);
	p1.addPerson(p2).addPerson(p2).addPerson(p2);
	cout << p1.age << endl;
}

int main()
{
	test1();//调用test
	system("pause");
	return 0;
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值