欢迎访问 生活随笔!

生活随笔

当前位置: 首页 >

立志10天学会C++基础应用—day02 代码清晰易懂 涉及数据结构算法的知识 写完了~我也麻了

发布时间:2025/3/15 70 豆豆
生活随笔 收集整理的这篇文章主要介绍了 立志10天学会C++基础应用—day02 代码清晰易懂 涉及数据结构算法的知识 写完了~我也麻了 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

  哈喽,很高兴又见面啦,一起加油一起学习,欢迎您的关注!https://blog.csdn.net/hanhanwanghaha学习路上有您的关注是我的荣幸,一起进步是我的动力,明天也一起加油啊!

以往链接

立志10天学会C++基础应用—day01

文章目录

    • 以往链接
  • 一、循环结构
    • A01while循环.cpp
    • A02while猜数字.cpp
    • A03dowhile循环.cpp
    • A04dowhile水仙花数.cpp
    • A05for循环敲桌子案例.cpp
  • 二、嵌套循环
    • A06嵌套循环星星图.cpp
    • A06嵌套循环乘法口诀表.cpp
  • 三、跳转语句
    • A07跳转语句break.cpp
    • A08跳转语句continue.cpp
    • A09跳转语句goto.cpp
  • 四、数组
    • A10数组的定义.cpp
    • A11八只狗狗比体重.cpp
    • A12元素的逆置.cpp
    • A13冒泡排序.cpp
    • A14二维数组的定义方式.cpp
    • A14二维数组的数组名称.cpp
    • A16二维数组的应用三位同学的总成绩.cpp
  • 五、函数
    • A17函数的调用.cpp
    • A17函数的值传递.cpp
    • A19函数的常见样式.cpp
    • A20函数的分文件编写.cpp
  • 六、指针
    • A21指针的定义和使用.cpp
    • A22指针内存大小空指针野指针.cpp
    • A23const修饰指针.cpp

一、循环结构

A01while循环.cpp

#include <iostream> using namespace std;int main() {int num = 0;num++;//()中填入循环条件//语法:while(循环条件){循环语句}while (num < 10) {num *= 2;cout << num << endl;}system("pause");return 0;}

A02while猜数字.cpp

#include <iostream> using namespace std; #include<ctime>int main() {//添加随机数种子,作用利用当前系统时间生成随机数,防止每次随机数都一样‘srand((unsigned int)time(NULL));//1、系统生成0到99的随机数int num = rand() % 100;//cout << num << endl;//2、玩家进行猜测cout << "请输入你猜的数" << endl;int ans = 0;//3、判断玩家的猜测//while死循环,等待中断信号while (1) {cin >> ans;if (ans<num) {cout << "猜测过小,请你再猜一遍" << endl;}else if(ans>num) {cout << "猜测过大,请你再猜一遍" << endl;}else {cout << "恭喜您猜对了" << endl;break;//中断信号}}//猜对 退出游戏//猜错 提示猜的结果 过大或者过小 重新返回第二步system("pause");return 0; }

A03dowhile循环.cpp

用这个写了一个死循环,无聊到爆~今天的白日梦乐趣

#include <iostream> using namespace std;int main() {/*语法:do{循环语句}while{循环条件}注意:与while的区别在于do...while会限制性一次循环语句,再判断循环条件*/int num = 0;/*do {cout <<"小王要挣" << num <<"元" << endl;num+=10000;} while (num);*///此时的num不为0,结果会陷入死循环//与while的区别:把上面的注释掉,这两个分别运行一下就知道了//其实就是do{}多了一个执行条件while(num) {cout << "噩梦" << num << "个" << endl;num --;}system("pause");return 0;}

A04dowhile水仙花数.cpp

#include <iostream> using namespace std;int main() {//查询100到999中的所有水仙花数int num = 100;do {int a = 0;//个位int b = 0;//十位int c = 0;//百位a = num % 10;//提取个位的数b = num / 10 % 10;//提取十位的数c = num / 100 % 10;//提取百位的数if (a * a * a + b * b * b + c * c * c == num) {//如果是水仙花数,就打印cout << num << endl;}num++;} while (num < 1000);system("pause");return 0; }

A05for循环敲桌子案例.cpp

案例描述:从1开始数到数字100,如果数字个位含有7,或者数字十位含有7,或者该数字是7的倍数,我们打印敲桌子,其余数字直接打印输出。

