stackoverflow 异常未处理

发布于 2024-10-08 15:09:36 字数 1264 浏览 6 评论 0原文

在下面的代码中,它应该乘以 2 个数字。它适用于 3 位和少于 3 位的数字,但是当我给出 4 位或更大的数字时,它会给出运行时错误:stackoverflow 异常未处理。我已经评论过问题出在哪里了。我认为问题是在 int 中定义变量并长时间更改它们,但问题仍然存在。错误在哪里?

编辑: 现在,你对这个问题有什么看法?它没有任何作用

        public long Prod2(long u, long v)
    {
        var numbers = textBox7.Text.Split(',').Select(p => long.Parse(p)).ToArray();
        int n = Math.Max((int)Math.Floor(Math.Log10(u) + 1),(int)Math.Floor(Math.Log10(v) + 1));        
        int threshold = 3;

        if (u == 0 || v == 0)
        {
            return 0;
        }
        else if (n <= threshold)
        {
            return u * v;
        }
        else
        {
            int m = (int)Math.Ceiling(n / 2.0);

            int x = (int)(u / Math.Pow(10, m));
            int y = (int)(u % Math.Pow(10, m));
            int w = (int)(u / Math.Pow(10, m));
            int z = (int)(v % Math.Pow(10, m));

            long r = Prod2(x + y, w + z);
            long p = Prod2(x, w);
            long q = Prod2(y, z);

            return p * (long)Math.Pow(10, 2 * m) + (r - p - q) * (long)Math.Pow(10, m) + q;
            long result = Prod2(numbers[0], numbers[1]);
            textBox1.Text = result.ToString();
        }
    }

In the code below it should multiply 2 numbers. It works for 3 and less than 3 digits numbers but when I give numbers with 4 digits or bigger it gives runtime error: stackoverflow exception was unhandled. I've commented where the problem is. I thought the problem is for defining variables in int and changed them in long but the problem still exists. Where is the mistake?

