#include<iostream>
using namespace std;
//算术运算符+,-,*,/,%,自增运算符++,自减运算符--
int main() {
int a = 10;
int a1 = 10;
int b = ++a * 10;//先自增后运算
int c = a1++ * 10;//先运算后自增
cout << "a=" << a << endl;
cout << "b=" << b << endl;//b=110
cout << "c=" << c << endl;//c=100
//关系运算符>,<,>=,<=,==,!=
//赋值运算符=,+=,-=,*=,/=,%=
int num = 10;
num += 5;//num=num+5
cout << "num=" << num << endl;//num=15
//逻辑运算符&&,||,!
//!:非,取反,true变false,false变true
//&&:与,两个操作数都为true,结果才为true
//||:或,两个操作数有一个为true,结果就为true
return 0;
}
4.循环,选择
cpp复制代码
#include<iostream>
using namespace std;
#include<ctime>//time系统时间头文件
int main() {
//选择结构if语句
//if(条件表达式){实体}else if (条件表达式){实体}...else{实体}
int score;
cout << "请输入成绩:";
cin >> score;
if (score >= 90 && score <= 100) {
cout << "优秀" << endl;
}
else if (score >= 80 && score < 90) {
cout << "良好" << endl;
}
else if (score >= 70 && score < 80) {
cout << "中等" << endl;
}
else if (score >= 60 && score < 70) {
cout << "及格" << endl;
}
else if (score >= 0 && score < 60) {
cout << "不及格" << endl;
}
else {
cout << "输入成绩有误!" << endl;
}
//三目运算符 expression1?expression2:expression3;如果expression1为true,
// 则表达式的值为expression2,否则表达式的值为expression3
int a = 10;
int b = 20;
int c;
c = a > b ? a : b;
cout << "c=" << c << endl;//c=20
//switch case的用法;case里面没有break,那么程序会一直向下执行
//1,提示用户给电影评分
cout << "请给电影评分" << endl;
//2.用户开始进行打分
int score1 = 0;
cin >> score1;
cout << "您打的分数为" << score1 << endl;
switch (score1) {
case 10:
cout << "您认为是经典电影" << endl;
break;
case 9:
cout << "您认为是高分电影" << endl;
break;
case 8:
cout << "可以算高分" << endl;
break;
default :
cout << "勉强可以" << endl;
break;
}
//while循环
//在屏幕上输出1到10
int i = 1;
while (i <= 10) {
cout << i << endl;
i++;
}
//do{ }while(条件)循环;至少执行一次循环体
int j = 1;
do
{
cout << j << endl;
j++;
} while (j<=1);
//利用rand()来生成随机数;
//添加随机数种子,保证每次运行程序生成的随机数不一样srand((unsigned int)time(NULL));
//这一点跟python不同,python的随机数种子是保证每次运行程序生成的随机数一样的
srand((unsigned int)time(NULL));
int age=rand() % 100+1;
int w_age;
while (true) {
cout << "继续猜一个数" << endl;
cin >> w_age;
if (w_age < age) {
cout << "猜的数字小了" << endl;
}
else if(w_age >age) {
cout << "猜的数字大了" << endl;
}
else {
cout << "恭喜你,猜对了" << age << "=" <<w_age<< endl;
break;
}
}
int shui=100;
do {
int shui1 = shui % 10;
int shui2 = shui / 10 % 10;
int shui3 = shui / 100;
if ((shui == shui1 * shui1 * shui1 + shui2 * shui2 * shui2 + shui3 * shui3 * shui3)) {
cout << shui << "为水仙花" << endl;
}
shui++;
} while (shui<=999);
//for循环
//语法for(起始表达式;条件;更新条件)
for (int i = 0; i < 9; i++) {
//实体
}
for (int i = 1; i < 100; i += i) {
cout << i << endl;//从i=1开始输出
}//更新条件在每次循环结束后执行
for (int i = 1; i <= 9; i++) {
for (int j = 1; j <= i; j++) {
cout << j << "x" << i <<"=" << i * j << " ";
}
cout << endl;
}
//goto语句,可以无条件跳转语句
//语法:goto 标记
cout << "1" << endl;
goto FLAG;//开始跳转,跳转到FLAG:处
cout << "2" << endl;//不再输出
FLAG:
cout << "3" << endl;
return 0;
}
5.数组
cpp复制代码
#include<iostream>
using namespace std;
int main() {
//一维数组的定义方式
int arr1[5]; //定义了一个可以存放5个int类型数据的数组
int arr2[3] = { 1,2,3 }; //定义了一个可以存放3个int类型数据的数组,并且进行了初始化
int arr3[] = { 4,5,6,7,8 }; //定义了一个数组,并且进行了初始化,数组的长度由初始化数据的个数决定
//冒泡排序
int arr[]={ 2, 4, 5, 3, 6, 1, 10, 8, 9 };
int t;
for (int i = 0; i < size(arr) - 1; i++) {
for (int j = 0; j < size(arr) - 1 - i; j++) {
if (arr[j] > arr[j + 1]) {
t = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = t;
}
}
}
for (int i = 0; i < size(arr); i++) {
cout << arr[i] << " ";
}
//二维数组的定义方式
int arr_2d[2][3]; //定义了一个2行3列的二维数组
int arr_2d1[2][3] = { {1,2,3},{4,5,6} };//定义并初始化二维数组
int arr_2d2[][4] = { {1,2,3,4},{5,6,7,8} };//定义并初始化二维数组,行数由初始化数据决定
//二维数组的首地址表示法
cout << "二维数组的首地址表示法" << endl;
cout << arr_2d1 << endl; //输出二维数组的首地址
cout << arr_2d1[0] << endl; //输出二维数组第一行的首地址
cout << &arr_2d1[0][0] << endl; //输出二维数组第一个元素的地址,这里是看元素的地址需要加&
return 0;
}