#include <iostream>
#include<string>
using namespace std;
char IntToChar(int i){
return i+'0';
}
int CharToInt(char c){
return c-'0';
}
// 단순 빼기// 순차적인 빼기다// 밖에서 '-' 버리고 들어옴
string Min(string a,string b){
int abufLen = a.length();
int bbufLen = b.length();
int* abuf = new int[abufLen];
int* bbuf = new int[bbufLen]; //메모리 할당
int dif=abufLen-bbufLen; // 자릿수 차이
cout<<"a.length= "<<a.length()<<endl;
cout<<"b.length="<<b.length()<<endl;
if(dif==0){ ///자릿수 같을때
cout<<"dif==0입니다."<<endl;
int NewBufIndex = bbufLen;
cout<<"NewBufIndex="<<NewBufIndex<<endl;
int* NewBuf = new int[NewBufIndex]; // 버퍼 인덱스 와 메모리할당
string NewStr= new char[NewBufIndex];
cout<<"NewStr의 길이:"<<NewStr.length()<<endl;
int* temp1 = new int[NewBufIndex];
int* temp2 = new int[NewBufIndex];
for(int i=0; i<NewBufIndex;i++){ // 자리마다 빼기 실행
abuf[i]=CharToInt(a[i]);
bbuf[i]=CharToInt(b[i]);
NewBuf[i]=abuf[i]-bbuf[i];
cout<<"abuf["<<i<<"]-bbuf["<<i<<"]="<<abuf[i]<<"-"<<bbuf[i]<<endl;
cout<<"NewBuf["<<i<<"]="<<NewBuf[i]<<endl;
}
for(int i=NewBufIndex-1; i>=1; i--){ // 마이너스 없애줌 맨 앞자리 - or 0
if(NewBuf[i]<0){
NewBuf[i]+=10;
NewBuf[i-1]--;
cout<<"마이너스 없애줌 "<<endl;
}
}
for(int i=0; i<NewBufIndex; i++){
cout<<"NewBuf["<<i<<"]="<<NewBuf[i]<<endl;
}
if(NewBuf[0]<0){ // 맨 앞자리 - 일때
cout<<"맨 앞자리 - 입니다."<<endl;
temp1[0]=-NewBuf[0]; // 앞에 자리 뽑아서 -붙혀주고 temp1에 저장
for(int i=1; i<NewBufIndex; i++){
temp1[i]=0;
cout<<"temp1["<<i<<"]="<<temp1[i]<<endl;
}
temp2[0]=0; // 그 뒤는 temp2에 저장
for(int i=1; i<NewBufIndex; i++){
temp2[i]=NewBuf[i];
cout<<"temp2["<<i<<"]="<<temp2[i]<<endl;
}
for(int i=0; i<NewBufIndex; i++){ // 맨 앞자리 - 뒷자리
NewBuf[i]=temp1[i]-temp2[i];
cout<<"NewBuf["<<i<<"]="<<NewBuf[i]<<endl;
}
for(int i=NewBufIndex-1; i>=1; i--){ // 마이너스 없애줌 맨 앞자리
if(NewBuf[i]<0){
NewBuf[i]+=10;
NewBuf[i-1]--;
}
}
for(int i=0; i<NewBufIndex; i++){
cout<<"NewBuf["<<i<<"]="<<NewBuf[i]<<endl;
}//////////////////////////////////////////////////////
int check =0;
bool sign = true;
while(sign==true){
if(NewBuf[check]!=0){
sign =false;
string temp = new char[NewBufIndex-check];
for(int i=check;i<NewBufIndex; i++){
temp[i-check] = IntToChar(NewBuf[i]);
}
for(int i=NewBufIndex-check; i<temp.length(); i++){
temp[i]=NULL;
}
return "-"+temp;
}else{
check++;
if(check==NewBufIndex){
return "0";
}
}
}
}
else{ // 앞자리 0일때
cout<<"앞자리 가 0 입니다."<<endl;
int check =0;
bool sign = true;
while(sign==true){
if(NewBuf[check]!=0){
sign =false;
string temp = new char[NewBufIndex-check];
for(int i=check;i<NewBufIndex; i++){
temp[i-check] = IntToChar(NewBuf[i]);
}
for(int i=NewBufIndex-check; i<temp.length(); i++){
temp[i]=NULL;
}
return temp;
}else{
check++;
if(check==NewBufIndex){
return "0";
}
}
}
}
}
else {
if(dif>0){ // a가 클때
cout<<"dif>0 입니다."<<endl;
int NewBufIndex = abufLen;
int* NewBuf = new int[NewBufIndex]; // 버퍼 인덱스 와 메모리할당
string NewStr= new char[NewBufIndex];
int* temp1 = new int[NewBufIndex];
int* temp2 = new int[NewBufIndex];
for(int i=0; i<dif; i++){
bbuf[i]=0;
}
for(int i=dif; i<NewBufIndex; i++){
bbuf[i]=CharToInt(b[i-dif]);
}
for(int i=0; i<NewBufIndex;i++){ // 자리마다 빼기 실행
abuf[i]=CharToInt(a[i]);
NewBuf[i]=abuf[i]-bbuf[i];
cout<<"abuf["<<i<<"]+bbuf["<<i<<"]="<<abuf[i]<<"-"<<bbuf[i]<<endl;
cout<<"NewBuf["<<i<<"]="<<NewBuf[i]<<endl;
}
for(int i=NewBufIndex-1; i>=1; i--){ // 마이너스 없애줌 맨 앞자리 - or 0
if(NewBuf[i]<0){
NewBuf[i]+=10;
NewBuf[i-1]--;
}
}
cout<<endl;
for(int i=0; i<NewBufIndex;i++){
cout<<"NewBuf["<<i<<"]="<<NewBuf[i]<<endl;
}
if(NewBuf[0]<0){ // 맨 앞자리 - 일때
cout<<"맨 앞자리 - 입니다."<<endl;
temp1[0]=-NewBuf[0]; // 앞에 자리 뽑아서 -붙혀주고 temp1에 저장
for(int i=1; i<NewBufIndex; i++){
temp1[i]=0;
cout<<"temp1["<<i<<"]="<<temp1[i]<<endl;
}
temp2[0]=0; // 그 뒤는 temp2에 저장
for(int i=1; i<NewBufIndex; i++){
temp2[i]=NewBuf[i];
cout<<"temp2["<<i<<"]="<<temp2[i]<<endl;
}
for(int i=0; i<NewBufIndex; i++){ // 맨 앞자리 - 뒷자리
NewBuf[i]=temp1[i]-temp2[i];
cout<<"NewBuf["<<i<<"]="<<NewBuf[i]<<endl;
}
for(int i=NewBufIndex-1; i>=1; i--){ // 마이너스 없애줌 맨 앞자리
if(NewBuf[i]<0){
NewBuf[i]+=10;
NewBuf[i-1]--;
}
}
for(int i=0; i<NewBufIndex; i++){
cout<<"NewBuf["<<i<<"]="<<NewBuf[i]<<endl;
}//////////////////////////////////////////////////////
int check =0;
bool sign = true;
while(sign==true){
if(NewBuf[check]!=0){
sign =false;
string temp = new char[NewBufIndex-check];
for(int i=check;i<NewBufIndex; i++){
temp[i-check] = IntToChar(NewBuf[i]);
}
for(int i=NewBufIndex-check; i<temp.length(); i++){
temp[i]=NULL;
}
return "-"+temp;
}else{
check++;
if(check==NewBufIndex){
return "0";
}
}
}
}
else{ // 앞자리 0일때
cout<<"앞자리 가 0 입니다."<<endl;
int check =0;
bool sign = true;
while(sign==true){
if(NewBuf[check]!=0){
sign =false;
string temp = new char[NewBufIndex-check];
for(int i=check;i<NewBufIndex; i++){
temp[i-check] = IntToChar(NewBuf[i]);
}
for(int i=NewBufIndex-check; i<temp.length(); i++){
temp[i]=NULL;
}
return temp;
}else{
check++;
if(check==NewBufIndex){
return "0";
}
}
}
}
}
else{ // b가 클때
cout<<"dif<0 입니다."<<endl;
int NewBufIndex = bbufLen;
int* NewBuf = new int[NewBufIndex]; // 버퍼 인덱스 와 메모리할당
string NewStr= new char[NewBufIndex];
int* temp1 = new int[NewBufIndex];
int* temp2 = new int[NewBufIndex];
int dif2 = -dif;
for(int i=0; i<dif2; i++){
abuf[i]=0;
}
for(int i=dif2; i<NewBufIndex; i++){
abuf[i]=CharToInt(a[i-dif2]);
}
for(int i=0; i<NewBufIndex;i++){ // 자리마다 빼기 실행
bbuf[i]=CharToInt(b[i]);
NewBuf[i]=abuf[i]-bbuf[i];
cout<<"abuf["<<i<<"]+bbuf["<<i<<"]="<<abuf[i]<<"-"<<bbuf[i]<<endl;
cout<<"NewBuf["<<i<<"]="<<NewBuf[i]<<endl;
}
for(int i=NewBufIndex-1; i>=1; i--){ // 마이너스 없애줌 맨 앞자리 - or 0
if(NewBuf[i]<0){
NewBuf[i]+=10;
NewBuf[i-1]--;
}
}
cout<<endl;
for(int i=0; i<NewBufIndex;i++){
cout<<"NewBuf["<<i<<"]="<<NewBuf[i]<<endl;
}
if(NewBuf[0]<0){ // 맨 앞자리 - 일때
cout<<"맨 앞자리 - 입니다."<<endl;
temp1[0]=-NewBuf[0]; // 앞에 자리 뽑아서 -붙혀주고 temp1에 저장
for(int i=1; i<NewBufIndex; i++){
temp1[i]=0;
cout<<"temp1["<<i<<"]="<<temp1[i]<<endl;
}
temp2[0]=0; // 그 뒤는 temp2에 저장
for(int i=1; i<NewBufIndex; i++){
temp2[i]=NewBuf[i];
cout<<"temp2["<<i<<"]="<<temp2[i]<<endl;
}
for(int i=0; i<NewBufIndex; i++){ // 맨 앞자리 - 뒷자리
NewBuf[i]=temp1[i]-temp2[i];
cout<<"NewBuf["<<i<<"]="<<NewBuf[i]<<endl;
}
for(int i=NewBufIndex-1; i>=1; i--){ // 마이너스 없애줌 맨 앞자리
if(NewBuf[i]<0){
NewBuf[i]+=10;
NewBuf[i-1]--;
}
}
for(int i=0; i<NewBufIndex; i++){
cout<<"NewBuf["<<i<<"]="<<NewBuf[i]<<endl;
}//////////////////////////////////////////////////////
int check =0;
bool sign = true;
while(sign==true){
if(NewBuf[check]!=0){
sign =false;
string temp = new char[NewBufIndex-check];
for(int i=check;i<NewBufIndex; i++){
temp[i-check] = IntToChar(NewBuf[i]);
}
for(int i=NewBufIndex-check; i<temp.length(); i++){
temp[i]=NULL;
}
return "-"+temp;
}else{
check++;
if(check==NewBufIndex){
return "0";
}
}
}
}
}
}
}
int main(){
cout<<Min("100","1000000000100")<<endl;
return 0;
}
/* 자릿수 같을때
진짜 같을 때 통과
a>b 일때
0 ,,, 1 ,,,,00000 ,,,0001 통과
a<b 일때 통과
자릿수 a> b 보다 클때 통과
자릿수 a<b 보다 작을때
*/
'C++ > 학교 숙제' 카테고리의 다른 글
session_6_ex 답 (0) | 2018.04.22 |
---|---|
session_6_ex (0) | 2018.04.22 |
string 문자열 덧셈계산 (0) | 2018.04.14 |
session_4_ex infint.h(객체 맴버변수가 string 일때) (0) | 2018.04.11 |
session_4_ex infint.cpp(객체 맴버변수가 string 일때) (0) | 2018.04.11 |
댓글