我正在为Project Euler编写一个程序来添加2 ^ 1000的所有数字。 到目前为止,我已经能够跟踪程序段分割错误,当它达到5位数字并尝试在函数carry()的第61行上将一个元素推送到向量上时。
#include <iostream> #include <vector> #include <string> using namespace std; class MegaNumber { vector<int> data; //carries an array of numbers under ten, would be char but for simplicity's sake void multiplyAssign(int operand, int index); //the recursive function called by the *= operator void carry(int index);//if one of the data entries becomes more than ten call this function public: void printNumber(); //does what it says on the can void operator*=(MegaNumber operand); void operator*=(int operand); void operator+=(int operand); MegaNumber(string); unsigned long int AddAllDigits();//returns the value of all of the digits summed }; MegaNumber::MegaNumber(string operand) { for(int i= operand.size()-1; i>=0;i--) //run it into the memory smallest digit first { data.push_back(operand[i]-48); //converts a text char to an int } } void MegaNumber::printNumber() { int temp = data.size(); for(unsigned int i=(temp); i>0;--i) { cout << (int)data[i-1]; } } void MegaNumber::operator*=(int operand) { if(operand > 9) { cout << "function does not yet deal with large ints than 9"; } else multiplyAssign(operand, 0); } void MegaNumber::multiplyAssign(int operand, int index) { data[index] *=operand; if(index<data.size()) multiplyAssign(operand, index+1); if(data[index] > 9) carry(index); } void MegaNumber::carry(int index) { int temp = (data[index] / 10); //calculate the amount to carry if(data.size()==index+1) { data.push_back(temp);//if there is no upper digit push it onto the stack } else { data[index+1]+=temp; //else add it to the next digit if(data[index+1]>9) carry(index+1); //rinse and repeat } data[index]-=temp*10; //remove what's been carried } unsigned long int MegaNumber::AddAllDigits() //does what it says on the can { unsigned long int Dagger = 0; for(int i=0; i<data.size();i++) Dagger+=data[i]; return Dagger; } int main() { MegaNumber A("2"); A.printNumber(); cout << "\n"; for(unsigned int i=0; i<20; i++) A*=2; A.printNumber(); cout << "\n"; cout << A.AddAllDigits() << "\n"; cout << "Hello world!" << endl; return 0; }什么可能造成这种情况?
I'm working on a program for Project Euler to add all the digits of 2^1000. So far I've been able to track the program segmentation faults when it reaches around 5 digits and tries to push a one onto the vector at line 61 in the function carry().
#include <iostream> #include <vector> #include <string> using namespace std; class MegaNumber { vector<int> data; //carries an array of numbers under ten, would be char but for simplicity's sake void multiplyAssign(int operand, int index); //the recursive function called by the *= operator void carry(int index);//if one of the data entries becomes more than ten call this function public: void printNumber(); //does what it says on the can void operator*=(MegaNumber operand); void operator*=(int operand); void operator+=(int operand); MegaNumber(string); unsigned long int AddAllDigits();//returns the value of all of the digits summed }; MegaNumber::MegaNumber(string operand) { for(int i= operand.size()-1; i>=0;i--) //run it into the memory smallest digit first { data.push_back(operand[i]-48); //converts a text char to an int } } void MegaNumber::printNumber() { int temp = data.size(); for(unsigned int i=(temp); i>0;--i) { cout << (int)data[i-1]; } } void MegaNumber::operator*=(int operand) { if(operand > 9) { cout << "function does not yet deal with large ints than 9"; } else multiplyAssign(operand, 0); } void MegaNumber::multiplyAssign(int operand, int index) { data[index] *=operand; if(index<data.size()) multiplyAssign(operand, index+1); if(data[index] > 9) carry(index); } void MegaNumber::carry(int index) { int temp = (data[index] / 10); //calculate the amount to carry if(data.size()==index+1) { data.push_back(temp);//if there is no upper digit push it onto the stack } else { data[index+1]+=temp; //else add it to the next digit if(data[index+1]>9) carry(index+1); //rinse and repeat } data[index]-=temp*10; //remove what's been carried } unsigned long int MegaNumber::AddAllDigits() //does what it says on the can { unsigned long int Dagger = 0; for(int i=0; i<data.size();i++) Dagger+=data[i]; return Dagger; } int main() { MegaNumber A("2"); A.printNumber(); cout << "\n"; for(unsigned int i=0; i<20; i++) A*=2; A.printNumber(); cout << "\n"; cout << A.AddAllDigits() << "\n"; cout << "Hello world!" << endl; return 0; }What may be causing this?
最满意答案
void MegaNumber::multiplyAssign(int operand, int index) { data[index] *=operand; if(index<data.size()) multiplyAssign(operand, index+1); if(data[index] > 9) carry(index); }index是基于0的,而data.size()是基于1的,也就是说,意思是data.size()返回的数字1大于最大的有效index 。 所以看起来你的意图是
if( index < data.size() - 1) multiplyAssign(operand, index+1);然后它工作。 PS将你的代码分解成行,任何必须维护你的代码的人都会感谢你:
if (index < data.size() - 1) { multiplyAssign(operand, index + 1); } void MegaNumber::multiplyAssign(int operand, int index) { data[index] *=operand; if(index<data.size()) multiplyAssign(operand, index+1); if(data[index] > 9) carry(index); }index is 0 based, while data.size() is 1 based so to say, meaning data.size() returns number 1 greater than the largest valid index. So looks like you intention was
if( index < data.size() - 1) multiplyAssign(operand, index+1);Then it works. P.S. break your code into lines, whoever has to maintain your code will thank you for that:
if (index < data.size() - 1) { multiplyAssign(operand, index + 1); }在向量我正在为Project Euler编写一个程序来添加2 ^ 1000的所有数字。 到目前为止,我已经能够跟踪程序段分割错误,当它达到5位数字并尝试在函数carry()的第61行上将一个元素推送到向量上时。
#include <iostream> #include <vector> #include <string> using namespace std; class MegaNumber { vector<int> data; //carries an array of numbers under ten, would be char but for simplicity's sake void multiplyAssign(int operand, int index); //the recursive function called by the *= operator void carry(int index);//if one of the data entries becomes more than ten call this function public: void printNumber(); //does what it says on the can void operator*=(MegaNumber operand); void operator*=(int operand); void operator+=(int operand); MegaNumber(string); unsigned long int AddAllDigits();//returns the value of all of the digits summed }; MegaNumber::MegaNumber(string operand) { for(int i= operand.size()-1; i>=0;i--) //run it into the memory smallest digit first { data.push_back(operand[i]-48); //converts a text char to an int } } void MegaNumber::printNumber() { int temp = data.size(); for(unsigned int i=(temp); i>0;--i) { cout << (int)data[i-1]; } } void MegaNumber::operator*=(int operand) { if(operand > 9) { cout << "function does not yet deal with large ints than 9"; } else multiplyAssign(operand, 0); } void MegaNumber::multiplyAssign(int operand, int index) { data[index] *=operand; if(index<data.size()) multiplyAssign(operand, index+1); if(data[index] > 9) carry(index); } void MegaNumber::carry(int index) { int temp = (data[index] / 10); //calculate the amount to carry if(data.size()==index+1) { data.push_back(temp);//if there is no upper digit push it onto the stack } else { data[index+1]+=temp; //else add it to the next digit if(data[index+1]>9) carry(index+1); //rinse and repeat } data[index]-=temp*10; //remove what's been carried } unsigned long int MegaNumber::AddAllDigits() //does what it says on the can { unsigned long int Dagger = 0; for(int i=0; i<data.size();i++) Dagger+=data[i]; return Dagger; } int main() { MegaNumber A("2"); A.printNumber(); cout << "\n"; for(unsigned int i=0; i<20; i++) A*=2; A.printNumber(); cout << "\n"; cout << A.AddAllDigits() << "\n"; cout << "Hello world!" << endl; return 0; }什么可能造成这种情况?
I'm working on a program for Project Euler to add all the digits of 2^1000. So far I've been able to track the program segmentation faults when it reaches around 5 digits and tries to push a one onto the vector at line 61 in the function carry().
#include <iostream> #include <vector> #include <string> using namespace std; class MegaNumber { vector<int> data; //carries an array of numbers under ten, would be char but for simplicity's sake void multiplyAssign(int operand, int index); //the recursive function called by the *= operator void carry(int index);//if one of the data entries becomes more than ten call this function public: void printNumber(); //does what it says on the can void operator*=(MegaNumber operand); void operator*=(int operand); void operator+=(int operand); MegaNumber(string); unsigned long int AddAllDigits();//returns the value of all of the digits summed }; MegaNumber::MegaNumber(string operand) { for(int i= operand.size()-1; i>=0;i--) //run it into the memory smallest digit first { data.push_back(operand[i]-48); //converts a text char to an int } } void MegaNumber::printNumber() { int temp = data.size(); for(unsigned int i=(temp); i>0;--i) { cout << (int)data[i-1]; } } void MegaNumber::operator*=(int operand) { if(operand > 9) { cout << "function does not yet deal with large ints than 9"; } else multiplyAssign(operand, 0); } void MegaNumber::multiplyAssign(int operand, int index) { data[index] *=operand; if(index<data.size()) multiplyAssign(operand, index+1); if(data[index] > 9) carry(index); } void MegaNumber::carry(int index) { int temp = (data[index] / 10); //calculate the amount to carry if(data.size()==index+1) { data.push_back(temp);//if there is no upper digit push it onto the stack } else { data[index+1]+=temp; //else add it to the next digit if(data[index+1]>9) carry(index+1); //rinse and repeat } data[index]-=temp*10; //remove what's been carried } unsigned long int MegaNumber::AddAllDigits() //does what it says on the can { unsigned long int Dagger = 0; for(int i=0; i<data.size();i++) Dagger+=data[i]; return Dagger; } int main() { MegaNumber A("2"); A.printNumber(); cout << "\n"; for(unsigned int i=0; i<20; i++) A*=2; A.printNumber(); cout << "\n"; cout << A.AddAllDigits() << "\n"; cout << "Hello world!" << endl; return 0; }What may be causing this?
最满意答案
void MegaNumber::multiplyAssign(int operand, int index) { data[index] *=operand; if(index<data.size()) multiplyAssign(operand, index+1); if(data[index] > 9) carry(index); }index是基于0的,而data.size()是基于1的,也就是说,意思是data.size()返回的数字1大于最大的有效index 。 所以看起来你的意图是
if( index < data.size() - 1) multiplyAssign(operand, index+1);然后它工作。 PS将你的代码分解成行,任何必须维护你的代码的人都会感谢你:
if (index < data.size() - 1) { multiplyAssign(operand, index + 1); } void MegaNumber::multiplyAssign(int operand, int index) { data[index] *=operand; if(index<data.size()) multiplyAssign(operand, index+1); if(data[index] > 9) carry(index); }index is 0 based, while data.size() is 1 based so to say, meaning data.size() returns number 1 greater than the largest valid index. So looks like you intention was
if( index < data.size() - 1) multiplyAssign(operand, index+1);Then it works. P.S. break your code into lines, whoever has to maintain your code will thank you for that:
if (index < data.size() - 1) { multiplyAssign(operand, index + 1); }
发布评论