【问题标题】:Can character array be using switch case in c [duplicate]字符数组可以在c中使用switch case吗?
【发布时间】:2013-09-03 11:32:50
【问题描述】:
void main()
{
  char day[20];
  printf("Enter the short name of day");

  scanf("%s", day);

  switch(day)
  {
    case "sun":
      printf("sunday");
      break;
    case "mon":
      printf("monday");
      break;
    case "Tue":
      printf("Tuesday");
      break;
    case "wed":
      printf("wednesday");
      break;
    case "Thu":
      printf("Thursday");
      break;
    case "Fri":
      printf("friday");
      break;
    case "sat":
      printf("saturday");
      break;
  }
}

这是我的代码。我在 switch case part.switch case 中遇到错误,没有检查这些情况。请帮助我。 提前致谢。

【问题讨论】:

  • 在 C 中,你不能 switch 处理除整数以外的任何内容。
  • 阅读switch
  • 如果不是只有 7 天,我会推荐散列。
  • 使用宏和模板trick

标签: c


【解决方案1】:

使用 c,我知道的唯一方法是:

if (strcmp(day, "sun") == 0) 
{
   printf("sunday");
} 
else if (strcmp(day, "mon") == 0)
{
   printf("monday");
}
/* more else if clauses */
else /* default: */
{
}

【讨论】:

    【解决方案2】:

    如前所述,switch 语句不适用于 C 中的字符串。您可以执行以下操作以使代码更简洁:

    #include <stdio.h>
    
    static struct day {
      const char *abbrev;
      const char *name;
    } days[] = {
      { "sun", "sunday"    },
      { "mon", "monday"    },
      { "tue", "tuesday"   },
      { "wed", "wednesday" },
      { "thu", "thursday"  },
      { "fri", "friday"    },
      { "sat", "saturday"  },
    };
    
    int main()
    {
      int i;
      char day[20];
      printf("Enter the short name of day");
    
      scanf("%s", day);
    
      for (i = 0; i < sizeof(days) / sizeof(days[0]); i++) {
        if (strcasecmp(day, days[i].abbrev) == 0) {
          printf("%s\n", days[i].name);
          break;
        }
      }
    
      return 0;
    }
    

    【讨论】:

      【解决方案3】:

      这应该可以。
      (但仅适用于 4 字节或更少的字符串)

      这会将字符串视为 4 字节整数。

      这被认为是丑陋的,“hacky”,而且根本不是好的风格。
      但它确实可以满足您的需求。

      #include "Winsock2.h"
      #pragma comment(lib,"ws2_32.lib")
      
      void main()
      {
        char day[20];
        printf("Enter the short name of day");
      
        scanf("%s", day);
      
        switch(htonl(*((unsigned long*)day)))
        {
          case 'sun\0':
            printf("sunday");
            break;
          case 'mon\0':
            printf("monday");
            break;
          case 'Tue\0':
            printf("Tuesday");
            break;
          case 'wed\0':
            printf("wednesday");
            break;
          case 'Thu\0':
            printf("Thursday");
            break;
          case 'Fri\0':
            printf("friday");
            break;
          case 'sat\0':
            printf("saturday");
            break;
        }
      }
      

      在 MSVC2010 中测试

      【讨论】:

        【解决方案4】:

        在 const 列表中查找字符串。如果找到,使用索引进行切换。

        enum daysOfWeek { EwdMon,EwdTues,EwdWed....}; 是一个很好的起点。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2018-06-08
          • 1970-01-01
          • 2019-03-31
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多