Last active
May 10, 2018 06:54
-
-
Save minhducsun2002/023827f11766da8bef24a534248dae72 to your computer and use it in GitHub Desktop.
Arithmetic operations
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <bits/stdc++.h> | |
char to_char(int i) | |
{ | |
switch (i) | |
{ | |
case 0: return '0'; case 1: return '1'; case 2: return '2'; | |
case 3: return '3'; case 4: return '4'; case 5: return '5'; | |
case 6: return '6'; case 7: return '7'; case 8: return '8'; case 9: return '9'; | |
} | |
} | |
int to_digit(char c) | |
{ | |
switch (c) | |
{ | |
case '0': return 0; case '1': return 1; case '2': return 2; | |
case '3': return 3; case '4': return 4; case '5': return 5; | |
case '6': return 6; case '7': return 7; case '8': return 8; case '9': return 9; | |
} | |
} | |
using namespace std; | |
typedef long long int llint; | |
void standardify(string &str1, string &str2) //pad strings with zeroes | |
{ | |
if (str1.size() == str2.size()) return; | |
if (str1.size() < str2.size()) str1.insert(str1.begin(),str2.length() - str1.length(), '0'); | |
else str2.insert(str2.begin(), str1.length() - str2.length(), '0'); | |
} | |
string add(string str1, string str2) | |
{ | |
standardify(str1, str2); | |
string answer = ""; | |
int carry = 0; | |
for (llint i = str1.size() - 1 ; i >= 0 ; i--) | |
{ | |
int currentSum = to_digit(str1[i]) + to_digit(str2[i]) + carry; | |
carry = (currentSum >> 1) / 5; | |
answer.push_back(to_char(currentSum % 10)); | |
}; | |
if (carry) answer.push_back('1'); | |
reverse(answer.begin(), answer.end()); return answer; | |
} | |
string sub(string str1, string str2) | |
{ | |
standardify(str1, str2); | |
bool neg = (str1 < str2) ? true : false; if (neg) swap(str1, str2); | |
int carry = 0; | |
string answer = ""; | |
for (llint i = str1.length() - 1; i >= 0 ; i--) | |
{ | |
int current = to_digit(str1[i]) - to_digit(str2[i]) - carry; carry = 0; //take the carry and clear the carry | |
if (current < 0) | |
{ | |
carry = 1; //if borrowing needed, raise the carry | |
current += 10; | |
}; | |
answer.push_back(to_char(current % 10)); | |
} | |
while (answer.back() == '0') answer.pop_back(); | |
reverse(begin(answer), end(answer)); return neg ? ("-" + answer) : answer; | |
} | |
string mul(string str1, string str2) | |
{ | |
string answer = "0"; | |
for (llint i = str2.size() - 1 ; i >= 0 ; i--) | |
{ | |
string digit_mul = "0"; | |
for (llint n = str1.size() - 1 ; n >= 0 ; n--) digit_mul = add(digit_mul, to_string(to_digit(str1[n]) * to_digit(str2[i])) + string(str1.size() - 1 - n, '0')); | |
answer = add(answer, digit_mul + string(str2.size() - 1 - i, '0')); | |
}; | |
return answer; | |
} | |
string decrement(string str) | |
{ | |
if (str == "0") return "-1"; | |
int carry = 1; | |
for (llint i = str.size() - 1 ; i >= 0 ; i--) | |
{ | |
int replace = to_digit(str[i]) - carry; | |
carry = 0; | |
if (replace < 0) | |
{ | |
replace += 10; carry = 1; | |
}; | |
str[i] = to_char(replace); | |
}; | |
return str; | |
} | |
int main() | |
{ | |
string str1;//, str2; | |
//string str2; | |
//cout << "Enter first number: "; cin >> str1; | |
cin >> str1; | |
//cout << "Enter second number: "; cin >> str2; | |
cout << decrement(str1); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment