此代碼添加兩個整數與加法操作,包括進位過程, 我在這裏有一個錯誤,但我不知道這裏到底是什麼! 該程序的目的是使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;
}
你的錯誤是哪一行?它感覺就像它可能在這裏:'結果+ =更大[o];' – mcalex
我輸入2個字符串後出現錯誤,它彈出一個窗口告訴我「表達式:字符串下標超出範圍」 – SUE