基本格式: 1 2 3 4 5 6 7 #include <iostream> using namespace std;int main () { system ("pause" ); return 0 ; }
输出函数cout << 1 2 3 4 5 cout << "输出内容" << endl; cout << "a = " << a << endl; cout << (int )ch <<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 int a = 0 ;cout <<"请给整型变量a赋值" << endl; cin >> a; cout << "整型变量a为" << a << endl; float b = 3.14f ;cout << "请给浮点型变量b赋值" << endl; cin >> b; cout << "浮点型变量b为" << b << endl; char ch = 'a' ;cout <<"请给字符变量ch赋值" << endl; cin >> ch; cout << "字符变量ch为" << ch << endl; #include <string> string str = "hello" ; cout <<"请给字符串变量str赋值" << endl; cin >> str; cout << "字符串变量str为" << str << endl; bool flag = false ;cout <<"请给布尔型变量flag赋值" << endl; cin >> flag; cout << "布尔型变量flag为" << flag << endl;
注释
变量使用(方面管理内存空间)
定义常量两种方法 1 2 3 4 5 6 7 8 9 10 #define Day 7 cout << "一周有:" << Day << "天" <<endl; int month = 12 ;month = 24 ; const int month = 12 ;month = 24 ;
标识符/关键字: 不能用来给变量或常量起名称
标识符命名规则: 不能是关键字;只能由字母、数字、下划线组成,第一个字符必须是字母或下划线;区分大小写;最好见名知义
数据类型(给变量分配合适的内存空间) 整型 1.整型int:4字节 -2^312^31-1 2.短整型short:2字节 -2^15(-32768)2^15-1(32767) 3.长整型long:Windows4字节,Linux(32位)4字节,(64位)8字节 4.长长整型:longlong:8字节
实型(浮点型) 1.单精度float:4字节,有效数字7位
2.双精度double:8字节,有效数字15~16位
3.科学计数法:
1 2 3 4 float num1 = 3.14; float num2 = 3.14f;//浮点数后加上f可以让电脑识别出为单精度,不然会以双精度接收 //但是默认都是以最高六位小数输出 float num3 =3e2;//3 * 10 ^ 2输出为300
字符型 1 2 3 4 5 6 7 char ch = 'a' ;cout << ch << endl; cout << "char字符型变量所占内存" << sizeof (char ) << endl; cout << (int )ch << endl;
字符串 1 2 3 4 5 6 7 char str1[] = "helloworld" ;cout << str1 << endl; string str2 = "helloworld" ; cout << str2 << endl;
布尔类型bool bool只有两个值,true——1,false——2,占用1字节
1 2 3 bool flag = true ;cout << flag << endl; cout <<sizeof (bool ) << endl;
sizeof:统计数据类型所占内存大小 1 2 3 cout <<sizeof (int ) << endl; cout <<sizeof (num1) << endl;
转义字符:用于表示不能显示出来的ASCLL字符 1.\n:换行
2.\\:反斜杠(\无法单独输出)
3.\t:水平制表符(输出对齐)
1 2 3 4 5 cout << "hello world\n" << endl; cout << "\\" << endl; cout << "aaaa\thelloworld" << endl; cout << "aaaaaa\thelloworld" << endl; cout << "aa\thelloworld" << endl;
运算符(执行代码的运算) 算数运算符 1、+ 正号/加
2、- 负号/减
3、* 乘
4、/ 除
5、% 取模、取余
6、++ 前置递增、后置递减
7、– 前置递减、后置递减
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 #include <iostream> using namespace std;int main () { int a1 = 10 ; int b1 = 3 ; cout << a1 + b1 << endl; cout << a1 - b1 << endl; cout << a1 * b1 << endl; cout << a1 / b1 << endl; double d1 = 0.5 ; double d2 = 0.25 ; cout << d1 / d2 << endl; cout << a1 % b1 <<endl; int a2 = 10 ; int b2 = ++a2 * 10 ; int a3 = 10 ; int c3 = a3++ * 10 ; cout << "b2 = " << b2 << endl; cout << "c3 = " << c3 << endl; system ("pause" ); return 0 ; }
赋值运算符 1、赋值=
2、+=: a = a + …
3、-=: a = a - …
4、*=: a = a * …
5、/=: a = a / …
6、%=: a = a % …
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 int a = 10 ;a = 100 ; cout << "a = " << a << endl; a = 10 ; a += 2 ; cout << "a = " << a << endl; a = 10 ; a -= 2 ; cout << "a = " << a << endl; a = 10 ; a *= 2 ; cout << "a = " << a << endl; a = 10 ; a /= 2 ; cout << "a = " << a << endl; a = 10 ; a %= 2 ; cout << "a = " << a << endl;
比较运算符 1、==等于
2、!=不等于
3、<小于
4、>大于
5、<=小于等于
6、>=大于等于
1 2 3 4 5 6 7 8 int a = 10; int b = 20; cout << (a == b) << endl; cout << (a != b) << endl; cout << (a > b) << endl; cout << (a < b) << endl; cout << (a >= b) << endl; cout << (a <= b) << endl;
逻辑运算符 1、!非
2、&&与
3、||或
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 int a = 10; cout << !a << endl;//0,对10(true)取反 cout << !!a << endl;//1 int b = 10; int c = 10; cout c< ( b && c ) << endl;//1 int c = 0; cout << ( b && c ) << endl;//0 int d = 10; int e = 0; cout << ( d || e ) << endl;//1 d = 0; cout << ( d || e ) << endl;//0
程序流程结构 顺序结构 选择结构 if语句 三种形式:
1、单行格式
1 2 3 4 5 6 7 8 9 10 11 12 13 int main () { int score = 0 ; cin >> score; cout << "您输入的分数为" << score << endl; if (score > 600 ) { cout << "恭喜您考上一本" << endl; } system ("pause" ); return 0 ; }
2、多行格式if else
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 () { int score = 0 ; cout << "请输入您的分数" << endl; cin >> score; cout << "您输入的分数为" << score << endl; if (score > 600 ) { cout << "恭喜考上一本" << endl; } else { cout << "未考上一本" << endl; } system ("pause" ); return 0 ; }
3、多条件格式if-else if-else if-else多条件判断
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 #include <iostream> using namespace std;int main () { int score = 0 ; cout << "请输入分数" << endl; cin >> score; if (score > 600 ) { cout << "您的分数为" << score << ",考上一本" << endl; } else if ( score > 500 ) { cout << "您的分数为" << score << ",考上二本" << endl; } else if (score > 400 ) { cout << "您的分数为" << score << ",考上三本" << endl; } else { cout << "您的分数为" << score << ",未考上本科" << endl; } system ("pause" ); return 0 ; }
4、嵌套格式
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;int main () { int score = 0 ; cout << "请输入分数" << endl; cin >> score; if (score > 600 ) { cout << "您的分数为" << score << ",考上一本" << endl; if (score > 700 ) { cout << "您的分数为" << score << ",考上清华" << endl; } else if (score > 650 ) { cout << "您的分数为" << score << ",考上北大" << endl; } else { cout << "您的分数为" << score << ",考上人大" << endl; } } else if (score >500 ) { cout << "您的分数为" << score << ",考上二本" << endl; } else if (score > 400 ) { cout << "您的分数为" << score << ",考上三本" << endl; } else { cout << "您的分数为" << score << ",未考上本科" << 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 #include <iostream> using name space std;int main () { int a = 0 ; int b = 0 ; int c = 0 ; cout << "请输入第一只小猪的体重" << endl; cin >> a; cout << "请输入第二只小猪的体重" << endl; cin >> b; cout << "请输入第三只小猪的体重" << endl; cin >> c; if (a > b) { if (a > c) { cout << "第一只小猪最重,体重为" << a << endl; } else { cout << "第三只小猪最重,体重为" << c << endl; } else { if (b > c) { cout << "第二只小猪最重,体重为" << b << endl; } else { cout << "第三只小猪最重,体重为" << c << endl; } } system ("pause" ); return 0 ; }
三目运算符 结构:表达式1?表达式2:表达式3
若表达式1为true,则执行表达式2,并返回2的结果,若表达式1为false,则执行表达式3,并返回3的结果
返回的是变量值,可以继续赋值
1 2 3 4 5 6 7 8 9 10 11 12 #include <iostream> using namespace std;int main () { int a = 0 ; int b = 10 ; int c = 0 ; c = a > b ? a : b; system ("pause" ); return 0 ; }
switch语句 语法:
switch (表达式){case 结果1;break;case 结果2;break;default:最后结果;break;}
缺点:判断的时候只能是整型或者字符型,不能是一个区间
优点:结构清晰,执行效率高
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 #include <iostream> using namespace std;int main () { int score = 0 ; cout << "请输入电影分数" << endl; cin >> score; cout << "您打的分数为" << score << endl; switch (score) { case 9 || 10 : cout << "您认为是经典电影" << endl; break ; case 7 || 8 : cout << "您认为是非常好的电影" << endl; break ; case 5 || 6 : cout << "您认为是一般电影" << endl; break ; default : cout << "您认为是很烂的电影" << endl; } system ("pause" ); return 0 ; }
循环结构 while循环 格式:while (循环条件) {循环语句}//条件为1时循环不结束,可以使用break退出循环
1 2 3 4 5 6 7 8 9 10 11 12 13 #include <iostream> using namespace std;int main () { int num1 = 0 ; while (num1 < 10 ) { cout << num1 << endl; num1++; } 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 #include <iostream> #include <ctime> using namespace std;int main () { srand ((unsigned int )time (Null)); int ran = rand ()%100 + 1 ; int num = 0 ; cout << "请输入数字" << endl; cin >> num; while (ran != num) { if (num > ran) { cout << "偏大" << endl; cin >> num; } if (num < ran) { cout << "偏小" << endl; cin >> num; } } cout << "恭喜你猜对啦!" << endl; system ("pause" ); return 0 ; }
do-while循环语句 与while的差别是会先执行一次循环内容,再判断循环条件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include <iostream> using namespace std;int main () { int num = 0 ; do { cout << num << endl; num++ } while (n < 10 ); 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 #include <iostream> using namespace std;int main () { int num = 100 ; int num1 = 0 ; int num2 = 0 ; int num3 = 0 ; while (num < 1000 ) { num1 = num % 10 ; num2 = num / 10 % 10 ; num3 = num / 100 ; if (num == num1 * num1 * num1 + num2 * num2 * num2 + num3 * num3 * num3) { cout << num << endl; } num++; } system ("pause" ); return 0 ; }
for循环 格式:for(起始表达式(创建变量i、赋值等);条件表达式(循环条件);末尾表达式(结束循环后的表达式)){ 循环语句;}
注意表达式之间用;分隔,结构清晰,常用
1 2 3 4 5 6 7 8 9 10 11 12 #include <iostream> using namespace std;int main () { for ( int i = 0 ; i < 10 ; i++) { cout << i << endl; } system ("pause" ); return 0 ; }
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 () { for ( int i = 1 ; i < 101 ; i++) { if ( i % 7 == 0 || i / 10 == 7 || i % 10 = 7 ) { cout << "敲桌子" << endl; } else { cout << i << endl; } } system ("pause" ); return 0 ; }
嵌套循环 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include <iostream> using namespace std;int main () { for (int j = 0 ; j < 10 ; j++) { for (int i = 0 ; i < 10 ; i++) { cout << "* " ; } cout << endl; } system ("pause" ); return 0 ; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include <iostream> using namespace std;int main () { for (int i = 1 ; i < 11 ; i++) { for (int j = 1 ; j <= i ; j++) { cout << j << "*" << i << "=" << j * i << "\t" ; } cout << endl; } system ("pause" ); return 0 ; }
跳转语句 break语句 1、switch条件语句中终止case并跳出switch
2、循环语句中跳出当前循环语句
3、嵌套循环中跳出最近的内层循环语句
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 #include <iostream> using namespace std;int main () { cout << "请选择副本难度前的序号" << endl; cout << "1、简单" << endl; cout << "2、普通" << endl; cout << "3、困难" << endl; int con = 0 ; cin >> con; switch (con) { case 1 : cout << "您选择的是简单" << endl; break ; case 2 : cout << "您选择的是普通" << endl; break ; case 3 : cout << "您选择的是困难" << endl; break ; default : break ; } system ("pause" ); return 0 ; }
continue语句 作用:在循环语句中,跳过本次循环中余下未执行的语句,继续执行下次循环(终止循环进入下次循环)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include <iostream> using namespace std;int main () { for (int i = 0 ; i < 100 ; i++) { if (i % 2 ==0 ) { continue ; } cout << i << endl; } system ("pause" ); return 0 ; }
goto语句 作用:可以无条件跳转语句,但是影响代码逻辑结构,不方便其他人阅读
语法:goto 标记;标记;
如果标记的名称存在,执行到goto语句时,会跳转到标记的位置
1 2 3 4 5 6 7 8 9 10 11 12 13 #include <iostream> using namespace std;int main () { int i = 0 ; cout << i + 1 << endl; goto FLAG; cout << i + 2 << endl; FLAG; cout << i + 3 << endl; system ("pause" ); return 0 ; }
数组 定义方法:
1、数据类型 数组名[数组长度];//数组下标从0开始索引
2、数据类型 数组名[数组长度] = {值1, 值2 ……};//初始数据不完全赋值,会用0来填补空缺
3、数据类型 数组名[ ] = {值1, 值2 ……};//不标注长度,自动识别
特点:
1、放在一块连续的内存空间中
2、数组中每个元素数据类型相同
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;int main () { int arr1[5 ]; arr1[0 ] = 10 ; cout << arr1[0 ] << endl; int arr2[5 ] = { 10 , 20 , 30 , 40 }; cout << arr2[0 ] << endl; for ( int i = 0 ; i <5 ; i++) { cout << arr2[i] << endl; } arr[] = {10 , 20 ,30 , 40 , 50 , 60 , 70 }; system ("pause" ); return 0 ; }
数组名的用途:
1、可以统计整个数组在内存中的长度
2、可以获取数组在内存中的首地址
//数组名是常量,不可以进行赋值操作
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include <iostream> using namespace std;int main () { int arr1[5 ] = {1 , 2 , 3 , 4 , 5 }; cout << "整个数组占用的内存空间为" << sizeof (arr1) << endl; cout << "每个元素占用的内存空间为" << sizeof (arr1[0 ]) << endl; cout << "数组类型首地址为" << arr1 << endl; cout << "数组类型首地址为" << (int )arr1 << endl; cout << "数组第一个元素的首地址" << (int )&arr1[0 ] <<endl; cout << "数组第二个元素的首地址" << (int )&arr1[1 ] <<endl; system ("pause" ); return 0 ; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 #include <iostream> using namespace std;int main () { int arr[5 ] = {300 , 350 , 400 , 200 , 250 }; int max = 0 ; for (int i = 0 ; i < 5 ; i++) { if (arr[i] > max) { max = arr[i]; } } cout << "最大的体重为" <<max << 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 #include <iostream> using namespace std;int main () { int arr1[5 ] = {1 , 2 , 3 , 4 , 5 }; int temp = 0 ; int left = 0 ; int right = sizeof (arr1)/sizeof (arr1[0 ]) - 1 ; for (; left < right; left++, right--) { temp = arr1[left]; arr1[left] = arr1[right]; arr1[right] = temp; } for (int i = 0 ; i < sizeof (arr1)/sizeof (arr1[0 ]); i++) { cout << arr1[i] << 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 #include <iostream> using namespace std;int main () { int arr1[5 ] = {1 , 2 , 3 , 4 , 5 }; int num = sizeof (arr1)/sizeof (arr1[0 ]); int arr2[num - 1 ]; for (int i = 0 ; i < num; i++) { arr2[num - 1 - i] = arr1[i]; } for (int j = 0 ; j < num; j++) { cout << arr2[j] << endl; } system ("pause" ); return 0 ; }