#include<iostream>
using namespace std;
//1.1HellowWorld
int main() {
cout << "Hello world" << endl;
system("pause");
return 0;
}
//1.(2./3.)变量
int main() {
//变量的定义
//语法:数据类型 变量名 = 初始值
int a = 10;
cout << "a = " << a << endl;
system("pause");
return 0;
}
//1.4.常量
//1、宏常量
#define day 7
int main() {
cout << "一周里总共有 " << day << " 天" << endl;
//day = 8; //报错,宏常量不可以修改
//2、const修饰变量->常量
const int month = 12;
//month = 24; //报错,常量是不可以修改的
cout << "一年里总共有 " << month << " 个月份" << endl;
system("pause");
return 0;
}
//1.5.关键字:在给变量或者常量起名称时候,
//不要用C++得关键字,否则会产生歧义。
//1.6.标识符命名规则:* 标识符不能是关键字 * 标识符只能由字母、数字、下划线组成
//* 第一个字符必须为字母或下划线* 标识符中字母区分大小写
//1.7.数据类型:给变量分配内存
//2.1/2.sizeof关键字:统计数据类型所占内存大小
int main() {
cout << "short类型 占用空间" << sizeof(short) << endl;
//int、long、long long
system("pause");
return 0;
}
//2.3实型(浮点型):表示小数
int main() {
float f1 = 3.14f;
double d1 = 3.14;
cout << f1 << endl;
cout << d1 << endl;
cout << "float sizeof=" << sizeof(f1) << endl;
cout << "float sizeof=" << sizeof(d1) << endl;
//科学计数
float f2 = 3e2;
float f3 = 3e-2;
cout << "f2=" << f2 << endl;
cout << "f3=" << f3 << endl;
system("pause");
return 0;
}
//2.4字符型
int main() {
char ch = 'a';
cout << ch << endl;
cout << sizeof(char) << endl;
cout << (int)ch << endl;//ASCII=97放入到存储单元
ch = 97;
cout << ch << endl;
system("pause");
return 0;
}
//2.5转义字符
int main() {
cout << "\\" << endl;//代表一个反斜线字符
cout << "\tHello" << endl;//(跳到下一个TAB位置)
cout << "\n" << endl;//换行
system("pause");
return 0;
}
//2.6 字符串型
//#include <string>
int main() {
char str1[] = "hellow world";//str1[]="",不需要头文件
//string str = "hellow world";//需要头文件
cout << str1 << endl;
system("pause");
return 0;
}
//布尔类型 bool
int main() {
bool flag = true;//false 0
cout << flag << endl;//1
cout << sizeof(bool) << endl;//1
system("pause");
return 0;
}
//2.8 数据的输入:用于从键盘获取数据
int main() {
int a = 0;//float、double、char、string、bool
cout << "请输入整形变量:" << endl;
cin >> a;
cout << a << endl;
system("pause");
return EXIT_SUCCESS;
}
//3.运算符
//3.1算术运算符
int main() {
//double a1 = 0.25;
//double b1 = 0.35;
int a1 = 10;
int b2 = 20;//0
cout << a1 / b2 << endl;//+、-、*、/(除数不能为0,小数可以相除)
//%取模(double 小数不可以取模),
//区别
//前置递增先对变量进行++,再计算表达式
int a2 = 10;
int b2 = ++a2 * 10;
cout << b2 << endl;
//后置递增先计算表达式,后对变量进行++
int a3 = 10;
int b3 = a3++ * 10;
cout << b3 << endl;
system("pause");
return 0;
}
//3.2赋值运算符:=,+=,-=,*=,/=.%=
//3.3比较运算符:==,!=,<=(==“真”用数字“1”来表示, “假”用数字“0”来表示)
//3.4逻辑运算符:!,&&,||
//4.程序流程结构
//4.1选择结构
int main() {
int score = 501;
if (score > 600) {
cout << "考上" << endl;
}
else if(score>500) {
cout << "考上二本" << endl;
}
else {
cout << "为考上" << endl;
}
system("pause");
return 0;
}
//4.1.2三目运算符
int main() {
int a = 10;
int b = 20;
int c = 0;
c = a > b ? a : b;
(a > b ? a : b) = 100;//b=100
cout << "c=" << c<< endl;
system("pause");
return 0;
}
//4.1.3 switch:switch不可以判断区间
int main() {
int score = 8;
switch (score) {//switch语句中表达式类型只能是整型或者字符型
case 10://case里如果没有break,那么程序会一直向下执行
case 9:
cout << "经典" << endl;
break;
case 8:
cout << "肥肠好" << endl;
break;
default:
cout << "烂片" << endl;
break;
}
system("pause");
return 0;
}
//4.2循环结构
//4.2.1 while
int main() {
int num = 0;
while (num < 10) {
cout << "num=" << num << endl;
num++;
}
system("pause");
return 0;
}
//4.2.2 do...while:与while的区别在于==do...while会先执行一次循环语句==,再判断循环条件
int main() {
int num = 10;
do{
cout << "num=" << num << endl;
num++;
} while (num < 10);
system("pause");
return 0;
}
//4.2.3 for
int main() {
for (int i = 0;i < 10;i++) {
cout << i << endl;
}
system("pause");
return 0;
}
//4.2.4 嵌套循环
int main() {
for (int i = 0;i < 10;i++) {
for (int j = 0;j < 10;j++) {
cout << '*' << ' ';
}
cout << endl;
}
system("pause");
return 0;
}
//4.3跳转语句
//4.3.1 break:在嵌套循环中,跳出最近的内层循环语句
int main() {
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;
}
//4.3.2continue语句
int main() {
for (int i = 0;i < 10;i++) {
if (i%2) {
continue;
}
cout << i << endl;
system("pause");
return 0;
}
//4.3.3 goto
int main() {
cout << '1' << endl;
goto FLAG;
cout << '2' << endl;
FLAG://是:
cout << '5' << endl;
system("pause");
return 0;
}
//5.数组
//5.2 一维数组
//5.2.1 一维数组定义方式
int main() {
//1)
int score[10];
score[0] = 100;
//cout << score[0] << endl;//score[1-9]!=0
//2)
int score2[10] = {100,90,80 };//如果{}内不足10个数据,剩余数据用0补全
for (int i = 0;i < 10;i++) {
//cout << score2[i] << endl;
}
//3)
int score3[] = {100,90,80,7,6,5,4,3,2,4};
for (int i = 0;i<10;i++) {
cout << score3[i] << endl;
}
system("pause");
return 0;
}
//5.2.2 一维数组数组名
int main() {
int arr[10] = { 1,2,3,4,5,6,7,8,9,10 };
cout << "数组内存" << sizeof(arr) << endl;
cout << "元素内存" << sizeof(arr[0]) << endl;
cout << "元素个数" << sizeof(arr) / sizeof(arr[0]) << endl;
cout << "数组首地址" << (int)arr<< endl;//无int,返回ASICLL
cout << "第元素地址" << (int)&arr[0] << endl;
//arr=100,数组名是常量,不可以赋值
system("pause");
return 0;
}
//5.3 二维数组
int main() {
//1)
int arr[2][3];
arr[1][2] = 1;
//2)
int arr2[2][3] = { {1,2,3},{4,5,6} };
//3)
int arr3[2][3] = { 1,2,3,4,5,6 };
//4)
int arr4[][3] = { 1,2,3,4,5,6 };
for (int i = 0;i < 2;i++) {
for (int j = 0;j < 3;j++) {
cout << arr2[i][j] << ' ';
}
cout << endl;
}
system("pause");
return 0;
}
//5.3.2 二维数组数组名
int main() {
int arr[2][3] = {{1,2,3},{4,5,6}};
cout << "数组大小" << sizeof(arr) << endl;
cout << "数组行数" << sizeof(arr) / sizeof(arr[0]) << endl;
cout << "数组列数" << sizeof(arr[0]) / sizeof(arr[0][0]) << endl;
cout << "数组首地址" << arr << endl;
cout << "第一行地址" << (int) & arr[0] << endl;
system("pause");
return 0;
}
//6 函数
//6.1 概述:代码封装
//6.2 函数的定义
//6.3 函数的调用
int add(int num1, int num2) {
int sum = num1 + num2;
return sum;
}
int main() {
int a = 1;
int b = 10;
int sum = add(a, b);
cout << "sum=" << sum << endl;
system("pause");
return 0;
}
//6.4 值传递:值传递时,形参是修饰不了实参的
void swap(int num1, int num2) {
cout << "交换前:" << endl;
cout << "num1=" << num1 << endl;
cout << "num2=" << num2 << endl;
int tmp = num1;
num1 = num2;
num2 = tmp;
cout << "交换后:" << endl;
cout << "num1=" << num1 << endl;
cout << "num2=" << num2 << endl;
//return ,void不需要return
}
int main() {
int a = 1;
int b = 10;
swap(a, b);
cout << "mian中的 a = " << a << endl;
cout << "mian中的 b = " << b << endl;
system("pause");
return 0;
}
//6.5 函数的常见样式
//1)无参无返
void test1(){
// void a=10;//注意无类型->无内存->无法分配
cout << "test1" << endl;
}
//2)无参有返
int test2() {
cout << "test2" << endl;
return 10;
}
//3)有参无返
void test3(int a) {
cout << "test3" << endl;
cout << "a=" << a << endl;
}
//4)有参有返
int test4(int a,int b) {
cout << "test4" << endl;
int sum = a + b;
return sum;
}
//6.6 函数的声明:告诉编译器函数名称及如何调用函数。
//函数的实际主体可以单独定义。
//声明->定义->调用
//声明可以多次,定义只能一次
//1.声明
int max(int a, int b);
int max(int a, int b);
//2.定义
int max(int a, int b) {
return a > b ? a : b;
}
//3.调用
int main() {
int a = 11;
int b = 111;
cout << max(a, b) << endl;//key
system("pause");
return 0;
}
//6.7 函数的分文件编写:让代码结构更加清晰
/*
//1..h的头文件:写函数的声明
//swap.h文件
#include<iostream>
using namespace std;
//2..cpp的源文件:写函数的定义
//swap.cpp文件
#include "swap.h"
void swap(int a, int b)
{
int temp = a;
a = b;
b = temp;
cout << "a = " << a << endl;
cout << "b = " << b << endl;
}
*/
//3.通过头文件,直接进行调用
#include "swap.h"
int main() {
int a = 10;
int b = 20;
swap(a, b);
system("pause");
return 0;
}
//7.指针:通过指针间接访问内存,利用指针变量保存地址
//7.2 指针变量的定义和使用
int main() {
int a = 1000;
int *p;
//1)地址:普通变量存放的是数据,指针变量存放的是地址
p = &a;//指针指向变量a的地址
cout << &a << endl;
cout << p << endl;
//2.对指针变量解引用:通过*操作指针变量指向的内存
cout << "*p=" << *p << endl;//1000
system("pause");
return 0;
}
//7.3 指针所占内存空间:所有指针类型在64/32位操作系统下是8/4个字节
int main() {
int a = 10;
int* p;
p = &a; //指针指向数据a的地址
cout << *p << endl; //* 解引用
cout << sizeof(p) << endl;
cout << sizeof(char*) << endl;
cout << sizeof(float*) << endl;
cout << sizeof(double*) << endl;
system("pause");
return 0;
}
//7.4 空指针和野指针:都不是我们申请的空间,因此不要访问
int main() {
//1)空指针:
int* p1 = NULL;//指针变量p指向内存地址编号为0的空间
//cout << *p1 << endl;//内存编号0 ~255为系统占用内存,不允许用户访问
//#野指针:指针变量指向非法的内存空间(权限空间)
int* p = (int*)0x1100;
cout << *p << endl;//访问野指针报错
system("pause");
return 0;
}
//7.5 const修饰指针:常量指针/指针常量
//技巧:看const右侧紧跟着的是指针还是常量, 是指针就是常量指针,是常量就是指针常量
int main() {
int a = 10;
int b = 10;
//1)const修饰的是指针,指向可以改,指向的值不可以更改
const int* p1 = &a;
p1 = &b;//正确
//*p1 = b;报错
//2)const修饰的是常量,指向不可以改,指向的值可以更改
int* const p2 = &a;
//p2 = &b;报错
*p2 = 100;//正确
//
//3)const既修饰指针又修饰常量
const int* const p3=&a;
//p3=&b;
//*p3=100//都报错
system("pause");
return 0;
}
//7.6 指针和数组;利用指针访问数组中元素
int main() {
int arr[] = { 0,2,3,4,5,6,7,8,9,10 };
int* p = arr;//区别:int a = 10; int * p1= &a;
cout << "指针访问第一个元素: " << *p << endl;//arr[0]
//指针-loop
for (int i = 0;i < 10;i++) {
cout << *p << endl;
p++;
}
system("pause");
return 0;
}
//7.7 指针和函数:利用指针作函数参数,可以修改实参的值
//总结:如果不想修改实参,就用值传递,如果想修改实参,就用地址传递
void swap1(int a, int b) {
int temp = a;
a = b;
b = temp;
}
void swap2(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}
int main() {
int a = 11;
int b = 12;
swap1(a, b);//值传递
cout << "a = " << a << endl;
cout << "b = " << b << endl;
swap2(&a, &b);//地址传递
cout << "a = " << a << endl;
cout << "b = " << b << endl;
system("pause");
return 0;
}
//7.8 指针、数组、函数(能改变原数组):例如-封装一个函数,利用冒泡排序,实现对整型数组的升序排序
//注意:当数组名传入到函数作为参数时,被退化为指向首元素的指针
void bubbleSort(int *arr, int len) {//也可以写为int arr[]
for (int i = 0;i < len - 1;i++) {
for (int j = 0; j < len - 1 - i; j++) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
void printArr(int arr[],int len){
for (int i = 0;i < len;i++) {
cout << arr[i] << endl;
}
}
int main() {
int arr[10] = { 4,3,6,9,1,2,10,8,7,5 };
int len = sizeof(arr) / sizeof(int);
bubbleSort(arr, len);
printArr(arr, len);
system("pause");
return 0;
}
//8 结构体:属于用户自定义的数据类型 ,允许用户存储不同的数据类型
//8.2 结构体定义和使用
//> 总结1:定义结构体时的关键字是struct,不可省略 总结2:创建结构体变量时,关键字struct可以省略
//总结3:结构体变量利用操作符 ''.'' 访问成员
//所有方式都要先定义结构体
struct student {
string name;
int age;
int score;
}stu3;
int main() {
//方式1
struct student stu1;//struct可以省略
stu1.name = "张三";
stu1.age = 18;
stu1.score = 100;
//方式2
struct student stu2 = { "李四",19,60 };
//方式3:
stu3.name = "王五";
stu3.age = 18;
stu3.score = 80;
cout << "姓名:" << stu3.name << " 年龄:" << stu3.age << " 分数:" << stu3.score << endl;
system("pause");
return 0;
}
//8.3 结构体数组:将自定义的结构体放入到数组中方便维护
struct student {
string name;
int age;
int score;
};
int main() {
struct student arr[3] = { {"张三",18,80 },{"李四",19,60 },{"王五",20,70 } };
for (int i = 0;i < 3;i++) {
cout << "姓名:" << arr[i].name << " 年龄:" << arr[i].age << " 分数:" << arr[i].score << endl;
}
system("pause");
return 0;
}
//8.4 结构体指针:通过指针访问结构体中的成员
//利用操作符 `-> `可以通过结构体指针访问结构体属性
struct student {
string name;
int age;
int score;
};
int main() {
struct student stu = { "张三",18,100, };
//
struct student* p = &stu;//区别:* p=arr数组
p->score = 80;
//
cout << "姓名:" << p->name << " 年龄:" << p->age << " 分数:" << p->score << endl;
system("pause");
return 0;
}
//8.5 结构体嵌套结构体:子结构体
//例如:一个老师的结构体中,记录一个学生的结构体
struct student{
string name;
int age;
int score;
};
struct teacher {
int id;
string name;
int age;
struct student stu;//key:子结构体 学生
};
int main() {
struct teacher t1;
t1.id = 10000;
t1.name = "老王";
t1.stu.name = "张三";
cout << "教师 职工编号: " << t1.id << " 姓名: " << t1.name << endl;
cout << "辅导学员 姓名: " << t1.stu.name << endl;
system("pause");
return 0;
}
//8.6 结构体做函数参数:将结构体作为参数向函数中传递-值传递、地址传递
struct student {
string name;
int age;
int score;
};
//值传递:不改实参
void printStu(student stu) {
stu.age = 28;
cout << "子函数中 姓名:" << stu.name << " 年龄: " << stu.age << " 分数:" << stu.score << endl;
}
//址传递:改实参
void printStu2(student* stu) {
stu->age=28;
cout << "子函数中 姓名:" << stu->name << " 年龄: " << stu->age << " 分数:" << stu->score << endl;
}
int main() {
struct student stu = { "张三",18,100 };
printStu(stu);
cout << "主函数中 姓名:" << stu.name << " 年龄: " << stu.age << " 分数:" << stu.score << endl;
printStu2(&stu);
cout << "主函数中 姓名:" << stu.name << " 年龄: " << stu.age << " 分数:" << stu.score << endl;
}
//8.7 结构体中 const使用场景:用const来防止误操作
struct student {
string name;
int age;
int score;
};
void printStu( const student* stu) {//加const防止函数体中的误操作
//stu->age=100;//key:会执行失败(const修饰的是指针,指向可以改,指向的值不可以更改)(student* const stu可以)
cout << "姓名:" << stu->name << " 年龄:" << stu->age << " 分数:" << stu->score << endl;
}
int main() {
struct student stu = { "张三",18,100 };
printStu(&stu);
system("pause");
return 0;
}
《c++基础语法笔记》
最新推荐文章于 2024-08-24 14:01:36 发布