首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何对各种输入使用不同的计算

如何对各种输入使用不同的计算
EN

Stack Overflow用户
提问于 2020-10-04 02:02:29
回答 1查看 22关注 0票数 0

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

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

这三种旅行方式是航空、火车和公共汽车,而来源城市是巴尔的摩、查塔努加、纳什维尔和帕萨迪纳。目的地城市是丹佛、麦迪逊、克拉克斯维尔和诺克斯维尔。

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

现在,我的第一个猜测是为每个条件编写一个非常长的if/else if语句。我试过了,但它似乎不起作用,因为它太长了,或者我在哪里搞砸了。

代码语言:javascript
复制
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。

有没有办法缩短这段时间?或者如果我在什么地方搞砸了,有人能帮我指出吗?

如有任何帮助,我们不胜感激!

EN

回答 1

Stack Overflow用户

发布于 2020-10-04 02:45:38

如前所述,仅使用大写选择或将选择转换为相同的大小写。此外,您可以将行程组合存储在数组中,以使代码更加整洁。

代码语言:javascript
复制
#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;
}

输出

代码语言:javascript
复制
5000
5000
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64187362

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档