基础语法部分
01.第一个项目
# include <iostream>
int main ( )
{
std:: cout << "Hello World!\n" ;
}
02.验证空间大小
# include <iostream>
int main ( )
{
std:: cout << "short: " << sizeof ( short ) << std:: endl;
std:: cout << "int: " << sizeof ( int ) << std:: endl;
std:: cout << "long: " << sizeof ( long ) << std:: endl;
std:: cout << "long long: " << sizeof ( long long ) << std:: endl;
std:: cout << "float: " << sizeof ( float ) << std:: endl;
std:: cout << "double: " << sizeof ( double ) << std:: endl;
std:: cout << "char: " << sizeof ( char ) << std:: endl;
std:: cout << "bool: " << sizeof ( bool ) << std:: endl;
return 0 ;
}
short : 2
int : 4
long : 4
long long : 8
float : 4
double : 8
char : 1
bool : 1
03.数据类型
# include <iostream>
int main ( )
{
bool number = true ;
int num;
num = 10 ;
std:: cout << num << std:: endl;
int num1 = 20 ;
std:: cout << num1 << std:: endl;
int chineseScore = 100 , mathScore, englishScore = 90 ;
mathScore = 98 ;
std:: cout << "chineseScore = " << chineseScore << ",mathScore = " << mathScore << ",englishScore = " << englishScore << std:: endl;
const float PI = 3.14 ;
return 0 ;
}
10
20
chineseScore = 100 , mathScore = 98 , englishScore = 90
04.各种数据类型的变量定义
# include <iostream>
# include <iomanip>
int main ( )
{
short num1 = 32767 ;
int num2 = 10 ;
long num3 = 10 ;
long long num4 = 10 ;
std:: cout << "num1 = " << num1 << " , num2 = " << num2 << " , num3 = " << num3 << " , num4 = " << num4 << std:: endl;
float f1 = 3.14f ;
double f2 = 3.14 ;
std:: cout << "f1 = " << f1 << std:: endl;
std:: cout << "f2 = " << f2 << std:: endl;
float f11 = 30000.12345678901234567890f ;
double f21 = 30000.12345678901234567890 ;
std:: cout << std:: setprecision ( 20 ) << "f11 = " << f11 << std:: endl;
std:: cout << "f21 = " << f21 << std:: endl;
bool b1 = true ;
bool b2 = false ;
std:: cout << "b1 = " << b1 << ",b2 = " << b2 << std:: endl;
return 0 ;
}
num1 = 32767 , num2 = 10 , num3 = 10 , num4 = 10
f1 = 3.14
f2 = 3.14
f11 = 30000.123046875
f21 = 30000.123456789013289
b1 = 1 , b2 = 0
05.字符数据类型和字符串类型
# include <iostream>
int main ( )
{
char b = 'aaaa' ;
char c = 'a' ;
std:: cout << c << std:: endl;
std:: string str = "Hello World" ;
return 0 ;
}
a
06.转义字符
# include <iostream>
int main ( )
{
char c1= '\'' ;
std:: string str1 = "Hello \" World" ;
std:: cout << c1 << std:: endl;
std:: cout << str1 << std:: endl;
std:: string str2 = "hello \n world" ;
std:: cout << str2 << std:: endl;
std:: cout << '\\' << std:: endl;
return 0 ;
}
'
Hello " World
hello
world
\
07.数据类型转换
# include <iostream>
int main ( )
{
int num1 = 100 ;
long long num2 = num1;
std:: cout << "num2 = " << num2 << std:: endl;
char c1 = 'a' ;
int num3 = c1;
std:: cout << "num3 = " << num3 << std:: endl;
float f1 = num1;
std:: cout << "f1 = " << f1 << std:: endl;
int num4 = ( int ) num2;
std:: cout << "num4 = " << num4 << std:: endl;
int num5 = 200 ;
char c2 = ( char ) num5;
std:: cout << "c2 + 1 = " << c2 + 1 << std:: endl;
int nn1 = 100 ;
long long nn2 = 200 ;
auto res = nn1 + nn2;
std:: cout << "sizeof(nn1 + nn2) = " << sizeof ( nn1 + nn2) << std:: endl;
short s1 = 100 , s2 = 100 ;
auto res2 = s1 + s2;
float f2 = 3.94f ;
int i1 = ( int ) f2;
std:: cout << "i1 = " << i1 << std:: endl;
return 0 ;
}
num2 = 100
num3 = 97
f1 = 100
num4 = 100
c2 + 1 = - 55
sizeof ( nn1 + nn2) = 8
i1 = 3
08.控制台输入
# include <iostream>
int main ( )
{
int n1 = 0 ;
std:: cout << "请输入一个整型的数字:" ;
std:: cin >> n1;
std:: cout << "n1 = " << n1 << std:: endl;
float n2 = 0 ;
std:: cout << "请输入一个浮点型的数字:" ;
std:: cin >> n2;
std:: cout << "n2 = " << n2 << std:: endl;
bool n3 = false ;
std:: cout << "请输入一个布尔值:" ;
std:: cin >> n3;
std:: cout << "n3 = " << n3 << std:: endl;
char n4 = 0 ;
std:: cout << "请输入一个字符:" ;
std:: cin >> n4;
std:: cout << "n4 = " << n4 << std:: endl;
std:: string n5 = "" ;
std:: cout << "请输入一个字符串:" ;
std:: cin >> n5;
std:: cout << "n5 = " << n5 << std:: endl;
return 0 ;
}
请输入一个整型的数字:5
n1 = 5
请输入一个浮点型的数字:3.14
n2 = 3.14
请输入一个布尔值:1
n3 = 1
请输入一个字符:a
n4 = a
请输入一个字符串:abc
n5 = abc
int main ( )
{
int num1 = 0 , num2 = 0 , num3 = 0 ;
std:: cin >> num1;
std:: cin. ignore ( std:: numeric_limits < std:: streamsize> :: max ( ) , '\n' ) ;
std:: cin >> num2;
std:: cin >> num3;
std:: cout << "num1 = " << num1 << ",num2 = " << num2 << ",num3 = " << num3 << std:: endl;
return 0 ;
}
3.14 234
123
456
num1 = 3 , num2 = 123 , num3 = 456
int main ( )
{
int num1 = 0 ;
float num2 = 0 ;
std:: string num3 = "" ;
std:: cin >> num1;
std:: cin. ignore ( std:: numeric_limits < std:: streamsize> :: max ( ) , '\n' ) ;
std:: cin >> num2;
std:: cin >> num3;
std:: cout << "num1 = " << num1 << ",num2 = " << num2 << ",num3 = " << num3 << std:: endl;
return 0 ;
}
3.14 369
12.3
abc
num1 = 3 , num2 = 12.3 , num3 = abc
int main ( )
{
int num1 = 0 , num2 = 0 , num3 = 0 ;
std:: cin >> num1 >> num2 >> num3;
std:: cout << "num1 = " << num1 << ",num2 = " << num2 << ",num3 = " << num3 << std:: endl;
return 0 ;
123 456 789
num1 = 123 , num2 = 456 , num3 = 789
int main ( )
{
int num4 = 0 ;
std:: cout << "请输入一个整型的数字: " ;
std:: cin >> num4;
std:: cout << "num4 = " << num4 << std:: endl;
std:: cout << "good = " << std:: cin. good ( ) << ",fail = " << std:: cin. fail ( ) << std:: endl;
std:: cin. clear ( ) ;
std:: cin. ignore ( std:: numeric_limits < std:: streamsize> :: max ( ) , '\n' ) ;
int num5 = 0 ;
std:: cout << "请输入一个整型的数字: " ;
std:: cin >> num5;
std:: cout << "num5 = " << num5 << std:: endl;
std:: cout << "good = " << std:: cin. good ( ) << ",fail = " << std:: cin. fail ( ) << std:: endl;
return 0 ;
}
请输入一个整型的数字: abc
num4 = 0
good = 0 , fail = 1
请输入一个整型的数字: 123
num5 = 123
good = 1 , fail = 0
09.宏定义
# include <iostream>
# define SUCCESS_CODE 0
# define EXPR 2 + 2
# define EXPR2 ( 2 + 2 )
int main ( ) {
std:: cout << "SUCCESS_CODE : " << SUCCESS_CODE << std:: endl;
std:: cout << "EXPR : " << EXPR << std:: endl;
std:: cout << "EXPR * EXPR : " << EXPR * EXPR << std:: endl;
std:: cout << "EXPR2 * EXPR2 : " << EXPR2 * EXPR2 << std:: endl;
return 0 ;
}
SUCCESS_CODE : 0
EXPR : 4
EXPR * EXPR : 8
EXPR2 * EXPR2 : 16
10.命名空间
# include <iostream>
namespace a {
int num1 = 10 ;
namespace a1 {
std:: string str = "hello world" ;
}
}
namespace b {
int num1 = 10 ;
}
namespace a {
int num2 = 100 ;
}
namespace constant1 {
const int MAX_SCORE = 100 ;
}
namespace constant2 {
const int MAX_SCORE = 150 ;
}
int main ( ) {
std:: cout << "1.命名空间基础的使用" << std:: endl;
std:: cout << a:: num1 << std:: endl;
std:: cout << b:: num1 << std:: endl;
std:: cout << a:: a1:: str << std:: endl;
std:: cout << a:: num2 << std:: endl;
std:: cout << "2.using指定命名空间中的指定成员" << std:: endl;
using a:: num1;
std:: cout << num1 << std:: endl;
std:: cout << a:: num2 << std:: endl;
std:: cout << "3.using命名空间" << std:: endl;
using namespace a;
std:: cout << num1 << std:: endl;
std:: cout << num2 << std:: endl;
std:: cout << a1:: str << std:: endl;
using namespace std;
cout << num2 << endl;
std:: cout << "4.注意事项" << std:: endl;
const int MAX_SCORE = 200 ;
cout << MAX_SCORE << endl;
using namespace constant1;
using namespace constant2;
cout << constant1:: MAX_SCORE << endl;
cout << constant2:: MAX_SCORE << endl;
return 0 ;
}
1. 命名空间基础的使用
10
10
hello world
100
2.u sing指定命名空间中的指定成员
10
100
3.u sing命名空间
10
100
hello world
100
4. 注意事项
200
100
150
11.算术运算符
# include <iostream>
using namespace std;
int main ( ) {
cout << "10 + 3 = " << 10 + 3 << endl;
cout << "10 - 3 = " << 10 - 3 << endl;
cout << "10 * 3 = " << 10 * 3 << endl;
cout << "10 / 3 = " << 10 / 3 << endl;
cout << "10 / 3.0 = " << 10 / 3.0 << endl;
cout << "10.0 / 3 = " << 10.0 / 3 << endl;
cout << "10.0 / 3.0 = " << 10.0 / 3.0 << endl;
cout << "10 % 3 = " << 10 % 3 << endl;
int num = 10 ;
cout << num++ << endl;
cout << num-- << endl;
cout << num << endl;
int number = 0 ;
cout << "请输入一个三位的数字:" ;
cin >> number;
int g = number % 10 ;
int s = number / 10 % 10 ;
int b = number / 100 ;
cout << g + s + b << endl;
return 0 ;
}
10 + 3 = 13
10 - 3 = 7
10 * 3 = 30
10 / 3 = 3
10 / 3.0 = 3.33333
10.0 / 3 = 3.33333
10.0 / 3.0 = 3.33333
10 % 3 = 1
10
11
10
请输入一个三位的数字:153
9
12.赋值运算符
# include <iostream>
using namespace std;
int main ( ) {
int a = 10 ;
a = 20 ;
cout << ( a = 200 ) << endl;
cout << a << endl;
int num = 10 ;
num += 10 ;
num -= 10 ;
num *= 10 ;
num /= 10 ;
num %= 3 ;
char c = 'a' ;
cout << ( c += 2 ) << endl;
return 0 ;
}
200
200
c
13.关系运算符
# include <iostream>
using namespace std;
int main ( ) {
cout << ( 10 > 2 ) << endl;
cout << ( 10 >= 10 ) << endl;
cout << ( 10 == 20 ) << endl;
return 0 ;
}
1
1
0
14.逻辑运算符
# include <iostream>
using namespace std;
int main ( ) {
cout << ( 13 & 21 ) << endl;
cout << ( 13 | 21 ) << endl;
cout << ( 13 ^ 21 ) << endl;
cout << ~ 13 << endl;
cout << ( 13 << 2 ) << endl;
cout << ( 13 >> 2 ) << endl;
cout << ( - 13 >> 2 ) << endl;
cout << ( 8 * 2 ) << endl;
cout << ( 8 << 1 ) << endl;
cout << ( 2 << 3 ) << endl;
return 0 ;
}
0
1
0
1
0
result = 0 , num1 = 11 , num2 = 20
15.位运算符
# include <iostream>
using namespace std;
int main ( ) {
cout << ( 13 & 21 ) << endl;
cout << ( 13 | 21 ) << endl;
cout << ( 13 ^ 21 ) << endl;
cout << ~ 13 << endl;
cout << ( 13 << 2 ) << endl;
cout << ( 13 >> 2 ) << endl;
cout << ( - 13 >> 2 ) << endl;
cout << ( 8 * 2 ) << endl;
cout << ( 8 << 1 ) << endl;
cout << ( 2 << 3 ) << endl;
return 0 ;
}
5
29
24
- 14
52
3
- 4
16
16
16
16.三目运算符
# include <iostream>
using namespace std;
int main ( ) {
cout << ( true ? 10 : 20 ) << endl;
cout << ( false ? 10 : 20 ) << endl;
int num1 = 0 , num2 = 0 ;
cout << "请输入一个整型的数字:" ;
cin >> num1;
cout << "请在输入一个整型的数字:" ;
cin >> num2;
cout << "最大的数字为:" << ( num1 > num2 ? num1 : num2) << endl;
cout << "最小的数字为:" << ( num1 < num2 ? num1 : num2) << endl;
return 0 ;
}
10
20
请输入一个整型的数字:20
请在输入一个整型的数字:12
最大的数字为:20
最小的数字为:12