c++ 检测到堆栈破碎

3yhwsihp  于 2022-12-24  发布在  其他
关注(0)|答案(2)|浏览(121)
#include <iostream>
using namespace std;
int main()
{
    int tablica[9];
    string inputromanum;
    cout << "ROMAN: ";
    cin >> inputromanum;
    int maxindeks;
    bool disablenextcomp = false;
    int readysolution = 0;
    maxindeks = inputromanum.length() - 1;{}{}
    for (int i = 0; i <= maxindeks; i++)
    {
        if (inputromanum[i] == 'M' || inputromanum[i] == 'm')
        {
            tablica[i] = 1000;
        }
        if (inputromanum[i] == 'D' || inputromanum[i] == 'd')
        {
            tablica[i] = 500;
        }
        if (inputromanum[i] == 'C'|| inputromanum[i] == 'c')
        {
            tablica[i] = 100;
        }
        if (inputromanum[i] == 'L' || inputromanum[i] == 'l')
        {
            tablica[i] = 50;
        }
        if (inputromanum[i] == 'X' || inputromanum[i] == 'x')
        {
            tablica[i] = 10;
        }
        if (inputromanum[i] == 'V' || inputromanum[i] == 'v')
        {
            tablica[i] = 5;
        }
        if (inputromanum[i] == 'I' || inputromanum[i] == 'i')
        {
            tablica[i] = 1;
        }
    }
    cout<<endl;
    for(int i4 = 0; i4 <= maxindeks; i4++)
    {
        cout<<"tablica["<<i4<<"] = "<<tablica[i4]<<endl;
    }
    for (int i2 = 0; i2 <= maxindeks; i2++)
    {
        int i5 = i2 + 1;
        if (i5 <= maxindeks)
        {
            //cout<<endl<<"tablica[i2 + 1] = "<<tablica[i2 + 1];
            //cout<<endl<<"tablica[i2] = "<<tablica[i2];
            //cout<<endl<<"tablica[i2 + 1] - tablica[i2] = "<<tablica[i2 + 1] - tablica[i2];
            if (tablica[i2 + 1] - tablica[i2] > 0 && disablenextcomp == false)
            { 
                //cout<<endl<<"readysolution + (tablica[i2 + 1] - tablica[i2]) = "<<readysolution + (tablica[i2 + 1] - tablica[i2])<<endl;
                readysolution = readysolution + (tablica[i2 + 1] - tablica[i2]);
                disablenextcomp = true;
            }
            else
            {
                if(disablenextcomp == false)
                {
                    //cout<<endl<<"readysolution + tablica[i2] = "<<readysolution + tablica[i2]<<endl;
                    readysolution = readysolution +  tablica[i2];
                }
                else
                {
                    disablenextcomp = false;
                }
            }
        }
        else
        {
            if(disablenextcomp == false)
            {
                //cout<<endl<<endl<<"OSTATNI INDEKS";
                //cout<<endl<<"tablica[i2] = "<<tablica[i2];
                //cout<<endl<<"readysolution + tablica[i2] = "<<readysolution + tablica[i2];
                readysolution = readysolution +  tablica[i2];
            }
        }
        i5++;
    }
    cout << endl << readysolution;
}

这是我的程序。用于将罗马数字解码为阿拉伯数字。它在大多数情况下都能正常工作,但是,我的一位同事发现在将MMMCMXCVIII输入程序时会产生以下错误:

检测到堆栈崩溃:终止的

之后它就不工作了。
除了MMMMMMMMMMM之外,我找不到其他会导致这个错误的数字。当tablica数组的索引超过10时,它似乎会失败。我不知道为什么会这样,因为我是c++的新手。它应该输出3999而不是出现错误。它应该成功处理的数字范围应该是1到5000。

vqlkdk9b

vqlkdk9b1#

感谢评论中的人们,我找到了原因。tablica[9]数组应该存储9个或更少的字符。(本例中为MMMCMXCVIII)具有更多字符,因此,它使for循环负责存储每个字符的值,从而导致上述错误,因为没有剩余的单元来存储值。我已经将tablica的存储空间扩展到25个字符。

iezvtpos

iezvtpos2#

在现代C++中,只要你能避免这种情况,使用C风格的数组和索引循环就被认为是不好的做法。所以,举个例子,你可以像这样重写第一个循环:

std::vector<int> tablica;
    tablica.reserve(inputromanum.size()); // This line is not necessary, but it can help optimize memory allocations
    for (char c : inputromanum)
    {
        if (c == 'M' || c == 'm')
        {
            tablica.push_back(1000);
        }
        if (c == 'D' || c == 'd')
        {
            tablica.push_back(500);
        }
        if (c == 'C'|| c == 'c')
        {
            tablica.push_back(100);
        }
        if (c == 'L' || c == 'l')
        {
            tablica.push_back(50);
        }
        if (c == 'X' || c == 'x')
        {
            tablica.push_back(10);
        }
        if (c == 'V' || c == 'v')
        {
            tablica.push_back(5);
        }
        if (c == 'I' || c == 'i')
        {
            tablica.push_back(1);
        }
    }

你会完全避免你的问题。类似的事情也可以用其他循环来做。这种方法也有好处(某种程度上)正确处理输入行有其他符号的情况,这不是罗马数字。在你的版本上试试,你会明白我的意思。
还有一点,当你需要根据一个变量的值做一些不同的事情时,就像你对所有那些if所做的那样,在C/C++中有一个特殊的语句:switch。因此,您可以这样做,而不是使用这些“如果”:

std::vector<int> tablica;
    tablica.reserve(inputromanum.size()); // This line is not necessary, but it can help optimize memory allocations
    for (char c : inputromanum)
    {
        switch(c)
        {
            case 'M':
            case 'm':
                tablica.push_back(1000);
                break;
            case 'D':
            case 'd':
                tablica.push_back(500);
                break;
            case 'C':
            case 'c':
                tablica.push_back(100);
                break;
            case 'L':
            case 'l':
                tablica.push_back(50);
                break;
            case 'X':
            case 'x':
                tablica.push_back(10);
                break;
            case 'V':
            case 'v':
                tablica.push_back(5);
                break;
            case 'I':
            case 'i':
                tablica.push_back(1);
                break;
        }
    }

相关问题