C dst数组中的数字计数。我们考虑了一下函数,认为零或负计数没有意义:如果不想得到任何输入,为什么有人会调用read\u numbers

C dst数组中的数字计数。我们考虑了一下函数,认为零或负计数没有意义:如果不想得到任何输入,为什么有人会调用read\u numbers,c,C,我们继续阅读,发现这似乎是一个很好的选择,可以确保函数没有因为编程错误而使用不正确的参数调用。请注意,assert()不得用于检测无效的程序输入!!。它仅用于帮助查找程序错误,即程序开发人员的错误,而不是用户的错误。如果必须检查用户输入,则必须明确使用条件语句(If)或检查输入的自定义函数(但决不能使用断言) 注意如何使用assert: 它告诉你,程序的人类读者,在程序的给定点,count必须大于零。它有助于对后续代码进行推理 编译器生成检查断言条件的代码,如果它不成立(为false),则中止。

我们继续阅读,发现这似乎是一个很好的选择,可以确保函数没有因为编程错误而使用不正确的参数调用。请注意,
assert()
不得用于检测无效的程序输入!!。它仅用于帮助查找程序错误,即程序开发人员的错误,而不是用户的错误。如果必须检查用户输入,则必须明确使用条件语句(
If
)或检查输入的自定义函数(但决不能使用
断言)

