zoukankan      html  css  js  c++  java
  • Python switch-case语句的实现 -- 字典模拟实现

    static void
    print_asru_status(int status, char *label)
    {
        char *msg = NULL;
    
        switch (status) {
        case 0:
            msg = dgettext("FMD", "ok and in service");
            break;
        case FM_SUSPECT_DEGRADED:
            msg = dgettext("FMD", "service degraded, "
                "but associated components no longer faulty");
            break;
        case FM_SUSPECT_FAULTY | FM_SUSPECT_DEGRADED:
            msg = dgettext("FMD", "faulted but still "
                "providing degraded service");
            break;
        case FM_SUSPECT_FAULTY:
            msg = dgettext("FMD", "faulted but still in service");
            break;
        case FM_SUSPECT_UNUSABLE:
            msg = dgettext("FMD", "out of service, "
                "but associated components no longer faulty");
            break;
        case FM_SUSPECT_FAULTY | FM_SUSPECT_UNUSABLE:
            msg = dgettext("FMD", "faulted and taken out of service");
            break;
        default:
            break;
        }
        if (msg) {
            (void) printf("%s     %s
    ", label, msg);
        }
    }

    以上代码是我做项目中遇到的一段代码,要求用Python语言实现,实现代码如下所示:

    def print_asru_status(status, label):
        dic = {0:"ok and in service",
            FM_SUSPECT_DEGRADED:"service degraded, but associated components no longer faulty",
            FM_SUSPECT_FAULTY | FM_SUSPECT_DEGRADED:"service degraded, but associated components no longer faulty",
            FM_SUSPECT_FAULTY:"faulted but still in service",
            FM_SUSPECT_UNUSABLE:"out of service, but associated components no longer faulty",
            FM_SUSPECT_FAULTY | FM_SUSPECT_UNUSABLE:"faulted and taken out of service"}
        if status not in dic:
            msg = ""
        else:
            msg = dic[status]
        if msg :
            print(label,"     ", msg)

    学习文章:http://www.cnblogs.com/russellluo/p/3299725.html

  • 相关阅读:
    P2207 Photo
    P1022 计算器的改良
    P1003 铺地毯
    P3014 [USACO11FEB]牛线Cow Line && 康托展开
    P4180 【模板】严格次小生成树[BJWC2010]
    P2776 [SDOI2007]小组队列
    P2426 删数
    P1948 [USACO08JAN]电话线Telephone Lines
    P1978 集合
    P1564 膜拜
  • 原文地址:https://www.cnblogs.com/fendou-999/p/3534839.html
Copyright © 2011-2022 走看看