杂
6 循环结构
6.1 打印图形
//双循环 外层循环控制行 内层循环控制列
for (int i = 0; i < 4; i++) { //图形的上半部分
for (int j = 0; j <= 2 - i; j++) {
cout << " ";
}
for (int j = 0; j <= 2*i; j++) {
cout << "*";
}
cout << endl;
}
for (int i = 0; i < 3; i++) { //图形的下半部分
for (int j = 0; j <= i; j++) {
cout << " ";
}
for (int j = 0; j <= 4 - 2 * i; j++) {
cout << "*";
}
cout << endl;
}
7 数组
变量:内存中的一块空间
数组:内存中一串连续空间
数组长度固定不变,避免数组越界
数组内的元素必须是相同的类型
数组必须先声明,然后才能使用
数组元素通过数组的下标来访问
二维数组可以看作是由以为数组的嵌套而构成的
7.1 求数组的和与平均值 奇数偶数个数 查找
const int N = 5;
int nums[N];
int numslen = sizeof(nums) / sizeof(int);
double sum = 0;
cout << "数组的大小是" << sizeof(nums) / sizeof(int) << endl;
for (int i = 0; i < sizeof(nums) / sizeof(int); i++) {
cout << "请输入第" << (i + 1) << "个数字:";
cin >> nums[i];
}
for (int i = 0; i < N; i++) {
cout << nums[i] << endl;
sum += nums[i]; //累加操作
}
cout << "数列的和为:" << sum << "\t平均值为:" << sum / 5 << endl;
//求出奇数和偶数的个数
int jcount = 0, ocount = 0;
for (int i = 0; i < numslen; i++) {
if (nums[i] % 2 == 0)
ocount++;
else
jcount++;
}
cout << "奇数的个数:" << jcount << "\t偶数的个数:" << ocount << endl;
//查找输入的数字再数组中的下标,没有则为-1
int searchnum;
int searchindex = -INT_MIN;
cout << "请输入要查找的数字:";
cin >> searchnum;
for (int i = 9; i < numslen; i++) { //使用循环查找
if (nums[i] == searchnum) {
searchindex = i; //记录下查到到的数字下标
break;
}
}
if (searchindex == INTPTR_MIN) {
cout << "没有找到" << endl;
}
else {
cout << searchnum << "在数组中的下标为:" << searchindex << endl;
}
7.2 数组排序
冒泡排序:
- 第一轮比较的次数:数组的长度-1
- 下一轮的比较次数:比上一次少1
//方案1:冒号排序
int nums[] = { 15,25,90,23,9 };
int temp;
for (int i = 0; i < 5; i++) { //外层控制比较的论述
for (int j = 0; j < 4 - i; j++) { //内层控制每轮的比较和交换
if (nums[j] < nums[j + 1]) {
//交换
temp = nums[j];
nums[j] = nums[j + 1];
nums[j + 1] = temp;
}
}
}
选择排序:
//方案2:选择排序
int nums[] = { 8,25,90,23,9 };
int numslen = sizeof(nums) / sizeof(int); //数组的长度
int min = nums[0]; //假设最小值是数组的第一个元素
int minindex = 0; //最小值的初始下标设为0
int temp;
for (int i = 0; i < numslen - 1; i++) {
min = nums[1]; //假设第i个为最小
minindex = i;
for (int j = i + 1; j < numslen ; j++) { //比较 这里视频中有误,已改正
if (nums[j] < min) {
min = nums[j];
minindex = j;
}
}
if (minindex > i) { //可以不写
temp = nums[minindex];
nums[minindex] = nums[i];
nums[i] = temp;
}
}
cout << "排序后:" << endl;
for (int i = 0; i < numslen; i++) {
cout << nums[i] << endl;
}
7.3 元素的删除和排序
//数组元素的删除和插入
double power[99]; //数组大小确定后不能更改
int powercount = 0; //默认插入到第一个元素的位置
double insertpower;
double temp;
int insertindex = 0;
power[powercount++] = 45771;
power[powercount++] = 42322;
power[powercount++] = 40644;
power[powercount++] = 40098;
for (int i = 0; i < powercount; i++) {
for (int j = 0; j < powercount - i - 1; j++) {
if (power[j] < power[j + 1]) {
temp = power[j];
power[j] = power[j + 1];
power[j + 1] = temp;
}
}
}
cout << "排序后" << endl;
for (int i = 0; i < powercount; i++) {
cout << power[i] << '\t';
}
cout << endl;
//插入
cout << "请输入要插入的数字:";
cin >> insertpower;
insertindex = powercount;
//1.找到第一个比插入数字大的位置insertindex
for (int i = 0; i < powercount; i++) {
if (insertpower > power[i]) {
insertindex = i;
break;
}
}
//2.重新排序
for (int i = powercount - 1; i >= insertindex; i--) {
power[i + 1] = power[i];
}
power[insertindex] = insertpower;
//3.数组总长度加一
powercount++;
cout << "插入后" << endl;
for (int i = 0; i < powercount; i++) {
cout << power[i] << '\t';
}
cout << endl;
7.4 二维数组
//使用二维数组
string stu_names[]{ "刘备","关羽","张飞" };
string course_names[]{ "语文","数学","英语" };
const int row = 3; //sizeof(stu_names) / sizeof(stu_names[0])
const int col = 3;
double scores[row][col];
for (int i = 0; i < row; i++) { //外层循环控制学生
for (int j = 0; j < col; j++) { //内层循环控制学科
cout << stu_names[i] << "的" << course_names[j] << "成绩:";
cin >> scores[i][j];
}
}
//打印结果
cout << '\t';
for (int i = 0; i < col; i++) {
cout << course_names[i] << '\t';
}
cout << endl;
for (int i = 0; i < row; i++) {
cout << stu_names[i] << '\t';
for (int j = 0; j < col; j++) {
cout << scores[i][j] << '\t';
}
cout << endl;
}
7.5 数组的替代品
向量容器vector
8 指针
指针是一个值为内存地址的变量(或数据对象)
double num = 1024.5;
//声明一个指针指向num
double* ptr_num = #
cout << "ptr_num的值:" << ptr_num << endl;
cout << "ptr_num指向空间的值是:" << *ptr_num << endl;
8.1 引用
指向常量的引用是非法的
引用必须初始化,所以引用前不需要测试有效性
8.2 数组
储存再一块连续的内存空间中
数组名为空间的首地址
double score[]{ 11,22,33,44,55 };
double* ptr_score = score;
cout << ptr_score[3] << endl;
cout << sizeof(score) << '\t' << sizeof(ptr_score) << endl;
9 实战演练
string value_master_name;
string value_pet_name;
int value_brith_month = 6;
int value_brith_date = 6;
string value_pet_constell; //宠物的星座
int choice, temp1, temp2, temp3;
/*宠物的基本信息*/
array<int,5> value_pet_baseinfo; //动态分配内存的数组
/*五项基本属性*/
array<string, 5>value_array_baseinfo_names = { "体力","智力","魅力","道德","气质" };
int value_pet_money = 500;
/*星座的二维数组*/
string constell_names[12][2] = {
{"山羊座","水瓶座"}, //一月
{"水瓶座","双鱼座"}, //二月
{"双鱼座","白羊座"}, //三月
{"白羊座","金牛座"}, //四月
{"金牛座","双子座"}, //五月
{"双子座","巨蟹座"}, //六月
{"巨蟹座","狮子座"}, //七月
{"狮子座","处女座"}, //八月
{"处女座","天秤座"}, //九月
{"天秤座","天蝎座"}, //十月
{"天蝎座","射手座"}, //十一月
{"射手座","山羊座"}, //十二月
};
/*各星座的跨月日期*/
int constell_dates[]{ 20,19,21,20,21,22,23,23,23,24,23,22 };
value_pet_constell = constell_names[value_brith_month - 1][value_brith_date / constell_dates[value_brith_month - 1]];
cout << "主人的姓名:";
cin >> value_master_name;
cout << "宠物的姓名:";
cin >> value_pet_name;
cout << "宠物店生日(MM DD):";
cin >> value_brith_month >> value_brith_date;
cout << "宠物的信息:" << endl;
cout << "姓名:" << value_pet_name << endl;
cout << "生日:2020-" << value_brith_month << "-" << value_brith_date << endl;
cout << "星座:" << value_pet_constell << endl;
cout << "金币:" << value_pet_money << endl;
/*根据信息为宠物设置基本参数*/
if (value_pet_constell == "金牛座"){
value_pet_baseinfo[0] = 11; value_pet_baseinfo[1] = 15; value_pet_baseinfo[2] = 15;
value_pet_baseinfo[3] = 81; value_pet_baseinfo[4] = 21;
}
else if (value_pet_constell == "双子座") {
value_pet_baseinfo[0] = 22; value_pet_baseinfo[1] = 32; value_pet_baseinfo[2] = 42;
value_pet_baseinfo[3] = 32; value_pet_baseinfo[4] = 82;
}
else if (value_pet_constell == "巨蟹座") {
value_pet_baseinfo[0] = 33; value_pet_baseinfo[1] = 13; value_pet_baseinfo[2] = 13;
value_pet_baseinfo[3] = 63; value_pet_baseinfo[4] = 23;
}
else if (value_pet_constell == "狮子座") {
value_pet_baseinfo[0] = 54; value_pet_baseinfo[1] = 14; value_pet_baseinfo[2] = 94;
value_pet_baseinfo[3] = 14; value_pet_baseinfo[4] = 24;
}
else if (value_pet_constell == "处女座") {
value_pet_baseinfo[0] = 85; value_pet_baseinfo[1] = 15; value_pet_baseinfo[2] = 15;
value_pet_baseinfo[3] = 15; value_pet_baseinfo[4] = 25;
}
else if (value_pet_constell == "天秤座") {
value_pet_baseinfo[0] = 80; value_pet_baseinfo[1] = 15; value_pet_baseinfo[2] = 16;
value_pet_baseinfo[3] = 11; value_pet_baseinfo[4] = 21;
}
else if (value_pet_constell == "天蝎座") {
value_pet_baseinfo[0] = 80; value_pet_baseinfo[1] = 15; value_pet_baseinfo[2] = 17;
value_pet_baseinfo[3] = 11; value_pet_baseinfo[4] = 21;
}
else if (value_pet_constell == "射手座") {
value_pet_baseinfo[0] = 80; value_pet_baseinfo[1] = 15; value_pet_baseinfo[2] = 18;
value_pet_baseinfo[3] = 11; value_pet_baseinfo[4] = 21;
}
else if (value_pet_constell == "白羊座") {
value_pet_baseinfo[0] = 80; value_pet_baseinfo[1] = 15; value_pet_baseinfo[2] = 19;
value_pet_baseinfo[3] = 11; value_pet_baseinfo[4] = 21;
}
else if (value_pet_constell == "山羊座") {
value_pet_baseinfo[0] = 13; value_pet_baseinfo[1] = 15; value_pet_baseinfo[2] = 10;
value_pet_baseinfo[3] = 11; value_pet_baseinfo[4] = 21;
}
else if (value_pet_constell == "水瓶座") {
value_pet_baseinfo[0] = 36; value_pet_baseinfo[1] = 25; value_pet_baseinfo[2] = 10;
value_pet_baseinfo[3] = 11; value_pet_baseinfo[4] = 21;
}
else if (value_pet_constell == "双鱼座") {
value_pet_baseinfo[0] = 80; value_pet_baseinfo[1] = 74; value_pet_baseinfo[2] = 10;
value_pet_baseinfo[3] = 11; value_pet_baseinfo[4] = 21;
}
for (int i = 0; i < 5; i++) {
cout << value_pet_baseinfo[i] << '\t';
}
cout << endl;
/*游戏大循环*/
for (int year = 2020; year <= 2020 + 10; year++) {
for (int month = (year ==2020) ? 6 : 1; month <= 12; month++) {
//判断本月是不是宠物生日
if (month == value_brith_month) {
cout << "本月是" << value_pet_name << "的生日,要不要送礼物呢?" << endl;
//根据输入进行判断
}
//显示游戏主菜单
cout << "\n1、查看状态\n2、安排行程\n3、亲自谈话\n4、存档\n5、读档" << endl;
cin >> choice;
switch (choice) {
case 1://查看状态
cout << "姓名:" << value_pet_name << endl;
cout << "生日:2020-" << value_brith_month << "-" << value_brith_date << endl;
cout << "星座:" << value_pet_constell << endl;
cout << "金币:" << value_pet_money << endl;
//打印核心属性
cout << left;//左对齐
for (int i = 0; i < 5; i++) {
cout << value_array_baseinfo_names[i] << ":" << setw(4) << value_pet_baseinfo[i];
//计算实心方框的数量
int solid_count = value_pet_baseinfo[i] / 10;
for (int j = 0; j < 10; j++) {
if (j <solid_count)
cout << "■";
else
cout << "□";
}
cout << endl;
}
break;
case 2: { //安排行程.一个月最多三个行程
string month_parts[] = { "上旬","中旬","下旬" };
for (int i = 0; i < 3; i++) {
cout << "-- " << month << "月--" << month_parts[i] << endl;
cout << "1、学习武艺\n2、上私塾\n3、学习礼法\n4、出城修行\n5、打工赚钱" << endl;
cout << "请选择:";
cin >> choice;
//需要注意没钱的情况:强制打工
srand(time(NULL));
if (choice == 1) { //+体力 +魅力 -金钱
value_pet_baseinfo[0] += temp1 = rand() % 11;
value_pet_baseinfo[2] += temp2 = rand() % 11;
value_pet_money = temp3 = rand() % 51;
cout << "学习张无忌好榜样!!" << endl;
cout << "体力+" << temp1 << ",魅力+" << temp2 << ",金钱-" << temp3 << endl;
}
else if (choice == 5) {
value_pet_money += temp3 = rand() % 101;
cout << "通过努力打工赚到了:" << temp3 << "金币!" << endl;
}
}
break;
}
case 3:
cout << "还未上线" << endl;
break;
case 4:
cout << "暂时不会" << endl;
break;
case 5:
cout << "暂时不会" << endl;
break;
default:
cout << "暂时不会" << endl;
break;
}
}
}
/*根据各项参数判定游戏结果*/
int sum = 0;
for (int i = 0; i < 5; i++) {
sum += value_pet_baseinfo[i];
}
if (sum >= 2000) {
cout << "最优游戏结果:宠物之王" << endl;
}
else if (sum >= 1800 && sum < 2000) {
cout << "次优游戏结果:ak小王子" << endl;
}
else if (sum >= 1200 && sum < 1600) {
int max_value = value_pet_baseinfo[0];
int max_index = 0;
for (int i = 1; i < 5; i++) {
if (max_value < value_pet_baseinfo[i]) {
max_value = value_pet_baseinfo[i];
max_index = i;
}
}
if ("道德" == value_array_baseinfo_names[max_index] && value_pet_baseinfo[2] > value_pet_baseinfo[4]) {
cout << "经过长期训练,已成长为高级祭祀" << endl;
}
}
10 自定义函数
#include<iostream>
using namespace std;
/*
三种形状计算体积
*/
//1.定义三个函数,分别用来计算三个体积
void calcCuboid();//计算长方形
void calcCylinder();//圆柱
void calcCone();//圆锥
int main()
{
int choise = -1;
while (choise) {
cout << "1、长方体" << endl;
cout << "2、圆柱体" << endl;
cout << "0、退出" << endl;
cin >> choise;
switch (choise)
{
case 1:
calcCuboid();
break;
case 2:
calcCylinder();
break;
}
}
return 0;
}
void calcCuboid()
{
double len, width, height;
cout << "请输入长宽高:";
cin >> len >> width >> height;
//计算体积
double v = len * width * height;
cout << "体积为:" << v << endl;
}
void calcCylinder()
{
double radius,height;
cout << "请输入半径和高:";
cin >> radius>>height;
//计算体积
double v = 3.14 * pow(radius, 2) * height;
cout << "体积为:" << v << endl;
}
//定义函数指针13.1
auto ptrCalc = addition;
double num1, num2;
char op;
cout << "请输入两个操作数:";
cin >> num1 >> num2;
cout << "请输入运算符号:";
cin >> op;
switch (op) {
case '+':
//调用加法
ptrCalc = addition;
break;
case '-':
//调用减法
ptrCalc = subtraction;
break;
case '*':
ptrCalc = multipliaction;
break;
case '/':
ptrCalc = division;
break;
}
//调用
print_result(ptrCalc, num1, num2);
11 引用
使用引用的理由
1、可以更加简便的书写代码
2、可以直接传递某个对象,而不是把对象复制一份
#include <iostream>
using namespace std;
void Swap1(int, int);
void Swap2(int*, int*);
void Swap3(int&, int&);
int main()
{
int num1 = 10, num2 = 5;
Swap1(num1, num2);
cout << "执行交换1后:" << num1 << '\t' << num2 << endl;
Swap2(&num1, &num2);
cout << "执行交换2后:" << num1 << '\t' << num2 << endl;
Swap3(num1, num2);
cout << "执行交换3后:" << num1 << '\t' << num2 << endl;
return 0;
}
void Swap1(int num1, int num2) {
int temp = num1;
num1 = num2;
num2 = temp;
}
void Swap2(int* p1, int* p2) {
int temp;
temp = *p1;
*p1 = *p2;
*p2 = temp;
}
//使用引用的理由
//1、可以更加简便的书写代码
//2、可以直接传递某个对象,而不是把对象复制一份
void Swap3(int& ref1, int& ref2) {
int temp;
temp = ref1;
ref1 = ref2;
ref2 = temp;
}
tips:不要返回局部变量的引用冰,函数中的局部变量会被内存回收。(回收并不是把内存保存的设置清零,而是把内存中程序申请的这块内存已经不是你的了)
#include <iostream>
using namespace std;
int& sum() {
int num = 10;
int& rNum = num;
//rNum实在sum函数中定义的,所以是局部变量
//rNum的生命周期只在sum函数中
return rNum;//返回了一个局部的引用变量
}
void test() {
int x = 1;
int y = 2;
int z = 1024;
}
int main()
{
//result在这里引用一个局部变量
int& result = sum();
test();
cout << "result = " << result << endl;
return 0;
}
模拟实现游戏中的私聊
#include <iostream>
#include<string>
using namespace std;
//模拟实现游戏中的私聊
string chatTo(const string& toName, const string& content);
string chatFrom(const string& fromNmae, const string& fromName);
int main()
{
string toName, content, chatMsg;
cout << "请输入对方的名称:";
getline(cin, toName);
cout << "请输入发送给对方的聊天信息:";
getline(cin, content);
chatMsg = chatTo(toName, content);
cout << endl << chatMsg << endl;
return 0;
}
string chatTo(const string& toName, const string& content) {
string msg = "■ 你悄悄的对[" + toName + "]说:" + content;
return msg;
}
11.2 默认参数
1、默认值可以在函数原型或者定义中给出,但是不能同时出现。
2、对于带参数列表的函数,必须从右向左添加默认值
11.3 函数重载
特征标
重载-编译器在编译的时候,根据参数列表对函数进行重命名
void swap(int a, int b) //编译器内= swap_int_int
void swap(float a, float b)
#include <iostream>
#include<string>
using namespace std;
void sort(int[],int len);
void sort(float[], int len); //重载,函数名相同,特征标不同
void sort(double[], int len);
void show(int[], int len);
void show(double[], int len);
void show(float[], int len);
int main()
{
int iNums[] = { 56,54,12,78,43 };
int fNums[] = { 78.0f, 85.6f, 5.7f, 99.1f };
double dNums[] = { 7.0, 8.6, 9.1, 5.3 };
cout << "iNum排序前:";
show(iNums, sizeof(iNums) / sizeof(iNums[0]));
sort(iNums, sizeof(iNums) / sizeof(int));
cout << "排序后:";
show(iNums, sizeof(iNums) / sizeof(iNums[0]));
cout << endl;
cout << "fNum排序前:";
show(fNums, sizeof(fNums) / sizeof(fNums[0]));
sort(fNums, sizeof(fNums) / sizeof(int));
cout << "排序后:";
show(fNums, sizeof(fNums) / sizeof(fNums[0]));
cout << endl;
return 0;
}
//数组是参数的时候,回自动降为指针
void sort(int nums[], int len) {
int temp;
cout << "sizeof(nums):" << sizeof(nums) << endl;
for (int i = 0; i < len; i++) {
for (int j = 0; j < len - i - 1; j++) {
if (nums[j] > nums[j + 1]) {
temp = nums[j];
nums[j] = nums[j + 1];
nums[j + 1] = temp;
}
}
}
}
void sort(double nums[], int len) {
int temp;
for (int i = 0; i < len; i++) {
for (int j = 0; j < len - i - 1; j++) {
if (nums[j] > nums[j + 1]) {
temp = nums[j];
nums[j] = nums[j + 1];
nums[j + 1] = temp;
}
}
}
}
void show(int nums[], int len) {
for (int i = 0; i < len; i++) {
cout << nums[i] << ",";
}
cout << endl;
}
void show(float nums[], int len) {
for (int i = 0; i < len; i++) {
cout << nums[i] << ",";
}
cout << endl;
}
11.4 模板函数
建立一个通用函数
a.函数定义时不指定具体的数据类型
b.函数被调用时根据实参反推数据类型-类型的参数化
template<typename T>
template<class T> //早期写法
12 面向对象
12.1 面向对象编程
所谓面向对象就是基于对象概念,以对象为中心,以类和继承为构造机制,来认识、理解、刻画客观世界和设计、构建相应的软件系统(模拟现实)
12.2 抽象和类
抽象:从具体事务抽取共同的本质特征
类:一种将抽象转换为用户定义类型的工具(类的实例称为对象,类中的变量和函数称为成员)