基礎
1 數據
1.1 常量、變量和helloworld的寫法
- #define day 7
- const修飾變量
#include <iostream>
using namespace std;
// 常量
#define day 7
int main() {
// 常量
const int a = 14;
// 變量
int b = 16;
cout << "a= " << day << endl;
cout << "Hello的World!" << endl;
return 0;
}
1.2 數據類型
short的取值範圍是-32768~32767
1.3 sizeof關鍵字
#include <iostream>
using namespace std;
// 常量
#define day 7
int main() {
int a = 10;
cout << "short類型所佔內存空間爲:" << sizeof(short)<<endl;
cout << "int類型所佔內存空間爲:" << sizeof(a)<<endl;
cout << "long類型所佔內存空間爲:" << sizeof(long)<<endl;
cout << "long類型所佔內存空間爲:" << sizeof(long long)<<endl;
return 0;
}
1.4 浮點型
float佔用空間4字節,7位有效數字(3.14是三位有效數字)
double佔用8字節,15~16位有效數字,默認是雙精度
1.5 字符型
需要單引號,佔位1
#include <iostream>
using namespace std;
int main() {
char ch = 'a'; // 需要單引號
cout << "ch: "<< ch << endl;
//字符型佔用內存大小
cout << "sizeof_char: " << sizeof(ch) << endl; // 1
cout << (int) ch << endl; //強轉int可得到ascii編碼
}
1.6 科學計算
#include <iostream>
using namespace std;
int main() {
float f2 = 3e2; // 3 * 10 ^ 2 = 300
cout << f2 << endl;
float f3 = 3e-2; // 3 * 0.1 ^ 2 = 0.03
cout << f3 << endl;
return 0;
}
1.7 轉義字符
1.8 字符串
#include <iostream>
#include <string>
using namespace std;
int main() {
// 字符串
string str1 = "hello world!";
cout << str1 << endl;
}
1.9 布爾類型bool
#include <iostream>
#include <string>
using namespace std;
int main() {
// 布爾,所佔空間都爲1
bool flag1 = true; // 1
cout << flag1 << endl;
bool flag2 = false; // 0
cout << flag2 << endl;
}
1.10 數據的輸入
輸入符號位cin >>
#include <iostream>
#include <string>
using namespace std;
int main() {
// 數據的輸入
int a = 0;
cout << "請給整型變量a賦值: " << endl;
cin >> a;
cout << "整型變量a= : " << a << endl;
}
2 運算符
2.1 加減乘除、取模、遞增和遞減運算符運算符
#include <iostream>
#include <string>
using namespace std;
int main() {
// 運算符
int a = 10;
int b = 3;
//加減乘除
cout << a + b << endl;
cout << a - b << endl;
cout << a * b << endl;
cout << a / b << endl; // 默認爲3,整型會去除小數部分
cout << "----------------" << endl;
// 取模運算
int c = 10;
int d = 3;
cout << c % d << endl; //d不能爲0,兩個小數不能做取模運算
cout << "----------------" << endl;
// 遞增、遞減運算符
int e = 10;
int f = 10;
++e;
cout << e << endl;
f++;
cout << f << endl;
int g = 15;
int h = ++g * 10;
cout << h << endl; // 160
int i = 15;
int j = i++ * 10;
cout << j << endl; // 160
}
2.2 賦值運算符
加減乘除都可以用,只放一個
#include <iostream>
#include <string>
using namespace std;
int main() {
// 賦值運算符
int a = 10;
a += 1; // a = a + 2
cout << a <<endl;
}
2.3 比較運算符
2.4 邏輯運算符
#include <iostream>
#include <string>
using namespace std;
int main() {
// 邏輯運算符
int a = 10; // 整型爲真(1)
cout << "!10: " << !a << endl;
int b = 0; // 0爲假(0)
cout << "!0: " << !b << endl;
cout << "0與1: " << (a&&b) << endl;
cout << "0或1: " << (a||b) << endl;
}
3 流程結構
3.1 if語句
#include <iostream>
#include <string>
using namespace std;
int main() {
// if語句
int score = 0;
cout << "請輸入一個分數:" << endl;
cin >> score;
cout << "您輸入的分數爲: " << score << endl;
if (score > 600)
cout << "恭喜您考上了一本大學!" << endl;
else if (score > 500)
cout << "您考上了二本大學!" << endl;
else
cout << "您沒有考上二本大學!" << endl;
}
3.2 三目運算符
#include <iostream>
#include <string>
using namespace std;
int main() {
// 三目運算符
int a = 10;
int b = 20;
int c = 0;
c = (a > b ? a : b);
cout << c << endl; // a > b就返回a,否則返回b
(a > b ? a : b) = 100;
cout << a << endl;
cout << b << endl; // a > b就給a賦值100,否則b
}
3.3 switch語句
#include <iostream>
#include <string>
using namespace std;
int main() {
// sitch語句
cout << "請給電影打分: " << endl;
int score = 0;
cin >> score;
cout << "您打的分數爲: " << score <<endl;
switch (score) {
case 10:
cout << "您認爲是經典電影" << endl;
break;
case 9:
cout << "您認爲是經典電影" << endl;
break;
case 8:
cout << "您認爲電影還可以" << endl;
break;
}
}
3.4 while循環
#include <iostream>
#include <string>
using namespace std;
int main() {
// while循環
int num = 0;
while(num < 10) {
cout << num << endl;
num += 1;
}
}
3.5 while循環小遊戲
#include <iostream>
#include <string>
#include <ctime>
using namespace std;
int main() {
// while循環練習案例:猜數字
// 生成0-100的隨機數
srand((unsigned int) time(NULL));
int num = rand() % 100 + 1;
// cout << num << endl;
int val = 0; // 玩家輸入的數據
while(1) {
cin >> val;
if (val > num) {
cout << "猜測過大!" << endl;
} else if (val < num) {
cout << "猜測過小!" << endl;
} else {
cout << "猜測成功!" << endl;
break;
}
}
}
3.6 for循環
#include <iostream>
using namespace std;
int main() {
// for循環
for (int i = 0; i < 10; i++) {
cout << i << endl;
}
}
3.7 for循環案例
#include <iostream>
using namespace std;
int main() {
// for循環
for (int i = 1; i <= 100; i++) {
if(i % 10 == 7){
cout << "敲桌子!" << endl;
} else if(i >= 70 && i <80){
cout << "敲桌子!" << endl;
}else if (i % 7 == 0){
cout << "敲桌子!" << endl;
}
else{
cout << i << endl;
}
}
}
3.8 循環案例 - 乘法口訣表
#include <iostream>
using namespace std;
int main() {
// for循環
for (int i = 1; i <= 9; i++){
for (int j = 1; j <= i; j++){
cout << i << "*" << j << " = " << i * j << " ";
}
cout << endl;
}
}
3.9 跳轉語句
3.9.1 break語句
跳出當前的循環語句,比continue跳的更外面
3.9.2 continue語句
在循環語句中,跳過本次循環中餘下尚未執行的語句,繼續執行下一次循環
3.9.3 goto語句
不推薦使用
4 數組
4.1 數組的聲明與賦值
#include <iostream>
using namespace std;
int main() {
// 數組
//第一種定義方式
int arr[5];
arr [0] = 10;
arr [1] = 20;
arr [2] = 30;
arr [3] = 40;
arr [4] = 50;
//第二種定義方式
int arr2[5] = {1, 2, 3, 4, 5};
//第三種定義方式
int arr3[] = {1, 2, 3, 4, 5, 6, 7};
for (int i = 0; i < 5; i++) {
cout << arr[i] << endl;
}
}
4.2 一維數組名的
用途:
- 統計數組的長度
- 獲取數組內存中的首地址
4.3 求數組最大值
#include <iostream>
using namespace std;
int main() {
// 數組
//第一種定義方式
int max = 0;
int arr[6] = {3, 1, 2, 3, 4, 2};
for (int i = 0; i < 6; i++) {
if (arr[i] > max){
max = arr[i];
}
}
cout << "最大值爲:" << max << endl;
}
4.4 數組逆置
#include <iostream>
using namespace std;
int main() {
// 數組
//第一種定義方式
int arr[6] = {3, 1, 2, 3, 4, 2};
int start = 0;
int end = sizeof(arr) / sizeof(int) -1;
while (start < end) {
int tem = arr[start];
arr[start] = arr[end];
arr[end] = tem;
start++;
end--;
}
for (int j = 0; j < 6; j++) {
cout << arr[j];
}
}
4.5 冒泡排序
#include <iostream>
using namespace std;
int main() {
// 數組
//第一種定義方式
int arr[6] = {3, 1, 2, 3, 4, 2};
for (int i = 0; i < 6 - 1; i++) {
for (int j = 0; j < 6 - 1 - i; j++) {
if (arr[j] > arr[j + 1]) {
int tem = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = tem;
}
}
}
for (int k = 0; k < 6; k++) {
cout << arr[k]<< endl;
}
}
4.6 二維數組
#include <iostream>
using namespace std;
int main() {
// 數組
//第一種定義方式
int arr1[2][3];
arr1[0][0] = 1;
arr1[0][1] = 2;
arr1[0][2] = 3;
arr1[1][0] = 4;
arr1[1][1] = 5;
arr1[1][2] = 6;
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 3; j++){
cout << arr1[i][j] << endl;
}
}
}
4.7 二維數組案例 - 成績統計
#include <iostream>
using namespace std;
int main() {
// 數組
//第一種定義方式
int arr1[2][3];
arr1[0][0] = 100;
arr1[0][1] = 100;
arr1[0][2] = 100;
arr1[1][0] = 90;
arr1[1][1] = 50;
arr1[1][2] = 100;
arr1[2][0] = 60;
arr1[2][1] = 70;
arr1[2][2] = 80;
for (int i = 0; i < 3; i++) {
int sum_ = 0;
for (int j = 0; j < 3; j++){
sum_ += arr1[i][j];
}
cout << sum_ << endl;
}
}
5 函數
作用:將一段經常使用的代碼封裝起來,減少重複代碼
5.1 函數的定義和調用
- 返回值類型
- 函數名
- 參數列表
- 函數體語句
- return表達式
#include <iostream>
using namespace std;
int add(int num1, int num2) {
return num1 + num2;
}
int main() {
// 函數
cout << add(1, 2) << endl;
}
5.2 值傳遞
函數中的值發生了改變,函數外的值沒變
#include <iostream>
using namespace std;
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 num1 = 10;
int num2 = 20;
swap(num1, num2);
cout << "實參沒變: " << endl;
cout << "num1: " << num1 << endl;
cout << "num2: " << num2 << endl;
}
5.3 函數的聲明
- 提前告訴編譯器有這個函數
- 可以有多次聲明
5.4 函數的分文件編寫
步驟:
- 創建後綴名爲.h的頭文件
- 創建後綴名爲.cpp的源文件
- 在頭文件中寫函數的生命
- 在源文件中寫函數的定義
示例:創建swap.cpp和swap.h 都需要在一個目錄下
swap.h文件:
#include <iostream>
using namespace std;
// 函數的聲明
void swap(int a, int b);
swap.cpp文件:
#include "swap2.h"
#include <iostream>
using namespace std;
//
// Created by 萬方名 on 2020-05-23.
//
void swap(int a, int b) {
int temp = a;
a = b;
b = temp;
cout << a<< endl;
cout << b<< endl;
}
主文件:
#include <iostream>
#include "swap2.h"
using namespace std;
int main() {
// 值傳遞
int num1 = 10;
int num2 = 20;
swap(num1, num2);
cout << "實參沒變: " << endl;
cout << "num1: " << num1 << endl;
cout << "num2: " << num2 << endl;
}
6 指針
6.1 指針的基本概念
作用:可以通過指針間接訪問內存
- 內存編號從0開始記錄,一般用十六進制數字表示
- 可以利用指針變量保存地址
6.2 指針的定義和使用
#include <iostream>
#include "swap2.h"
using namespace std;
int main() {
// 指針
// 定義變量
int a = 10;
// 定義指針
int *p;
// 讓指針記錄變量a的地址
p = &a;
cout << p << endl;
// 解指針
cout << *p <<endl;
// 通過指針更改變量的值
*p = 1000;
cout << "a: " << a << endl;
cout << "*p: " << *p << endl;
}
6.3 指針所佔內存空間
- 32位系統下佔四個字節
- 64位系統下佔八個字節
#include <iostream>
using namespace std;
int main() {
// 指針
// 定義變量
int a = 10;
int *p = &a;
cout << *p << endl;
cout << sizeof(p) << endl;
cout << sizeof(char *) << endl;
cout << sizeof(float *) << endl;
cout << sizeof(double *) << endl;
}
6.4 空指針和野指針
6.4.1 空指針:指針變量指向內存中編號爲0的空間
- 用途:初始化指針變量
- 注意:空指針指向的內存是不可訪問的
#include <iostream>
using namespace std;
int main() {
// 空指針
int *p = NULL;
// 不可訪問
cout << *p << endl;
}
6.4.2 野指針:指針變量指向非法的內存空間
在程序中儘量避免出現野指針
#include <iostream>
using namespace std;
int main() {
// 野指針
int *p = (int *) 0x1100;
// 報錯
cout << *p << endl;
}
6.5 const修飾指針
三種情況:
- const修飾指針 - 常量指針
- const修飾常量 - 指針常量
- const即修飾指針又修飾常量
#include <iostream>
using namespace std;
int main() {
// 1.常量指針
int a = 10;
int b = 20;
// 定義常量指針
const int *p = &a;
// *p = 20; // 報錯,常量指針的值不可修改
p = &b; // 不報錯,指針可以重新指向b
cout << *p << endl;
// 2.指針常量
int c = 30;
int d = 40;
// 定義指針常量
int *const p2 = &c;
*p2 = 50; // 不報錯,指針的值可以修改
// p2 = &d; // 報錯,指針不可更改指向
cout << *p2 << endl;
//3. const修飾指針和指針常量時不可更改值和指向
}
6.6 指針和數組
作用:利用指針訪問數組元素
#include <iostream>
using namespace std;
int main() {
// 指針和數組
int arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int *p = arr;
cout << *p << endl; // 返回數組的第一個元素
// 利用指針遍歷數組
for (int i = 0; i < 10; i++) {
cout << *p << endl;
p++;
}
}
6.7 指針和函數
作用:利用指針作函數參數,可以修改實參的值
#include <iostream>
using namespace std;
void swap4(int a, int b) {
cout << "值傳遞"<< endl;
int temp = a;
a = b;
b = temp;
}
void swap5(int *a, int *b) {
cout << "地址傳遞"<< endl;
int temp2 = *a;
*a = *b;
*b = temp2;
}
int main() {
// 指針和函數
//1. 值傳遞
int a = 10;
int b = 20;
swap4(a, b);
cout << a<< endl;
cout << b<< endl;
cout << "------分界線------"<< endl;
//2. 地址傳遞
swap5(&a, &b);
cout << a<< endl;
cout << b<< endl;
}
6.8 指針、數組和函數的案例
案例描述:封裝一個函數,利用冒泡排序,實現對整型數組的升序排序
#include <iostream>
using namespace std;
void bubble_sort(int *arr, int len){
for (int i = 0; i < len - 1; i++) {
for (int j = 0; j < len - 1 - i; j++) {
if (arr[j] > arr[j + 1]) {
int tem = arr[j];
arr[j] = arr[j+1];
arr[j+1] = tem;
}
}
}
}
int main() {
// 定義數組
int arr[] = {1, 2, 32, 3, 5, 6, 23};
bubble_sort(arr, sizeof(arr) / sizeof(arr[0]));
for (int i = 0; i < sizeof(arr) / sizeof(arr[0]);i++) {
cout << arr[i] << endl;
}
}
進階
7 結構體
結構體屬於用戶自定義的數據類型,允許用戶存儲不同的數據類型
7.1 結構體的定義與使用
通過結構體創建變量的方式有三種:
- struct結構體名 變量名
- struct結構體名 變量名 = {成員1值,成員2值…}
- 定義結構體時順便創建變量
#include <iostream>
using namespace std;
int main() {
//結構體
struct student {
string name; // 姓名
int age; // 年齡
int score; // 分數
};
student a = student();
a.age = 10;
cout << a.age << endl;
}
7.2 結構體數組
作用:將自定義的結構體放入到數組中方便維護
#include <iostream>
using namespace std;
struct student {
string name; // 姓名
int age; // 年齡
int score; // 分數
};
int main() {
//結構體
student arr[3] = {
{"張三", 18, 60},
{
"李四", 20, 70
}
};
for (int i = 0; i < 3; i++) {
cout << "姓名:" << arr[i].name
<< "年齡: " << arr[i].age
<< "分數: " << arr[i].score << endl;
}
}
7.3 結構體指針
作用:通過指針訪問結構體中的成員
#include <iostream>
using namespace std;
struct student {
string name; // 姓名
int age; // 年齡
int score; // 分數
};
int main() {
//結構體指針
student stu = {"張三", 18, 80};
student *p = &stu;
cout << p->score<< endl;
}
7.4 結構體嵌套結構體
作用:結構體中的成員可以是另一個結構體
#include <iostream>
using namespace std;
struct student {
// 成員列表
string name; // 姓名
int age; // 年齡
int score; // 分數
};
struct teacher {
// 成員列表
int id;
string name; // 姓名
int age; // 年齡
struct student stu; // 輔導的學生
};
int main() {
//結構體嵌套
teacher t;
t.id = 1;
t.name = "老萬";
t.age = 50;
t.stu.name = "小萬";
t.stu.age = 18;
t.stu.score = 80;
cout << "學生的年齡:" << t.stu.age << endl;
}
7.5 結構體做函數參數
將結構體作爲參數向函數中傳遞
傳遞方式:
- 值傳遞
- 地址傳遞
#include <iostream>
using namespace std;
struct student {
// 成員列表
string name; // 姓名
int age; // 年齡
int score; // 分數
};
// 值傳遞
void print_stu(student stu){
stu.age = 28;
cout << stu.name << endl;
cout << stu.age << endl;
}
// 地址傳遞
void print_stu2(student *stu){
stu->age = 28;
cout << stu->name << endl;
cout << stu->age << endl;
}
int main() {
//結構體作爲函數參數
student t = {"張三", 20, 80};
print_stu(t);
cout << t.age << endl; // 值傳遞,函數外沒有發生改變
cout << "------分界線------" << endl; // 值傳遞,函數外沒有發生改變
student t2 = {"李四", 30, 100};
print_stu2(&t2);
cout << t2.age << endl; // 地址傳遞,函數外發生改變
}
7.6 結構體中的const
作用:用const來防止誤操作
void print_stu(const student *stu){
}
這樣在函數中就不能修改stu的值了
7.7 結構體案例
#include <iostream>
using namespace std;
struct Hero {
string name;
int age;
string sex;
};
int main() {
//結構體案例
struct Hero hero_arr[5] = {
{"劉備", 23, "男"},
{"張飛", 20, "男"},
{"關羽", 22, "男"},
{"趙雲", 21, "男"},
{"貂蟬", 19, "女"},
};
int len_hero_arr = sizeof(hero_arr) / sizeof(Hero);
cout << len_hero_arr << endl;
for (int i = 0; i < len_hero_arr; i++) {
for (int j = 0; j < len_hero_arr - i; j++) {
if (hero_arr[j].age <= hero_arr[j + 1].age) {
Hero tem = hero_arr[j];
hero_arr[j] = hero_arr[j + 1];
hero_arr[j + 1] = tem;
}
}
}
for (int i = 0; i < len_hero_arr; i++) {
cout << hero_arr[i].name << endl;
}
}
8 項目一 - 通訊錄管理系統