Define the class newInt, including:
-
Data member of type int.
-
Overloaded operator '+'. The calculation rule is: add the numbers in the corresponding positions of A and B, and only keep one digit as the number in the corresponding position of the result. For example: 876 + 543 = 319. Note: this operation does not change the value of two operands.
-
Overloads input and output operators, which are used to input and output property values of objects.
-
Parameterless and parameterless constructors.
Input
Line 1, n > 0, indicates the number of test cases.
Each test case consists of two non negative integers separated by spaces.
Output
See the example.
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
HINT
You cannot use characters such as string, char, or string types.
#include <iostream> #include <iomanip> using namespace std; class newInt { private: int x; public: newInt():x(0){} newInt(int a) { x=a; } ~newInt() { } friend ostream &operator<<(ostream &os,newInt&p) { os<<p.x; return os; } friend istream&operator>>(istream&is,newInt&p) { is>>p.x; return is; } friend newInt operator+(const newInt &a,const newInt&b) { newInt p; int a1[100]; int a2[100];//Separate numbers for storing separate bits int a3[100];//Show the final result for(int i=0;i<100;i++) { a1[i]=0; } for(int i=0;i<100;i++) { a2[i]=0; } for(int i=0;i<100;i++) { a3[i]=0; } int count1=0,count2=0; int num1=a.x; int num2=b.x; int i=0; int j=0; while(num1>0) { a1[i++]=num1%10; num1=num1/10; count1++; } while(num2>0) { a2[j++]=num2%10; num2=num2/10; count2++; } int countmax; if(count1>count2) countmax=count1; else countmax=count2; for(int i=0;i<countmax;i++) { if(a1[i]+a2[i]<10) a3[i]=a1[i]+a2[i]; else { a3[i]=a1[i]+a2[i]-10; } } int sum=0; for(int i=countmax;i>=0;i--) { sum=10*sum; sum+=a3[i]; } p.x=sum; return p; } }; 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; }