年轻人,你的职责是平整土地,而非焦虑时光,你做三四月的事,在八九月自有答案。
—— 余世存 《时间之书》
定义宏常量 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 #include <iostream> using namespace std;#define Day 7; int main () { int x = Day; const int Month= 12 ; cout <<"一周有" << x << "天" << endl; cout <<"一年有" << Month << "月" << endl; system ("pause" ); return 0 ; }
变量命名规则 作用:C++规定给标识符(变量、常量)命名时,有一套自己的规则
数据类型 整型 作用:证书类型数据。
数据类型
占用空间
取值范围
short(短整型)
2字节
(-2^15~2^15-1)
int(整型)
4字节
(-2^15~2^15-1)
long(长整形)
Windows为4字节,Linux为4字节(32位),8字节(64位)
(-2^31~ 2^31-1)
long long(长长整形)
8字节
(-2^63~2^63-1)
sizeof关键字 作用:统计数据类型说占内存大小。
语法:sizeof(数据类型 / 变量)
short < int <= long <= longlong
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 #include <iostream> using namespace std;#define Day 7; int main () { int c = 1111111 ; cout << sizeof (c) << endl; system ("pause" ); return 0 ; }
实型(浮点型) 作用:表示小数
单精度float
双精度double
数据类型
占用空间
有效字范围
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 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 #include <iostream> using namespace std;#define Day 7; int main () { float f1 = 3.1415926f ; double d1 = 3.1415926 ; cout << "单精度:" << f1 << endl; cout << "双精度:" << d1 << endl; cout << "sizeof(float):" << sizeof (f1) << endl; cout << "sizeof(double):" << sizeof (d1) << endl; float f2 = 3e2 ; cout << f2 << endl; float f3 = 3e-2 ; cout << f3 << endl; system ("pause" ); return 0 ; }
字符型 作用:字符型变量用于显示单个字符
语法:char ch = 'a';
注意1:在显示字符型变量时,用单引号将字符括起来,不要用双引号
注意2:单引号内只能有—个字符,不可以是字符串
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 #include <iostream> using namespace std;#define Day 7; int main () { char ch = 's' ; cout << ch << endl; cout << "字符串占内存大小:" << ch << endl; cout << (int )ch << endl; system ("pause" ); return 0 ; }
ASCII码表 ASCI码大致由以下两部分组成:
ASCII码表
转义字符 作用:用于表示—些不能显示出来的ASCIl字符
现阶段我们常用的转义字符有:\n \\ \t
转义字符
意义
ASCII码值(十进制)
\a
响铃(BEL)
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
空字符(NUL)
000
\ddd
1到3位八进制数所代表的任意字符
三位八进制
\xhh
十六进制所代表的任意字符
十六进制
字符串类型 作用:用于表示—串字符I
两种风格:
C风格字符串:char变量名[] = “字符串值”
C++风格字符串:string 变量名 = “字符串值”
(需引用头文件)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 #include <iostream> using namespace std;#include <string> #define Day 7; int main () { char str1[] = "HelloWorld" ; string str2 = "HelloWorld" ; cout << str1 << endl; cout << str1[0 ] << endl; cout << str2 << endl; cout << str2[1 ] << endl; system ("pause" ); return 0 ; }
布尔数据类型 作用:布尔数据类型代表真或假的值
bool类型只有两个值:
true —真(本质是1)
false —假(本质是0)
bool类型占1个字节大小
非0值都为真
数据输入 作用:用于从键盘获取数据
关键字:cin
语法: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 #include <iostream> using namespace std;#include <string> #define Day 7; int main () { int x = 100 ; int a = 0 ; int b = 0 ; int c = 0 ; int y = 0 ; do { c = x % 10 ; b = x / 10 % 10 ; a = x / 100 ; y = a*a*a + b*b*b + c*c*c ; if (y == x) { cout << x << endl; } x++; } while (x < 1000 ); system ("pause" ); 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> using namespace std;#include <string> int main () { for (int x1 = 1 ; x1 < 10 ; x1++) { string output = "" ; for (int x2 = 1 ; x2 <= x1; x2++) { string rowoutput = to_string (x2) + " * " + to_string (x1) + " = " + to_string (x1 * x2); output =output + rowoutput + "\t" ; } cout << output << endl; } system ("pause" ); return 0 ; }
一个小箭头 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 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 #include <iostream> using namespace std;#include <string> void WhiteRectangle (int r, int countR) ;void WhiteTriangle (int r, int count) ;void BlackRectangle (int r, int countR) ;void BlackTriangle (int r, int count) ;int main () { short mak = 1 ; int r = 0 ; int count = 93 ; int countR = (count + 1 ) / 2 ; while (true ) { WhiteRectangle (r, countR); WhiteTriangle (r,count); BlackRectangle (r, countR); BlackTriangle (r,count); } system ("pause" ); return 0 ; } void WhiteRectangle (int r, int countR) { for (; r < countR; r++) { string str = "" ; for (int r1 = 0 ; r1 < 21 ; r1++) { str = "█" + str; } for (int r2 = 0 ; r2 < 51 ; r2++) { str = str + "□" ; } for (int r3 = 0 ; r3 < 21 ; r3++) { str = str + "█" ; } cout << str << endl; } } void WhiteTriangle (int r,int count) { for (; r < (count +1 )/2 ; r++) { string str = "" ; for (int r1 = 0 ; r1 < (count - 2 * r); r1++) { str = "□" + str; } for (int r2 = 0 ; r2 < r; r2++) { str = "█" + str; } for (int r2 = 0 ; r2 < r; r2++) { str = str + "█" ; } cout << str << endl; } string str = "" ; for (int rr = 0 ; rr < 93 ; rr++) { str = str + "□" ; } cout << str << endl; } void BlackRectangle (int r, int countR) { for (; r < countR; r++) { string str = "" ; for (int r1 = 0 ; r1 < 21 ; r1++) { str = "□" + str; } for (int r2 = 0 ; r2 < 51 ; r2++) { str = str + "█" ; } for (int r3 = 0 ; r3 < 21 ; r3++) { str = str + "□" ; } cout << str << endl; } } void BlackTriangle (int r,int count) { for (; r < (count +1 )/2 ; r++) { string str = "" ; for (int r1 = 0 ; r1 < (count - 2 * r); r1++) { str = "█" + str; } for (int r2 = 0 ; r2 < r; r2++) { str = "□" + str; } for (int r2 = 0 ; r2 < r; r2++) { str = str + "□" ; } cout << str << endl; } string str = "" ; for (int rr = 0 ; rr < 93 ; rr++) { str = str + "█" ; } cout << str << endl; }