首页 > 解决方案 > 如何对各种输入使用各种计算

问题描述

我正在编写一个程序,让用户输入各种类型的信息来计算度假旅行的费用。

我得到了三种不同交通方式的成本,以及四个来源城市和四个目的地城市。

三种出行方式分别是飞机、火车和巴士,而客源城市是巴尔的摩、查塔努加、纳什维尔和帕萨迪纳。目的地城市是丹佛、麦迪逊、克拉克斯维尔和诺克斯维尔。

我需要能够根据用户输入的内容(旅行形式和每个城市)计算每个来源和每个目的地城市之间的旅行成本。

现在,我的第一个猜测是为每个条件编写一个非常长的 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 美元,尽管用户输入了什么。

有什么办法可以缩短这个吗?或者,如果我在某个地方搞砸了,有人可以为我指出吗?

任何帮助表示赞赏!

标签: cif-statement

解决方案


至少这个问题:Follow 代码不transportation_type'A'transportation_typeto比较'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,,这里不需要。


推荐阅读