#include <iostream>
#include<string>
using namespace std;
char IntToChar(int i){
return i+'0';
}
int CharToInt(char c){
return c-'0';
}
string add(string a,string b){
int abufLen=a.length();
cout<<abufLen<<endl;
int bbufLen=b.length();
cout<<bbufLen<<endl;
int* abuf=new int[abufLen];
for(int i=0; i<abufLen; i++){
abuf[i]=CharToInt(a[i]);
}
int* bbuf=new int[bbufLen];
for(int i=0; i<bbufLen; i++){
bbuf[i]=CharToInt(b[i]);
}
int dif=abufLen-bbufLen;
if(dif>0){
cout<<"dif>0 입니다"<<endl;
int* bbuf2= new int[bbufLen+dif];
for(int i=0; i<dif; i++){
bbuf2[i]=0;
}
for(int i=dif; i<bbufLen+dif; i++){
bbuf2[i]=bbuf[i-dif];
}
int NewBufIndex = abufLen+1;
int* NewBuf = new int[NewBufIndex];
for(int i=0; i<NewBufIndex; i++){
NewBuf[i]=0;
}
for(int i=1; i<NewBufIndex; i++){
NewBuf[i]=abuf[i-1]+bbuf2[i-1];
}
for(int i=1; i<NewBufIndex; i++){
if(NewBuf[i]>9){
NewBuf[i-1]++;
}
}
if(NewBuf[0]==0){
cout<<"NewBuf[0]==0입니다"<<endl;
for(int i=0; i<NewBufIndex; i++){
NewBuf[i]=NewBuf[i+1];
}
NewBufIndex--;
cout<<"NewBufIndex:"<<NewBufIndex<<endl;
string NewStr = new char[NewBufIndex];
for(int i=0; i<NewBufIndex; i++){
NewStr[i]=IntToChar(NewBuf[i]);
}
for(int i=NewBufIndex; i<NewStr.length();i++){
NewStr[i]=NULL;
}
cout<<"NewStr 길이:"<<NewStr.length()<<endl;
return NewStr;
}
else{
string NewStr = new char[NewBufIndex];
cout<<NewStr.length()<<endl;
for(int i=0; i<NewBufIndex; i++){
NewStr[i]=IntToChar(NewBuf[i]);
}
for(int i=NewBufIndex; i<NewStr.length();i++){
NewStr[i]=NULL;
}
return NewStr;
}
}
if(dif<0){
cout<<"dif<0 입니다"<<endl;
int dif2=-dif;
int* abuf2= new int[abufLen+dif2];
for(int i=0; i<dif2; i++){
abuf2[i]=0;
}
for(int i=dif2; i<abufLen+dif2; i++){
abuf2[i]=abuf[i-dif2];
}
int NewBufIndex = bbufLen+1;
int* NewBuf = new int[NewBufIndex];
for(int i=0; i<NewBufIndex; i++){
NewBuf[i]=0;
}
for(int i=1; i<NewBufIndex; i++){
NewBuf[i]=abuf2[i-1]+bbuf[i-1];
}
for(int i=1; i<NewBufIndex; i++){
if(NewBuf[i]>9){
NewBuf[i-1]++;
}
}
if(NewBuf[0]==0){
for(int i=0; i<NewBufIndex; i++){
NewBuf[i]=NewBuf[i+1];
}
NewBufIndex--;
string NewStr = new char[NewBufIndex];
for(int i=0; i<NewBufIndex;i++){
NewStr[i]=IntToChar(NewBuf[i]);
}
for(int i=NewBufIndex; i<NewStr.length();i++){
NewStr[i]=NULL;
}
return NewStr;
}
else{
string NewStr=new char[NewBufIndex];
for(int i=0; i<NewBufIndex; i++){
NewStr[i]=IntToChar(NewBuf[i]);
}
for(int i=NewBufIndex; i<NewStr.length();i++){
NewStr[i]=NULL;
}
return NewStr;
}
}
if(dif==0){
cout<<"dif==0 입니다"<<endl;
int NewBufIndex = bbufLen+1;
int* NewBuf = new int[NewBufIndex];
for(int i=0; i<NewBufIndex; i++){
NewBuf[i]=0;
}
for(int i=1; i<NewBufIndex; i++){
NewBuf[i]=abuf[i-1]+bbuf[i-1];
}
for(int i=1; i<NewBufIndex; i++){
if(NewBuf[i]>9){
NewBuf[i-1]++;
}
}
if(NewBuf[0]==0){
for(int i=0; i<NewBufIndex; i++){
NewBuf[i]=NewBuf[i+1];
}
NewBufIndex--;
string NewStr = new char[NewBufIndex];
for(int i=0; i<NewBufIndex;i++){
NewStr[i]=IntToChar(NewBuf[i]);
}
for(int i=NewBufIndex; i<NewStr.length();i++){
NewStr[i]=NULL;
}
return NewStr;
}
else{
string NewStr=new char[NewBufIndex];
for(int i=0; i<NewBufIndex; i++){
NewStr[i]=IntToChar(NewBuf[i]);
}
for(int i=NewBufIndex; i<NewStr.length();i++){
NewStr[i]=NULL;
}
return NewStr;
}
}
}
int main(){
cout<<add("10000000000000000000","123456789012345678900")<<endl;
return 0;
}
///범위를 지정해줬는데 왜 문자열 길이가 늘어날까??
'C++ > 학교 숙제' 카테고리의 다른 글
session_6_ex (0) | 2018.04.22 |
---|---|
string 문자열 뺄셈계산 (0) | 2018.04.15 |
session_4_ex infint.h(객체 맴버변수가 string 일때) (0) | 2018.04.11 |
session_4_ex infint.cpp(객체 맴버변수가 string 일때) (0) | 2018.04.11 |
session_4_ex infint.cpp(객체 맴버변수가 int일때) (0) | 2018.04.10 |
댓글