c - 如何对各种输入使用各种计算
问题描述
我正在编写一个程序,让用户输入各种类型的信息来计算度假旅行的费用。
我得到了三种不同交通方式的成本,以及四个来源城市和四个目的地城市。
三种出行方式分别是飞机、火车和巴士,而客源城市是巴尔的摩、查塔努加、纳什维尔和帕萨迪纳。目的地城市是丹佛、麦迪逊、克拉克斯维尔和诺克斯维尔。
我需要能够根据用户输入的内容(旅行形式和每个城市)计算每个来源和每个目的地城市之间的旅行成本。
现在,我的第一个猜测是为每个条件编写一个非常长的 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 美元,尽管用户输入了什么。
有什么办法可以缩短这个吗?或者,如果我在某个地方搞砸了,有人可以为我指出吗?
任何帮助表示赞赏!
解决方案
至少这个问题:Follow 代码不transportation_type
与'A'
和
transportation_type
to比较'a'
。1
transportation_type == 'A', 'a' // bad
而是折叠外壳。
// transportation_type == 'A', 'a'
toupper((unsigned char)transportation_type) == 'A'
对其他人也一样。
建议在if()
.
#include <ctype.h>
transportation_type = toupper((unsigned char) transportation_type);
src_city = toupper((unsigned char) src_city);
dest_city = toupper((unsigned char) dest_city);
// if(transportation_type == 'A', 'a' && src_city == 'B', 'b' && dest_city == 'D', 'd')
if(transportation_type == 'A' && src_city == 'B' && dest_city == 'D')
1 transportation_type == 'A', 'a'
使用逗号运算符。结果是'a'
。比较结果丢失。
逗号运算符&&
使事情进一步复杂化。IAC,,
这里不需要。
推荐阅读
- azure-devops - 如何将 powerapps 与 azure devops 集成
- javascript - 如何在完成后重置计时器组件
- javascript - useMemo 与 useEffect + useState
- lisp - Lisp中的线性搜索,数组错误
- python - 使用视图从模型类中获取所有数据到模板上
- css - 如何使用 Flex CSS 将文本垂直居中用于条形图
- node.js - 使用 node 和 mssql 从数据库中检索值
- python - 部署到应用引擎标准环境时如何修复“ImportError:无法导入名称库”?
- excel - 我想根据放置在前一个单元格中的值隐藏或显示行
- python - py_script 从 mysql bd 写入 xlsx 文件