简体   繁体   English

我正在尝试编写欧几里得算法,但是当我在C代码中使用循环时程序崩溃

[英]i am trying to write Euclidean Algorithm but program crash when i use loop in c code

I am trying to write the Euclidean Algorithm in C code. 我正在尝试用C代码编写欧几里得算法 When I use the do while or for loop the program crashes: 当我使用do whilefor循环时,程序崩溃:

#include<stdio.h>
int gcd(int a,int b);

int main()
{
    int x,r,y;
    printf("Enter two values:\n");
    scanf("%d%d",&x,&y);
    if(x<y)r=x,x=y,y=r;
    r=gcd(x,y);
    printf("\n\n   Result: %d",r);
    return 0;
}

int gcd(int a,int b)
{
    if(a==0||b==0)
    {
        if(a==0)return b;
        if(b==0)return a;
    }
    else
    {
        for(;;a>0||b>0)//also try with do while loop but problem same
        {
            int x;
            x=a%b;
            a=b;
            b=x;
        }
    }
}

Can you please explain me the reason behind the crash of the program? 您能否解释一下程序崩溃的原因? I use codeblocks with gcc 我在gcc使用代码块

This: 这个:

for(;;a>0||b>0)

is an infinite loop, then end condition is the middle part, it should be: 是一个无限循环,那么结束条件是中间部分,它应该是:

for (; a > 0 || b > 0;)

I doubt the logic of that condition, shouldn't it be && ? 我怀疑这种情况的逻辑,不是&&吗?

Instead of if(x<y)r=x,x=y,y=r; 代替if(x<y)r=x,x=y,y=r; you will need to do: 您将需要执行以下操作:

if(x < y){
    r = x;
    x = y;
    y = r;
}

Also you will need to place condition in second statement of for loop. 另外,您需要将条件放在for循环的第二条语句中。 Instead of: 代替:

for(;;a>0||b>0)
{
    ...
}

you could do while(a > 0 && b > 0) . 你可以做while(a > 0 && b > 0) Depending on your familiarity you could use recursion like: 根据您的熟悉程度,您可以使用以下递归:

int hcf(int a, int b)
{
    if (b == 0)
        return a;
    else
       return hcf(b, a%b);

}

Also note that your hcf function does not have return statement in else block. 还要注意,您的hcf函数在else块中没有return语句。 So you will have to use conditional return: 因此,您将必须使用条件返回:

if (a == 0) return b;
else return a;

at the end of hcf function hcf函数的末尾

This statement 这个说法

for(;;a>0||b>0)

does nothing so you have an endless loop. 什么也没做,所以你有一个无限循环。

What seem to happen is that b becomes zero and then you do a % 0 which is illegal and cause a crash. 似乎发生的事情是b变为零,然后执行a % 0 ,这是非法的并导致崩溃。

Remember that it is the second statement in the for-loop that is the condition for terminating the loop - it's not the third. 请记住,是for循环中的第二条语句是终止循环的条件-不是第三条。 So maybe you wanted: 所以也许您想要:

for( ; a>0||b>0; )

暂无
暂无

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

相关问题 我正在尝试用C编写程序以将DS1302 RTC与PIC16F877a结合使用 - I am trying to write a program in C to use an DS1302 RTC with a PIC16F877a 为什么当我尝试在 for 循环中使用 strcat() 时程序崩溃? - Why the program crashes when I am trying to use strcat() in for-loop? 我正在尝试在 C 中创建一个代码抛光程序 - I am trying to create a code polisher program in C 尝试将C代码转换为C ++-交互式二进制Euclidean算法 - Trying to convert C code to C++ - interative binary Euclidean algorithm 每当我使用 for 循环时字符串程序崩溃 - String program crash whenever i use a for loop 我正在尝试编写一个程序来计算 C 中连词(或具有 1,2 或 3 个字母的单词)的出现次数,我的代码运行但显示 0 个单词 - I am trying to write a program that counts the occurrences of conjunctions(or words with 1,2 or 3 letters) in C, my code runs but it shows 0 words 当我尝试分配新内存时,为什么realloc会崩溃我的程序? - why does realloc crash my program when i am trying to allocate new memory? 我对编程非常陌生,我试图用C编写此程序,并且在编译后仍然收到错误消息 - I am extremely new to programming, i am trying to write this program in C and after compiling I keep getting an error message 我试图用C编写一个程序,提示用户输入名称,然后输出其首字母缩写 - I am trying to write a program in C that prompts user for a name and then outputs its initials 为什么我写同样的算法时c#中的代码与c中的代码不同? - Why does the code in c# differs from the code in c when I write the same algorithm?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM