# include <iostream.h> # include <string.h> # include <stdlib.h> # include <stdio.h> # include <conio.h> #define MAX_DIGITS 10 #define MAX_LENGTH (MAX_DIGITS+5) //------------------------ Function Prototypes ------------------------// class BigNumber { private: char cSign; char sValue[MAX_LENGTH]; int iLength; public: BigNumber( ); void read( ); void show( ); void plus(BigNumber); void minus(BigNumber); const char* add(char[MAX_LENGTH],char[MAX_LENGTH]); const char* subtract(char[MAX_LENGTH],char[MAX_LENGTH]); }; BigNumber::BigNumber( ) { cSign=\' \'; strset(sValue,NULL); iLength=0; } void BigNumber::read( ) { char sTemp[MAX_LENGTH]; strset(sTemp,NULL); strset(sValue,NULL); gets(sTemp); iLength=strlen(sTemp); if (sTemp[0]==\'+\' || sTemp[0]==\'-\') { cSign=sTemp[0]; for(int i=1; i<iLength; i++) sValue[(i-1)]=sTemp[i]; sValue[(i-1)]=NULL; } else strcpy(sValue,sTemp); iLength=strlen(sValue); for(int j=0; j<iLength; j++) { if (sValue[j] < \'0\' || sValue[j] > \'9\') { cSign=\' \'; strcpy(sValue,\"0\"); iLength=1; break; } } } void BigNumber::show( ) { if (cSign != \' \') cout<<cSign; cout<<sValue; } void BigNumber::plus(BigNumber Temp) { if ((cSign==\'+\' || cSign==\' \') && (Temp.cSign==\'+\' || Temp.cSign==\' \')) { cSign=\' \'; strcpy(sValue,add(sValue,Temp.sValue)); } else if (cSign==\'-\' && Temp.cSign==\'-\') strcpy(sValue,add(sValue,Temp.sValue)); else { if (iLength>Temp.iLength) strcpy(sValue,subtract(sValue,Temp.sValue)); else if (iLength<Temp.iLength) { cSign=Temp.cSign; strcpy(sValue,subtract(Temp.sValue,sValue)); } else { int iFlag=0; for (int i=0; i<iLength; i++) { if (sValue[i]>Temp.sValue[i]) { strcpy(sValue,subtract(sValue,Temp.sValue)); iFlag=1; break; } else if (sValue[i]<Temp.sValue[i]) { cSign=Temp.cSign; strcpy(sValue,subtract(Temp.sValue,sValue)); iFlag=1; break; } } if (iFlag==0) { cSign=\' \'; strcpy(sValue,\"0\"); } } } iLength=strlen(sValue); } void BigNumber::minus(BigNumber Temp) { if ((cSign==\'+\' || cSign==\' \') && (Temp.cSign==\'+\' || Temp.cSign==\' \')) { if (iLength>Temp.iLength) strcpy(sValue,subtract(sValue,Temp.sValue)); else if (iLength<Temp.iLength) { cSign=\'-\'; strcpy(sValue,subtract(Temp.sValue,sValue)); } else { int iFlag=0; for (int i=0; i<iLength; i++) { if (sValue[i]>Temp.sValue[i]) { strcpy(sValue,subtract(sValue,Temp.sValue)); iFlag=1; break; } else if (sValue[i]<Temp.sValue[i]) { cSign=\'-\'; strcpy(sValue,subtract(Temp.sValue,sValue)); iFlag=1; break; } } if (iFlag==0) { cSign=\' \'; strcpy(sValue,\"0\"); } } } else if ((cSign==\'+\' || cSign==\' \') && Temp.cSign==\'-\') { if (iLength>Temp.iLength) strcpy(sValue,add(sValue,Temp.sValue)); else if (iLength<Temp.iLength) { cSign=\'-\'; strcpy(sValue,add(sValue,Temp.sValue)); } else { int iFlag=0; for (int i=0; i<iLength; i++) { if (sValue[i]>Temp.sValue[i]) { strcpy(sValue,add(sValue,Temp.sValue)); iFlag=1; break; } else if (sValue[i]<Temp.sValue[i]) { cSign=\'-\'; strcpy(sValue,add(sValue,Temp.sValue)); iFlag=1; break; } } if (iFlag==0) { cSign=\' \'; strcpy(sValue,add(sValue,Temp.sValue)); } } } else if (cSign==\'-\' && (Temp.cSign==\'+\' || Temp.cSign==\' \')) { if (iLength>Temp.iLength) strcpy(sValue,add(sValue,Temp.sValue)); else if (iLength<Temp.iLength) { cSign=Temp.cSign; strcpy(sValue,add(sValue,Temp.sValue)); } else { int iFlag=0; for (int i=0; i<iLength; i++) { if (sValue[i]>Temp.sValue[i]) { strcpy(sValue,add(sValue,Temp.sValue)); iFlag=1; break; } else if (sValue[i]<Temp.sValue[i]) { cSign=Temp.cSign; strcpy(sValue,add(sValue,Temp.sValue)); iFlag=1; break; } } if (iFlag==0) { cSign=\' \'; strcpy(sValue,add(sValue,Temp.sValue)); } } } iLength=strlen(sValue); } const char* BigNumber::add(char A[MAX_LENGTH],char B[MAX_LENGTH]) { if (strlen(A) < strlen(B)) { strrev(A); while(strlen(A)<strlen(B)) strcat(A,\"0\"); strrev(A); } if (strlen(B) < strlen(A)) { strrev(B); while(strlen(B)<strlen(A)) strcat(B,\"0\"); strrev(B); } int length=strlen(A); int carry=0; int sum=0; int digit_1=0; int digit_2=0; char Sum[5]={NULL}; char Result[MAX_LENGTH]={NULL}; strset(Result,NULL); strrev(A); strrev(B); for(int i=0; i<length; i++) { digit_1=(int(B[i])-48); digit_2=(int(A[i])-48); sum=(digit_1+digit_2+carry); if(sum>9) { sum%=10; carry=1; } else carry=0; itoa(sum,Sum,10); strcat(Result,Sum); } if(carry) strcat(Result,\"1\"); if (strlen(Result)>MAX_DIGITS) strcpy(Result,\"0\"); strrev(Result); return Result; } const char* BigNumber::subtract(char A[MAX_LENGTH],char B[MAX_LENGTH]) { if (strlen(A) < strlen(B)) { strrev(A); while(strlen(A)<strlen(B)) strcat(A,\"0\"); strrev(A); } if (strlen(B) < strlen(A)) { strrev(B); while(strlen(B)<strlen(A)) strcat(B,\"0\"); strrev(B); } int length=strlen(A); int borrow=0; int difference=0; int digit_1=0; int digit_2=0; char Difference[5]={NULL}; char Result[MAX_LENGTH]={NULL}; strset(Result,NULL); strrev(A); strrev(B); for(int i=0;i<length;i++) { digit_1=(int(A[i])-48); digit_2=(int(B[i])-48); difference=(digit_1-digit_2-borrow); if(difference<0) { difference+=10; borrow=1; } else borrow=0; itoa(difference,Difference,10); strcat(Result,Difference); } while(Result[(strlen(Result)-1)]==\'0\') Result[(strlen(Result)-1)]=NULL; strrev(Result); return Result; } //------------------------------ main( ) ------------------------------// int main( ) { clrscr( ); BigNumber A, B; cout<<\"Enter the value of A = \"; A.read( ); cout<<\"Enter the value of B = \"; B.read( ); cout<<\"Enter Operation (+ | -) : \"; char cOperation=getch(); cout<<endl<<endl<<\"A = \"; A.show( ); cout<<endl<<\"B = \"; B.show( ); cout<<endl; if (cOperation==\'+\') { A.plus(B); cout<<endl<<\"Sum (A+B) = \"; A.show( ); } else if (cOperation==\'-\') { A.minus(B); cout<<endl<<\"Difference (A-B) = \"; A.show( ); } else cout<<endl<<\"Invalid Operation!\"; getch( ); return 0; }