C++ experiment---the novel addition operation

Novel addition operation

Description
defines the class newInt, including:
data members of type int.
Overload operator "+". The calculation rule is: add the numbers in the corresponding positions of A and B, and only keep the single digit as the number in the corresponding position of the result. For example: 876 + 543 = 319. Note: This operation does not change the values ​​of the two operands.
Overload input and output operators for input and output object attribute values.
No parameter constructor and parameter constructor.

Input
row 1 N>0, which means the number of test cases.
Each test case includes 2 non-negative integers, separated by spaces. See the example for
Output
.
Sample Input

4
876 543
999 9999
9 1999
199 88

Sample Output

876 + 543 = 319
999 + 9999 = 9888
9 + 1999 = 1998
199 + 88 = 177

Title given code

int main()
{
    
    
    int cases;
    newInt a, b, c;
    cin>>cases;
    for (int i = 0; i < cases; i++)
    {
    
    
        cin>>a>>b;
        c = a + b;
        cout<<a<<" + "<<b<<" = "<<c<<endl;
    }
    return 0;
}

code:

#include<iostream>
#include<math.h>
using namespace std;

class newInt{
    
    
	int data;
public:
	newInt(int D){
    
    //代参构造函数
		data=D;
	}
	
	newInt(){
    
    //空参构造函数
		data=0;
	}
	
	newInt operator +(newInt &B){
    
    //重载‘+’运算符
		int x=data;
		int y=B.data;
		int s=0;
		
		if(x>y){
    
    
			int v_x=x;
			int len=0;
			
			while(v_x){
    
    
				len++;
				v_x/=10;
			}
			if(len==0)len=1;
			
			for(int i=1;i<=len;i++){
    
    
				s+=(x%10+y%10)%10*pow(10,i-1);
				x/=10;
				y/=10;
			}
		}else{
    
    
			int v_y=y;
			int len=0;
			
			while(v_y){
    
    
				len++;
				v_y/=10;
			}
			if(len==0)len=1;
			
			for(int i=1;i<=len;i++){
    
    
				s+=(x%10+y%10)%10*pow(10,i-1);
				x/=10;
				y/=10;
			}	
		}
		return newInt(s);
	}
	
	friend istream& operator >>(istream &is,newInt &B){
    
    //友元重载  >> 
		is>>B.data;
		return is;
	}
	
	friend ostream& operator <<(ostream &os,const newInt &B){
    
    //友元重载  << 
		os<<B.data;
		return os;
	}
};

int main()
{
    
    
    int cases;
    newInt a, b, c;
    cin>>cases;
    for (int i = 0; i < cases; i++)
    {
    
    
        cin>>a>>b;
        c = a + b;
        cout<<a<<" + "<<b<<" = "<<c<<endl;
    }
    return 0;
}

										***重点!!!:***

The general form of overload>> :

friend istream& operator >>(istream &is,class &A){
    
    
	is>>A.data;
	return is;
}

The general form of overload <<:

friend ostream& operator <<(ostream &os,const class &A){
    
    
	os<<A.data;
	return os;
}

The general form of the overloaded operator'+':

className operator+(className &A){
    
    
	//注意:最后要返回一个相加后的对象
}

The overloaded operator'-' is similar!

The general form of overloaded assignment operator:

className& operator=(const className &r){
    
    
	//....
}

Prefix increment operator definition: (form ++i)

CDemo& operator++();
CDemo& operator–();

Suffix increment operator definition: (form like i++)

ClassName operator++(int);
ClassName operator–(int);

Operators that cannot be overloaded:
1) Ternary operator: ?:
2) Member operator:.
3) Member pointer operator: *
4) Scope operator : ::
5) sizeof operator

Guess you like

Origin blog.csdn.net/timelessx_x/article/details/115034610