C++基础入门
输出
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| #include <iostream> using namespace std; int main() { int a=10; cout << "hello C++" << endl; cout << a << endl; cout << "a=" << a << endl; system("pause");
return 0; }
|
using namespace std的作用
输出中文乱码改编码格式为GBK
常量
define 宏常量:#define 常量名 常量值
通常在文件上方定义,表示一个常量
- const修饰的变量:const 数据类型 常量名 = 常量值
通常在变量定义前加关键字const 修饰变量为常量 不可修改s
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| #include <iostream> using namespace std; #define DAY 7 int main() { const int month = 12; cout << "一周有" << DAY <<"天"<<endl; cout << "一年有" << month <<"月"<<endl; system("pause");
return 0; }
|
数据类型
整型
| 数据类型 |
占用空间 |
取值范围 |
| short(短整型) |
2字节 |
(-2^15 ~ 2^15-1) |
| int(整型) |
4字节 |
(-2^31 ~ 2^31-1) |
| long(长整形) |
Windows为4字节,Linux为4字节(32位),8字节(64位) |
(-2^31 ~ 2^31-1) |
| long long(长长整形) |
8字节 |
(-2^63 ~ 2^63-1) |
sizeof关键字
作用:利用sizeof关键字可以统计数据类型所占内存大小
语法: sizeof( 数据类型 / 变量)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| #include <iostream> using namespace std; int main() { short a=10; int b=4; long c=5; long long d = 6; cout << sizeof(a)<<sizeof(b)<<sizeof(c)<<sizeof(d)<<endl; system("pause"); return 0; }
|
实型(浮点型)
| 数据类型 |
占用空间 |
有效数字范围 |
| float |
4字节 |
7位有效数字 |
| double |
8字节 |
15~16位有效数字 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| #include <iostream> using namespace std; int main() { float pi=3.14f; cout<<"pi="<<pi<<endl; cout<<"float="<<sizeof(float)<<endl; cout<<"double="<<sizeof(double)<<endl; float f=3e2; cout<<"f="<<f<<endl; return 0; }
|
字符型
1 2 3 4 5 6 7 8 9 10 11 12
| char ch='a';
cout<<ch<<endl; cout<<sizeof(char)<<endl; cout<<"a的ASCII码值"<<(int)ch<<endl; cout<<"A的ASCII码值"<<(int)'A'<<endl;
|
转义字符
| 转义字符 |
含义 |
ASCII码值(十进制) |
| \a |
警报 |
007 |
| \b |
退格(BS) ,将当前位置移到前一列 |
008 |
| \f |
换页(FF),将当前位置移到下页开头 |
012 |
| \n |
换行(LF) ,将当前位置移到下一行开头 |
010 |
| \r |
回车(CR) ,将当前位置移到本行开头 |
013 |
| \t |
水平制表(HT) (跳到下一个TAB位置) |
009 |
| \v |
垂直制表(VT) |
011 |
| \\ |
代表一个反斜线字符”” |
092 |
| ’ |
代表一个单引号(撇号)字符 |
039 |
| “ |
代表一个双引号字符 |
034 |
| ? |
代表一个问号 |
063 |
| \0 |
数字0 |
000 |
| \ddd |
8进制转义字符,d范围0~7 |
3位8进制 |
| \xhh |
16进制转义字符,h范围09,af,A~F |
3位16进制 |
字符串型
1 2 3 4 5 6 7 8 9 10 11 12 13
| #include <iostream> #include <string> using namespace std; int main() { char str1[]="Hello C++1"; string str2="Hello C++2"; cout<<str1<<endl; cout<<str2<<endl; return 0; }
|
布尔类型
1 2 3 4 5 6 7 8 9 10
| bool flag_t= true; bool flag_f= false; cout<<flag_t<<endl; cout<<flag_f<<endl; cout<<sizeof(bool)<<endl;
|
输入
cin>>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
| #include <iostream> #include <string> using namespace std; int main() { int a; cout<<"输入整型变量:"<<endl; cin>>a; cout<<"a="<<a<<endl;
float b; cout<<"输入浮点型变量:"<<endl; cin>>b; cout<<"b="<<b<<endl;
char c; cout<<"输入字符型变量:"<<endl; cin>>c; cout<<"c="<<c<<endl;
string str; cout<<"输入字符串型变量:"<<endl; cin>>str; cout<<"str="<<str<<endl;
bool flag; cout<<"输入布尔型变量:"<<endl; cin>>flag; cout<<"flag="<<flag<<endl; return 0; }
|
运算符
算术运算符
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| #include <iostream> #include <string> using namespace std; int main() { int a=10,b=3; cout<<"a+b="<<a+b<<endl; cout<<"a-b="<<a-b<<endl; cout<<"a*b="<<a*b<<endl; cout<<"a/b="<<a/b<<endl; cout<<"a%b="<<a%b<<endl; return 0; }
|
两数相除得实际结果
1 2 3 4 5 6 7 8 9 10
| float c = 180 / 100.f; cout<<c<<endl;
int a = 180,b = 100; float c = 0; c = (float)a / b; cout<<c<<endl;
|
递增递减
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
| #include <iostream> #include <string> using namespace std; int main() { int a=10,b; b=++a*10; cout<<"a="<<a<<endl; cout<<"b="<<b<<endl; int c=10,d; d=c++*10; cout<<"c="<<c<<endl; cout<<"d="<<d<<endl; int e=10,f; f=--e*10; cout<<"e="<<e<<endl; cout<<"f="<<f<<endl; int g=10,h; h=g--*10; cout<<"g="<<g<<endl; cout<<"h="<<h<<endl; return 0; }
|
赋值运算符
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| int main() { int a=10; cout<<"a="<<a<<endl; a+=2; cout<<"a+=2="<<a<<endl; a-=1; cout<<"a-=1="<<a<<endl; a*=3; cout<<"a*=3="<<a<<endl; a/=5; cout<<"a/=5="<<a<<endl; return 0; }
|
比较运算符
真为1
假为0
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| int main() { int a=10,b=5; cout<<(a==b)<<endl; cout<<(a!=b)<<endl; cout<<(a>b)<<endl; cout<<(a<b)<<endl; cout<<(a>=b)<<endl; cout<<(a<=b)<<endl; return 0; }
|
逻辑运算符
| 运算符 |
术语 |
示例 |
结果 |
| ! |
非 |
!a |
如果a为假,则!a为真; 如果a为真,则!a为假。 |
| && |
与 |
a && b |
如果a和b都为真,则结果为真,否则为假。 |
| \ |
\ |
|
或 |
a \ |
\ |
b |
如果a和b有一个为真,则结果为真,二者都为假时,结果为假。 |
程序流程结构
选择结构
if语句
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
| if(条件语句) { 执行语句 } else { 执行语句 }
if(条件语句) { 执行语句 } else if(条件语句) { 执行语句 } .... else { 执行语句 }
if(条件语句) { if(嵌套语句) { 执行语句 } ...... } ......
|
eg:三个数字比较大小并排序
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| #include <iostream> #include <string> using namespace std; int main() { int num1,num2,num3,t; cout<<"Input num1:"<<endl; cin>>num1; cout<<"Input num2:"<<endl; cin>>num2; cout<<"Input num3:"<<endl; cin>>num3; if(num1>num2) { t=num1;num1=num2;num2=t; } if(num1>num3) { t=num1;num1=num3;num3=t; } if(num2>num3) { t=num2;num2=num3;num3=t; } cout<<num1<<' '<<num2<<' '<<num3<<' '<<endl; }
|
三目运算符
作用: 通过三目运算符实现简单的判断
语法:表达式1 ? 表达式2 :表达式3
如果表达式1的值为真,执行表达式2,并返回表达式2的结果;
如果表达式1的值为假,执行表达式3,并返回表达式3的结果。
1 2 3 4 5 6
| int a=10,b=15,c; c=(a>b?a:b); cout<<"c="<<c<<endl;
|
switch语句
1 2 3 4 5 6 7
| switch(表达式) { case 结果1:执行语句;break; case 结果2:执行语句;break; ...... default:执行语句;break; }
|
循环结构
while循环语句
1 2 3 4 5 6 7 8 9
| int main() { int a=5; while(a>0) { cout<<"a="<<a<<endl; a--; } }
|
猜数字
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
| #include <iostream> #include <string> #include "ctime" using namespace std; int main() { srand((unsigned int)time(NULL)); int num=rand()%100+1; int ans,t=0; bool flag=false; cout<<"------猜数字游戏开始------"<<endl; cout<<"请输入一个1-100之间的整数"<<endl; cin>>ans; while(ans!=num) { if(t==5) { flag=true; cout<<"机会已用完,猜测失败!!!"<<endl; cout<<"正确数字为"<<num<<endl; break; } if(ans>num) { cout<<"猜大了!!!"<<endl; } else { cout << "猜小了!!!" << endl; } cout<<"还有"<<(5-t)<<"次机会!"<<endl; t++; cout<<"继续输入1-100之间的整数"<<endl; cin>>ans; } if(!flag) { cout << "------恭喜你猜中了!!!------" << endl; } system("pause"); return 0; }
|
do….while循环语句
水仙花数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| int main() { int num=100,a,b,c; do { a=num%10; b=num%100/10; c=num/100; if(a*a*a+b*b*b+c*c*c==num) { cout<<a<<"^3"<<"+"<<b<<"^3"<<"+"<<c<<"^3"<<"="<<num<<endl; } num++; }while(num<1000); return 0; }
|
for循环语句
敲桌子
案例描述:从1开始数到数字100,如果数字个位含有7,或者数字十位含有7,或者该数字是7的倍数,我们敲桌子一下,最后打印输出敲桌子总数。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| int main() { int t=0; for(int i=1;i<=100;i++) { if(i%10==7||i/10==7||i%7==0) { t++; } } cout<<"共"<<t<<"次敲桌子"<<endl; return 0; }
|
for循环的嵌套
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| int main() { for(int i=0;i<4;i++) { for(int j=0;j<6;j++) { cout << "* "; } cout<<endl; } return 0; }
|
乘法口诀
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| int main() { for(int i=1;i<10;i++) { for(int j=1;j<=i;j++) { cout<<j<<"*"<<i<<"="<<i*j<<' '; }cout<<endl; } return 0; }
|
跳转语句
break语句
作用: 用于跳出选择结构或者循环结构
break使用的时机:
- 出现在switch条件语句中,作用是终止case并跳出switch
- 出现在循环语句中,作用是跳出当前的循环语句
- 出现在嵌套循环中,跳出最近的内层循环语句
continue语句
作用:在循环语句中,跳过本次循环中余下尚未执行的语句,继续执行下一次循环
goto语句
作用:可以无条件跳转语句
语法: goto 标记;
解释:如果标记的名称存在,执行到goto语句时,会跳转到标记 的位置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| int main() { cout<<"11111"<<endl; cout<<"22222"<<endl; goto FLAG; cout<<"33333"<<endl; cout<<"44444"<<endl; FLAG: cout<<"55555"<<endl; return 0; }
|