i have operator - error

This is a discussion on i have operator - error within the C++ Programming forums, part of the General Programming Boards category; i have practice CBigInt, but it dont run in operator -, when it down - up, can you help me? ...

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    41

    i have operator - error

    i have practice CBigInt, but it dont run in operator -, when it down - up, can you help me? thanks .
    Code:
    #include "BigInt.h"
    #include "iostream"
    #include "string"
    using namespace std;
    BigInt::BigInt(){
    
    	for(int i=0;i<N;i++){
    		buffer[i]=0;
    	}
    }
    //
    istream &operator >>(istream &inp,BigInt &num){
    	inp >>num.buffer;
    	num.Format();
    	return inp;
    }
    //
    ostream &operator <<(ostream &out,BigInt &num){
    	//out<<num.buffer;
    	for(int i=0;i<N;i++){
    		cout<<(int)num.buffer[i];
    	}
    	return out;
    
    }
    
    
    void BigInt::Format(){
    	int len=strlen(buffer);
    	for(int i=1;i<=len;i++){
    		buffer[N-i]=buffer[len-i];//go to end array
    	}
    	for(int i=N-len-1;i>=0;i--){//convert all var still ->0;
    		buffer[i]='0';
    	}
    	for(int i=0;i<N;i++){
    		buffer[i]-=48;
    	}
    }
    //
    BigInt &BigInt::operator =(BigInt &num){
    	for(int i=0;i<N;i++){
    		buffer[i]=num.buffer[i];
    	}
    	return *this;
    }
    //
    BigInt BigInt::operator +(BigInt &num){
    	BigInt C;
    	int mem=0;
    	for(int i=N-1;i>=0;i--){
    		C.buffer[i]=buffer[i]+num.buffer[i]+mem;
    		if(C.buffer[i]>10){
    			C.buffer[i]-=10;
    			mem=1;
    		}
    		else mem=0;
    	
    	}
    	return C;
    }
    
    BigInt BigInt::operator -(BigInt &num){
    	BigInt h;int mem=0;
    	if(len>=num.len){
    	for(int i=N-1;i>=0;i--){
    		h.buffer[i]=buffer[i]-num.buffer[i]-mem;
    		if(buffer[i]>=num.buffer[i]){
    			h.buffer[i]=buffer[i]-(num.buffer[i]+mem);
    			mem=0;
    		}
    		if(buffer[i]<num.buffer[i])
    		{
    			h.buffer[i]=(buffer[i]+10)-num.buffer[i]-mem;
    			mem=1;
    		}
    	
    	}
    	}
    	if(len<num.len){
    		for(int i=N-1;i>=0;i--){
    		h.buffer[i]=num.buffer[i]-buffer[i]-mem;
    		if(buffer[i]>=num.buffer[i]){
    			h.buffer[i]=num.buffer[i]-(buffer[i]+mem);
    			mem=0;
    		}
    		if(buffer[i]<num.buffer[i])
    		{
    			h.buffer[i]=(num.buffer[i]+10)-buffer[i]-mem;
    			mem=1;
    		}
    	
    		}
    	}
    	return h;
    }
    Code:
    #include "iostream"
    using namespace std;
    #define N 10
    class BigInt{
    public :
    	friend istream &operator >>(istream &inp,BigInt &num);
    	friend	ostream &operator <<(ostream &out,BigInt &num);
    	char buffer[N];
    	BigInt();
    	void Format();
    	int len;
    	BigInt &operator =(BigInt &num);
    	BigInt operator +(BigInt &num);
    	BigInt operator -(BigInt &num);
    	
    
    };
    
    //
    Code:
    #include "iostream"
    #include "BigInt.h"
    
    using namespace std;
    void main(){
    	BigInt A;
    	cout<<"Input A:";
    	cin>>A;
    	//cout<<A;
    	BigInt B;
    	//B=A;cout<<endl;
    	cout<<"Input B:";
    	cin>>B;
    
    	BigInt C;
    	//C=A+B;
    	
    	C=A-B;
    	cout<<C;
    	//BigInt K;
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    I'm not sure what you mean. Does it compile but not give the right answer?

    You should look at your borrow -- you only check whether the top number is >= your bottom number without regard of whether there had been a borrow. Since you compute h.buffer before the check, just use that result -- if it's negative, borrow 10.

  3. #3
    Registered User
    Join Date
    Sep 2009
    Posts
    41
    I'm not sure what you mean. Does it compile but not give the right answer?

    You should look at your borrow -- you only check whether the top number is >= your bottom number without regard of whether there had been a borrow. Since you compute h.buffer before the check, just use that result -- if it's negative, borrow 10.
    thanks but, i cann't performace operator - with down up if input number after > input number before

  4. #4
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    839
    maybe you should try the google translator. it is very difficult to understand you.

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    We don't know how your class is designed -- you do exactly the same thing when the second number is bigger as when the first number is bigger. You will probably need to do something different, to deal with the fact that it is a negative number (unless you plan to store the number in 10's complement or something).

  6. #6
    Registered User
    Join Date
    Sep 2009
    Posts
    41
    thanks for you

  7. #7
    Registered User
    Join Date
    Sep 2009
    Posts
    41
    I have one operator *, it have run, but error logic, after input 2 BigInt, can u help me, instructor for me. thanks
    Code:
    #include "BigInt.h"
    #include "iostream"
    #include "string"
    using namespace std;
    BigInt::BigInt(){
    
    	for(int i=0;i<N;i++){
    		buffer[i]=0;
    	}
    }
    //
    istream &operator >>(istream &inp,BigInt &num){
    	inp >>num.buffer;
    	num.Format();
    	return inp;
    }
    //
    ostream &operator <<(ostream &out,BigInt &num){
    	//out<<num.buffer;
    	for(int i=0;i<N;i++){
    		cout<<(int)num.buffer[i];
    	}
    	return out;
    
    }
    
    
    //int compare(char[] _m, BigInt _n){
    	//if(_m.len>_n.len)
    	//int len=strlen(buffer);
    
    //
    void BigInt::Format(){
    	len=strlen(buffer);
    	for(int i=1;i<=len;i++){
    		buffer[N-i]=buffer[len-i];//go to end array
    	}
    	for(int i=N-len-1;i>=0;i--){//convert all var still ->0;
    		buffer[i]='0';
    	}
    	for(int i=0;i<N;i++){
    		buffer[i]-=48;
    	}
    }
    //
    int Compare(char *A, char *B) {
    	// scan from left to right
    	// which one bigger is bigger
    	for (int i=0; i<N; i++) {
    		if (A[i] >B[i])
    			return 1;
    		if (B[i] > A[i])
    			return -1;
    	}
    	// otherwise, equal
    	return 0;
    }
    
    
    
    //
    BigInt &BigInt::operator =(BigInt &num){
    	for(int i=0;i<N;i++){
    		buffer[i]=num.buffer[i];
    	}
    	return *this;
    }
    //
    BigInt BigInt::operator +(BigInt &num){
    	BigInt C;
    	int mem=0;
    	for(int i=N-1;i>=0;i--){
    		C.buffer[i]=buffer[i]+num.buffer[i]+mem;
    		if(C.buffer[i]>10){
    			C.buffer[i]-=10;
    			mem=1;
    		}
    		else mem=0;
    	
    	}
    	return C;
    }
    
    
    
    BigInt BigInt::operator *(BigInt &num){
    	//int _Mul;
    	int carry=0,count=0;
    	BigInt MUL,SUM;
    	if(Compare(buffer,num.buffer)==1){
    		for(int j=0;j<num.len;j++){
    			for(int i=N-1;i>=0;i--){
    				MUL.buffer[i]=num.buffer[i]*buffer[i]+carry;
    				if(MUL.buffer[i]>=10){
    					MUL.buffer[i]=MUL.buffer[i]%10;
    					carry=MUL.buffer[i]/10;
    				}
    				else
    				{
    					MUL.buffer[i]=MUL.buffer[i];
    					carry=0;
    				}
    				count =j;
    
    			}
    			
    		for (int k=0; k<count; k++)
    				move(MUL.buffer);
    			SUM=SUM+MUL;
    
    		}
    	}
    	return SUM;
    }
    void BigInt::move(char* A){
    	int k=N-strlen(A);
    	for(int i=k;i<N-1;i++){
    		A[i-1]=A[i];
    		A[i]=0;
    	}
    }

  8. #8
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,661
    Subtraction is just like adding a negative to a positive number, so why not just do it that way in the class? Addition works, doesn't it?

  9. #9
    Registered User
    Join Date
    Sep 2009
    Posts
    41
    i like it simple, i have do function Subtraction.
    but now i have problem operator *. can you help me?

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You would have to tell us what the problem is for us to help you.

  11. #11
    Registered User
    Join Date
    Sep 2009
    Posts
    41
    its operator *, multiplication, it have run, but errror logic, i dont know, it err where.

    Code:
    BigInt BigInt::operator *(BigInt &num){
    	//int _Mul;
    	int _y=0;
    	int carry=0,count=0;
    	BigInt MUL,SUM;
    	if(Compare(buffer,num.buffer)==1){
    		while(_y<N-num.len){
    		for(int j=N-1;j>N-num.len;j--){
    			for(int i=N-1;i>=N-len;i--){
    				MUL.buffer[i]=num.buffer[j]*buffer[i]+carry;
    				if(MUL.buffer[i]>=10){
    					MUL.buffer[i]=MUL.buffer[i]%10;
    					carry=MUL.buffer[i]/10;
    				}
    				else
    				{
    					//MUL.buffer[i]=MUL.buffer[i];
    					carry=0;
    				}
    					count =j;
    			}
    		for (int k=0; k<_y; k++)
    				move(MUL.buffer);
    			SUM=SUM+MUL;
    			_y++;
    		}
    	}
    	}
    	return SUM;
    }
    void BigInt::move(char* A){
    	int k=N-strlen(A);
    	for(int i=k;i<N-1;i++){
    		A[i-1]=(int)A[i];
    		A[i]=0;
    	}
    }

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21