#include <iostream> using namespace std;int main() {//语法:for(起始表达式:条件表达式:末尾表达式){循环语句}for (int i = 0;i <= 100;i++) {//数字个位含有7,或者数字十位含有7,或者该数字是7的倍数if (i / 7 == 0 || i / 10 ==7 || i % 7 == 0) {cout << "敲桌子" << endl;}else{cout << i << endl;}}system("pause");return 0;}

二、嵌套循环

A06嵌套循环星星图.cpp

打印一个星星图

#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; }

A06嵌套循环乘法口诀表.cpp

#include <iostream> using namespace std;int main() {//乘法口诀表//打印行数for (int i = 1;i < 10;i++) {for (int j = 1;j <= i;j++) {cout << j << "*" << i << "=" << j * i <<" ";}cout << endl;}system("pause");return 0; }

欢迎关注:https://blog.csdn.net/hanhanwanghaha

三、跳转语句

A07跳转语句break.cpp

#include <iostream> using namespace std;int main() {//break的几种用法//1、出现在switch中cout << "请选择您的成绩等级" << endl;cout << "1、A" << endl;cout << "2、B" << endl;cout << "3、C" << endl;//创建结果的变量int grade = 0;//用户输入等级cin >> grade;switch (grade){case 1:cout << "您可以读市重点" << endl;break;case 2:cout << "您可以读区重点" << endl;break;case 3:cout << "您可以读乡镇学校" << endl;break;default:break;}//2、出现在for循环中for (int i = 0; i < 10; i++){if (i == 7) {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;}

A08跳转语句continue.cpp

执行到continue那一行,就不会执行后面的了,然后循环前面的

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

A09跳转语句goto.cpp

#include <iostream> using namespace std;int main() {//语法:goto 标记//如果标记的名称存在,执行到goto的语句时,会跳转到标记的位置cout << "我是宝藏" << endl;cout << "女孩" << endl;goto baby;cout << "❥❥❥❥❥❥❥❥❥❥❥❥" << endl;baby:cout << "的成长日记" << endl;cout << "小伙伴你好啊" << endl;system("pause");return 0;}

四、数组

A10数组的定义.cpp

#include <iostream> using namespace std;int main() {//数组/*数组的三种定义类型1、数据类型 数组名[数组长度];2、数据类型 数组名[数组长度]{ 值1,值2 ...};3、数据类型 数组名[ ]= { 值1,值2 ...};*///3、时间关系就演示第三个(常用)int arr[] = { 1,2,5,34,344,221,666,777,9998 };for (int i = 0;i < 2; i++ ) {cout << arr[i] << endl;}/*数组名称的作用:1、可以通过数组名统计整个数组占用内存大小2、可以通过数组名查看数组首地址*///1、可以通过数组名统计整个数组占用内存大小cout << "整个数组所占空间为" << sizeof(arr) << endl;cout << "第一个数组所占空间为" << sizeof(arr[0]) << endl;cout << "数组的元素个数为" << sizeof(arr)/ sizeof(arr[0]) << endl;//2、可以通过数组名查看数组首地址cout << "数组首地址为" << (int)arr << endl;cout << "数组第一个地址为" << (int)&arr[0] << endl;cout << "数组第二个地址为" << (int)&arr[0] << endl;//注意:数组名是常量不可以赋值system("pause");return 0;}

A11八只狗狗比体重.cpp

#include <iostream> using namespace std;int main() {//八个小狗狗比体重//1、创建8个狗狗的体重的数组int arr[] = { 200,99,98,110,20,50,55,77 };//2、从数组中找到最大值int max = 0;//先认定一个最大值为0for (int i = 0;i < 8;i++) {if (arr[i]>max) {max = arr[i];}}cout << "狗狗的体重最大值为" << max << endl;system("pause");return 0;}

A12元素的逆置.cpp

此时此刻我看到这个代码的心情是复杂的,因为我以前好像考到过这个代码,仿佛遇见了前世的爱人那般熟悉。不过有个更简单方法(我记得是利用离散数学中的求逆,我之前写过这个代码,懒得找了)

