当前位置:   article > 正文

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

wchar c语言

概述

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

IDE:vscode

1、源码

  1. #define ARRAY_SIZE(array) (sizeof(array) / sizeof(array[0]))
  2. typedef enum
  3. {
  4. language_SChinese = 0,
  5. language_English,
  6. }LANGUAGE_T;
  7. typedef struct
  8. {
  9. volatile uint8_t value;
  10. volatile LANGUAGE_T language;
  11. }__attribute__((packed)) SysType_ty;
  12. SysType_ty Sys;
  13. typedef struct
  14. {
  15. uint8_t item_icon;
  16. uint8_t item_title;
  17. }MenuList;
  18. /**
  19. * footwear
  20. */
  21. typedef enum {
  22. S_badminton = 0,
  23. S_baseball,
  24. S_hockey,
  25. S_football,
  26. S_basketball,
  27. S_rugby,
  28. S_volleyball, //6
  29. S_tennis,
  30. S_table_tennis,
  31. S_billiards, //9
  32. S_ping_pong,
  33. S_golf,
  34. S_count,
  35. }footwearTy;
  36. typedef enum {
  37. t_ERROR = 0,
  38. t_BADMINTON,
  39. t_BASEBALL,
  40. t_HOCKEY,
  41. t_FOOTBALL,
  42. t_BASKETBALL,
  43. t_RUGBY,
  44. t_VOLLEYBALL, //6
  45. t_TENNIS,
  46. t_TABLE_TENNIS,
  47. t_BILLIARDS, //9
  48. t_PING_PONG,
  49. t_GOLF,
  50. t_count,
  51. }String_id;
  52. static MenuList const item_lists[] = {
  53. {S_volleyball, t_VOLLEYBALL},
  54. {S_rugby, t_RUGBY},
  55. {S_tennis, t_TENNIS},
  56. {S_billiards, t_BILLIARDS},
  57. {S_hockey, t_HOCKEY},
  58. };
  59. /**
  60. * show_footwear
  61. */
  62. uint16_t const init_footwear_index_Lists[S_count] = {
  63. [S_badminton ] = 0,
  64. [S_baseball ] = 2,
  65. [S_hockey ] = 7,
  66. [S_football ] = 0,
  67. [S_basketball ] = 1,
  68. [S_rugby ] = 3,
  69. [S_volleyball ] = 0,
  70. [S_tennis ] = 5,
  71. [S_table_tennis ] = 6,
  72. [S_billiards ] = 8,
  73. [S_ping_pong ] = 4,
  74. [S_golf ] = 9,
  75. };
  76. static wchar_t * const footwear_title_lists_en[t_count] = {
  77. [t_ERROR ] = L"error",
  78. [t_BADMINTON ] = L"badminton",
  79. [t_BASEBALL ] = L"baseball",
  80. [t_HOCKEY ] = L"hockey",
  81. [t_FOOTBALL ] = L"football",
  82. [t_BASKETBALL ] = L"basketball",
  83. [t_RUGBY ] = L"rugby",
  84. [t_VOLLEYBALL ] = L"volleyball",
  85. [t_TENNIS ] = L"tennis",
  86. [t_TABLE_TENNIS ] = L"table_tennis",
  87. [t_BILLIARDS ] = L"billiards",
  88. [t_PING_PONG ] = L"ping_pong",
  89. [t_GOLF ] = L"golf",
  90. };
  91. static wchar_t * const footwear_title_lists_cn[t_count] = {
  92. [t_ERROR ] = L"错误",
  93. [t_BADMINTON ] = L"羽毛球",
  94. [t_BASEBALL ] = L"棒球",
  95. [t_HOCKEY ] = L"曲棍球",
  96. [t_FOOTBALL ] = L"足球",
  97. [t_BASKETBALL ] = L"篮球",
  98. [t_RUGBY ] = L"橄榄球",
  99. [t_VOLLEYBALL ] = L"排球",
  100. [t_TENNIS ] = L"网球",
  101. [t_TABLE_TENNIS ] = L"桌球",
  102. [t_BILLIARDS ] = L"台球",
  103. [t_PING_PONG ] = L"乒乓球",
  104. [t_GOLF ] = L"高尔夫球",
  105. };
  106. static wchar_t * const * string_list = footwear_title_lists_en;
  107. wchar_t * string_get(String_id id)
  108. {
  109. return (wchar_t *) (string_list[id]);
  110. }
  111. LANGUAGE_T language_get(void)
  112. {
  113. return Sys.language;
  114. }
  115. void init_language(void)
  116. {
  117. Sys.language = language_English;
  118. switch (Sys.language)
  119. {
  120. case language_SChinese:
  121. /* code */
  122. string_list = footwear_title_lists_cn;
  123. break;
  124. case language_English:
  125. /* code */
  126. string_list = footwear_title_lists_en;
  127. break;
  128. default:
  129. Sys.language = language_English;
  130. init_language();
  131. break;
  132. }
  133. }
  134. int main()
  135. {
  136. init_language();
  137. printf("str:%s\r\n", string_get((String_id)item_lists[1].item_title));
  138. printf("str:%s\r\n", footwear_title_lists_cn[item_lists[1].item_title]);
  139. for (int i = 0; i < ARRAY_SIZE(item_lists); i++)
  140. {
  141. printf("i:%d, item_icon: %d, item_title: %d\r\n", i, item_lists[i].item_icon, item_lists[i].item_title);
  142. 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));
  143. }
  144. system("pause");
  145. return 0;
  146. }

2、运行结果

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/869650
推荐阅读
相关标签
  

闽ICP备14008679号