【问题标题】:A shorter way to write following c++ code (if-else)编写以下 c++ 代码的更短方法(if-else)
【发布时间】:2019-05-05 11:48:51
【问题描述】:

我需要知道是否有更好/更短的方法来用 C++ 编写以下代码。 (在通过主 if-else 时(逐步),将添加对变量 'y' 的额外检查)

int x = 5, y = 4;        

if(x == 1){
  if(y == 1)
      printf("ok");
  else
      printf("not ok");
}
else if(x == 2){
  if((y == 1) || (y == 2))
      printf("ok");
  else
      printf("not ok");
}
else{
  if((y == 1) || (y == 2) || (y == 3))
      printf("ok");
  else
      printf("not ok");
}

【问题讨论】:

  • 也许吧。但最重要的是让你的代码清晰易懂。
  • @Max 简化有时会导致代码更易于理解并减少冗余
  • @Christophe 在当前案例中的简化使得代码更易于理解,只需一个 printf ^^
  • 请以'x'和'y'的值为例,它们不是固定值。

标签: c++ if-statement optimization logic


【解决方案1】:
if(0<y && y<4 && (y<=x || x<1))
    printf("ok");
else
    printf("not ok");

@Christophe 你是对的,解释可能会有所帮助:

int x = 5, y = 4;        

if(x == 1){
  if(y == 1)
      printf("ok");
  else
      printf("not ok");
}
else if(x == 2){
  if((y == 1) || (y == 2))
      printf("ok");
  else
      printf("not ok");
}
else{
  if((y == 1) || (y == 2) || (y == 3))
      printf("ok");
  else
      printf("not ok");
}

当'printf("ok");'被执行,'(y == 1) || (y == 2) || (y == 3)' 必须是 'true'。

'x' 和 'y' 是整数意味着:

'(y == 1) || (y == 2) || (y == 3)' 等价于 '0

当你写作时:

if(0<y && y<4)
    printf("ok");
else
    printf("not ok");

如果你进入这个分支,就会有太多的'ok':

if(x == 1){
  ...
}

或者进入这个:

else if(x == 2){
  ...
}

当您进入其中一个分支时,两个分支中的“1

这意味着:如果 'x

当你写作时:

if(0<y && y<4 && x<1)
    printf("ok");
else
    printf("not ok");

当 'x' 是其中之一 {1,2,3,4,5,...} 并且 'y' 是正确的时,'not ok' 会太多。

  1. 'x' 为 1: '0
  2. 'x' 为 2: '0
  3. 'x' 为 3 或更大: '0

对于 1. 和 2. '0

  1. 'y' 为 1,当 'x' 为 1 且
  2. 'y' 为 1 或 'y' 为 2,当 'x' 为 2 且
  3. 当“x”为 3 或更大时,“y”为 1 或“y”为 2 或“y”为 3。

对于 1。“x”是 1: 'y

对于 2。“x”是 2: 'y

对于 3。“x”为 3 或更大: 'y

你会得到答案。

【讨论】:

  • 是目前最好的答案......假设一个类似的问题,其中 xy 的值是未知的;-)(@ 987654328@ 甚至printf("not ok" + (!(0&lt;y &amp;&amp; y&lt;4 &amp;&amp; (y&lt;=x || x&lt;1))) * 4); ? ^^)
  • 非常令人印象深刻:ideone.com/t6sJXD !恭喜!但是,您确实意识到这绝对比其他替代方案更难理解,而且更难维护,不是吗?
  • @bruno '... 假设 x 和 y 的值未知的类似问题 ;-)' 好笑话 :D 和 'printf("not ok" + (!(0
【解决方案2】:

你可以先测试y

if (y == 1 
|| (y == 2 && x != 1)
|| (y == 3 && x != 1 && x != 2))
    printf(“ok”);
else
    printf(“not ok”);

(可能会有多余的括号,但这是我个人认为可能多余的括号是一件好事。)

【讨论】:

    【解决方案3】:

    我回答问题stricto sensu,如果 xy 的值未知,这是其他问题

    感谢int x = 5, y = 4;这一行,xy的值是已知的,代码可以简化为

    printf("not ok");
    

    gcc 知道,有(我将“ok”替换为“is ok”以不允许使用“not ok”+4 来获得“ok”):

    #include <stdio.h>
    
    int main()
    {
      int x = 5, y = 4;
    
      if(x == 1){
        if(y == 1)
          printf("is ok");
        else
          printf("not ok");
      }
      else if(x == 2){
        if((y == 1) || (y == 2))
          printf("is ok");
        else
          printf("not ok");
      }
      else{
        if((y == 1) || (y == 2) || (y == 3))
          printf("is ok");
        else
          printf("not ok");
      }
      return 0;
    }
    

    编译生成汇编器:

    pi@raspberrypi:/tmp $ gcc -S -O2 c.c
    pi@raspberrypi:/tmp $ more c.s
        .arch armv6
        .eabi_attribute 28, 1
        .eabi_attribute 20, 1
        .eabi_attribute 21, 1
        .eabi_attribute 23, 3
        .eabi_attribute 24, 1
        .eabi_attribute 25, 1
        .eabi_attribute 26, 2
        .eabi_attribute 30, 2
        .eabi_attribute 34, 1
        .eabi_attribute 18, 4
        .file   "c.c"
        .section    .text.startup,"ax",%progbits
        .align  2
        .global main
        .syntax unified
        .arm
        .fpu vfp
        .type   main, %function
    main:
        @ args = 0, pretend = 0, frame = 0
        @ frame_needed = 0, uses_anonymous_args = 0
        push    {r4, lr}
        ldr r0, .L3
        bl  printf
        mov r0, #0
        pop {r4, pc}
    .L4:
        .align  2
    .L3:
        .word   .LC0
        .size   main, .-main
        .section    .rodata.str1.4,"aMS",%progbits,1
        .align  2
    .LC0:
        .ascii  "not ok\000"
        .ident  "GCC: (Raspbian 6.3.0-18+rpi1+deb9u1) 6.3.0 20170516"
        .section    .note.GNU-stack,"",%progbits
    pi@raspberrypi:/tmp $ grep ok c.s
        .ascii  "not ok\000"
    

    main 中没有测试或等效项,并且字符串“is ok”不在生成的代码中

    【讨论】:

      【解决方案4】:

      不光彩但仍然..

      if(x == 1 && y == 1) {
           printf("ok");
      } else if(x == 2 && y == 1) {
           printf("ok");
      } else if(x == 2 && y == 2) {
           printf("ok");
      } else if(y == 1 || y == 2 || y == 3) {
           printf("ok");
      } else {
           printf("not okay");
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-11-26
        • 2011-11-19
        • 2015-06-23
        • 1970-01-01
        • 2018-07-10
        • 2011-12-15
        • 2014-12-10
        • 2013-05-18
        相关资源
        最近更新 更多