简体   繁体   English

else 语句在 C 中有效

[英]else statement does work in C

I'm trying to work out Babbage's difference machine in C. I wrote all the basic idea of the concept: If you are about to find square of 10, for example, first you have to find square of integers before 10. This is how Babbage's machine works.我正在尝试用 C 语言计算 Babbage 的差分机。我写了这个概念的所有基本思想:例如,如果你要找到 10 的平方,首先你必须找到 10 之前的整数平方。这就是方法巴贝奇的机器工作正常。 So in my program we have a list of integers and we assume that we already squares of these integers.所以在我的程序中,我们有一个整数列表,我们假设我们已经对这些整数进行了平方。 Giving this information, it must be capable of working out square of any positive integer, without making any multiplication.提供此信息,它必须能够计算任何正整数的平方,而无需进行任何乘法运算。

BUT, the code just does not execute the "else" part in if-else statements.但是,代码只是不执行 if-else 语句中的“else”部分。 However, when I deleted everything in "else" statement and simply wrote a printf function, it supplied the conditions well.但是,当我删除“else”语句中的所有内容并简单地编写一个 printf 函数时,它很好地提供了条件。

So something must be wrong in the else statement, am I doing appending items to array wrong?所以 else 语句中一定有问题,我是否正在将项目附加到数组错误? Is it not allowed to write for loops in If-Else statement in C?是否不允许在 C 中的 If-Else 语句中编写 for 循环? I wrote the same algorithm in python and it works well!我在 python 中编写了相同的算法,效果很好!

ps: I already tried to write a do-while instead of "else" part. ps:我已经尝试过写一个 do-while 而不是“else”部分。

please help, thanks in advance请帮忙,提前致谢

#include<stdio.h>

int main(){
int integers[] = {0,1,2};
int sq_of_integers[] = {0,1,4};
int first_differences[] = {1,3};
int second_difference[] = {2};
int input;
int i;
int x;
int end2 = 2;
int end1 = 1;

for ( i = 0 ; i<=2 ; i++){
    printf("Enter a number to be squared: ");
    scanf(" %d", &input);

    if (input <= integers[end2]){
        printf("It is already known, sir.\n");
    }else{
        for( x = 0 ; x<= input - integers[-1] ; x++){

            // Calculation of the square root:
            integers[end2+1] = integers[end2]+1;
            sq_of_integers[end2+1] = second_difference[0] + first_differences[end1] + sq_of_integers[end2];
            first_differences[end1+1] = sq_of_integers[end2] +sq_of_integers[-2];
            printf(sq_of_integers[end2]);
            end2 = end2 + 1;
            end1 = end1 + 1;
        }
    }
}
return 0;
}

input - integers[-1] has undefined value. input - integers[-1]有未定义的值。 Probably it was negative and so you exit the for loop without processing it once.可能它是负数,所以你退出for循环而不处理它一次。

Edit: Complete review of code.编辑:完整的代码审查。 Please ask if you don't understand something.如果您有什么不明白的地方,请询问。

You were trying to use printf() without a format argument, probably you compile without warnings.您试图在没有格式参数的情况下使用printf() ,可能您编译时没有警告。 Try to use the command I give to compile.尝试使用我给出的命令进行编译。

There was also an index error in your calculation of first_differences values.您在计算first_differences值时也存在索引错误。

small.c:小c:

/*
Compile with MinGW for Windows:
gcc -Wall -Wextra -Werror -std=gnu99 -o small.exe small.c

Compile with linux gcc:
gcc -Wall -Wextra -Werror -std=gnu99 -o small small.c
*/

#define N_UPPER_LIMIT 1023

#include <stdio.h>

int main()
{
    long integers[N_UPPER_LIMIT + 1] = {0, 1, 2};
    long sq_of_integers[N_UPPER_LIMIT + 1] = {0, 1, 4};
    long first_differences[N_UPPER_LIMIT] = {1, 3};
    long second_difference[] = {2};
    int end1 = 1;
    int end2 = 2;

    for (int i = 0; i < 2; i++)
    {
        int input;

        printf("Enter a number to be squared: ");
        fflush(stdout);
        scanf(" %d", &input);

        if (input < 0)
        {
            printf("It is negative, sir.\n");
        }
        else if (input <= integers[end2])
        {
            printf("It is already known, sir.\n");
            printf("square(%ld) = %ld\n", integers[input], sq_of_integers[input]);
        }
        else if (input > N_UPPER_LIMIT)
        {
            printf("Please do not go further than %d, sir.\n", N_UPPER_LIMIT);
        }
        else
        {
            while (input > integers[end2])
            {
                // Calculation of the square root:
                integers[end2 + 1] = integers[end2] + 1;
                sq_of_integers[end2 + 1] = second_difference[0]
                    + first_differences[end1] + sq_of_integers[end2];
                first_differences[end1 + 1] = sq_of_integers[end2 + 1]
                    - sq_of_integers[end2];
                end2++;
                end1++;
                printf("square(%ld) = %ld\n", integers[end2], sq_of_integers[end2]);
            }
        }
    }

    return 0;
}

It could be a good exercise to get rid of the static limit.摆脱静态限制可能是一个很好的练习。 If you want to do so, you will have to allocate memory for your variables with malloc() , and either put a limit as a program argument, or try to change the size allocated with calls to aither malloc() of realloc() .如果你想这样做,你将不得不使用malloc()为变量分配内存,或者将限制作为程序参数,或者尝试更改通过调用realloc()另一个malloc()分配的大小。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM