- 三目运算符
- 仿真小球自由落体C++ easyx
- C++流程控制-选择结构-计算货物运费(分段函数) if-else if-else
- C++流程控制-选择结构-根据成绩打印分数段 switch-case
三目运算符
条件运算
操作数1?操作数2:操作数3
a?b:c
等价于
if(a){//判断a是否为真
//执行b语句
}else{
//执行c语句
}
仿真小球自由落体
#include "stdafx.h"
#include <iostream>
#include <graphics.h>
#include <conio.h>
#include <stdio.h>
using namespace std;
int main()
{
/*int y;
int step;
y = 100;
step = 100;
initgraph(600, 600);
cleardevice();
fillcircle(300, y, 20); // 画圆
Sleep(1000);
y = y + step;
cleardevice();
fillcircle(300, y, 20); // 画圆
Sleep(1000);
y = y + step;
cleardevice();
fillcircle(300, y, 20); // 画圆
Sleep(1000);
y = y + step;
cleardevice();
fillcircle(300, y, 20); // 画圆
Sleep(1000);
y = y + step;
cleardevice();
fillcircle(300, y, 20); // 画圆
Sleep(1000);
_getch();
closegraph();*/
float y; // y坐标
float vy; // y方向速度
float g; // y方向加速度
y = 100;
vy = 0;
g = 0.5;
initgraph(600, 600); //初始化窗口画面,宽600,高600
while (1) {
cleardevice(); // 清除掉之前绘制的内容
vy = vy + g;
y = y + vy;
if (y >= 580) // 碰到地面
vy = float(-0.95)*vy; // 受到阻力,速度反向且绝对值变小
if (y > 580)//600-20直径,防止穿地
y = 580;
fillcircle(300, int(y), 20);
Sleep(20);
}
_getch();
closegraph();
return 0;
}
选择结构-计算货物运费(分段函数)
#include "stdafx.h"
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
double t, p, w, s;
cout << "input weight: ";
cin >> w;
cout << "input distance: ";
cin >> s;
if (s < 100) p = 30;
else if (s < 200) p = 27.5;
else if (s < 300) p = 25;
else if (s < 400) p = 22.5;
else p = 20;
t = p*w*s;
cout << "cost = " << setiosflags(ios::fixed)<<setprecision(2) << t << '$' << endl;
system("pause");
return 0;
}
选择结构-根据成绩打印分数段
#include "stdafx.h"
#include <iostream>
//#include <iomanip>
using namespace std;
int main()
{
char grade;
cout << "input grade of score(a-d):";
cin >> grade;
switch (grade) {
case 'a':cout << "85-100\n"; break;
case 'b':cout << "70-84\n"; break;
case 'c':cout << "60-69\n"; break;
case 'd':cout << "<60\n"; break;
default :cout << "error\n"; break;
}
system("pause");
return 0;
}