注意如何使用
assert

  • 它告诉你,程序的人类读者,在程序的给定点,
    count
    必须大于零。它有助于对后续代码进行推理

  • 编译器生成检查断言条件的代码,如果它不成立(为false),则中止。检查通常只在程序的调试版本中执行,而不在发布版本中执行

  • 为了保持程序流程清晰,我们向前声明
    read_numbers()
    ,在
    main()
    中使用它,最后定义(实现)函数,这样它就不会模糊一些东西:

    #include <assert.h>
    #include <stdio.h>
    #include <stdlib.h>
    
    void read_numbers(int *dst, int count);
    
    int main() {
      enum { N = 3 };
      int numbers[N];
    
      read_numbers(numbers, N);
    
      char all_equal = 1;
      int max = numbers[0];
      for (int i = 1; i < N; i++) {
        all_equal = all_equal && numbers[i] == max;
        if (numbers[i] > max) max = numbers[i];
      }
    
      if (all_equal) printf("All numbers are equal.\n");
      else printf("%d is the greatest among the numbers entered.\n", max);
    }
    
    void read_numbers(int *dst, int count) {
      assert(count > 0);
      printf("Enter %d numbers.\n", count);
      for (int i = 0; i < count; /*empty*/) {
        printf("#%d: ", i+1);
        int c = scanf("%d", &dst[i]);
        if (c == EOF) abort();
        if (c != 1) {
          printf("Sorry, invalid input. Try again.\n");
          while ((c = getchar()) != EOF && c != '\n');
          if (c == EOF) abort();
        } else
          i++;
      }
    }
    
    注意
    结果
    局部变量如何:

    如果需要独立于变量声明执行初始化,则希望使用-现代C语言中一个鲜为人知但非常重要的符号快捷方式:

    Result r;
    // some intervening code
    r = (Result){.all_equal = 1, .max = numbers[0]};
    
    或许:

    void function(Result r);
    
    void example(void) {
      function((Result){.all_equal = 1, .max = numbers[0]});
    }
    
    #包括
    最大整数(整数a,整数b)
    {
    返回(a>b)?a:b;
    }
    内部主(空)
    {
    INTA、b、c;
    printf(“输入三个数字:”);
    scanf(“%d%d%d”、&a、&b和&c);
    如果((a==b)和&(b==c))
    {
    printf(“人人平等”);
    }
    其他的
    {
    printf(“%d是所有中最大的”,max(a,max(b,c));
    }
    返回0;
    }
    
    您的代码很好;事实上,除其他外,Realized by在内部循环中使用了相同的机制来查找三个值的中间值。除了

  • 如果所有值都相同,它不会“找出三个数字中的最大值”,而是短路并找出最大值出现的次数(三)。这是一个单独的问题
  • 把一个人的逻辑与输出分开通常是值得的
  • 我已经把你的代码放到一个函数中,去掉了输出,去掉了等式

    #include <assert.h>
    
    static int hi3(const int a, const int b, const int c) {
        /* Same as `return a > b ? a > c ? a : c : b > c ? b : c;`. */
        if(a > b) {
            if(a > c) {
                return a;
            } else {
                return c;
            }
        } else {
            if(b > c) {
                return b;
            } else {
                return c;
            }
        }
    }
    
    int main(void) {
    
        /* Permutations of 3 distinct values. */
        assert(hi3(1, 2, 3) == 3
            && hi3(1, 3, 2) == 3
            && hi3(2, 1, 3) == 3
            && hi3(2, 3, 1) == 3
            && hi3(3, 1, 2) == 3
            && hi3(3, 2, 1) == 3);
    
        /* Permutation of 2 distinct values. */
        assert(hi3(1, 2, 2) == 2
            && hi3(2, 1, 2) == 2
            && hi3(2, 2, 1) == 2);
        assert(hi3(1, 1, 2) == 2
            && hi3(1, 2, 1) == 2
            && hi3(2, 1, 1) == 2);
    
        /* All the same value. */
        assert(hi3(1, 1, 1) == 1);
    
        return 0;
    }
    
    #包括
    静态整数hi3(常数整数a、常数整数b、常数整数c){
    /*与“返回a>b?a>c?a:c:b>c?b:c;”相同*/
    如果(a>b){
    如果(a>c){
    返回a;
    }否则{
    返回c;
    }
    }否则{
    如果(b>c){
    返回b;
    }否则{
    返回c;
    }
    }
    }
    内部主(空){
    /*3个不同值的排列*/
    断言(hi3(1,2,3)==3
    &&hi3(1,3,2)=3
    &&hi3(2,1,3)=3
    &&hi3(2,3,1)=3
    &&hi3(3,1,2)=3
    &&hi3(3,2,1)==3;
    /*两个不同值的排列*/
    断言(hi3(1,2,2)==2
    &&hi3(2,1,2)=2
    &&hi3(2,2,1)==2;
    断言(hi3(1,1,2)==2
    &&hi3(1,2,1)=2
    &&hi3(2,1,1)==2;
    /*都是同样的价值*/
    断言(hi3(1,1,1)==1);
    返回0;
    }
    

    您的代码成功地测试了所有相对大小的组合。

    而不是复制
    printf(“%d是所有变量中最大的”,变量)我建议在所有分支中为变量指定最大值,然后打印它。类似于
    inta,b,c,grest<代码>如果(a>b){如果(a>c)最大=a;否则最大=c}
    <代码>printf(“%d是所有中最大的”,最大的)
    scanf(“%d%d%d”,…)
    <代码>%d“
    表示“忽略可选空格并读取一个整数”,
    %d“
    表示“忽略可选空格两次并读取一个整数”我投票结束这个问题,因为这是一个改进工作代码的开放式请求。Try codereview.stackexchange.com还返回3的最大值数,但不返回2的最大值数。这很让人困惑。@Neil抱歉,我不知道你想说什么,除了我同意第二句话(我真的很困惑):(如果有一个或两个最大值,一个只打印最大值,如果有三个,它打印出三个相等;回答这个问题让我看到这也是原始行为,所以可能OP意味着会发生这种情况。在严格的总顺序下,最大值集的基数是0或1:)我相信OP想要表明值相等,或者它们与最大值不相等。因此,输出假定严格的总顺序(
    Yes;例如
    {2,2,1}
    prints 2和
    {1,1,1}
    prints都相等,可以将其视为非严格/严格的混合。
    #define N 3
    int numbers[N] = {-50, 1, 30}; // mock data
    
    char all_equal = 0; // mock data
    int max = numbers[0];
    for (int i = 1; i < N; i++) {
      if (numbers[i] > max) max = numbers[i];
    }
    
    const int N = 3;
    int numbers[N];
    
    enum { N = 3 };
    int numbers[N] = {-50, 1, 30}; // mock data
    
    char all_equal = 0; // mock data
    int max = numbers[0];
    for (int i = 1; i < n; i++) {
      if (numbers[i] > max) max = numbers[i];
    }
    
    enum { N = 3 };
    int numbers[N] = {-50, 1, 30}; // mock data
    
    char all_equal = 1;
    int max = numbers[0];
    for (int i = 1; i < N; i++) {
      all_equal = all_equal && numbers[i] == max;
      if (numbers[i] > max) max = numbers[i];
    }
    // here we have valid all_equal and max
    
    #include <stdio.h>
    
    int main() {
      enum { N = 3 };
      int numbers[N] = {-50, 1, 30}; // mock data
    
      char all_equal = 1;
      int max = numbers[0];
      for (int i = 1; i < N; i++) {
        all_equal = all_equal && numbers[i] == max;
        if (numbers[i] > max) max = numbers[i];
      }
    
      if (all_equal) printf("All numbers are equal.\n");
      else printf("%d is the greatest among the numbers entered.\n", max);
    }
    
    #include <stdio.h>
    
    int main() {
      enum { N = 3 };
      int numbers[N];
      printf("Enter %d numbers:\n", N);
      for (int i = 0; i < N; i++) scanf("%d", &numbers[i]);
    
      char all_equal = 1;
      int max = numbers[0];
      for (int i = 1; i < N; i++) {
        all_equal = all_equal && numbers[i] == max;
        if (numbers[i] > max) max = numbers[i];
      }
    
      if (all_equal) printf("All numbers are equal.\n");
      else printf("%d is the greatest among the numbers entered.\n", max);
    }
    
    int main() {
      enum { N = 3 };
      int numbers[N];
      printf("Enter %d numbers.\n", N);
      for (int i = 0; i < N; /*empty!*/) {
        printf("#%d: ", i+1);
        if (scanf("%d", &numbers[i]) != 1)
          printf("Sorry, invalid input. Try again.\n");
        else
          i++;
      }
      ...
    }
    
    int main() {
      enum { N = 3 };
      int numbers[N];
      printf("Enter %d numbers.\n", N);
      for (int i = 0; i < N; /*empty*/) {
        printf("#%d: ", i+1);
        if (scanf("%d", &numbers[i]) != 1) {
          printf("Sorry, invalid input. Try again.\n");
          char c;
          while ((c = getchar()) != '\n'); // remove input until end of line
        } else
          i++;
      }
      ...
    }
    
    #include <stdlib.h> // added for abort()
    
    int main() {
      enum { N = 3 };
      int numbers[N];
      printf("Enter %d numbers.\n", N);
      for (int i = 0; i < N; /*empty*/) {
        int c;
        printf("#%d: ", i);
        c = scanf("%d", &numbers[i]);
        if (c == EOF) abort();
        if (c != 1) {
          printf("Sorry, invalid input. Try again.\n");
          while ((c = getchar()) != EOF && c != '\n');
          if (c == EOF) abort();
        } else
          i++;
      }
      ...
    }
    
    #include <stdio.h>
    #include <stdlib.h>
    
    int main() {
      enum { N = 3 };
      int numbers[N];
      printf("Enter %d numbers.\n", N);
      for (int i = 0; i < N; /*empty*/) {
        printf("#%d: ", i+1);
        int c = scanf("%d", &numbers[i]);
        if (c == EOF) abort();
        if (c != 1) {
          printf("Sorry, invalid input. Try again.\n");
          while ((c = getchar()) != EOF && c != '\n');
          if (c == EOF) abort();
        } else
          i++;
      }
    
      char all_equal = 1;
      int max = numbers[0];
      for (int i = 1; i < N; i++) {
        all_equal = all_equal && numbers[i] == max;
        if (numbers[i] > max) max = numbers[i];
      }
    
      if (all_equal) printf("All numbers are equal.\n");
      else printf("%d is the greatest among the numbers entered.\n", max);
    }
    
    void read_numbers(int *dst, int count);
    
    #include <assert.h>
    #include <stdio.h>
    #include <stdlib.h>
    
    void read_numbers(int *dst, int count);
    
    int main() {
      enum { N = 3 };
      int numbers[N];
    
      read_numbers(numbers, N);
    
      char all_equal = 1;
      int max = numbers[0];
      for (int i = 1; i < N; i++) {
        all_equal = all_equal && numbers[i] == max;
        if (numbers[i] > max) max = numbers[i];
      }
    
      if (all_equal) printf("All numbers are equal.\n");
      else printf("%d is the greatest among the numbers entered.\n", max);
    }
    
    void read_numbers(int *dst, int count) {
      assert(count > 0);
      printf("Enter %d numbers.\n", count);
      for (int i = 0; i < count; /*empty*/) {
        printf("#%d: ", i+1);
        int c = scanf("%d", &dst[i]);
        if (c == EOF) abort();
        if (c != 1) {
          printf("Sorry, invalid input. Try again.\n");
          while ((c = getchar()) != EOF && c != '\n');
          if (c == EOF) abort();
        } else
          i++;
      }
    }
    
    #include <assert.h>
    #include <stdio.h>
    #include <stdlib.h>
    
    typedef struct {
        char all_equal; // true if all numbers were equal
        int max; // the greatest number
    } Result;
    
    void read_numbers(int *dst, int count);
    Result find_greatest(int *numbers, int count);
    void output_result(const Result *r);
    
    int main() {
      enum { N = 3 };
      int numbers[N];
    
      read_numbers(numbers, N);
      const Result r = find_greatest(numbers, N);
      output_result(&r);
    }
    
    void read_numbers(int *dst, int count) {
      assert(count > 0);
      printf("Enter %d numbers.\n", count);
      for (int i = 0; i < count; /*empty*/) {
        printf("#%d: ", i+1);
        int c = scanf("%d", &dst[i]);
        if (c == EOF) abort();
        if (c != 1) {
          printf("Sorry, invalid input. Try again.\n");
          while ((c = getchar()) != EOF && c != '\n');
          if (c == EOF) abort();
        } else
          i++;
      }
    }
    
    Result find_greatest(int *numbers, int count) {
      assert(count > 0);
      Result r = {.all_equal = 1, .max = numbers[0]};
      for (int i = 1; i < count; i++) {
        r.all_equal = r.all_equal && numbers[i] == r.max;
        if (numbers[i] > r.max) r.max = numbers[i];
      }
      return r;
    }
    
    void output_result(const Result *r) {
      if (r->all_equal) printf("All numbers are equal.\n");
      else printf("%d is the greatest among the numbers entered.\n", r->max);
    }
    
    Result r = {.all_equal = 1, .max = numbers[0]};
    
    Result r;
    // some intervening code
    r = (Result){.all_equal = 1, .max = numbers[0]};
    
    void function(Result r);
    
    void example(void) {
      function((Result){.all_equal = 1, .max = numbers[0]});
    }
    
    #include <stdio.h>
    
    int max(int a, int b)
    {
        return (a > b) ? a : b;
    }
    
    int main(void)
    {
        int a, b, c;
        printf("Enter three number: ");
        scanf("%d %d %d", &a, &b, &c);
    
        if ((a == b) && (b == c))
        {
            printf("all are equal.");
        }
        else
        {
            printf("%d is the greatest among all", max(a, max(b, c)));
        }
    
        return 0;
    }
    
    #include <assert.h>
    
    static int hi3(const int a, const int b, const int c) {
        /* Same as `return a > b ? a > c ? a : c : b > c ? b : c;`. */
        if(a > b) {
            if(a > c) {
                return a;
            } else {
                return c;
            }
        } else {
            if(b > c) {
                return b;
            } else {
                return c;
            }
        }
    }
    
    int main(void) {
    
        /* Permutations of 3 distinct values. */
        assert(hi3(1, 2, 3) == 3
            && hi3(1, 3, 2) == 3
            && hi3(2, 1, 3) == 3
            && hi3(2, 3, 1) == 3
            && hi3(3, 1, 2) == 3
            && hi3(3, 2, 1) == 3);
    
        /* Permutation of 2 distinct values. */
        assert(hi3(1, 2, 2) == 2
            && hi3(2, 1, 2) == 2
            && hi3(2, 2, 1) == 2);
        assert(hi3(1, 1, 2) == 2
            && hi3(1, 2, 1) == 2
            && hi3(2, 1, 1) == 2);
    
        /* All the same value. */
        assert(hi3(1, 1, 1) == 1);
    
        return 0;
    }