#include <iostream> using namespace std;int main() {//1、实现12345的逆置//1、创建数组int arr[] = { 1,2,3,4,5 };cout << "数组逆置前为" << endl;for (int i = 0;i < 5;i++) {cout << arr[i] << endl;}//2、实现逆置int start = 0;//初始位置为0//最后一个位置可以用整个数组的长度除以第一个元素位置的长度-1int end = sizeof(arr) / sizeof(arr[0]) - 1;//末尾位置while (start < end) {//实现元素互换int temp = arr[start];arr[start] = arr[end];arr[end] = temp;//下标更新start++;end--;}//3、打印逆置后的数组cout << "数组逆置后为" << endl;for (int i = 0;i < 5;i++) {cout << arr[i] << endl;}system("pause");return 0;}

https://blog.csdn.net/hanhanwanghaha宝藏女孩 欢迎您的关注!
欢迎关注微信公众号:宝藏女孩的成长日记
让这个可爱的宝藏女孩在努力的道路上与你一起同行!
如有转载,请注明出处(如不注明,盗者必究)

A13冒泡排序.cpp

冒泡排序python版本

如果不懂话建议看一下王道数据结构的讲解,这个比较详细,这涉及到数据结构的内容。

这里安利一个之前我看到的各种排序算法动画演示过程
http://tools.jb51.net/static/api/paixu_ys/index.html

冒泡排序的具体过程
1.比较相邻的元素。如果第一个比第二个大,就交换他们两个。
2.对每一对相邻元素做同样的工作,执行完毕后,找到第一个最大值。
3.重复以上的步骤,每次比较次数-1,直到不需要比较

#include <iostream> using namespace std;int main() {int arr[] = { 99,33,45,22,556,775,32,45,66 };cout<<"排序前" << endl;for (int i = 0;i < 9;i++) {cout<< arr[i]<<" ";}cout<< endl;//开始冒泡排序//排序总轮数 = 元素个数 - 1;(这个注释看不懂的话就拿几个数验证一遍就知道了)for (int i = 0;i < 9 - 1;i++) {//内层循环:每轮对比次数=元素个数–排序轮数–1 :for (int j = 0;j < 9 - i - 1;j++) {//如果前一个数字比后一个数字大,那么交换两个数字if (arr[j] > arr[j + 1]) {int temp = arr[j];arr[j] = arr[j + 1];arr[j + 1] = temp;}} }cout << "排序后" << endl;for (int i = 0;i < 9;i++) {cout << arr[i] << " ";}system("pause");return 0;}

A14二维数组的定义方式.cpp

1. 数据类型数组名[行数][列数];
2.数据类型数组名[行数][列数]= { {数据1,数据2},{数据3,数据4 } };
3.数据类型数组名[行数][列数]={数据1,数据2,数据3,数据4};
4.数据类型数组名[][列数]={数据1,数据2,数据3,数据4};
推荐用第二种

#include <iostream> using namespace std;int main() {//2.数据类型数组名[行数][列数]= { {数据1,数据2}{数据3,数据4 } };int arr1[2][3] = {{1,2,3},{4,5,6}};for (int i = 0;i < 2;i++) {for (int j = 0;j < 3;j++) {cout << arr1[i][j] << " ";}cout<< endl;}//3.数据类型数组名[行数][列数] = { 数据1,数据2,数据3,数据4 };int arr2[2][2] = { 1234 };for (int i = 0;i < 2;i++) {for (int j = 0;j < 2;j++) {cout << arr1[i][j] << " ";}cout << endl;}system("pause");return 0;}

A14二维数组的数组名称.cpp

#include <iostream> using namespace std;int main() {//二维数组名称用途//1、可以查看占用内存空间大小double 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][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;}

A16二维数组的应用三位同学的总成绩.cpp

#include <iostream> using namespace std; #include <string>int main() {//二维数组的案例——三位同学的的三门课的总成绩统计//1、创建二维数组int scores[3][3]{{100,99,99},{100,100,100},{16,89,100}};string names[3]{ "周杰伦","昆凌","蔡依林" };for (int i = 0;i < 3;i++) {int sum = 0;//统计分数总和变量for (int j = 0;j < 3;j++) {sum += scores[i][j];}cout << names[i] << "的总成绩为" << sum << endl;}system("pause");return 0;}

五、函数

函数说白了就是封装一个功能
函数的定义

A17函数的调用.cpp

#include <iostream> using namespace std; #include <string>//随便定义一个加法函数,num1和num2为形参 int add(int num1, int num2) {int sum = num1 + num2;return sum; };int main() {//在主函数中调用int a = 66;int b = 88;//函数调用方法:函数名称(参数)//ab为实参//当调用函数时,实参赋给形参int c = add(a, b);cout << "最终结果为:" << c << endl;system("pause");return 0;}