edited:
now,whats do you think about the problem?it doesnt do anything

        public long Prod2(long u, long v)
    {
        var numbers = textBox7.Text.Split(',').Select(p => long.Parse(p)).ToArray();
        int n = Math.Max((int)Math.Floor(Math.Log10(u) + 1),(int)Math.Floor(Math.Log10(v) + 1));        
        int threshold = 3;

        if (u == 0 || v == 0)
        {
            return 0;
        }
        else if (n <= threshold)
        {
            return u * v;
        }
        else
        {
            int m = (int)Math.Ceiling(n / 2.0);

            int x = (int)(u / Math.Pow(10, m));
            int y = (int)(u % Math.Pow(10, m));
            int w = (int)(u / Math.Pow(10, m));
            int z = (int)(v % Math.Pow(10, m));

            long r = Prod2(x + y, w + z);
            long p = Prod2(x, w);
            long q = Prod2(y, z);

            return p * (long)Math.Pow(10, 2 * m) + (r - p - q) * (long)Math.Pow(10, m) + q;
            long result = Prod2(numbers[0], numbers[1]);
            textBox1.Text = result.ToString();
        }
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

渡你暖光 2024-10-15 15:09:36

此时您将进入无限递归循环,

 long result = bigzarb(x, w) * Math.Pow(10, m) + (bigzarb(x, w) + bigzarb(w, y)) * Math.Pow(10, m) + bigzarb(y, z);///here
    textBox1.Text = result.ToString();

我注意到仅当 intn > > 时才执行此行。 3,那么也许你有一个逻辑错误?

更新:读完你的评论后,我可以看到这个测试的目的是说“如果这个字符串的长度<= 3那么......”而事实上,正如所写的那样,它实际上是在说“如果转换后的字符串的 VALUE <= 3 则...”

You are getting into an infinite recursive loop at this point

 long result = bigzarb(x, w) * Math.Pow(10, m) + (bigzarb(x, w) + bigzarb(w, y)) * Math.Pow(10, m) + bigzarb(y, z);///here
    textBox1.Text = result.ToString();

I notice this line is executed only when intn > 3, so perhaps you have a logic bug there?

Update: After reading your comments I can see that this test is intended to say "if the length of this string is <= 3 then..." when in fact as written it is actually saying "if the VALUE of this converted string is <= 3 then..."

亚希 2024-10-15 15:09:36

编辑:我已经为您完全翻译了书中描述的算法:

public long Prod2(long u, long v)
{
    int n = Math.Max((int)Math.Floor(Math.Log10(u) + 1), (int)Math.Floor(Math.Log10(v) + 1));
    int threshold = 3;

    if(u == 0 || v == 0)
    {
        return 0;
    }
    else if(n <= threshold)
    {
        return u * v;
    }
    else
    {
        int m = (int)Math.Ceiling(n / 2.0);

        int x = (int)(u / Math.Pow(10, m));
        int y = (int)(u % Math.Pow(10, m));
        int w = (int)(u / Math.Pow(10, m));
        int z = (int)(v % Math.Pow(10, m));

        long r = Prod2(x + y, w + z);
        long p = Prod2(x, w);
        long q = Prod2(y, z);

        return p * (long)Math.Pow(10, 2 * m) + (r - p - q) * (long)Math.Pow(10, m) + q;
    }
}

为了获得正确的结果,您可以从一些其他方法中调用此方法,如下所示:

void Main()
{

    // Call the method and store the result in variable 'r'.
    long r = Prod2(1234, 5678);
    Console.WriteLine(r);

    /////////////////////////////////
    //
    // OR - In your case read from textBox7 and then store the result in textBox1    
    //
    /////////////////////////////////
    var numbers = textBox7.Text.Split(',').Select(p => long.Parse(p)).ToArray();
    long result = prod2(numbers[0], numbers[1]);
    textBox1.Text = result.ToString();
}

因此在您的事件处理程序中,例如对于 button1,您需要执行这个 来进行调用:

public void button1_Click()
{
    var numbers = textBox7.Text.Split(',').Select(p => long.Parse(p)).ToArray();
    long result = prod2(numbers[0], numbers[1]);
    textBox1.Text = result.ToString();
}

不要修改我的 Prod2,只需将其粘贴到您的代码。这样,Prod2 就会进行计算,然后您的 button1_Click 控制输入以及如何处理输出

EDIT: I've completely translated the algorithm described in the book for you:

public long Prod2(long u, long v)
{
    int n = Math.Max((int)Math.Floor(Math.Log10(u) + 1), (int)Math.Floor(Math.Log10(v) + 1));
    int threshold = 3;

    if(u == 0 || v == 0)
    {
        return 0;
    }
    else if(n <= threshold)
    {
        return u * v;
    }
    else
    {
        int m = (int)Math.Ceiling(n / 2.0);

        int x = (int)(u / Math.Pow(10, m));
        int y = (int)(u % Math.Pow(10, m));
        int w = (int)(u / Math.Pow(10, m));
        int z = (int)(v % Math.Pow(10, m));

        long r = Prod2(x + y, w + z);
        long p = Prod2(x, w);
        long q = Prod2(y, z);

        return p * (long)Math.Pow(10, 2 * m) + (r - p - q) * (long)Math.Pow(10, m) + q;
    }
}

To get the right result, you'd call this method from some other method like this:

void Main()
{

    // Call the method and store the result in variable 'r'.
    long r = Prod2(1234, 5678);
    Console.WriteLine(r);

    /////////////////////////////////
    //
    // OR - In your case read from textBox7 and then store the result in textBox1    
    //
    /////////////////////////////////
    var numbers = textBox7.Text.Split(',').Select(p => long.Parse(p)).ToArray();
    long result = prod2(numbers[0], numbers[1]);
    textBox1.Text = result.ToString();
}

So in your event handler, for example for button1, you'd do this to make the call:

public void button1_Click()
{
    var numbers = textBox7.Text.Split(',').Select(p => long.Parse(p)).ToArray();
    long result = prod2(numbers[0], numbers[1]);
    textBox1.Text = result.ToString();
}

Don't modify the Prod2 that I have, and just paste it with your code. This way, Prod2 does the calculation and then your button1_Click controls the input and what to do with the output.

云归处 2024-10-15 15:09:36

简而言之,您可能会遇到以下情况(根据输入而变化):

function bigzarb()
{
    bigzarb()
}

只要 textBox7 中的数字 > > 3、即未闭合的递归循环,这必然会成为stackoverflow。

在有问题的行上放置一个断点,您很快就会发现问题。在不知道你的方法做什么的情况下(我不认识该算法),我无法帮助清理它,但第一步可能是有一个 get out 子句有条件地从函数返回。但是,我还看到您在使用输入参数 uv 之前覆盖了它们,所以也许您在算法中犯了错误?

In a nutshell you have a potential (varying on input) case of:

function bigzarb()
{
    bigzarb()
}

so long as the numbers in textBox7 are > 3, i.e. an unclosed recursion loop, which will inevitably become a stackoverflow.

Put a breakpoint on the line in question and you'll quickly see the problem. Without knowing what your method does (I don't recognise the algorithm) I can't help much about cleaning it up, but the first step might be to have a get out clause to return from the function conditionally. However, I also see you overwriting the input arguments u and v before they get used so perhaps you've made a mistake in the algo?

自由范儿 2024-10-15 15:09:36

Pow 方法返回 double,所以我认为你的 x、y、z、w 和 z 也应该声明为 double。

Pow method returns double, so I think your x, y, z, w, and z should be declared as double as well.

半窗疏影 2024-10-15 15:09:36

您将获得递归调用的“StackoverFlow”。最好对代码中发现的漏洞进行模制。我建议你改变一下逻辑。

静态 int 调用栈 = 0;

    public double bigzarb(long u, long v)
    {
        callStack++;
         ............
         ............

you are getting "StackoverFlow" for the Recursive call. its better to mold the holes found in the code. I suggest you to change the logic.

static int callStack = 0;

    public double bigzarb(long u, long v)
    {
        callStack++;
         ............
         ............
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文