这里有一个错误:"string subscript is out of range"
There is an Error here : "string subscript is out of range"
此代码正在添加两个整数,其中包括附加过程,包括随身携带过程,我在这里有一个错误,但我不知道这里是什么!该程序的目的是使C 要做一个"大于INT大的大小",并进行加法过程。.我真的需要帮助!谢谢 !
#include "stdafx.h"
#include <iostream>
#include <string>
#include <math.h>
using namespace std;
class BigDecimalInt
{
protected:
int num1;
public:
string s1;
string getstring()
{
return s1;
}
void setstring(string s2)
{
s1=s2;
}
BigDecimalInt (string decStr){s1=decStr;}
BigDecimalInt (int decInt); // Initialize from integer
BigDecimalInt operator+ ();
BigDecimalInt operator- (BigDecimalInt anotherDec);
};
BigDecimalInt operator+ (BigDecimalInt firstDec,BigDecimalInt secDec)
{
int size,diff,carry=0,x,y,z,counter=0;
BigDecimalInt temp("");
string c1,c2,result,smaller,bigger;
c1=firstDec.getstring();
c2=secDec.getstring();
if(c1.length()<c2.length())
{
smaller = c1;
bigger = c2;
}
else
{
smaller = c2;
bigger = c1;
}
diff = bigger.length()-smaller.length();
size = smaller.length();
for(int i=size-1;i>=0;i--)
{
y = int(bigger[i+diff]);
y-=48;
x = int(smaller[i]);
x-=48;
z = x+y+carry;
if(z<=9)
{
result+=char(z+48);
carry = 0;
}
else
{
result+=char(z+38);
carry = 1;
}
}
while((carry==1)&&(counter<bigger.length()))
{
counter = size;
x = int(bigger[counter])-48+carry;
if(x<=9)
{
result+=char(x+48);
carry = 0;
}
else
{
result+=char(x+38);
carry = 1;
}
counter++;
}
if(counter==bigger.length())
{
if(carry==1)
result+=char(49);
}
else
for(int o=counter;o>=0;o++)
result+=bigger[o];
temp.s1=result;
return temp;
}
int _tmain(int argc, _TCHAR* argv[])
{
string k1,k2;
cout<<"Enter first num : n";
cin>>k1;
BigDecimalInt c1(k1);
cout<<"Enter second num : n";
cin>>k2;
BigDecimalInt c2(k2);
BigDecimalInt c3("");
c3=c1+c2;
cout<<c3.s1;
system("pause");
return 0;
}
循环以不同的
脱颖而出for(int o=counter;o>=0;o++)
所有其他人都在计算 down 零,但这似乎正在计算。
如果您使用bigger.length()
的counter == bigger.length()
bigger
,而最后一个有效索引是bigger.length() - 1
。
相关文章:
- Why is UINT32_MAX + 1 = 0?
- C++错误:"error: int aaa::bbb is protected within this context"
- 创建具有 new in 函数和"this is nullptr"异常的对象
- IN, OUT, INOUT Parameters
- 使用 cmake 的 Linux 终端上的"Conversion to non-scalar type is requested"错误
- 即使使用调试编译标志,表达式也是"optimized out"
- C++双重释放或损坏(out)
- Is !NaN not a NaN?
- 将 out/in out 参数与 if/switch 的 init 语句一起使用
- Directx 11 - CompileFromFile() is not compiling
- Centos7 g++ "to_string is not in a member of std"
- "Unable to start debugging. No process is associated with this object." - 在Visual Studio Code中使用GDB
- Where is std::hardware_destructive_interference_size?
- 在命令行上将输入显示到数组中时如何删除 ./a.out?
- 从 MSVC14 切换到 MSVC16 会导致"compiler is out of heap space (C1060)"错误
- 庞大的初始化列表,如何修复"fatal error C1060: compiler is out of heap space"
- 在析构函数中调用"delete"运算符时"compiler is out of heap space"编译器错误
- 在 Eclipse 中使用 GDB 时出错:"Command '-list-features' is timed out"
- 这里有一个错误:"string subscript is out of range"
- VC++ Winforms自动生成的项目无法调试VS2010:"This Project is out of Date"