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
廣告