1、通用模板
#include <iostream>
using namespace std;
int main()
{
system("pause")
return 0;
}
2、注释
(1)单行注释
//单行注释
(2)多行注释
/* 多行注释 */
3、变量
//定义变量
int a = 0;
//输出变量
cout << a << endl
4、常量
//常量不可修改 //1、宏常量 define 常量名 常量值 //2、用const方法将变量转化为常量 const int a = 0;
5、关键字
6、数据类型
1、整形
(1)短整形
short num1 = 10;
(2)整形
int num2 = 10;
(3)长整形
long num3 = 20;
(4)长长整形
long long num4 = 100000;
2、sizeof 关键字
//short 类型占2个字节
short num1 = 10;
cout << "short类型所占内存大小为:" << sizeof(short) << endl;
//int 类型占4个字节
int num2 = 12;
cout << "int类型所占内存大小为:" << sizeof(int) << endl;
//long 类型占4个字节
long num3 = 45;
cout << "long类型所占内存大小为:" << sizeof(long) << endl;
//long long 类型占8个字节
long long num4 = 78;
cout << "long long类型所占内存大小为:" << sizeof(long long) << endl;
3、实型
(1)单精度
//小数默认显示6位有效数据
//float 类型占4个字节
float num1 = 3.14f;
//3e2 ---> 3*10^2
float f1 = 3e2;
(2)双精度
//double 类型占8个字节
double num2 = 2.1154;
//3e - 2 ---> 3*(0.1)^2
double f2 = 3e-2;
4、字符型
//创建字符型变量
/*
创建字符型变量常见的错误:
1、创建字符型变量时用单引号 : ''
2、单引号内只有一个字符
*/
//char 类型占1个字节
char ch = 'a';
//输出字符型变量所对应的ASCII编码
cout << (int)ch << endl;
5、字符串
/*
创建字符串常见的错误:
1、创建字符串时用双引号 : ""
*/
//C风格字符串
char str[] = "hello world";
//C++风格字符串
string str1 = "hello world";
6、转义字符
//换行 \n
//反斜杠 \\
//水平制表符 \t
cout << "aaa\thelloworld" << endl;
cout << "a\thelloworld" << endl;
cout << "aaaaa\thelloworld" << endl;
7、布尔类型
//bool 类型占1个字节
bool flag = true;
cout << flag << endl;
//输出: 1
flag = flase;
cout << flag << endl;
//输出: 0
7、数据输入
#include <iostream>
using namespace std;
#include <string>
int main()
{
//定义变量a
int a;
cout << "请输入a的值:" << endl;
//输入a的值
cin >> a;
//输出a的值
cout << a << endl;
system("pause");
return 0;
}
8、运算符
1、算数运算
int a1 = 10;
int b1 = 3;
//加减乘除
cout << "a1+b1 = " << a1 + b1 << endl; //13
cout << "a1-b1 = " << a1 - b1 << endl; //7
cout << "a1*b1 = " << a1 * b1 << endl; //30
cout << "a1/b1 = " << a1 / b1 << endl; //3
// b1++ 先运算再加 , ++a1 先加再运算
cout << b1++ << endl;
cout << ++a1 << endl;
//取余 小数之间不能做取余运算
cout << a1 % b1 << endl; //1
//小数相除 默认显示6位有效数字
double d1 = 2.5;
double d2 = 1.2;
cout << d1 / d2 << endl; //2.08333
2、比较运算
int num1 = 4;
int num2 = 3;
cout << (num1 == num2) << endl; //0
cout << (num1 != num2) << endl; //1
cout << (num1 > num2) << endl; //1
cout << (num1 < num2) << endl; //0
cout << (num1 >= num2) << endl; //1
cout << (num1 <= num2) << endl; //0
3、逻辑运算符
int a = 10;
int b = 10;
cout << (!!a) << endl; //1
cout << (a && b) << endl; //1
cout << (a || b) << endl; //1
4、三目运算符
int a = 10;
int b = 20;
int c = 0;
c = (a > b ? a : b);
(a > b ? a : b) = 100;
cout << "a = " << a << endl;
cout << "b = " << b << endl;
cout << "c = " << c << endl;
//输出 a = 10,b = 20,c = 100
9、if 语句
if (条件){
//执行任务;
}
else if (条件){
//执行任务;
}
else{
//执行任务;
}
例子:
#include <iostream>
using namespace std;
int main()
{
cout << "--------------------高考查分--------------------" << endl;
int a = 0;
cout << "请输入一个分数:" << endl;
cin >> a;
cout << "你的分数为:" << a << "分!" << endl;
if (a >= 600) {
if (a > 720) {
cout << "恭喜你考上了清华大学!" << endl;
}
else if (a > 700) {
cout << "恭喜你考上了北京大学!" << endl;
}
else if (a > 650) {
cout << "恭喜你考上了西安交大!" << endl;
}
}
else if (a > 500) {
cout << "恭喜你考上一本!" << endl;
}
else if (a > 400) {
cout << "恭喜你考上二本!" << endl;
}
else {
cout << "很遗憾,你没有考上大学!" << endl;
}
system("pause");
return 0;
}
10、switch 语句
switch (条件)
{
case num:
//执行任务
break;
default:
//执行任务
break;
}
例子:
#include <iostream>
using namespace std;
int main20()
{
int sorce = 0;
cout << "请输入你要打的分数:" << endl;
cin >> sorce;
cout << "你的评分为:" << sorce << endl;
switch (sorce)
{
case 10:
case 9:
cout << "你认为这部电影非常经典!" << endl;
break;
case 8:
case 7:
cout << "你认为这部电影非常好!" << endl;
break;
case 6:
case 5:
cout << "你认为这部电影很一般!" << endl;
break;
default:
cout << "你认为这部电影是烂片!" << endl;
break;
}
cout << "感谢你的评价!" << endl;
system("pause");
return 0;
}
11、while 语句
while (条件){
//执行的任务;
}
例子:
//从1输出到100
int a = 1;
while (a<=100){
cout << a << endl;
a++;
}
12、do while 语句
do {
执行的任务
}while (条件);
例子:
//输出0-10
int num = 0;
do {
cout << num << endl;
num++;
} while (num < 10);
13、for 循环
for (int i=1; i<=10; i++){
cout << i << endl;
}