Skip to content

Instantly share code, notes, and snippets.

@msg555
Created October 21, 2013 20:33
Show Gist options
  • Save msg555/7090496 to your computer and use it in GitHub Desktop.
Save msg555/7090496 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
string S;
for(cin >> S; S != "END"; cin >> S) {
int pos = S.find('+');
if(pos == -1) {
pos = S.find('*');
}
string A = S.substr(0, pos);
string B = S.substr(pos + 1);
reverse(A.begin(), A.end());
reverse(B.begin(), B.end());
vector<int> C(200, 0);
if(S[pos] == '+') {
for(int i = 0; i < A.size(); i++) {
C[i] += A[i] - '0';
}
for(int i = 0; i < B.size(); i++) {
C[i] += B[i] - '0';
}
} else {
for(int i = 0; i < A.size(); i++) {
for(int j = 0; j < B.size(); j++) {
C[i + j] += (A[i] - '0') * (B[j] - '0');
}
}
}
int carry = 0;
for(int i = 0; i < 200; i++) {
C[i] += carry;
carry = C[i] / 10;
C[i] %= 10;
}
while(C.size() > 1 && C.back() == 0) {
C.resize(C.size() - 1);
}
for(int i = C.size() - 1; i >= 0; i--) {
cout << (char)('0' + C[i]);
}
cout << '\n';
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment