Problem D: 新奇的加法运算 Time Limit: 1 Sec Memory Limit: 128 MB Submit: 2153 Solved: 1312 [Submit][Stat

Problem D: 新奇的加法运算

Time Limit: 1 Sec   Memory Limit: 128 MB
Submit: 2153   Solved: 1312
[ Submit][ Status][ Web Board]

Description

定义类newInt,包括:

1. int类型的数据成员。

2. 重载运算符“+”。计算规则为:将A、B对应位置上的数字相加,只保留个位数作为结果的对应位置上的数字。比如:876 + 543 = 319。注意:该运算不改变两个操作数的值。

3. 重载输入和输出运算符,用于输入和输出对象的属性值。

4. 无参构造函数和带参构造函数。

Input

第1行N>0,表示测试用例数量。

每个测试用例包括2个非负整数,用空格隔开。

Output

见样例。

Sample Input

4876 543999 99999 1999199 88

Sample Output

876 + 543 = 319999 + 9999 = 98889 + 1999 = 1998199 + 88 = 177

HINT

 不能使用string、char等字符或字符串类型。

Append Code

不要用数组直接输出,举个栗子,999+111,如果是数组最后是000.总之一般就是前置零的问题啦,原本打算要数据看看的,后来想了想数组输出的区别,菜啊......
#include<bits/stdc++.h>
using namespace std;
class newInt
{
private:
    int chengyuan[20];
    int length;
    int sum;
public:
    newInt()
    {
        for(int i=0; i<20; i++)
            chengyuan[i] = 0;
        length = 0;
    }
    newInt(int a)
    {
        int i=0;
        int x=a;
        while(x)
        {
            int c=x%10;
            chengyuan[i++] = c;
            x/=10;
        }
    }
    friend istream& operator>>(istream& in,newInt& abc);
    friend ostream& operator<<(ostream& ou,newInt& abc);
    newInt operator+(newInt& another)
    {
        newInt abc;
        abc.length = max(another.length,length);
        for(int i=0; i<abc.length; i++)
        {
            abc.chengyuan[i] = chengyuan[i]+another.chengyuan[i];
            if(abc.chengyuan[i]>=10)abc.chengyuan[i]-=10;
        }
        abc.sum = 0;
        for(int i=abc.length;i>=0;i--)
            {
                abc.sum=10*abc.sum;
                abc.sum += abc.chengyuan[i];
            }
        return abc;
    }
};
istream& operator>>(istream& in,newInt& abc)
{
    for(int i=0; i<20; i++)
        abc.chengyuan[i] = 0;
    in>>abc.sum;
    int x =abc.sum;
    int i=0;
    while(x)
    {
        int c=x%10;
        abc.chengyuan[i++] = c;
        x/=10;
    }
    abc.length = i;
    return in;
}
ostream& operator<<(ostream& ou,newInt& abc)
{
    ou<<abc.sum;
    return ou;
}
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;
}
/**************************************************************
    Problem: 1829
    User: 201701060928
    Language: C++
    Result: Accepted
    Time:0 ms
    Memory:1268 kb
****************************************************************/




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值