1

我创建了一个程序来确定最大数字,但我的讲师说它并不完美,有人可以让它完美吗?

 #include <stdio.h>
 int main () {
     double a,b=0,n, i;
     printf("limit of n input: ");
     scanf ("%lf",&n);

     for (i=1;i<=n;i++) {
         scanf("%lf",&a);
         if (a>b) b=a;
     }

     printf("%.2lf", b);

     return 0;
 }
4

3 回答 3

2

如果“不完美”她的意思是“不能正确处理负数或空集”,那么你会想要

  1. 将 n<1 视为特殊情况(为什么 0 应该是空集的最大值?)
  2. 读取循环外的第一个数字,因此您不会对可能的最小数字做出假设
于 2013-10-24T13:07:22.033 回答
1

我会那样做,对不起大量的文字。我认为它来自典型的 Objective-C 风格的长词编程:

#include <stdio.h>

int clean_stdin() {
    while (getchar()!='\n');

    return 1;
}

int main(int argc, char *argv[]) {
    char c;
    signed int count = 0;    // number of numbers to scan

    unsigned int fireErrorMessage = 0;
    do {
        if (fireErrorMessage == 1) {
            printf("You entered not a positive natural number. Please enter a number >0 Examples: 1 22 4012\n"); // output for the user
        }
        if (fireErrorMessage == 0) {
            fireErrorMessage = 1;
        }
        printf("How many integers do you want to insert (Inser a number >0)? ");
    } while (((scanf("%d%c", &count, &c) != 2 || c != '\n') && clean_stdin()) || count < 1);

    signed int indexOfNumber; // for index, declared outside because of output at the end

    signed int highestNumberIndex;
    double highestNumber; // saving the highest value in a helper variable

    fireErrorMessage = 0;
    for (indexOfNumber = 1; indexOfNumber <= count; indexOfNumber++) {
        double scannedNumber;
        do {
            if (fireErrorMessage == 1) {
                printf("You entered not a number. Please enter a number. Examples: 3.0 -1 14\n"); // output for the user
            }
            if (fireErrorMessage == 0) {
                fireErrorMessage = 1;
            }
            printf("Input number %d: ", indexOfNumber); // output for the user
        } while (((scanf("%lf%c", &scannedNumber, &c) != 2 || c != '\n') && clean_stdin()));

        fireErrorMessage = 0;

        if (indexOfNumber == 1 || scannedNumber > highestNumber) {
            highestNumberIndex = indexOfNumber;
            highestNumber = scannedNumber;
        }
    }

    printf("Highest input number on index %d, the value is about %.2lf\n", highestNumberIndex, highestNumber);

    return 0;
}

输出

How many integers do you want to insert (Inser a number >0)? aa5
You entered not a positive natural number. Please enter a number >0 Examples: 1 22 4012
How many integers do you want to insert (Inser a number >0)? -3
You entered not a positive natural number. Please enter a number >0 Examples: 1 22 4012
How many integers do you want to insert (Inser a number >0)? 3
Input number 1: aa
You entered not a number. Please enter a number. Examples: 3.0 -1 14
Input number 1: -50.0001
Input number 2: 51a
You entered not a number. Please enter a number. Examples: 3.0 -1 14
Input number 2: -1.00
Input number 3: -0.1
Highest input number on index 3, the value is about -0.10
于 2013-10-24T13:26:30.827 回答
1

此代码适用于负数,而不是循环索引的数字输入,以及循环内的负数而不是数字输入。谢谢

  #include <stdio.h>
  #include <math.h>
  int main () {
             int n, i;
             double a,b=0;
             printf("limit of n input: ");
             scanf ("%lf",&n);

            if(n < 0){
                      printf("value of n cannot be negative");
                      return 0;
                     }
           else if (n == 0)
                     return 0;
           else if (isnan(n))
                     return 0;

           else{
             for (i=1;i<=n;i++) 
               {
                   scanf("%lf",&a);
                   if(!isnan(a) && a > 0)
                         { 
                          if (a>b) b=a;
                         }

               }
              printf("%.2lf", b);

              return 0;
               }
             }
于 2013-10-24T13:54:18.050 回答