Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <algorithm>
- char compare(std::string s1, std::string s2)
- {
- if(s1.length() != s2.length()) {
- return (s1.length() > s2.length()) ? '>' : '<';
- } else {
- while(s1.length()) {
- if(s1[0] > s2[0]) {
- return '>';
- } else if (s1[0] < s2[0]) {
- return '<';
- }
- s1.erase(0, 1);
- s2.erase(0, 1);
- }
- return '=';
- }
- }
- int toi(char ch) {
- return ch - '0';
- }
- char toc(int i) {
- return i + '0';
- }
- std::string op_plus(std::string s1, std::string s2) {
- std::string s_add = "";
- int tmp = 0;
- while (s1 != "" || s2 != "" || tmp != 0) {
- if (s1 != "") {
- tmp += toi(s1[s1.length()-1]);
- s1.erase(s1.length()-1);
- }
- if (s2 != "") {
- tmp += toi(s2[s2.length()-1]);
- s2.erase(s2.length()-1);
- }
- s_add += toc(tmp%10);
- tmp /= 10;
- }
- while (s_add[s_add.length()-1] == '0' && s_add.length() != 1) {
- s_add.erase(s_add.length()-1);
- }
- std::reverse(s_add.begin(), s_add.end());
- return s_add;
- }
- std::string op_minus(std::string s1, std::string s2) {
- std::string s_sub = "";
- int tmp = 0;
- char cmp = compare(s1, s2);
- if (cmp == '=') {
- return "0";
- } else if (cmp == '<'){
- std::swap(s1, s2);
- }
- while (s1 != "") {
- if (s2 != "") {
- tmp += toi(s1[s1.length()-1]) - toi(s2[s2.length()-1]);
- s2.erase(s2.length()-1);
- } else {
- tmp += toi(s1[s1.length()-1]);
- }
- s1.erase(s1.length()-1);
- if (tmp < 0) {
- s_sub += toc(tmp + 10);
- tmp = -1;
- } else {
- s_sub += toc(tmp);
- tmp = 0;
- }
- }
- while (s_sub[s_sub.length()-1] == '0' && s_sub.length() != 1) {
- s_sub.erase(s_sub.length()-1);
- }
- if (cmp == '<') {
- s_sub += "-";
- }
- std::reverse(s_sub.begin(), s_sub.end());
- return s_sub;
- }
- std::string op_time_by_one_digit(std::string s1, char s2) {
- std::string s_mul = "0";
- if (s2 == '0') {
- return "0";
- } else {
- for (int i = 0; i < (s2-'0'); ++i) {
- s_mul = op_plus(s_mul, s1);
- }
- return s_mul;
- }
- }
- std::string op_time(std::string s1, std::string s2) {
- std::string s_mul = "0", zero = "";
- if (compare(s1, s2) == '<') {
- std::swap(s1, s2);
- }
- while(s2 != "") {
- s_mul = op_plus(s_mul, op_time_by_one_digit(s1, s2[s2.length()-1]) + zero);
- s2.erase(s2.length()-1);
- zero += "0";
- }
- return s_mul;
- }
- std::string op_divide(std::string s1, std::string s2) {
- std::string s_div = "1";
- if (s1 == s2) {
- return "1";
- }
- while (compare(op_time(s_div, s2), s1) != '>') {
- s_div += "0";
- }
- s_div.erase(s_div.length()-1);
- std::string tmp = s_div;
- s_div = "0";
- for (int i = 0; i < s_div.length(); ++i) {
- for (int j = 0; j < 9; ++j) {
- if (compare(op_time(s_div, s2), s1) == '>') {
- s_div = op_minus(s_div, tmp);
- break;
- } else if (compare(op_time(s_div, s2), s1) == '=') {
- return s_div;
- }
- s_div = op_plus(s_div, tmp);
- }
- if (tmp != "") {
- tmp.erase(tmp.length()-1);
- }
- }
- return s_div;
- }
- int main()
- {
- std::string s1, s2, ans;
- char ope;
- while (std::cin >> s1 >> ope >> s2) {
- ans = "";
- switch (ope) {
- case '+':
- ans = op_plus(s1, s2);
- break;
- case '-':
- ans = op_minus(s1, s2);
- break;
- case '*':
- ans = op_time(s1, s2);
- break;
- case '/':
- ans = op_divide(s1, s2);
- break;
- default:
- std::cerr << "Operator was not defined." << std::endl;
- return 1;
- }
- std::cout << ans << std::endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement