#include<iostream>
using namespace std;
int main() {
//选择结构中的多条件if语句
//用户输入一个分数,如果分数大于600,则视为考上一本大学,再输出,如果没有考上一本大学,则打印未考上一本大学
int score;
//1.用户输入分数
cout << "请输入一个分数:" << endl;
cin >> score;
//2.打印用户输入的分数
cout << "您输入的分数为:" << score << endl;
//3.判断分数是否大于600,如果大于则输出考上一本大学,如果大于500分则考上二本大学,,如果大于400分考上三本大学,如果都没考上则输出未考上本科
//在一本分数中,如果大于700分,考上北大,大于650考上清华,大于600考上人大
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;
}
system("pause");
return 0;
}
请输入一个分数:
666
您输入的分数为:666
恭喜您考入一本大学
您考上清华
请按任意键继续. . .
案例:比较两个数的大小
有三种可能性:
x = y
x > y
x < y
#include<iostream>
using namespace std;
int main() {
int x, y;
cout << "enter x and y:"<<endl;
cin >> x >> y;
if (x != y) {
if (x > y) {
cout << "x > y" << endl;
}
else {
cout << "x < y" << endl;
}
}
else {
cout << "x = y" << endl;
}
system("pause");
return 0;
}
enter x and y:
2 2
x = y
请按任意键继续. . .
if语句案例:
有三只小猪ABC,请输入三只小猪的体重,并判断哪只小猪最重。
#include<iostream>
using namespace std;
int main() {
//声明三只小猪的体重,并输入小猪的体重
int A;
int B;
int C;
cout << "请输入三只小猪的体重(中间用空格隔开,体重要不相等):\n";
cin >> A;
cin >> B;
cin >> C;
cout << "A小猪的体重是:" << A<< endl;
cout << "B小猪的体重是:" << B << endl;
cout << "C小猪的体重是:" << C << endl;
//判断小猪的体重谁最大
if ((A > B && B > C)||(A > C && C > B)) {
cout << "A小猪的体重最大" << endl;
}
else if ((B > A && A >C)||(B > C && C > A)) {
cout << "B小猪的体重最大" << endl;
}
else if((C > B && B > A) || (C > A && A > B)) {
cout << "C小猪的体重最大" << endl;
}
system("pause");
return 0;
}
请输入三只小猪的体重(中间用空格隔开):
7 8 6
A小猪的体重是:7
B小猪的体重是:8
C小猪的体重是:6
B小猪的体重最大
请按任意键继续. . .
根据问题描述,你需要在C++画图程序中根据特定路径在表格中画出八条路径,需要用到四个if语句。具体操作步骤如下:
void drawPath(int startX, int startY, int endX, int endY) {
// 根据起点和终点坐标绘制路径
// 具体的绘制方法根据你的程序而定
}
int paths[8][4] = {
{1, 1, 1, 8}, // 第一条路径的起点为(1,1),终点为(1,8)
{1, 1, 8, 1}, // 第二条路径的起点为(1,1),终点为(8,1)
{1, 8, 8, 8}, // 第三条路径的起点为(1,8),终点为(8,8)
{8, 1, 8, 8}, // 第四条路径的起点为(8,1),终点为(8,8)
{1, 1, 8, 8}, // 第五条路径的起点为(1,1),终点为(8,8)
{8, 1, 1, 8}, // 第六条路径的起点为(8,1),终点为(1,8)
{1, 4, 8, 4}, // 第七条路径的起点为(1,4),终点为(8,4)
{4, 1, 4, 8} // 第八条路径的起点为(4,1),终点为(4,8)
};
for (int i = 0; i < 8; i++) {
drawPath(paths[i][0], paths[i][1], paths[i][2], paths[i][3]);
}
void drawPath(int startX, int startY, int endX, int endY) {
int dx = abs(endX - startX);
int dy = abs(endY - startY);
int sx = (startX < endX) ? 1 : -1;
int sy = (startY < endY) ? 1 : -1;
int err = dx - dy;
while (startX != endX || startY != endY) {
// 在这里绘制直线
// 具体的绘制方法根据你的程序而定
int e2 = err * 2;
if (e2 > -dy) {
err -= dy;
startX += sx;
}
if (e2 < dx) {
err += dx;
startY += sy;
}
}
// 在这里绘制终点
// 具体的绘制方法根据你的程序而定
}
这里使用while循环遍历起点和终点之间的所有点,在循环中使用Bresenham算法计算出需要绘制的点的坐标,并使用绘图函数绘制直线或其他路径。最后在绘制出相应的路径后,在终点处绘制结束标记。
通过以上步骤,可以实现在表格中绘制出八条路径。如果需要优化程序,可以考虑使用更高效的绘图算法、缓存已经绘制的路径等方法。