#include <iostream>
#include <ctime>
#include <string>
#include"swap.h"
using namespace std;
//1.单行注释
//2.多行注释
/*
main是一个程序的入口,
每个程序都必须有这么一个函数
有且仅有一个
*/
//1.#define 宏常量
#define Day 7
//函数的定义
int add(int num1, int num2)
{
int sum12 = num1 + num2;
return sum12;
}
//如果函数不需要返回值,声明时可以写void
//无参无返函数
void test01(){
cout << "this is test01" << endl;
}
//有参无返函数
void test02(int o) {
cout << "this is test02 o=" <<o<< endl;
}
//无参有返函数
int test03() {
cout << "this is test03" << endl;
return 1000;
}
//有参有返函数
int test04(int a04) {
cout << "this is test04" <<a04<< endl;
return 1000;
}
//提前告诉函数的存在,可以利用函数的声明
void bubbleSort(int* arr, int len);
int bigger(int x04, int y04);
void swap(int* p1, int *p2) {
int temp = *p1;
*p1 = *p2;
*p2 = temp;
}
//结构体
struct student
{
string name;
int age;
int score;
};
//new的基本语法
int* func(){
int* p = new int(10);
return p;
}
void test05() {
int* p = func();
cout << *p << endl;
delete p; //释放内存
}
//函数的分文件编写
/*
1.创建一个.h的头文件
2.创建.cpp的源文件
3.在头文件中函数的声明
4.在源文件中写函数的定义
*/
int main()
{
test05();
cout << "一周总共有:" << Day << "天" << endl;
//2.const修饰的变量也是常量
const int month = 12;
cout << "一年有" << month << "个月" << endl;
//3.短整行(-32768~32767)
short numShort = 10;
cout << numShort << endl;
//4.sizeof可以求出数据类型占用内存大小
cout << "short占用的内存空间为" << sizeof(numShort) << endl;
//5.单精度 float
//6.双精度 double
//默认情况下 输出一个小数,显示6位有效数字
float f1 = 3.14f;
cout << "f1=" << f1 << endl;
//科学计数法
float f2 = 3e2;//3*10^2
cout << "f2=" << f2 << endl;
float f3 = 3e-2;//3*0.1^2
cout << "f3=" << f3 << endl;
int i = 0;
i = (3 + 4, 5 + 6, 7 + 8);
cout << "f3=" << f3 << endl;
//7.字符型变量创建方式
char ch = 'a';
cout << "ch=" << ch << endl;
//字符型变量对应的ASCII编码
cout << (int)ch << endl;
//a - 97
//A - 65
//转义字符:换行符 \n 反斜杠 \\ 水平制表符 \t
//创建bool类型
bool flag = true;
cout << flag << endl;
cout << "\\" << endl;
cout << "hello world\n";
cout << "aaa\tworld" << endl;
// string your_name;
//cin >> your_name;
//cout << "Hello, C++!\nMy name is " << your_name << "." << endl;
//return 0;
//前置递增 先让变量+1 然后进行表达式运算
int a = 10;
int b = ++a * 10;
cout << "a=" << a << endl;
cout << "b=" << b << endl;
//后置递增 先进行表达式运算,然后让变量+1
int a1 = 10;
int b1 = a1++ * 10;
cout << "a1=" << a1 << endl;
cout << "b1=" << b1 << endl;
// %=
a %= 2;
cout << "a=" << a << endl;
// !=代表不等于
cout << (a != b) << endl;
//逻辑运算符 !非 &&与(同真为真,其余为假) ||或(同假为假,其余为真)
//除了0都是真
cout << !a << endl; //0
cout << !!a << endl; //1
//三目运算符(返回的是变量,可以继续赋值)
//三个变量 a b c
//a和b比较,将变量大的值赋值给c
int a3 = 10;
int b3 = 20;
int c3 = 0;
c3 = (a3 > b3 ? a3 : b3);
cout << c3 << endl;
//switch语句:执行多条件语句
//switch语句缺点:判断时只能是整型或者字符型,不能是区间
int score = 7;
switch (score)
{
case 10:
cout << "你认为是经典" << endl;
break;//退出当前分支
case 9:
cout << "你认为是经典" << endl;
break;//退出当前分支
case 7:
cout << "你认为一般" << endl;
break;//退出当前分支
default:
cout << "你认为是垃圾" << endl;
break;
}
int number = 0;
while (number < 10)
{
cout << number << endl;
number++;
}
//添加随机数种子,防止每次随机数都一样
//系统生成随机数
srand((unsigned int)time(NULL));
int num3 = rand() % 100 + 1;//rand()%100+1生成0+1~99+1随机数
cout << num3 << endl;
//do...while语句
//和whlie的区别在于do...while语句会先执行一次循环语句
int num4 = 0;
do
{
cout << num4 << endl;
num4++;
} while (num4 < 10);
//for(起始表达式;条件表达式;末尾循环体)
//求出所有水仙花数
for (int i = 100; i <= 999; i++)
{
int a = i / 100;//百位数
int b = i / 10 % 10;//十位数
int c = i % 10;//个位数
if (a * a * a + b * b * b + c * c * c == i)
{
cout << i << endl;
}
}
//continue语句可以筛选条件,执行到此就不再向下执行,去执行下一次循环
//break会退出循环,而continue不会
cout << "1.xxx" << endl;
cout << "2.xxx" << endl;
goto FLAG; //goto语句:执行到goto语句时,会跳转到标记的位置
cout << "3.xxx" << endl;
cout << "4.xxx" << endl;
FLAG: //是冒号,不是分号
cout << "5.xxx" << endl;
//数组
/*
1.数据类型 数组名[数组长度];
2.数据类型 数组名[数组长度] ={值1,值2...};
3.数据类型 数组名[ ] ={值1,值2...};
*/
int arr[3];
arr[0] = 10;
arr[1] = 20;
arr[2] = 30;
cout << arr[1] << endl;
int arr2[3] = { 10,20,30 };
for (int i = 0; i < 3; i++)
{
cout << arr2[i] << endl;
}
int arr3[] = { 30,20,10 };
int arr1[80] = { 30,40,34,14,45 };
cout << sizeof(arr1[0]) << endl;
cout << sizeof(arr1) << endl;
//可以获取数组在内存中的首地址
cout << arr1 << endl;
cout << (int)arr1 << endl; //转换成十进制
cout << "数组中的第一个元素地址为:" << (int)&arr[0] << endl;
int arr4[5] = { 300,350,200,400,250 };
int max = 0;
int x = 0;
for (x = 0; x < 5; x++)
{
if (max < arr4[x]) {
max = arr4[x];
}
}
cout << max << endl;
//数组元素的逆置
int arr5[] = { 1,6,2,5,4,7 };
int start = 0;//起始元素下标
int end = sizeof(arr5) / sizeof(arr5[0]) - 1;//末尾元素下标
int temp = arr5[start];
for (; start < end; start++, end--) {
arr5[start] = arr5[end];
arr5[end] = temp;
}
for (int i = 0; i < 6; i++)
{
cout << arr5[i] << endl;
}
//冒泡排序实现升序排列
int arr6[9] = { 4,2,8,0,5,7,1,3,9 };
cout << "排序前" << endl;
for (int i = 0; i < 9; i++)
{
cout << arr6[i] << " ";
}
cout << endl;
//开始冒泡排序
//总共排序轮数为元素个数-1
for (int i = 0; i < 9 - 1; i++)
{
//内层循环对比 次数=元素个数-当前轮数-1
for (int j = 0; j < 9 - i - 1; j++)
{
//如果第一个数字比第二个大,交换两个数字
if (arr6[j] > arr6[j + 1])
{
int temp2 = arr6[j];
arr6[j] = arr6[j + 1];
arr6[j + 1] = temp2;
}
}
}
//排序后结果
for (int i = 0; i < 9; i++)
{
cout << arr6[i] << " ";
}
cout << endl;
//二维数组
int arr7[2][3] =
{
{ 2,3,4 },
{5, 6, 7}
};
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 3; j++)
{
cout << arr7[i][j]<<" ";
}
cout << endl;
}
cout << endl;
//查看二维数组行列数
cout << "二维数组行数为" << sizeof(arr7) / sizeof(arr7[0]) << endl;
cout << "二维数组列数为" << sizeof(arr7[0]) / sizeof(arr7[0][0]) << endl;
//查看二维数组的首地址
cout << "二维数组首地址为" << (int)arr7 << endl;
cout << "二维数组第一行首地址为" << (int)arr7[0] << endl;
cout << "二维数组第一个元素首地址为" << (int)&arr7[0][0] << endl;
//查看具体元素的首地址要加上取址符&
//函数的调用
int aa = add(a3, b3);
cout << "aa ="<<aa << endl;
//值传递时,函数的形参发生改变,并不会影响实参
swap(a3, b3);
test01();
test02(100);
int num_test03 = test03();
cout << "num_test03=" << num_test03 << endl;
int a04 = test04(99999);
cout << "a04=" << a04 << endl;
cout << bigger(aa, a3) << endl;
swap(a3, b3);
//定义指针
int a6 = 10;
//定义指针的语法:数据类型*指针变量名;
int* p;
//让指针记录变量a6的地址
p = &a6;
cout << "指针p为" << p << endl;
//使用指针
//可以通过解引用的方式来找到指针指向的内存
//指针前加*代表解引用,找到指针指向的内存的数据
*p = 1000;
cout << "a6为" << a6 << endl;
//空指针:指针变量指向内存地址为0的空间
int* p2 = NULL;
//空指针不可以访问
//常量指针:指针指向的值不可以改,指针指向可以改
const int* p3 = &a;
//指针常量:指针指向的值可以改,指针指向不可以改
int* const p4 = &a;
//利用指针遍历数组
int* p5 = arr4;
for (int i = 0;i < 4; i++)
{
cout << *p5 << endl;
p5++;
}
//如果是地址传递,可以修饰形参
swap(&a, &b);
cout << a << endl;
cout << b << endl;
//数组长度
int len = sizeof(arr4) / sizeof(arr4[0]);
bubbleSort(arr4, len);
//结构体定义
struct student
{
string name;
int age;
int score;
};
struct student s1;
s1.name = " Wzy ";
s1.age = 18;
s1.score = 100;
cout << s1.name << endl;
struct student s2 = {" wzy ",19,88};
cout << s2.name << endl;
//创建结构体数组
struct student stuArr[3] =
{
{"张三",18 ,100},
{"李四",23,99},
{"王于",21,94}
};
cout << stuArr[0].age << endl;
return 0;
}
int bigger(int x04, int y04){
return x04 > y04 ? x04 : y04;
}
void bubbleSort(int* arr, int len) {
for (int i = 0; i < len - 1; i++)
{
//内层循环对比 次数=元素个数-当前轮数-1
for (int j = 0; j < len - i - 1; j++)
{
//如果第一个数字比第二个大,交换两个数字
if (arr[j] > arr[j + 1])
{
int temp2 = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp2;
}
}
}
//排序后结果
for (int i = 0; i < len; i++)
{
cout << arr[i] << " ";
}
cout << endl;
//struct 默认权限是 公共 public
//class 默认权限是 私有 private
黑马程序员C++笔记(上)
最新推荐文章于 2024-10-17 21:38:17 发布