std :: _ throw_out_of_range无处不在

时间:2015-09-27 11:09:05

标签: c++ stderr palindrome discrete-mathematics number-theory

我是c ++的绝对初学者。从字面上看。这只是一个星期。 今天我正在编写一个程序来测试需要多少次迭代来制作一定数量的回文。 这是代码:

#include <iostream>
#include <string>
#include <algorithm>

/*  This program calculates the steps needed
    to make a certain number palindromic.
    It is designed to output the values for
    numbers 1 to 1000
*/
using namespace std;

class number
{
public:
    string value;
    void reverse();
};

void number::reverse()
{
    std::reverse(value.begin(),value.end());
}

void palindrome(number num)
{
    string n=num.value;
    number reversenum, numsum, numsumreverse;
    reversenum=num;
    reversenum.reverse();
    numsum.value=num.value;
    numsumreverse.value=numsum.value;
    numsumreverse.reverse();
    int i=0;
    while (numsum.value.compare(numsumreverse.value) !=0)
    {
        reversenum=num;
        reversenum.reverse();
        numsum.value=to_string(stoll(num.value,0,10)+stoll(reversenum.value,0,10));
        numsumreverse.value=numsum.value;
        numsumreverse.reverse();
        num.value=numsum.value;
        i++;
    }
    cout << "The number " << n << " becomes palindromic after " << i << " steps : " << num.value << endl;
}

int main()
{
    number temp;
    int i;
    for (i=1; i<1001; i++)
    {
        temp.value=to_string(i);
        palindrome(temp);
    }
    return 0;
}

对于高达195的数字,它会顺利进行。但是,如果是196,我会收到错误。 它说:

  

在抛出&#39; std :: out_of_range&#39;的实例后终止调用   what():stoll

我无法弄清楚该怎么做。我尝试从196开始,但错误仍然存​​在。任何帮助将不胜感激。 :)

更新:这次我尝试使用ttmath库来做到这一点。但是啊!它再次停在195,甚至没有报错!我可能会做一些愚蠢的事情。任何意见将不胜感激。这是更新后的代码:

#include <iostream>
#include <string>
#include <algorithm>
#include <ttmath/ttmath.h>

/*  This program calculates the steps needed
    to make a certain number palindromic.
    It is designed to output the values for
    numbers 1 to 1000
*/
using namespace std;

class number
{
public:
    string value;
    void reverse();
};

void number::reverse()
{
    std::reverse(value.begin(),value.end());
}

template <typename NumTy>
string String(const NumTy& Num)
{
    stringstream StrStream;
    StrStream << Num;
    return (StrStream.str());
}

void palindrome(number num)
{
    string n=num.value;
    number reversenum, numsum, numsumreverse;
    reversenum=num;
    reversenum.reverse();
    numsum.value=num.value;
    numsumreverse.value=numsum.value;
    numsumreverse.reverse();
    ttmath::UInt<100> tempsum, numint, reversenumint;
    int i=0;
    while (numsum.value.compare(numsumreverse.value) !=0)
    {
        reversenum=num;
        reversenum.reverse();
        numint=num.value;
        reversenumint=reversenum.value;
        tempsum=numint+reversenumint;
        numsum.value=String<ttmath::UInt<100> >(tempsum);
        numsumreverse.value=numsum.value;
        numsumreverse.reverse();
        num.value=numsum.value;
        i++;
    }
    cout << "The number " << n << " becomes palindromic after " << i << " steps : " << num.value << endl;
}

int main()
{
    number temp;
    int i;
    for (i=196; i<1001; i++)
    {
        temp.value=to_string(i);
        palindrome(temp);
    }
    return 0;
}

更新:它已经解决了。一些研究表明196可能是Lychrel Number。在暗示ttmath库之后我得到的结果只是让我放心我的算法有效。我已经尝试过所有高达10000的数字,它给出了完美的结果。这是最终的代码:

#include <iostream>
#include <string>
#include <algorithm>
#include <ttmath/ttmath.h>
#include <limits>

/*  This program calculates the steps needed
    to make a certain number palindromic.
    It is designed to output the values for
    numbers inside a desired range
*/
using namespace std;
string LychrelList;
int LychrelCount=0;

class number
{
public:
    string value;
    void reverse();
};

void number::reverse()
{
    std::reverse(value.begin(),value.end());
}

template <typename NumTy>
string String(const NumTy& Num)
{
    stringstream StrStream;
    StrStream << Num;
    return (StrStream.str());
}

void palindrome(number num)
{
    string n=num.value;
    number reversenum, numsum, numsumreverse;
    reversenum=num;
    reversenum.reverse();
    numsum.value=num.value;
    numsumreverse.value=numsum.value;
    numsumreverse.reverse();
    ttmath::UInt<100> tempsum, numint, reversenumint;
    int i=0;
    while ((numsum.value.compare(numsumreverse.value) !=0) && i<200)
    {
        reversenum=num;
        reversenum.reverse();
        numint=num.value;
        reversenumint=reversenum.value;
        tempsum=numint+reversenumint;
        numsum.value=String<ttmath::UInt<100> >(tempsum);
        numsumreverse.value=numsum.value;
        numsumreverse.reverse();
        num.value=numsum.value;
        i++;
    }
    if (i<200) cout << "The number " << n << " becomes palindromic after " << i << " steps : " << num.value << endl;
    else
    {
        cout << "A solution for " << n << " could not be found!!!" << endl;
        LychrelList=LychrelList+n+" ";
        LychrelCount++;
    }
}

