我正在写一个程序,让用户输入各种类型的信息来计算度假旅行的费用。
我得到了三种不同交通方式的成本,以及四个源城市和四个目的地城市的成本。
这三种旅行方式是航空、火车和公共汽车,而来源城市是巴尔的摩、查塔努加、纳什维尔和帕萨迪纳。目的地城市是丹佛、麦迪逊、克拉克斯维尔和诺克斯维尔。
我需要能够根据用户输入的内容(旅行形式和每个城市)来计算每个来源和每个目的地城市之间的旅行成本。
现在,我的第一个猜测是为每个条件编写一个非常长的if/else if语句。我试过了,但它似乎不起作用,因为它太长了,或者我在哪里搞砸了。
if(transportation_type == 'A', 'a' && src_city == 'B', 'b' && dest_city == 'D', 'd')
transportation_price = 5000;
else if(transportation_type == 'A', 'a' && src_city == 'B', 'b' && dest_city == 'M', 'm')
transportation_price = 4000;
else if(transportation_type == 'A', 'a' && src_city == 'B', 'b' && dest_city == 'K', 'k')
transportation_price = 5000;
else if(transportation_type == 'A', 'a' && src_city == 'B', 'b' && dest_city == 'C', 'c')
transportation_price = 2500;
else if(transportation_type == 'T', 't' && src_city == 'B', 'b' && dest_city == 'D', 'd')
transportation_price = 2500;
else if(transportation_type == 'T', 't' && src_city == 'B', 'b' && dest_city == 'M', 'm')
transportation_price = 2000;
else if(transportation_type == 'T', 't' && src_city == 'B', 'b' && dest_city == 'K', 'k')
transportation_price = 2500;
else if(transportation_type == 'T', 't' && src_city == 'B', 'b' && dest_city == 'C', 'c')
transportation_price = 800;
else if(transportation_type == 'B', 'b' && src_city == 'B', 'b' && dest_city == 'D', 'd')
transportation_price = 2000;
else if(transportation_type == 'B', 'b' && src_city == 'B', 'b' && dest_city == 'M', 'm')
transportation_price = 1000;
else if(transportation_type == 'B', 'b' && src_city == 'B', 'b' && dest_city == 'K', 'k')
transportation_price = 2000;
else if(transportation_type == 'B', 'b' && src_city == 'B', 'b' && dest_city == 'C', 'c')
transportation_price = 2000;
else if(transportation_type == 'A', 'a' && src_city == 'C', 'c' && dest_city == 'D', 'd')
transportation_price = 2500;
else if(transportation_type == 'A', 'a' && src_city == 'C', 'c' && dest_city == 'M', 'm')
transportation_price = 4000;
else if(transportation_type == 'A', 'a' && src_city == 'C', 'c' && dest_city == 'K', 'k')
transportation_price = 4000;
else if(transportation_type == 'A', 'a' && src_city == 'C', 'c' && dest_city == 'C', 'c')
transportation_price = 6000;
else if(transportation_type == 'T', 't' && src_city == 'C', 'c' && dest_city == 'D', 'd')
transportation_price = 500;
else if(transportation_type == 'T', 't' && src_city == 'C', 'c' && dest_city == 'M', 'm')
transportation_price = 2300;
else if(transportation_type == 'T', 't' && src_city == 'C', 'c' && dest_city == 'K', 'k')
transportation_price = 1600;
else if(transportation_type == 'T', 't' && src_city == 'C', 'c' && dest_city == 'C', 'c')
transportation_price = 2000;
else if(transportation_type == 'B', 'b' && src_city == 'C', 'c' && dest_city == 'D', 'd')
transportation_price = 600;
else if(transportation_type == 'B', 'b' && src_city == 'C', 'c' && dest_city == 'M', 'm')
transportation_price = 1300;
else if(transportation_type == 'B', 'b' && src_city == 'C', 'c' && dest_city == 'K', 'k')
transportation_price = 1400;
else if(transportation_type == 'B', 'b' && src_city == 'C', 'c' && dest_city == 'C', 'c')
transportation_price = 1700;
else if(transportation_type == 'A', 'a' && src_city == 'N', 'n' && dest_city == 'D', 'd')
transportation_price = 5000;
else if(transportation_type == 'A', 'a' && src_city == 'N', 'n' && dest_city == 'M', 'm')
transportation_price = 2500;
else if(transportation_type == 'A', 'a' && src_city == 'N', 'n' && dest_city == 'K', 'k')
transportation_price = 4000;
else if(transportation_type == 'A', 'a' && src_city == 'N', 'n' && dest_city == 'C', 'c')
transportation_price = 4500;
else if(transportation_type == 'T', 't' && src_city == 'N', 'n' && dest_city == 'D', 'd')
transportation_price = 1500;
else if(transportation_type == 'T', 't' && src_city == 'N', 'n' && dest_city == 'M', 'm')
transportation_price = 900;
else if(transportation_type == 'T', 't' && src_city == 'N', 'n' && dest_city == 'K', 'k')
transportation_price = 1500;
else if(transportation_type == 'T', 't' && src_city == 'N', 'n' && dest_city == 'C', 'c')
transportation_price = 1700;
else if(transportation_type == 'B', 'b' && src_city == 'N', 'n' && dest_city == 'D', 'd')
transportation_price = 1400;
else if(transportation_type == 'B', 'b' && src_city == 'N', 'n' && dest_city == 'M', 'm')
transportation_price = 700;
else if(transportation_type == 'B', 'b' && src_city == 'N', 'n' && dest_city == 'K', 'k')
transportation_price = 1000;
else if(transportation_type == 'B', 'b' && src_city == 'N', 'n' && dest_city == 'C', 'c')
transportation_price = 1300;
else if(transportation_type == 'A', 'a' && src_city == 'P', 'p' && dest_city == 'D', 'd')
transportation_price = 5000;
else if(transportation_type == 'A', 'a' && src_city == 'P', 'p' && dest_city == 'M', 'm')
transportation_price = 4500;
else if(transportation_type == 'A', 'a' && src_city == 'P', 'p' && dest_city == 'K', 'k')
transportation_price = 3000;
else if(transportation_type == 'A', 'a' && src_city == 'P', 'p' && dest_city == 'C', 'c')
transportation_price = 4500;
else if(transportation_type == 'T', 't' && src_city == 'P', 'p' && dest_city == 'D', 'd')
transportation_price = 2000;
else if(transportation_type == 'T', 't' && src_city == 'P', 'p' && dest_city == 'M', 'm')
transportation_price = 1900;
else if(transportation_type == 'T', 't' && src_city == 'P', 'p' && dest_city == 'K', 'k')
transportation_price = 1200;
else if(transportation_type == 'T', 't' && src_city == 'P', 'p' && dest_city == 'C', 'c')
transportation_price = 1700;
else if(transportation_type == 'B', 'b' && src_city == 'P', 'p' && dest_city == 'D', 'd')
transportation_price = 1400;
else if(transportation_type == 'B', 'b' && src_city == 'P', 'p' && dest_city == 'M', 'm')
transportation_price = 1300;
else if(transportation_type == 'B', 'b' && src_city == 'P', 'p' && dest_city == 'K', 'k')
transportation_price = 800;
else if(transportation_type == 'B', 'b' && src_city == 'P', 'p' && dest_city == 'C', 'c');
transportation_price = 1300;我用字符来表示每个城市和交通方式。这个if/else if语句不起作用,因为在我的输出中,不管用户输入什么,它给我的值都是$1300。
有没有办法缩短这段时间?或者如果我在什么地方搞砸了,有人能帮我指出吗?
如有任何帮助,我们不胜感激!
发布于 2020-10-04 02:45:38
如前所述,仅使用大写选择或将选择转换为相同的大小写。此外,您可以将行程组合存储在数组中,以使代码更加整洁。
#include <stdio.h>
#include <math.h>
int main(){
char transportation_type = 'A';
char src_city = 'B';
char dest_city = 'K';
int transportation_price = 0;
if(transportation_type == 'A' && src_city == 'B' && dest_city == 'D')
transportation_price = 5000;
else if(transportation_type == 'A' && src_city == 'B' && dest_city == 'M')
transportation_price = 4000;
else if(transportation_type == 'A' && src_city == 'B' && dest_city == 'K')
transportation_price = 5000;
printf("%i\n",transportation_price);
// better way, store trip combinations in array
char trips[4][3] = {
{'A','B','D'},
{'A','B','M'},
{'A','B','K'},
{'A','B','C'}
};
int cost[4] = {
5000,4000,5000,2500
};
for (int t=0; t<3; t++) {
if(transportation_type == trips[t][0] && src_city == trips[t][1] && dest_city == trips[t][2]) {
transportation_price = cost[t];
break;
}
}
printf("%i\n",transportation_price);
return transportation_price;
}输出
5000
5000https://stackoverflow.com/questions/64187362
复制相似问题