一、运算符
#include<iostream>
#include <string>
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;
cout << a1 % b1 << endl;
cout << 10 % 20 << endl;
double d1 = 0.5;
double d2 = 0.25;
double d3 = 0.22;
cout << d1 / d2 << endl;
cout << d1 / d3 << endl;
return 0;
}
#include<iostream>
#include <string>
using namespace std;
int main()
{
int a = 10;
++a;
cout << "a:" << a << endl;
int b = 10;
b++;
cout << "b:" << b << endl;
int a2 = 10;
int b2 = ++a2 * 10;
cout << "b2" << b2 << endl;
int a3 = 10;
int b3 = a3++ * 10;
cout << "b3:" << b3 << endl;
return 0;
}
三目运算符练习代码:
#include<iostream>
#include <string>
using namespace std;
int main()
{
int a = 10;
int b = 20;
int c = a > b ? a : b;
cout << c << endl;
int d = a < b ? a : b;
cout << d << endl;
return 0;
}
#include<iostream>
using namespace std;
int main()
{
int a = 10;
int b = 0;
cout << !a << endl;
cout << (a && b) << endl;
cout << (a || b) << endl;
return 0;
}
- && :同真为真,其余为假
- || : 同假为假,其余为真
二、程序流程结构
C/C++支持最基本的三种程序运行结构:顺序结构、选择结构、循环结构
(一)选择结构
if语句
作用:执行满足条件的语句
if语句的三种形式:
练习代码如下:
#include<iostream>
using namespace std;
int main()
{
int score = 0;
cout << "请输入分数:" << endl;
cin >> score;
cout << "您输入的分数为:" << score << endl;
if (score > 600)
{
cout << "恭喜考上一本大学" << endl;
}
return 0;
}
#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;
}
return 0;
}
#include<iostream>
using namespace std;
int main()
{
int score = 0;
cout << "请输入您的分数" << endl;
cin >> score;
cout << "您输入的分数为:" << score << endl;
if (score > 600)
{
cout << "恭喜考上一本大学" << endl;
}
else if (score > 500)
{
cout << "恭喜考上二本大学" << endl;
}
else if (score > 400)
{
cout << "恭喜考上三本大学" << endl;
}
else
{
cout << "很遗憾您没有考上大学!" << endl;
}
return 0;
}
#include<iostream>
using namespace std;
int main()
{
int score = 0;
cout << "请输入您的分数" << endl;
cin >> score;
cout << "您输入的分数为:" << score << endl;
if (score > 600)
{
cout << "恭喜考上一本大学" << endl;
if (score > 700)
{
cout << "恭喜考入北京大学" << endl;
}
else if (score > 650)
{
cout << "恭喜考入清华大学" << endl;
}
else
{
cout << "恭喜考入人民大学" << endl;
}
}
else if (score > 500)
{
cout << "恭喜考上二本大学" << endl;
}
else if (score > 400)
{
cout << "恭喜考上三本大学" << endl;
}
else
{
cout << "很遗憾您没有考上大学!" << endl;
}
return 0;
}
案例练习:三只小猪称体重?
有三只小猪ABC,请分别输入三只小猪的体重,并且判断哪只小猪最重?
代码如下:
#include<iostream>
using namespace std;
int main()
{
int num1 = 0;
int num2 = 0;
int num3 = 0;
cout << "请输入A的重量:" << endl;
cin >> num1;
cout << "请输入B的重量:" << endl;
cin >> num2;
cout << "请输入C的重量:" << endl;
cin >> num3;
cout << "A的体重为:" << num1 << endl;
cout << "B的体重为:" << num2 << endl;
cout << "C的体重为:" << num3 << endl;
if (num1 > num2)
{
if (num1 > num3)
{
cout << "小猪A最重" << endl;
}
else
{
cout << "小猪C最重" << endl;
}
}
else
{
if (num1 > num3)
{
cout << "小猪B最重" << endl;
}
else
{
cout << "小猪C最重" << endl;
}
}
return 0;
}
switch语句
作用:执行多条件分支语句
练习代码如下:
#include<iostream>
using namespace std;
int main()
{
int score = 0;
cout << "请输入你电影的评分:" << endl;
cin >> score;
cout << "你打的分数为:" << score << endl;
switch (score)
{
case 10:
cout << "你认为该电影是经典电影" << endl;
break;
case 9:
cout << "你认为该电影是经典电影" << endl;
break;
case 8:
cout << "你认为该电影非常好" << endl;
break;
case 7:
cout << "你认为该电影非常好" << endl;
break;
case 6:
cout << "你认为该电影一般好" << endl;
break;
case 5:
cout << "你认为该电影一般好" << endl;
break;
default:
cout << "你认为这是烂片" << endl;
break;
}
return 0;
}
(二)循环结构
while循环
作用:满足循环条件,执行循环语句
练习代码如下:
#include<iostream>
using namespace std;
int main()
{
int num = 0;
while (num < 10)
{
cout << num << endl;
num++;
}
return 0;
}
案例练习:系统随机生成一个1-100之间的数字,玩家进行猜测,提示玩家数字过大或过小,如果猜对恭喜玩家胜利,并退出游戏
案例流程图如下:
案例代码如下:
#include<iostream>
#include <ctime>
using namespace std;
int main()
{
srand((unsigned int)time(NULL));
int num = rand() % 100 + 1;
cout << num << endl;
int val = 0;
while (1)
{
cin >> val;
if (val > num)
{
cout << "您猜测的值过大" << endl;
}
else if (val < num)
{
cout << "您猜测的值过小" << endl;
}
else
{
cout << "恭喜您猜测正确" << endl;
break;
}
}
return 0;
}
do…while循环
- 与while的区别在于do…while先执行一次循环语句,再判断循环条件
练习代码如下:
#include<iostream>
using namespace std;
int main()
{
int num = 0;
do
{
cout << num << endl;
num++;
} while (num < 10);
return 0;
}
案例:水仙花数
水仙花数是指一个三位数,它的每个位上的数字的三次幂之和等于它本身,利用do…while语句,求出所有3位数中的水仙花数
案例代码如下:
#include<iostream>
using namespace std;
int main()
{
int num = 100;
do
{
int a = 0;
int b = 0;
int c = 0;
a = num % 10;
b = num / 10 % 10;
c = num / 100;
if (a * a * a + b * b * b + c * c * c == num)
{
cout << num << endl;
}
num++;
} while (num < 1000);
return 0;
}
for循环
作用:满足循环条件,执行条件语句
练习代码如下:
#include<iostream>
using namespace std;
int main()
{
int i = 0;
for (i; i < 10; i++)
{
cout << i << endl;
}
for (int i = 0; i < 10; i++)
{
cout << i << endl;
}
return 0;
}
练习案例:从1-100,如果数字个位含有7,或者数字十位含有7,或者数字是7的倍数,我们打印敲桌子,其余数字直接打印输出
案例代码如下:
#include<iostream>
using namespace std;
int main()
{
for (int i = 1; i <= 100; i++)
{
if (i % 10 == 7 || i / 10 % 10 == 7 || i % 7 == 0)
{
cout << "敲桌子" << endl;
}
else
{
cout << i << endl;
}
}
return 0;
}
嵌套循环
作用:在循环中再嵌套一层循环,通常用来解决一些实际性问题
练习代码如下:
#include<iostream>
using namespace std;
int main()
{
for (int i = 0; i < 8; i++)
{
for (int j = 0; j < 8; j++)
{
cout << "*" << " ";
}
cout << endl;
}
return 0;
}
案例:乘法口诀表
案例代码如下:
#include <iostream>
using namespace std;
int main()
{
for (int i = 1; i <= 9; i++)
{
for (int j = 1; j <= i; j++)
{
cout << j << "*" << i << "=" << i * j << " ";
}
cout << endl;
}
return 0;
}
三、跳转语句
(一)break语句
练习代码如下:
#include <iostream>
using namespace std;
int main()
{
cout << "请选择副本难度" << endl;
cout << "1、普通" << endl;
cout << "2、中等" << endl;
cout << "3、困难" << endl;
int select = 0;
cin >> select;
switch (select)
{
case 1:
cout << "选择了普通难度" << endl;
break;
case 2:
cout << "选择了中等难度" << endl;
break;
case 3:
cout << "选择了困难难度" << endl;
break;
default:
break;
}
for (int i = 0; i < 10; i++)
{
if (i == 5)
{
break;
}
cout << i << endl;
}
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 10; j++)
{
if (j == 5)
{
break;
}
cout << "*" << " ";
}
cout << endl;
}
return 0;
}
(二)continue语句
作用:在循环语句中,跳过本次循环中余下的尚未执行的语句,执行下一次循环
练习代码如下:
#include <iostream>
using namespace std;
int main()
{
for (int i = 0; i <= 100; i++)
{
if (i % 2 == 0)
{
continue;
}
cout << i << endl;
}
return 0;
}
(三)goto语句
作用:可以无条件跳转语句
练习代码如下:
#include <iostream>
using namespace std;
int main()
{
cout << "1" << endl;
cout << "2" << endl;
cout << "3" << endl;
cout << "4" << endl;
cout << "5" << endl;
cout << "6" << endl;
cout << "1" << endl;
cout << "2" << endl;
goto FLAG;
cout << "3" << endl;
cout << "4" << endl;
cout << "5" << endl;
FLAG:
cout << "6" << endl;
return 0;
}
???
|