我的任务是计算直升机的必要燃油摄入量。直升机可以执行四种操作:
- 控股
- 提高
- 登陆
- 笔直
用户通过输入参数来选择直升机的 Action 。用户可以根据需要选择任意数量的操作。最后,根据用户的参数和操作选择计算总燃料。我需要用 OOP 方法创建这个系统。
这是我到目前为止所做的:
#include <iostream>
#include <stdio.h>
using namespace std;
class FlyingMode {
protected:
float time, fuel_rate, start, end, pace, distance;
float total;
public:
FlyingMode(float a=0, float b=0, float c=0,
float d=0, float e=0, float f=0){
time = a;
fuel_rate = b;
start = c;
end = d;
pace = e;
distance = f;
total = 0;
}
virtual int calcFuel(){
return 0;
}
};
class Holding: public FlyingMode{
public:
Holding(float a=0, float b=0, float c=0,
float d=0, float e=0, float f=0):FlyingMode(a, b, c, d, e, f) { }
int calcFuel(){
total = (time * fuel_rate * 60);
return total;
}
};
class Raising: public FlyingMode{
public:
Raising(float a=0, float b=0, float c=0,
float d=0, float e=0, float f=0):FlyingMode(a, b, c, d, e, f) { }
int calcFuel (){
if(start < end && pace != 0 ){
total = (end - start)/pace * fuel_rate;
return (end - start)/pace * fuel_rate;
}else{
return 0;
}
}
};
class Landing: public FlyingMode{
public:
Landing(float a=0, float b=0, float c=0,
float d=0, float e=0, float f=0):FlyingMode(a, b, c, d, e, f) { }
int calcFuel (){
if(start > end && pace != 0 ){
total = (start - end)/pace * fuel_rate;
return (start - end)/pace * fuel_rate;
}else{
return 0;
}
}
};
class Straight: public FlyingMode{
public:
Straight(float a=0, float b=0, float c=0,
float d=0, float e=0, float f=0):FlyingMode(a, b, c, d, e, f) { }
int calcFuel (){
if(distance != 0 || pace != 0 ){
total = distance/pace * fuel_rate;
return distance/pace * fuel_rate;
}else{
return 0;
}
}
};
// Main function for the program
int main( ){
float a=0, b=0;
float c=0, d=0, e=0, f=0;
float total = 0;
char op;
Holding hold;
Raising raise;
Landing land;
Straight straight;
while(op != 'x') {
cout << "Enter the move : " << endl;
cout << "1 ---> Holding Flight" << endl;
cout << "2 ---> Raising" << endl;
cout << "3 ---> Landing " << endl;
cout << "4 ---> Straight Flight " << endl;
cout << "5 ---> Calculate fuel" << endl;
cout << "x ---> Exit " << endl;
op = std::getchar();
if(op == '1') {
cout << "Enter time : ";
cin >> a;
cout << "Enter fuel rate: ";
cin >> b;
}
if(op == '2') {
cout << "Enter starting altitude: ";
cin >> c;
cout << "Enter ending altitude: ";
cin >> d;
cout << "Enter raising pace: ";
cin >> e;
cout << "Enter fuel rate: ";
cin >> f;
}
if(op == '3') {
cout << "Enter starting altitude: ";
cin >> c;
cout << "Enter ending altitude: ";
cin >> d;
cout << "Enter landing pace: ";
cin >> e;
cout << "Enter fuel rate: ";
cin >> b;
}
if(op == '4') {
cout << "Enter ending altitude: ";
cin >> d;
cout << "Enter starting altitude: ";
cin >> c;
cout << "Enter fuel rate: ";
cin >> b;
}
if(op == '5') {
FlyingMode *mode;
hold = Holding(a, b, 0, 0, 0, 0);
raise = Raising(0, f, c, d, e, 0);
land = Landing(0, b, d, c, e, 0);
straight = Straight(0, b, c, d, 0, 0);
//store holding address
//call holding fuel
mode = &hold;
float hold_result = mode -> calcFuel();
//call raising fuel
mode = &raise;
float raise_result = mode -> calcFuel();
//call landing fuel
mode = &land;
float land_result = mode -> calcFuel();
//call straight fuel
mode = &straight;
float str_result = mode -> calcFuel();
//calculate total
/*cout << "hold_result" << hold_result;
cout << "raise_result" << raise_result;
cout << "land_result" << land_result;
cout << "str_result" << str_result;*/
total = hold_result + raise_result + land_result + str_result;
cout <<"Total required fuel : "<< total << " kg/second "<< endl;
}
if(op == 'x') {
cout << "System will exit..." << endl;
return 0;
}
else {
//if(op==(1|2|3|4|5)){}
//else cout << "Wrong selection." << endl;
}
}
return 0;
}
但是,它只计算用户第一次操作选择的总值。关于如何解决这个问题的任何想法?
最佳答案
首先,尽可能使用 switch 而不是 if,这会使您的代码更快,不要重复自己。我看到重复的说明...
此处的快速修复,我建议您将所有变量移动到 while 循环中,这样它们就不会被覆盖;或者,如果这是您的意图,请添加一些额外的变量,以便将它们唯一地分配给您的每个 cin 语句。但是,您仍然应该改进代码设计。
关于c++ - 使用 C++ OOP 设计更新总值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32760982/