int main()
{
    cout << "From where to start?" << endl << ">";
    int lbd,ubd;
    cin >> lbd;
    cout << endl << "And where to stop?" << endl <<">";
    cin >> ubd;
    cout << endl;
    number temp;
    int i;
    for (i=lbd; i<=ubd; i++)
    {
        temp.value=to_string(i);
        palindrome(temp);
    }
    if (LychrelList.compare("") !=0) cout << "The possible Lychrel numbers found in the range are:" << endl << LychrelList << endl << "Total - " << LychrelCount;
    cout << endl << endl << "Press ENTER to end the program...";
    cin.ignore(numeric_limits<streamsize>::max(), '\n');
    string s;
    getline(cin,s);
    cout << "Thanks for using!";
    return 0;
}

这是一个非常棒的社区。特别感谢Marco A。 :)

再次更新:我设计了自己的add()函数,该函数削减了程序对外部库的依赖性。它导致了更小的可执行文件和更快的性能。这是代码:

#include <iostream>
#include <string>
#include <algorithm>
#include <limits>

/*  This program calculates the steps needed
    to make a certain number palindromic.
    It is designed to output the values for
    numbers inside a desired range
*/
using namespace std;
string LychrelList;
int LychrelCount=0;

string add(string sA, string sB)
{
    int iTemp=0;
    string sAns;
    int k=sA.length()-sB.length();
    int i;
    if (k>0){for (i=0;i<k;i++) {sB="0"+sB;}}
    if (k<0) {for (i=0;i<-k;i++) {sA="0"+sA;}}
    for (i=sA.length()-1;i>=0;i--)
    {
        iTemp+=sA[i]+sB[i]-96;
        if (iTemp>9)
        {
            sAns=to_string(iTemp%10)+sAns;
            iTemp/=10;
        }
        else
        {
            sAns=to_string(iTemp)+sAns;
            iTemp=0;
        }
    }
    if (iTemp>0) {sAns=to_string(iTemp)+sAns;}
    return sAns;
}

void palindrome(string num)
{
    string n=num;
    string reversenum, numsum, numsumreverse;
    numsum=num;
    numsumreverse=numsum;
    reverse(numsumreverse.begin(),numsumreverse.end());
    int i=0;
    while ((numsum.compare(numsumreverse) !=0) && i<200)
    {
        reversenum=num;
        reverse(reversenum.begin(),reversenum.end());
        numsum=add(num,reversenum);
        numsumreverse=numsum;
        reverse(numsumreverse.begin(),numsumreverse.end());
        num=numsum;
        i++;
    }
    if (i<200) cout << "The number " << n << " becomes palindromic after " << i << " steps : " << num << endl;
    else
    {
        cout << "A solution for " << n << " could not be found!!!" << endl;
        LychrelList=LychrelList+n+" ";
        LychrelCount++;
    }
}

int main()
{
    cout << "From where to start?" << endl << ">";
    int lbd,ubd;
    cin >> lbd;
    cout << endl << "And where to stop?" << endl <<">";
    cin >> ubd;
    cout << endl;
    string temp;
    int i;
    for (i=lbd; i<=ubd; i++)
    {
        temp=to_string(i);
        palindrome(temp);
    }
    if (LychrelList.compare("") !=0) cout << "The possible Lychrel numbers found in the range are:" << endl << LychrelList << endl << "Total - " << LychrelCount;
    cout << endl << endl << "Press ENTER to end the program...";
    cin.ignore(numeric_limits<streamsize>::max(), '\n');
    string s;
    getline(cin,s);
    cout <<endl << "Thanks for using!";
    return 0;
}

你们这里帮助我找到了自己的方式。感谢大家。 :)

2 个答案:

答案 0 :(得分:2)

您已经溢出 long long,因为num.valuereversenum.value的最后两个有效值是7197630720180367016和6107630810270367917,它们加在一起是方式高于long long的最大尺寸(我机器上的9223372036854775807)。这将产生负值并破坏您对stoll

的下一次调用
  如果转换后的值超出结果类型的范围,或者基础函数(std :: strtol或std :: strtoll)将errno设置为ERANGE,则抛出

std :: out_of_range。

reference

  • 如果您尝试获取下一个最小的回文,则应使用其他方法,例如I explained here

您可以在这里找到 Live Example

  • 如果您希望/必须继续使用您的方法,您应该在字符串上手动添加或使用 bigint 库(再次查看here并修改{ {1}}功能符合您的喜好)

答案 1 :(得分:1)

来自http://www.cplusplus.com/reference/string/stoll/

  

如果读取的值超出可表示值的范围超长,则抛出out_of_range异常。

ll数据类型无法处理字符串长度。我的调试器告诉我196值的中断 std::stoll (__str=\"9605805010994805921-\", __idx=0x0, __base=10)

漫长的太长了。

您可能希望对字符串本身进行添加,而无需使用数字类型。