A17函数的值传递.cpp

#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 = 99;int b = 89;cout << "a:" << a << endl;cout << "b:" << a << endl;//当我们做值传递的时候,函数的形参发生改变,并不会影响实参swap(a, b);cout << "a:" << a << endl;cout << "b:" << b << endl;system("pause");return 0;}

A19函数的常见样式.cpp

常见的函数样式有4种
1.无参无返
2.有参无返
3.无参有返
4.有参有返

#include <iostream> using namespace std;//1、无参无返 void test1() {cout << "哈喽 小姐姐你好!" << endl; }//2、有参无返 void test2(int a) {cout << "哈喽 小姐姐你好!我已经" <<a<<"岁了" << endl; }//3、无参有返 int test3() {cout << "哈喽 小姐姐你好!" << endl;return 888; } //4、有参有返int test4(int a) {cout << "哈喽 小姐姐你好!" << endl;return 888; }int main() {test1();test2(20);int num = test3();cout << "num=" << num << endl;int num1 = test4(888);cout << "num1=" << num1 << endl;system("pause");return 0;}

A20函数的分文件编写.cpp

作用:让代码结构更加清晰简洁
函数分文件编写─般有4个步骤

  • 创建后缀名为.h的头文件
  • 创建后缀名为.cpp的源文件
  • 在头文件中写函数的声明
  • 在源文件中写函数的定义
  • 这种造型

    A20函数的分文件编写.cpp

    #include <iostream> using namespace std; #include "swap.h"//函数的分文件编写//实现两个数字进行交换的函数 函数的声明 //void swap(int num1, int num2); 函数的定义 //void swap(int num1, int num2) { // int temp = num1; // num1 = num2; // num2 = temp; // // // cout << "num1=" << num1 << endl; // cout << "num2=" << num2 << endl; //}int main() {int a = 99;int b = 88;swap(a,b);system("pause");return 0;}

    swap.h

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

    swap.cpp

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

    六、指针

    A21指针的定义和使用.cpp

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

    A22指针内存大小空指针野指针.cpp

    #include <iostream> using namespace std;int main() {//不管是什么数据类型,要求熟记://在32位操作系统下,指针是占4个字节空间大小,//在64位操作系统下,指针是占8个字节空间大小cout << "sizeof(int *=)" << sizeof(int *) << endl;cout << "sizeof(float *=)" << sizeof(float *) << endl;cout << "sizeof(double *=)" << sizeof(double *) << endl;cout << "sizeof(char *=)" << sizeof(char *) << endl;//空指针//指针变量p指向内存地址编号为0的空间int* p = NULL;//访问空指针报错//内存编号0 ~255为系统占用内存,不允许用户访问//cout<<*p<<endl;//野指针:指针变量指向非法的内存空间int* p = (int *)0x1100;//访问野指针运行不出来cout << *p << endl;system("pause");return 0;}

    A23const修饰指针.cpp

    const修饰指针有三种情况:

  • const修饰指针—常量指针
  • const修饰常量—指针常量工
  • const既修饰指针,又修饰常量
  • 我自己是这么记的,谁在后面谁就可以变

    #include <iostream> using namespace std;int main() {// 1、const修饰指针常量指针int a = 8;int b = 6;const int* p = &a;//指针指向的值不可以改,指针的指向可以改//* p = 20;错误p = &b;//正确// 2、const修饰常量指针常量//指针的指向不可以改,指针指向的值可以改int* const p2 = &a;*p2 = 88;//p2 = &b;错误//3、const修饰指针和常量const int* const p3 = &a;//指针的指向和指针指向的值都不可以改//*p3 =100;//错误//p3 = &b;//错误system("pause");return 0;}

    https://blog.csdn.net/hanhanwanghaha 宝藏女孩的成长日记 欢迎您的关注!
    欢迎关注微信公众号:宝藏女孩的成长日记
    让这个可爱的宝藏女孩在努力的道路上与你一起同行!
    如有转载,请注明出处(如不注明,盗者必究)

    终于写完了,完成任务,看武林外传去!

    总结

    以上是生活随笔为你收集整理的立志10天学会C++基础应用—day02 代码清晰易懂 涉及数据结构算法的知识 写完了~我也麻了的全部内容,希望文章能够帮你解决所遇到的问题。

    如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。