在C+中模拟自顶向下加法时出现分段错误+; 我试图编写一个C++程序,它包含两个包含数字的字符串,并将它们相加以返回一个“和”(这也是一个字符串)。 我用java和python编写了类似的程序,所以我决定学习C++时,我也可以写一些类似的东西。我不知道为什么会收到这些错误,并且在使用Visual Studio或g++编译时没有收到任何错误
以下是主程序中有关函数的参考在C+中模拟自顶向下加法时出现分段错误+; 我试图编写一个C++程序,它包含两个包含数字的字符串,并将它们相加以返回一个“和”(这也是一个字符串)。 我用java和python编写了类似的程序,所以我决定学习C++时,我也可以写一些类似的东西。我不知道为什么会收到这些错误,并且在使用Visual Studio或g++编译时没有收到任何错误,c++,pointers,segmentation-fault,addition,C++,Pointers,Segmentation Fault,Addition,以下是主程序中有关函数的参考 Number base(NULL); Number r = base.addNums("1", "1"); cout << r.toString() << endl; 我写了一些评论,试图解释我在写头文件时的思考过程。有关方法如下: Number addNums(string in1, string in2){ // Calling number 1: X, and number 2: Y const cha
Number base(NULL);
Number r = base.addNums("1", "1");
cout << r.toString() << endl;
我写了一些评论,试图解释我在写头文件时的思考过程。有关方法如下:
Number addNums(string in1, string in2){
// Calling number 1: X, and number 2: Y
const char* x;
const char* y;
x = in1.c_str();
y = in2.c_str();
// Flag for one number having more digits
bool flag = false;
// Flag for X having more digits
bool xIsBigger = false;
// For storing the sum later
string summ = "";
// Check and see if the flags are needed
if (!(strlen(x) == strlen(y))){
flag = true;
if (strlen(x) > strlen(y)){
xIsBigger = true;
}
}
// Prepend the zeroes to the necessary variable
// to make it work as written addition does
if (flag){
if (xIsBigger){
string zeroes;
for (unsigned int i = 0; i < (strlen(x) - strlen(y)); ++i){
zeroes += "0";
}
string newYStr = zeroes + in2;
const char* newY = newYStr.c_str();
// Add zeroes to Y variable
y = newY;
} else{
string zeroes;
for (unsigned int i = 0; i < (strlen(y) - strlen(x)); ++i){
zeroes += "0";
}
string newXStr = zeroes + in1;
const char* newX = newXStr.c_str();
// Add variables to X value
x = newX;
}
}
// If we encounter x + y > 9, we need this
int carry = 0;
// Current digit being processed
char digitX, digitY;
// Digit to be carried
char toCarry;
// Iterate through the number right to left
// to simulate top-down addition
for (int i = strlen(x) - 1; i >= 0; --i){
digitX = x[i];
digitY = y[i];
// If we're carrying a 1, add it to the top number
if (carry > 0){
digitX += 1;
carry = 0;
}
// Add together the two numbers stored in characters
int currentSum = atoi(&digitX) + atoi(&digitY);
// If x + y > 9, we need to carry
if (currentSum > 9){
string sumString = "" + currentSum;
// Max possible is 9 + 9, so we only have to carry 1
carry = 1;
// Add the second digit in the number to the position in the sum
summ = sumString.at(1) + summ;
}
// Didn't need a carry
else{
string sumString = "" + currentSum;
summ = sumString + summ;
}
}
// Return the object containing the sum
return Number(summ);
}
Number addNums(字符串in1,字符串in2){
//呼叫号码1:X和号码2:Y
常量字符*x;
常数char*y;
x=in1.c_str();
y=in2.c_str();
//具有多个数字的一个数字的标志
布尔标志=假;
//具有更多数字的X的标志
布尔=假;
//用于以后存储总和
字符串sum=“”;
//检查并查看是否需要这些标志
如果(!(strlen(x)=strlen(y))){
flag=true;
如果(strlen(x)>strlen(y)){
更大=正确;
}
}
//将零前置到必要的变量
//让它像书面加法一样工作
国际单项体育联合会(旗){
如果(更大){
字符串零;
对于(无符号整数i=0;i<(strlen(x)-strlen(y));+i){
零+=“0”;
}
字符串newYStr=0+in2;
const char*newY=newYStr.c_str();
//向Y变量添加零
y=新的;
}否则{
字符串零;
对于(无符号整数i=0;i<(strlen(y)-strlen(x));+i){
零+=“0”;
}
字符串newXStr=0+in1;
const char*newX=newXStr.c_str();
//将变量添加到X值
x=newX;
}
}
//如果我们遇到x+y>9,我们需要这个
整数进位=0;
//正在处理的当前数字
char digitX,digitY;
//要携带的数字
炭载体;
//从右到左遍历该数字
//模拟自上而下的加法
对于(inti=strlen(x)-1;i>=0;--i){
digitX=x[i];
数字=y[i];
//如果我们携带的是1,则将其添加到顶部数字
如果(进位>0){
digitX+=1;
进位=0;
}
//将以字符形式存储的两个数字相加
int currentSum=atoi(&digitX)+atoi(&digitY);
//如果x+y>9,我们需要携带
如果(当前总和>9){
字符串sumString=“”+currentSum;
//最大可能是9+9,所以我们只需要携带1
进位=1;
//将数字中的第二位数字添加到总和中的位置
sum=sumString.at(1)+sum;
}
//不需要随身携带
否则{
字符串sumString=“”+currentSum;
sum=sumString+sum;
}
}
//返回包含和的对象
返回编号(总和);
}
我对指针的使用还比较陌生,但在学习更多语言和编写这个程序的过程中,通过大量搜索语法和语言细节,我被迫使用指针
非常抱歉,我不能提供更多信息,我感谢任何能帮助我的帮助或批评
提前谢谢 “字符串newYStr”可能超出范围?尝试在顶部声明它,然后检查。这不应标记为
C
,IMHO.Sorry。没有注意到我也把它标记为C。不,你不应该在这个程序中使用指针。到底是什么让你认为你需要它们?你能给我们一个答案吗?你也不需要数组。字符串类型包含您需要的所有内容。
Number addNums(string in1, string in2){
// Calling number 1: X, and number 2: Y
const char* x;
const char* y;
x = in1.c_str();
y = in2.c_str();
// Flag for one number having more digits
bool flag = false;
// Flag for X having more digits
bool xIsBigger = false;
// For storing the sum later
string summ = "";
// Check and see if the flags are needed
if (!(strlen(x) == strlen(y))){
flag = true;
if (strlen(x) > strlen(y)){
xIsBigger = true;
}
}
// Prepend the zeroes to the necessary variable
// to make it work as written addition does
if (flag){
if (xIsBigger){
string zeroes;
for (unsigned int i = 0; i < (strlen(x) - strlen(y)); ++i){
zeroes += "0";
}
string newYStr = zeroes + in2;
const char* newY = newYStr.c_str();
// Add zeroes to Y variable
y = newY;
} else{
string zeroes;
for (unsigned int i = 0; i < (strlen(y) - strlen(x)); ++i){
zeroes += "0";
}
string newXStr = zeroes + in1;
const char* newX = newXStr.c_str();
// Add variables to X value
x = newX;
}
}
// If we encounter x + y > 9, we need this
int carry = 0;
// Current digit being processed
char digitX, digitY;
// Digit to be carried
char toCarry;
// Iterate through the number right to left
// to simulate top-down addition
for (int i = strlen(x) - 1; i >= 0; --i){
digitX = x[i];
digitY = y[i];
// If we're carrying a 1, add it to the top number
if (carry > 0){
digitX += 1;
carry = 0;
}
// Add together the two numbers stored in characters
int currentSum = atoi(&digitX) + atoi(&digitY);
// If x + y > 9, we need to carry
if (currentSum > 9){
string sumString = "" + currentSum;
// Max possible is 9 + 9, so we only have to carry 1
carry = 1;
// Add the second digit in the number to the position in the sum
summ = sumString.at(1) + summ;
}
// Didn't need a carry
else{
string sumString = "" + currentSum;
summ = sumString + summ;
}
}
// Return the object containing the sum
return Number(summ);
}