C语言提高篇(wchar_t)字符类型

概述

        原来C语言也阔以这么秀^_^~,来自于灵感的编程思想。在很多大型项目上见过类似的写法,所以今天写个Demo,记录一下,方便以后赏阅。

IDE:vscode

1、源码

#define ARRAY_SIZE(array) (sizeof(array) / sizeof(array[0]))

typedef enum
{
    language_SChinese = 0,
    language_English,
}LANGUAGE_T;

typedef struct
{
    volatile uint8_t     value;
    volatile LANGUAGE_T  language;
}__attribute__((packed)) SysType_ty;

SysType_ty Sys;

typedef struct 
{
    uint8_t item_icon;
    uint8_t item_title;
}MenuList;

/**
 * footwear
 */
typedef enum {
    S_badminton = 0,
    S_baseball,
    S_hockey,
    S_football,
    S_basketball,
    S_rugby,
    S_volleyball,   //6
    S_tennis,
    S_table_tennis,
    S_billiards,    //9
    S_ping_pong,
    S_golf,         
    S_count,
}footwearTy;

typedef enum {
    t_ERROR = 0,    
    t_BADMINTON,
    t_BASEBALL,
    t_HOCKEY,
    t_FOOTBALL,
    t_BASKETBALL,
    t_RUGBY,
    t_VOLLEYBALL,   //6
    t_TENNIS,
    t_TABLE_TENNIS,
    t_BILLIARDS,    //9
    t_PING_PONG,
    t_GOLF,
    t_count,
}String_id;


static MenuList const item_lists[] = {
    {S_volleyball,  t_VOLLEYBALL},
    {S_rugby,       t_RUGBY},
    {S_tennis,      t_TENNIS},
    {S_billiards,   t_BILLIARDS},
    {S_hockey,      t_HOCKEY},
};

/**
 * show_footwear
 */
uint16_t const init_footwear_index_Lists[S_count] = {
    [S_badminton    ] = 0,
    [S_baseball     ] = 2,
    [S_hockey       ] = 7,
    [S_football     ] = 0,
    [S_basketball   ] = 1,
    [S_rugby        ] = 3,
    [S_volleyball   ] = 0,
    [S_tennis       ] = 5,
    [S_table_tennis ] = 6,
    [S_billiards    ] = 8,
    [S_ping_pong    ] = 4,
    [S_golf         ] = 9,
};

static wchar_t * const footwear_title_lists_en[t_count] = {
    [t_ERROR        ]   =  L"error",
    [t_BADMINTON    ]   =  L"badminton",
    [t_BASEBALL     ]   =  L"baseball",
    [t_HOCKEY       ]   =  L"hockey",
    [t_FOOTBALL     ]   =  L"football",
    [t_BASKETBALL   ]   =  L"basketball",
    [t_RUGBY        ]   =  L"rugby",
    [t_VOLLEYBALL   ]   =  L"volleyball",
    [t_TENNIS       ]   =  L"tennis",
    [t_TABLE_TENNIS ]   =  L"table_tennis",
    [t_BILLIARDS    ]   =  L"billiards",
    [t_PING_PONG    ]   =  L"ping_pong",
    [t_GOLF         ]   =  L"golf",
};

static wchar_t * const footwear_title_lists_cn[t_count] = {
    [t_ERROR        ]   =  L"错误",
    [t_BADMINTON    ]   =  L"羽毛球",
    [t_BASEBALL     ]   =  L"棒球",
    [t_HOCKEY       ]   =  L"曲棍球",
    [t_FOOTBALL     ]   =  L"足球",
    [t_BASKETBALL   ]   =  L"篮球",
    [t_RUGBY        ]   =  L"橄榄球",
    [t_VOLLEYBALL   ]   =  L"排球",
    [t_TENNIS       ]   =  L"网球",
    [t_TABLE_TENNIS ]   =  L"桌球",
    [t_BILLIARDS    ]   =  L"台球",
    [t_PING_PONG    ]   =  L"乒乓球",
    [t_GOLF         ]   =  L"高尔夫球",
};

static wchar_t * const * string_list = footwear_title_lists_en; 
wchar_t * string_get(String_id id)
{
    return (wchar_t *) (string_list[id]);
}

LANGUAGE_T language_get(void)
{
	return Sys.language;
}

void init_language(void)
{
     Sys.language = language_English;

    switch (Sys.language)
    {
    case language_SChinese:
        /* code */
        string_list = footwear_title_lists_cn;
        break;

    case language_English:
        /* code */
        string_list = footwear_title_lists_en;
        break;    
    
    default:
        Sys.language = language_English;
        init_language();
        break;
    }

}


int main()
{  
    init_language();

    printf("str:%s\r\n", string_get((String_id)item_lists[1].item_title));
    printf("str:%s\r\n", footwear_title_lists_cn[item_lists[1].item_title]);

    for (int i = 0; i < ARRAY_SIZE(item_lists); i++)
    {
        printf("i:%d, item_icon: %d, item_title: %d\r\n", i, item_lists[i].item_icon, item_lists[i].item_title);
        printf("i:%d, show_icon: %d, show_title: %s\r\n", i, init_footwear_index_Lists[item_lists[i].item_icon], string_get((String_id)item_lists[i].item_title));
    }

    system("pause");
    return 0;
}

2、运行结果

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Ch_champion

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值