language: C++ 4.7.2 (gcc-4.7.2)
date: 148 days 10 hours ago
link:
visibility: private
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#include<iostream>
#include<string>
#include<algorithm>
#include<iomanip>
#include<vector>
#define sz size()
using namespace std;
string str1Copy(""),strAns;
vector<string> store,storeAdd;
void getSum(string str1,string str2) {
    strAns.erase();
    if(str2=="0") {
        strAns=str1;
        return;
    }
    int carry=0,i=0,j=0,sum=0;
    for(i=str1.sz-1,j=str2.sz-1;i>=0&&j>=0;i--,j--) {
        sum=(str1[i]-'0')+(str2[j]-'0')+carry;
        carry=sum==0?0:sum/10;
        sum%=10;
        strAns=char(sum+'0')+strAns;
    }
    while(i>=0) {
        sum=carry+(str1[i]-'0');
        carry=sum==0?0:sum/10;
        sum%=10;
        strAns=char(sum+'0')+strAns;
        i--;
    }
    while(j>=0) {
        sum=carry+(str2[j]-'0');
        carry=sum==0?0:sum/10;
        sum%=10;
        strAns=char(sum+'0')+strAns;
        j--;
    }
    strAns=carry>0?char(carry+'0')+strAns:strAns;
}
 
void getDiff(string str1, string str2) {
    strAns.erase();
    int carry=0,i=0,j,diff=0,temp=0;
    for(i=str1.sz-1,j=str2.sz-1;i>=0&&j>=0;i--,j--) {
        diff=(str1[i]-'0')-(str2[j]-'0');
        if(diff>=0) {
        strAns=char(diff+'0')+strAns;
        continue;
        }
        diff+=10;
        strAns=char(diff+'0')+strAns;
        temp=i-1;
        while(str1[temp]=='0') {
            str1[temp]='9';
            temp--;
        }
        str1[temp]--;
    }
    while(i>=0) {
        strAns=str1[i]+strAns;
        i--;
    }
    for(i=0;strAns[i]=='0';)
            strAns.erase(i,1);
}
 
int main() {
    int setwLen=0,cases=0,i=0,pos=0,j=0,caseVar=0;
    string str(""),str1(""),str2(""),strDash(""),tempStr1("");
    cin>>cases;
    for(caseVar=0;caseVar<cases;caseVar++) {
        strDash.erase();
        str.erase();
        str1.erase();
        str2.erase();
        cin>>str;
        pos=str.find_first_of("+-*");
        if(str[pos]=='+') {
            str1=str.substr(0,pos);
            str2=str.substr(pos+1);
            getSum(str1,str2);
            setwLen=max(strAns.sz,max(str2.sz+1,str1.sz));
            cout<<setw(setwLen)<<str1<<"\n";
            cout<<setw(setwLen-str2.sz)<<'+'<<str2<<"\n";
            //setwLen=max(strAns.sz,str2.sz+1);
            //cout<<setfill('-')<<setw(setwLen)<<"-"<<"\n";
            for(i=0;i<max(strAns.sz,str2.sz+1);i++)
                strDash+='-';
            cout<<setw(setwLen)<<strDash<<"\n";
            setwLen=max(strAns.sz,max(str2.sz+1,str1.sz));
            cout<<setfill(' ')<<setw(setwLen)<<strAns<<"\n\n";
        }
        else if(str[pos]=='-')  {
            strDash.erase();
            str1Copy.erase();
            str1Copy=str1=str.substr(0,pos);
            str2=str.substr(pos+1);
            getDiff(str1Copy,str2);
            setwLen=max(strAns.sz,max(str2.sz+1,str1.sz));
            cout<<setw(setwLen)<<str1<<"\n";
            cout<<setw(setwLen-str2.sz)<<'-'<<str2<<"\n";
            for(i=0;i<max(strAns.sz,str2.sz+1);i++)
                strDash+='-';
            cout<<setw(setwLen)<<strDash<<"\n";
            setwLen=max(strAns.sz,max(str2.sz+1,str1.sz));
            cout<<setfill(' ')<<setw(setwLen)<<strAns<<"\n\n";
        }
        else if(str[pos]=='*') {
            tempStr1.erase();
            strDash.erase();
            store.erase(store.begin(),store.end());
            storeAdd.erase(store.begin(),store.end());
            str1=str.substr(0,pos);
            str2=str.substr(pos+1);
            tempStr1=str1;
            for(i=str2.sz-1;i>=0;i--) {
                if(str2[i]=='0') {
                    tempStr1="0";
                    goto skip;
                }
                else if(str2[i]=='1') {
                    tempStr1=str1;
                    goto skip;
                }
                for(j=0,tempStr1=str1;j<(str2[i]-'0')-1;j++) {
                getSum(tempStr1,str1);
                tempStr1=strAns;
                }
                skip:
                store.push_back(tempStr1);
                storeAdd.push_back(tempStr1);
            }
            for(i=1;i<store.sz;i++)
                for(j=0;j<i;j++)
                    storeAdd[i]+='0';
            tempStr1=storeAdd[0];
            for(i=1;i<store.size();i++) {
                getSum(tempStr1,storeAdd[i]);
                tempStr1=strAns;
            }
            setwLen=max(tempStr1.sz,max(str1.sz,str2.sz+1));
            cout<<setw(setwLen)<<str1<<"\n";
            cout<<setw(setwLen-str2.sz)<<'*'<<str2<<"\n";
            for(i=0;i<max(str2.sz+1,store[0].sz);i++)
                strDash+='-';
            cout<<setw(setwLen)<<strDash<<"\n";
            for(i=0;i<store.size();i++)
                cout<<setw(setwLen-i)<<store[i]<<"\n";
            if(store.sz==1)
                continue;
            strDash.erase();
            for(i=0;i<max(tempStr1.sz,store[store.sz-1].sz);i++)
                strDash+='-';
            cout<<setw(setwLen)<<strDash<<"\n";
            cout<<setw(setwLen)<<tempStr1<<"\n\n";
        }
    }
    return 0;
}
 
 
 
  • upload with new input
  • result: Success     time: 0.01s    memory: 2828 kB     returned value: 0

    1
    0+0
     0
    +0
    --
     0