输入大数时我的计算显示错误?

时间:2016-12-03 14:09:51

标签: c

#include <stdio.h>

main()
{
int choice, no;

printf("1. Show sum of odd/even number to N term\n");
printf("2. Smallest, largest and average of the supplied numbers\n");
printf("3. Terminate the programs\n\n");

printf("Enter your choice[1|2|3]: ");
scanf("%d", &choice);

if (choice == 1)
{
    int i , no , sum = 0, j, sum2 = 0;

    printf("\nEnter any number: ");
    scanf("%d", &no);

    for (i = 2; i <= no; i = i + 2)
    {
        sum = sum + i;
    }

    printf("\nSum of all even number between 1 to %d = %d\n", no, sum);

    for (j = 1; j <= no; j = j + 2)
    {
        sum2 = sum2 + j;
    }
    printf("Sum of all odd number between 1 to %d = %d\n", no, sum2);
}
else if(choice == 2)    
{
    float max, min, avg, num,counter=0, sum = 1;
    printf("\nPlease enter all the number you want![0 to end]: ");
    scanf("%f", &num);
    max = min = num;
    while (num != 0)
    {
        printf("Please enter all the number you want![0 to end]: ");
        scanf("%f", &num);
        if (max < num && num > 0)
            max = num;
        else if (min > num && num > 0)
            min = num;
        sum = sum + num;
        counter++;
    }
    printf("\nThe smallest and largest of entered numbers are %.2f and %.2f respectively.\n", min, max);
    avg = sum / counter;
    printf("The sum of entered number is %.2f\n", sum);
    printf("The average of entered number is %.2f\n", avg);
}

}

我的问题是,当我选择数字2时,它会显示最小和最大的数字但是当我输入200这样的大数字时,总和显示错误!但是当我输入小值时它工作正常!?

small number Big number

包含图片

3 个答案:

答案 0 :(得分:1)

你的金额从未算过第一个输入。初始值sum = 1,

对于你的小数字:你的总和=(1 + 1 + 1 + 2)恰好是正确的。 但对于你的大数字:你的总和=(1 + 100 + 100 + 200)= 400.1(你可以看到你错过了第一个输入100);

你的错误:

  1. sum应初始化为0;

  2. 你没有计算第一个输入(在循环之前):不是计算总和也不是计数器++

  3. 当用户最后输入0时,您不应该继续label.setValue(attributedString, forKey: "accessibilityLabel"),因为&#39; 0&#39;不是有效的输入。

答案 1 :(得分:0)

您的计划有几个问题:

  • 您将总和初始化为1,而不是0,因为它应该是。
  • 您可以不同方式处理第一个值和后续值。这基本上没问题,但要确保两种情况下的治疗方法相同。在您的代码中,您可以正确地为第一个值分配min和max,但是会错过递增总和和计数器。
  • 您的代码不会检查是否输入了有效的浮点数。这意味着如果用户输入的不是浮点数,它就会挂起。你的程序应该处理这样的输入,好像它是零。
  • 理论上,当它为零时,你不应该除以counter。在实践中,这种情况不会发生,因为您还会在计数器中考虑终止零点。

也许将所有其他值视为第一个值会更好。然后,您可以将min和max初始化为大值和小值(例如,来自FLT_MAX的±<float.h>),或者您可以检查循环内的count == 0以实现第一个和第一个的不同行为以下数值。

在这种情况下,当给出无效输入或零时,您可以break出无限循环。这可能看起来很复杂,但会导致更简单的代码:

#include <stdio.h>
#include <float.h>

int main(void)
{
    float max = -FLT_MAX;       // minimum possible float value
    float min = FLT_MAX;        // maximum possible float value
    float sum = 0.0f;
    int count = 0;

    for (;;) {
        float num;

        printf("Please enter all the number you want![0 to end]: ");

        if (scanf("%f", &num) < 1 || num == 0) break;            

        if (max < num) max = num;
        if (min > num) min = num;
        sum += num;
        count++;
    }

    if (count) {
        float avg = sum / count;

        printf("%d values\n", count);
        printf("Smallest: %.2f\n", min);
        printf("Largest:  %.2f\n", max);

        printf("Sum:      %.2f\n", sum);
        printf("Average:  %.2f\n", avg);
    }

    return 0;
}

答案 2 :(得分:0)

#include <stdio.h>
main()
{
    int choice = 0;


    for (;choice != 3;)
    {
        printf("_____________________________________________________________\n\n");
        printf("1. Show sum of odd/even number to N term\n");
        printf("2. Smallest, largest and average of the supplied numbers\n");
        printf("3. Terminate the programs\n\n");
        printf("Enter your choice[1|2|3]: ");
        scanf("%d", &choice);
        printf("_____________________________________________________________\n\n");

        if (choice == 1)
        {
            int i, no, sumc1 = 0, j, sum2c1 = 0;

            printf("\nEnter any number: ");
            scanf("%d", &no);

            for (i = 2; i <= no; i = i + 2)
            {
                sumc1 = sumc1 + i;
            }

            printf("\nSum of all even number between 1 to %d = %d\n", no, sumc1);

            for (j = 1; j <= no; j = j + 2)
            {
                sum2c1 = sum2c1 + j;
            }
            printf("Sum of all odd number between 1 to %d = %d\n\n\n", no, sum2c1);
        }
        else if (choice == 2)
        {

            float counter, num, large, small, num2, sum = 0, avg;
            printf("\nEnter first number[Enter 0 to stop]: ");
            scanf("%f", &num);
            num2 = num;
            large = num;
            small = num;
            for (counter = 0; num != 0; counter++)
            {
                printf("Enter another number [Enter 0 to stop]: ");
                scanf("%f", &num);
                if (num > large && num > 0)
                    large = num;
                if (num<small && num > 0)
                    small = num;
                sum = sum + num;
            }
            sum = sum + num2;
            avg = sum / counter;
            printf("\nThe largest number is %.2f\n", large);
            printf("The smallest number is %.2f\n", small);
            printf("The sum of entered numbers are %.2f\n", sum);
            printf("The average of entered number are %.2f\n\n\n", avg);

        }
    }
}

我只是想知道所有的主要问题,虽然一些回复给了我完整的代码,我只需要使用简单的代码,因为我只是开始学习基本的。感谢。

此代码可能对某人有用。

// // uniten.encik