C++ 的程序检测水箱在给定时间内是否溢出、不足或添加满
给出水箱的灌装速度、水箱的高度和水箱的半径,任务是检查水箱是否在给定时间内溢出、不足和加满。
示例
Input-: radius = 2, height = 5, rate = 10 Output-: tank overflow Input-: radius = 5, height = 10, rate = 10 Output-: tank undeflow
下面使用的做法如下 −
- 输入水箱的灌装速度、高度和半径
- 计算水箱的体积以找到水的原始流速。
- 检查条件以确定结果
- 如果期望值 < 原始值,则水箱会溢出
- 如果期望值 > 原始值,则水箱会不足
- 如果期望值 = 原始值,则水箱会在时间内加满
- 打印结果输出
算法
Start Step 1->declare function to calculate volume of tank float volume(int rad, int height) return ((22 / 7) * rad * 2 * height) step 2-> declare function to check for overflow, underflow and filled void check(float expected, float orignal) IF (expected < orignal) Print "tank overflow" End Else IF (expected > orignal) Print "tank underflow" End Else print "tank filled" End Step 3->Int main() Set int rad = 2, height = 5, rate = 10 Set float orignal = 70.0 Set float expected = volume(rad, height) / rate Call check(expected, orignal) Stop
示例
#include <bits/stdc++.h> using namespace std; //calculate volume of tank float volume(int rad, int height) { return ((22 / 7) * rad * 2 * height); } //function to check for overflow, underflow and filled void check(float expected, float orignal) { if (expected < orignal) cout << "tank overflow"; else if (expected > orignal) cout << "tank underflow"; else cout << "tank filled"; } int main() { int rad = 2, height = 5, rate = 10; float orignal = 70.0; float expected = volume(rad, height) / rate; check(expected, orignal); return 0; }
输出
tank overflow
广告