help me with some test cases!
here’s my code:
#include <bits/stdc++.h>
using namespace std;
void add(string s)
{
string a;
string b;
for(int i = 0; s[i] != ‘+’; ++i)
a.insert(a.end(), 1, s[i]);
for(int i = a.length() + 1; i < s.length(); ++i)
b.insert(b.end(), 1, s[i]);
int x, y;
int sum = 0;
string ans;
int carry = 0;
string c;
for(int i = a.length() - 1, j = b.length() - 1; i >= 0; --i, --j)
{
if(j >= 0)
{
x = a[i] - 48;
y = b[j] - 48;
sum = x + y + carry;
while(sum > 0)
{
ans.insert(ans.begin(), 1, (sum % 10) + 48);
sum = sum / 10;
}
if(i == 0 && j == 0)
{
if(ans.length() > 1)
{
c.insert(0 , 1, ans[1]);
c.insert(0 , 1, ans[0]);
}
else
c.insert(0, 1, ans[0]);
}
else if(ans.length() > 1)
{
c.insert(0, 1, ans[1]);
carry = ans[0] - 48;
}
else
{
carry = 0;
c.insert(0, 1, ans[0]);
}
ans.erase(ans.begin(), ans.end());
// cout << x << " " << y << " " << carry << " " << c << endl;
// system(“pause”);
}
else
{
x = a[i] - 48;
sum = x + carry;
while(sum > 0)
{
ans.insert(ans.begin(), 1, (sum % 10) + 48);
sum = sum / 10;
}
if(i == 0 && ans.length() > 1)
{
c.insert(0, 1, ans[1]);
c.insert(0, 1, ans[0]);
}
else if(ans.length() > 1)
{
c.insert(0, 1, ans[1]);
carry = ans[0] - 48;
}
else
{
carry = 0;
c.insert(0, 1, ans[0]);
}
ans.erase(ans.begin(), ans.end());
}
// cout << x << " " << y << " " << carry << " " << c << endl;
// system(“pause”);
}
int count = 0;
for(int i = 0; i < a.length(); ++i)
{
if(a[0] == ‘0’ && b[0] == ‘0’)
count++;
else break;
}
if(count == a.length())
{
for(int i = 0; i < a.length(); ++i)
c.insert(0, 1, ‘0’);
}
//cout << c;
b.insert(0, 1, ‘+’);
if(a.length() == b.length() - 1)
{
string dash(b.length(), ‘-’);
cout << setw(b.length()) << a << endl;
cout << setw(b.length()) << b << endl;
cout << setw(b.length()) << dash << endl;
cout << setw(b.length()) << c << endl;
}
else
{
string dash(a.length(), ‘-’);
cout << setw(a.length()) << a << endl;
cout << setw(a.length()) << b << endl;
cout << setw(a.length()) << dash << endl;
cout << setw(a.length()) << c << endl;
}
cout << endl;
}
void sub(string s)
{
string x;
string a;
string b;
string c;
for(int i = 0; s[i] != ‘-’; ++i)
a.insert(a.end(), 1, s[i]);
x.append(a);
// cout << a;
for(int i = a.length() + 1; i < s.length(); ++i)
b.insert(b.end(), 1, s[i]);
// cout << endl << b;
for(int i = a.length() - 1, j = b.length() - 1; i >= 0; --i, --j)
{
int x, y;
int sub;
if(j >= 0)
{
x = a[i] - 48;
y = b[j] - 48;
if(x < y)
{
sub = (x - y) + 10;
if(a[i - 1] == ‘0’)
{
a[i - 1] = ‘9’;
a[i - 2] -= 1;
}
else
a[i - 1] -= 1;
}
else
sub = x - y;
c.insert(0, 1,sub + 48);
}
else
{
c.insert(0, 1,a[i]);
}
//cout << x << " " << y << " " << sub << " " << c << endl;
//cout << c;
}
// cout << c;
// cout << endl << a;
b.insert(0, 1, ‘-’);
if(c[0] == ‘0’)
{
while(c[0] == ‘0’)
{
if(c.length() == 1)
break;
else
c.erase(c.begin());
}
}
int max;
if(a.length() > c.length())
max = a.length();
else
max = c.length();
if(x.length() == b.length() - 1)
{
string dash(b.length(), ‘-’);
cout << setw(b.length()) << x << endl;
cout << setw(b.length()) << b << endl;
cout << setw(b.length()) << dash << endl;
cout << setw(b.length()) << c << endl;
}
else
{
string dash(max, ‘-’);
cout << setw(x.length()) << x << endl;
cout << setw(x.length()) << b << endl;
cout << setw(x.length()) << dash << endl;
cout << setw(x.length()) << c << endl;
}
cout << endl;
}
void mul(string s)
{
string a;
string b;
for(int i = 0; s[i] != ‘’; ++i)
a.insert(a.end(), 1, s[i]);
for(int i = a.length() + 1; i < s.length(); ++i)
b.insert(b.end(), 1, s[i]);
// cout << a;
// cout << b;
int x, y;
int mul;
int carry = 0;
string ans;
//system(“pause”);
string c[b.length()];
string d[b.length()];
// cout << b.length();
// system(“pause”);
for(int i = 0; i < b.length(); ++i)
{
for(int j = 0; j < a.length() + b.length(); ++j)
{
d[i].insert(0, 1, ‘0’);
}
// cout << d[i] << endl;
}
// system(“pause”);
// int k = 0;
// int l = 0;
for(int i = b.length() - 1; i >= 0; --i)
{
carry = 0;
for(int j = a.length() - 1; j >=0; --j)
{
// l = (c[i].length() - 1) + k;
x = a[j] - 48;
y = b[i] - 48;
mul = (x * y) + carry;
if(mul !=0)
{
while(mul > 0)
{
ans.insert(0, 1, (mul % 10) + 48);
mul = mul / 10;
}
}
else
ans.insert(0, 1, mul + 48);
if(j == 0 && ans.length() > 1)
{
c[i].insert(0, 1, ans[1]);
c[i].insert(0, 1, ans[0]);
// c[i][l] = ans[0];
// l–;
// c[i][l] = ans[1];
// l–;
}
else if(ans.length() > 1)
{
c[i].insert(0, 1, ans[1]);
// c[i][l] = ans[1];
// l–;
carry = ans[0] - 48;
}
else
{
carry = 0;
c[i].insert(0, 1, ans[0]);
// c[i][l] = ans[0];
// l–;
}
ans.erase(ans.begin(), ans.end());
}
//k–;
//cout << c[i] << endl;
}
int k = 0;
int l;
int count = 0;
for(int i = b.length() - 1; i >= 0; --i)
{
l = (d[i].length() - 1) + k;
for(int j = c[i].length() - 1; j >= 0; --j)
{
d[count][l] = c[i][j];
l–;
}
count++;
// cout << d[i] << endl;
k–;
}
// for(int i = 0; i < b.length(); ++i)
// cout << d[i] << endl;
int sum = 0;
string main;
carry = 0;
for(int i = d[0].length() - 1; i >= 0; --i)
{
sum = 0;
for(int j = 0; j < b.length(); ++j)
{
x = d[j][i] - 48;
sum += x;
// cout << x << endl;
// system(“pause”);
}
sum += carry;
// cout << sum << endl;
// system(“pause”);
if(sum > 0)
{
while(sum > 0)
{
// cout << sum % 10 << endl;
ans.insert(0, 1, (sum % 10) + 48);
sum /= 10;
}
}
else
ans.insert(0, 1, sum + 48);
// cout << ans << endl;
// system(“pause”);
if(i == 0 && ans.length() > 1)
{
main.insert(0, 1, ans[1]);
main.insert(0, 1, ans[0]);
}
else if(ans.length() > 1)
{
main.insert(0, 1,ans[1]);
carry = ans[0] - 48;
}
else
{
carry = 0;
main.insert(0, 1, ans[0]);
}
// cout << sum << " " << ans << " " << main << endl;
// system(“pause”);
ans.erase(ans.begin(), ans.end());
}
for(int i = b.length() - 1; i >= 0; --i)
{
while(c[i][0] == ‘0’)
{
if(c[i].length() == 1)
break;
c[i].erase(c[i].begin());
}
}
while(main[0] == ‘0’)
{
main.erase(main.begin());
}
// cout << endl << main;
b.insert(0, 1, '’);
if(b.length() == 2)
{
string dash1(main.length(), ‘-’);
cout << setw(main.length()) << a << endl;
cout << setw(main.length()) << b << endl;
cout << setw(main.length()) << dash1 << endl;
cout << setw(main.length()) << main << endl;
}
else
{
string dash1(b.length(), ‘-’);
string dash2(main.length(), ‘-’);
cout << setw(main.length()) << a << endl;
cout << setw(main.length()) << b << endl;
cout << setw(main.length()) << dash1 << endl;
int w = 0;
for(int i = b.length() - 2; i >= 0; --i)
{
cout << setw(main.length() + w) << c[i] << endl;
w–;
}
cout << setw(main.length()) << dash2 << endl;
cout << setw(main.length()) << main << endl;
}
cout << endl;
}
int main(void)
{
int t;
cin >> t;
string s[t];
for(int i = 0; i < t; ++i)
{
cin >> s[i];
}
for(int i = 0; i < t; ++i)
{
for(int j = 0; j < s[i].length(); ++j)
{
switch(s[i][j])
{
case ‘+’:
add(s[i]);
break;
case ‘-’:
sub(s[i]);
break;
case ‘*’:
mul(s[i]);
break;
}
}
}
return 0;
}