博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
用enum类型数据解决switch case选择字符串的问题
阅读量:7077 次
发布时间:2019-06-28

本文共 2397 字,大约阅读时间需要 7 分钟。

hot3.png

 前几天写了一篇关于利用switch()中的参数转换成含有字符串的表达式来处理字符串的选择的问题,相信许多的人都和我有同一种感觉,就是三目运算符 ?:  用多了总是容易落下一两个括号之类的,有时拗口的选择关系把自己都弄昏了。在看《C primer  plus》第十四章的时侯,讲到了enum类型的数据,及其用法。其中涉及到一个swith()case的选择语句,感觉非常好,后来查查谭浩强的《C程序设计》也有个类似的小例子。
       还是以上次的那个内容作例子。程序如下:
 
/************************************************************************/
/*Name   : windows_main             */
/*Author : Aben               */
/*Date  : 21/03/2008                            */
/************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
 
enum choice {fingerPrintIdentify, osPatchDetect, softAssertAna, netAppSoftDetect, systemConfigAnaly, hardwareInterfaceAnaly};
const char *choices[] = {
 "fingerPrintIdentify", "osPatchDetect", "softAssertAna", 
 "netAppSoftDetect", "systemConfigAnaly", "hardwareInterfaceAnaly"};
void del(char str[]);           //由于使用fgets()函数,会获得最后的回车符,用此函数除去之
 
int main(int argc, char *argv[])
{
 FILE *fp = NULL;
 enum choice i;
 char buf[30];
 if ((fp = fopen("config\\server_test_config.txt","r")) == NULL)
 {
  printf("Can not open the config file, please make sure it's exist!\n");
  fclose(fp);
  exit(0);
 }
 memset(buf, '\0', sizeof(buf));
 while (fgets(buf, 30, fp))
 {
  del(buf);
  printf("%s",buf);
  for(i=fingerPrintIdentify; i<=hardwareInterfaceAnaly; i++)
  {
   if (strcmp(buf, choices[i]) == 0)
   {
    break;
   }
  }
  switch(i)
  {
  case fingerPrintIdentify:
   printf("fingerPrintIdentify is exist!\n");
   break;
  case osPatchDetect:
   printf("osPatchDetect is exist!\n");
   break;
  case softAssertAna:
   printf("softAssertAna is exist!\n");
   break;
  case netAppSoftDetect:
   printf("netAppSoftDetect is exist!\n");
   break;
  case systemConfigAnaly:
   printf("systemConfigAnaly is exist!\n");
   break;
  case hardwareInterfaceAnaly:
   printf("hardwareInterfaceAnaly is exist!\n");
   break;
  default:
   printf("ERROR!\n");
   break;
  }
  memset(buf, '\0', sizeof(buf));
 }
    system("PAUSE");
 return 0;
}
void del(char str[])
{
 char *found;
 found = strchr(str, '\n');
 if (found)
 {
  *found = '\0';
 }
 
}
 
这里有一点要说明的是,如果你的编译器不支持C99标准的话,就不要试了,我的Visual C++ 6.0编译器也不支持,老是给我报enum类型的数据不能够进行i++的运算,我把它修改为i=i+1后然后就报不能将enum类型的数据转换成int类型进行此种运算。最后,还是将这些东西考在dev-C++(支持C99的编译器)上运行,结果如下:
 
netAppSoftDetectnetAppSoftDetect is exist!
systemConfigAnalysystemConfigAnaly is exist!
softAssertAnalyERROR!
hardwareInterfaceAnalyhardwareInterfaceAnaly is exist!
osPatchDetectosPatchDetect is exist!
fingerPrintIdentifyfingerPrintIdentify is exist!
请按任意键继续. . .
bingo!!

转载于:https://my.oschina.net/ypimgt/blog/89647

你可能感兴趣的文章
asp.net core 中灵活的配置方式
查看>>
3.垃圾回收器
查看>>
第39级台阶 每步1个或2个台阶 有多少种上法
查看>>
FastDfs 分布式文件系统 安装与配置 (实测成功)
查看>>
手把手教你如何安装Pycharm——靠谱的Pycharm安装详细教程
查看>>
@for /l %i in (1,1,10) do md %i 批处理自动建立目录
查看>>
Linux快速构建LAMP网站平台
查看>>
邪恶的三位一体:机器学习、黑暗网络和网络犯罪
查看>>
浏览器被2345主页劫持
查看>>
Sql Server内置函数实现MD5加密
查看>>
Startssl SSL 证书申请图解
查看>>
配置Apache虚拟主机,实现在一台服务器上运行多个网站
查看>>
第八课-第一讲 08_01_facl及用户及Linux终端
查看>>
Confluence 6 示例 - https://confluence.atlassian.com/
查看>>
oracle中无法用退格和上下翻命令解决
查看>>
互融云保理业务系统助力企业快速拓展业务
查看>>
浅谈MySQL中SQL优化的常用方法
查看>>
谷歌智能音箱更新,玩转摄像头,但名归Nest麾下
查看>>
经常被问到的十个 Java 面试题?你Get了吗?
查看>>
iOS开发之网络数据解析(二)--XML解析简介
查看>>