藍橋杯零基礎到獲獎-第3章 C++ 變量和常量
文章目錄
- 一、變量和常量
- 1.變量的創建
- 2.變量初始化
- 3.變量的分類
- 4.常量
- 4.1 字?常量
- 4.2 #define定義常量
- 4.3 const 定義常量
- 4.4 練習
- 練習1:買票https://www.nowcoder.com/practice/0ad8f1c0d7b84c6d8c560298f91d5e66
- 練習2:A + B 問題https://www.luogu.com.cn/problem/B2007
- 練習3:雞兔同籠
一、變量和常量
1.變量的創建
了解清楚了類型,我們使?類型做什么呢?類型是?來創建變量的。
什么是變量呢?把經常變化的值稱為變量,不變的值稱為常量。
變量創建的語法形式是這樣的:
data_type name;
| |
| |
數據類型 變量名
int age; //整型變量
char ch; //字符變量
double weight; //浮點型變量
變量的命名規則遵循以下原則:
? 變量名只能由字?、數字和下劃線組成,且必須以字?或下劃線開頭。
? 變量名不能以純數字開頭,也不能包含特殊字符,如空格、連字符等。
? 變量名不能使?語?的關鍵字,如 int、char、if等。
? 變量名應具有意義,有助于理解變量的含義和?途。
? 變量名應簡短明了,避免使?過?的名稱。
? 變量名應區分??寫,例如 myVariable 和 myvariable 被視為兩個不同的變量。
2.變量初始化
變量在創建的時候就給?個初始值,就叫初始化。
int age = 18;
char ch = 'w';
double weight = 48.0;
unsigned int height = 100;
3.變量的分類
? 全局變量:在?括號外部定義的變量就是全局變量全局變量的使?范圍更?,整個?程中想使?,都是有辦法使?的
? 局部變量:在?括號內部定義的變量就是局部變量局部變量的使?范圍是?較局限,只能在??所在的局部范圍內使?的
#include <iostream>
using namespace std;
int global = 2023; //全局變量
int main()
{int local = 2018; //局部變量cout << local << endl;cout << global << endl;return 0;
}
如果局部和全局變量,名字相同呢?
#include <iostream>
using namespace std;
int n = 1000;
int main()
{int n = 10;cout << n << endl; //打印的結果是多少呢?return 0;
}
其實當局部變量和全局變量同名的時候,局部變量優先使?。
? 未初始化狀態下的全局變量和局部變量
//未初始化的局部變量
#include <iostream>
using namespace std;
int main()
{//局部變量int a;char c;float f;double d;cout << "int:" << a << endl;cout << "char:" << c << endl;cout << "float:" << f << endl;cout << "double:" << d << endl;return 0;
}
//未初始化的全局變量
#include <iostream>
using namespace std;
//全局變量
int a;
char c;
float f;
double d;
int main()
{cout << "int:" << a << endl;cout << "char:" << c << endl;cout << "float:" << f << endl;cout << "double:" << d << endl;return 0;
}
在Dev-C++下:
局部變量: 全局變量:
4.常量
常量就是不能被改變的值,通常我們會使?三種常量:
? 字?常量
? #define 定義的常量
? const 定義的常量
下?分別介紹?下。
4.1 字?常量
整型常量: 100,-5,0,0x123 整型常量?般可以寫成10進制、8進制、16進制。
10進制數字,如: 6、17、22、123
8進制數字,?般是數字0開頭的,?如: 012 , 016
16進制數字,?般是0x開頭的數字,?如: 0x123 , 0xFF
字符常量: ‘a’
浮點型常量: 3.14 , 1E6 (科學計數法的形式)
4.2 #define定義常量
有時候也會使? #define 來定義常量,?如:
代碼舉例:
#include <iostream>
using namespace std;
#define M 100
#define CH 'x'
#define PI 3.14159
int main()
{cout << M << endl;cout << CH << endl;cout << PI << endl;return 0;
}
這?定義的 M、CH、PAI 都是常量,可以直接使?,但是不能被修改。
使? #define 定義常量的時候是不關注類型的,只關注常量的名字叫啥,常量的值是啥,編譯在處
理這種常量的時候就是直接替換,在出現常量名字的地?,通通替換成常量的內容。
上?的代碼被編譯器替換后就是:
#include <iostream>
using namespace std;
int main()
{cout << 100 << endl;cout << 'x' << endl;cout << 3.14159 << endl;return 0;
}
4.3 const 定義常量
除了上?的?式之外,C++中還可以使? const 來定義常量,這種常量會有具體的類型。?#define 定義常量更加嚴謹。語法形式如下:
?如:const double PI = 3.14159;
使??下:
#include <iostream>
using namespace std;
const double PI = 3.14159;
int main()
{int r = 0;cin >> r;cout << "周?:" << 2 * PI * r << endl;cout << "?經:" << PI * r * r << endl; //PI = 3.14;//這種寫法是錯誤的,常量不能被修改return 0;
}
習慣上,這種常量的名字?般會寫成?寫,?普通變量的名字不會全?寫,這樣就可以做?個區分。
使? const 定義的常量的好處:
? 增加了程序的可讀性, PI ? 3.14159 更加容易理解和書寫、使?。
? 增加了程序的可維護性,如果改變常量的值,只要在定義的部分修改,使?的地?也就隨之改變
了,做到了"?改全改"的效果。
? 常量是不能修改的,當然 const 定義的常量?然也不能修改
#include <iostream>
using namespace std;
int main()
{const int num = 10;num = 20; //修改num,編譯器會報錯的return 0;
}
4.4 練習
練習1:買票https://www.nowcoder.com/practice/0ad8f1c0d7b84c6d8c560298f91d5e66
#include<bits/stdc++.h>
using namespace std;
int main()
{int x;cin>>x;cout<<x*100;return 0;
}
練習2:A + B 問題https://www.luogu.com.cn/problem/B2007
#include<bits/stdc++.h>
using namespace std;
int main()
{int x,y;cin>>x>>y;cout<<x+y;}
練習3:雞兔同籠
https://www.luogu.com.cn/problem/B2614
#include<bits/stdc++.h>
using namespace std;
int main()
{cout<<12<< " "<<23;return 0;
}