Advertisement
Guest User

calc

a guest
Dec 19th, 2018
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 11.53 KB | None | 0 0
  1. #include "pch.h"
  2. #include <iostream>
  3. #include <string>
  4. #include <cmath>
  5.  
  6. using namespace std;
  7.  
  8. const string alph = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  9. int E = 5;
  10.  
  11. string replace(string in)
  12. {
  13.     int k;
  14.  
  15.     if (in.length() % 2 == 0)
  16.         k = in.length() / 2;
  17.     else
  18.         k = in.length() / 2 + 1;
  19.     for (int i = 0; i < k; i++)
  20.     {
  21.         swap(in[i], in[in.length() - i - 1]);
  22.     }
  23.     return in;
  24. }
  25.  
  26. char find_symbol(int a)
  27. {
  28.     return alph[a];
  29. }
  30.  
  31. int find_number(char a)
  32. {
  33.     int i = 0;
  34.  
  35.     while (a != alph[i])
  36.     {
  37.         i++;
  38.         //cout << i << " ";
  39.     }
  40.     return i;
  41. }
  42.  
  43. int to_ten_base(string first, int osn)
  44. {
  45.     int second = 0;
  46.     int k = 1;
  47.  
  48.     while (k <= first.length())
  49.     {
  50.         second += find_number(first[k - 1]) * pow(osn, (first.length() - k));
  51.         k++;
  52.     }
  53.     return second;
  54. }
  55.  
  56. string to_x_base(int ten, int out)
  57. {
  58.     int x;
  59.     string res;
  60.     while ((ten / out) > 0)
  61.     {
  62.         x = ten % out;
  63.         res = find_symbol(x) + res;
  64.         ten = ten / out;
  65.     }
  66.     res = find_symbol(ten) + res;
  67.     return res;
  68. }
  69.  
  70. char sravn(string a, string b)
  71. {
  72.  
  73.     if (a.length() > b.length())
  74.         return '>';
  75.     else if (a.length() < b.length())
  76.         return '<';
  77.     else
  78.     {
  79.         for (int i = 0; i < a.length(); i++)
  80.         {
  81.             if (a[i] > b[i])
  82.             {
  83.                 return '>';
  84.             }
  85.             else if (a[i] < b[i])
  86.             {
  87.                 return '<';
  88.             }
  89.         }
  90.         return '=';
  91.     }
  92. }
  93.  
  94. string sum(string a, string b, int osn)
  95. {
  96.     string summed;
  97.     int buf = 0;
  98.     int x = 0, i;
  99.  
  100.     a = replace(a);
  101.     b = replace(b);
  102.  
  103.     if (a.length() > b.length())
  104.     {
  105.         for (i = b.length(); i < a.length(); i++)
  106.         {
  107.             b = b + '0';
  108.         }
  109.     }
  110.     else if (b.length() > a.length())
  111.     {
  112.         for (i = a.length(); i < b.length(); i++)
  113.         {
  114.             a = a + '0';
  115.         }
  116.     }
  117.     a += '0';
  118.     b += '0';
  119.  
  120.     for (i = 0; i < a.length(); i++)
  121.     {
  122.         x = find_number(a[i]) + find_number(b[i]) + buf;
  123.  
  124.         if (x >= osn)
  125.         {
  126.             buf = 1;
  127.             x = x - osn;
  128.         }
  129.         else
  130.         {
  131.             buf = 0;
  132.         }
  133.         summed = summed + find_symbol(x);
  134.     }
  135.  
  136.     i = summed.length() - 1;
  137.     if (to_ten_base(summed, osn) != 0)
  138.     {
  139.         while (summed[i] == '0')
  140.         {
  141.             summed.erase(i, 1);
  142.             i--;
  143.         }
  144.     }
  145.     else
  146.         summed = '0';
  147.     summed = replace(summed);
  148.     return summed;
  149. }
  150.  
  151. string proizv(string a, string b, int n)
  152. {
  153.     string x;
  154.     int kolvo = to_ten_base(b, n);
  155.  
  156.     if (to_ten_base(a, n) == 0 || to_ten_base(b, n) == 0)
  157.     {
  158.         x = '0';
  159.         return x;
  160.     }
  161.  
  162.     for (int i = 0; i < kolvo; i++)
  163.     {
  164.         x = sum(x, a, n);
  165.     }
  166.     return x;
  167. }
  168.  
  169. string difference(string a, string b, int osn)
  170. {
  171.     string diff;
  172.     bool flag = 0;
  173.     int x = 0, i, buf = 0, k = 0;
  174.  
  175.     a = replace(a);
  176.     b = replace(b);
  177.  
  178.     if (a.length() > b.length())
  179.     {
  180.         for (i = b.length(); i < a.length(); i++)
  181.         {
  182.             b = b + '0';
  183.         }
  184.     }
  185.     else if (b.length() > a.length())
  186.     {
  187.         for (i = a.length(); i < b.length(); i++)
  188.         {
  189.             a = a + '0';
  190.         }
  191.     }
  192.  
  193.     for (i = 0; i < a.length(); i++)
  194.     {
  195.         x = find_number(a[i]) - find_number(b[i]) - buf;
  196.  
  197.         if (x < 0)
  198.         {
  199.             x = x + osn;
  200.             buf = 1;
  201.         }
  202.         else
  203.         {
  204.             buf = 0;
  205.         }
  206.         diff = diff + find_symbol(x);
  207.     }
  208.  
  209.     i = diff.length() - 1;
  210.     if (to_ten_base(diff, osn) != 0)
  211.     {
  212.         while (diff[i] == '0')
  213.         {
  214.             diff.erase(i, 1);
  215.             i--;
  216.         }
  217.     }
  218.     else
  219.         diff = '0';
  220.     diff = replace(diff);
  221.     return diff;
  222. }
  223.  
  224. string del(string a, string b, int n)
  225. {
  226.     string x;
  227.     int answer = 0, k = 0;
  228.     bool flag = 0;
  229.  
  230.     while (to_ten_base(a, n) >= to_ten_base(b, n))
  231.     {
  232.         a = difference(a, b, n);
  233.         answer++;
  234.     }
  235.     x = to_x_base(answer, n);
  236.     return x;
  237. }
  238.  
  239. string del_s_ost(string a, string b, int n)
  240. {
  241.     string x, na = to_x_base(n, n), chastn, kolvo;
  242.     int answer = 0, k = 0;
  243.     bool flag = 0;
  244.  
  245.     while (to_ten_base(a, n) >= to_ten_base(b, n))
  246.     {
  247.         a = difference(a, b, n);
  248.         answer++;
  249.     }
  250.  
  251.     x = to_x_base(answer, n);
  252.  
  253.     if (to_ten_base(a, n) != 0)
  254.     {
  255.  
  256.         if (E > 0)
  257.             x = x + ".";
  258.  
  259.         while (to_ten_base(a, n) != 0 && k < E)
  260.         {
  261.             a = a + '0';
  262.             chastn = del(a, b, n);
  263.  
  264.             x = x + chastn;
  265.  
  266.             if (to_ten_base(a, n) >= to_ten_base(b, n))
  267.             {
  268.                 kolvo = del(a, b, n);
  269.                 a = difference(a, proizv(kolvo, b, n), n);
  270.             }
  271.             if (to_ten_base(proizv(chastn, b, n), n) == n)
  272.                 break;
  273.             k++;
  274.         }
  275.     }
  276.  
  277.     return x;
  278. }
  279.  
  280. int main()
  281. {
  282.     string one, two, one10, two10, in1, in2;
  283.     int dex, first, second;
  284.     char otvet, task;
  285.     bool sign_first = 0, sign_second = 0;
  286.  
  287.     setlocale(LC_ALL, "Russian");
  288.  
  289.     cout << "Введите число a: ";
  290.     cin >> in1;
  291.     cout << "Введите число b: ";
  292.     cin >> in2;
  293.     cout << "Введите операцию: ";
  294.     cin >> task;
  295.     cout << "Введите основание —— , в которой будет проводитьс¤ операци¤: ";
  296.     cin >> dex;
  297.  
  298.     if (in1[0] == '-')
  299.     {
  300.         in1.erase(0, 1);
  301.         sign_first = 1;
  302.     }
  303.     first = to_ten_base(in1, 10);
  304.  
  305.     if (in2[0] == '-')
  306.     {
  307.         in2.erase(0, 1);
  308.         sign_second = 1;
  309.     }
  310.     second = to_ten_base(in2, 10);
  311.  
  312.     one10 = to_x_base(first, 10);
  313.     two10 = to_x_base(second, 10);
  314.     one = to_x_base(first, dex);
  315.     two = to_x_base(second, dex);
  316.  
  317.     switch (task)
  318.     {
  319.     case '+':
  320.     {
  321.         if (one10 == two10)
  322.         {
  323.             if (sign_first == 1 && sign_second == 0)
  324.                 cout << "-" << one << " + " << two << " = 0 = 0" << endl;
  325.             else if (sign_first == 0 && sign_second == 1)
  326.                 cout << one << " + (-" << two << ") = 0 = 0" << endl;
  327.             else if (sign_first == 1 && sign_second == 1)
  328.                 cout << "-" << one << " + (-" << two << ") = -" << sum(one, two, dex) << " = -" << to_ten_base(sum(one, two, dex), dex) << endl;
  329.             else
  330.                 cout << one << " + " << two << " = " << sum(one, two, dex) << " = " << to_ten_base(sum(one, two, dex), dex) << endl;
  331.         }
  332.         else if (one10 > two10)
  333.         {
  334.             if (sign_first == 1)
  335.             {
  336.                 if (sign_second == 1)
  337.                     cout << "-" << one << " + (-" << two << ") = -" << sum(one, two, dex) << " = -" << to_ten_base(sum(one, two, dex), dex) << endl;
  338.                 else
  339.                     cout << "-" << one << " + " << two << " = -" << difference(one, two, dex) << " = -" << to_ten_base(difference(one, two, dex), dex) << endl;
  340.             }
  341.             else
  342.             {
  343.                 if (sign_second == 1)
  344.                     cout << one << " + (-" << two << ") = -" << difference(one, two, dex) << " = -" << to_ten_base(difference(one, two, dex), dex) << endl;
  345.                 else
  346.                     cout << one << " + " << two << " = " << sum(one, two, dex) << " = " << to_ten_base(sum(one, two, dex), dex) << endl;
  347.             }
  348.         }
  349.         else
  350.         {
  351.             if (sign_first == 1)
  352.             {
  353.                 if (sign_second == 1)
  354.                     cout << "-" << one << " + (-" << two << ") = -" << sum(one, two, dex) << " = -" << to_ten_base(sum(one, two, dex), dex) << endl;
  355.                 else
  356.                     cout << "-" << one << " + " << two << " = " << difference(two, one, dex) << " = " << to_ten_base(difference(two, one, dex), dex) << endl;
  357.             }
  358.             else
  359.             {
  360.                 if (sign_second == 1)
  361.                     cout << one << " + (-" << two << ") = -" << difference(two, one, dex) << " = -" << to_ten_base(difference(two, one, dex), dex) << endl;
  362.                 else
  363.                     cout << one << " + " << two << " = " << sum(one, two, dex) << " = " << to_ten_base(sum(one, two, dex), dex) << endl;
  364.             }
  365.         }
  366.         break;
  367.     }
  368.     case '-':
  369.     {
  370.         if (one10 == two10)
  371.         {
  372.             if (sign_first == 0 && sign_second == 0)
  373.                 cout << one << " - " << two << " = 0 = 0" << endl;
  374.             else if (sign_first == 1 && sign_second == 0)
  375.                 cout << "-" << one << " - " << two << " = " << sum(one, two, dex) << " = -" << to_ten_base(sum(one, two, dex), dex) << endl;
  376.             else if (sign_first == 0 && sign_second == 1)
  377.                 cout << one << " - (-" << two << ") = " << sum(one, two, dex) << " = " << to_ten_base(sum(one, two, dex), dex) << endl;
  378.             else
  379.                 cout << "-" << one << " - (-" << two << ") = 0 = 0" << endl;
  380.  
  381.         }
  382.         else if (one10 > two10)
  383.         {
  384.             if (sign_first == 1)
  385.             {
  386.                 if (sign_second == 1)
  387.                 {
  388.                     cout << "-" << one << " - (-" << two << ") = " << difference(two, one, dex) << " = " << to_ten_base(difference(two, one, dex), dex) << endl;
  389.                 }
  390.                 else
  391.                     cout << "-" << one << " - " << two << " = -" << sum(one, two, dex) << " = -" << to_ten_base(sum(one, two, dex), dex) << endl;
  392.             }
  393.             else
  394.             {
  395.                 if (sign_second == 1)
  396.                     cout << one << " - (-" << two << ") = " << sum(one, two, dex) << " = " << to_ten_base(sum(one, two, dex), dex) << endl;
  397.                 else
  398.                     cout << one << " - " << two << " = " << difference(one, two, dex) << " = " << to_ten_base(difference(one, two, dex), dex) << endl;
  399.             }
  400.         }
  401.         else
  402.         {
  403.             if (sign_first == 1)
  404.             {
  405.                 if (sign_second == 1)
  406.                     cout << "-" << one << " - (-" << two << ") = -" << difference(one, two, dex) << " = -" << to_ten_base(difference(one, two, dex), dex) << endl;
  407.                 else
  408.                     cout << "-" << one << " - " << two << " = -" << sum(one, two, dex) << " = -" << to_ten_base(sum(one, two, dex), dex) << endl;
  409.             }
  410.             else
  411.             {
  412.                 if (sign_second == 1)
  413.                     cout << one << " - (-" << two << ") = " << sum(one, two, dex) << " = " << to_ten_base(sum(one, two, dex), dex) << endl;
  414.                 else
  415.                     cout << one << " - " << two << " = -" << difference(two, one, dex) << " = -" << to_ten_base(difference(two, one, dex), dex) << endl;
  416.             }
  417.         }
  418.         break;
  419.     }
  420.     case '*':
  421.     {
  422.         if (to_ten_base(one, 10) == 0 || to_ten_base(two, 10) == 0)
  423.         {
  424.             if (sign_first) cout << "-";
  425.             cout << one << " * ";
  426.             if (sign_second) cout << "-";
  427.             cout << two << " = 0 = 0" << endl;
  428.         }
  429.         else if (sign_first == 1)
  430.         {
  431.             if (sign_second == 1)
  432.                 cout << "-" << one << " * (-" << two << ") = " << proizv(one, two, dex) << " = " << to_ten_base(proizv(one, two, dex), dex) << endl;
  433.             else
  434.                 cout << "-" << one << " * " << two << " = -" << proizv(one, two, dex) << " = -" << to_ten_base(proizv(one, two, dex), dex) << endl;
  435.         }
  436.         else
  437.         {
  438.             if (sign_second == 1)
  439.                 cout << one << " * (-" << two << ") = -" << proizv(one, two, dex) << " = -" << to_ten_base(proizv(one, two, dex), dex) << endl;
  440.             else
  441.                 cout << one << " * " << two << " = " << proizv(one, two, dex) << " = " << to_ten_base(proizv(one, two, dex), dex) << endl;
  442.         }
  443.         break;
  444.     }
  445.     case '/':
  446.     {
  447.         cout << "Введите количество знаков после зап¤той: ";
  448.         cin >> E;
  449.         if (to_ten_base(one, 10) == 0 && sign_second == 1)
  450.             cout << " 0 / (-" << two << ") = 0 = 0" << endl;
  451.         else if (to_ten_base(two, dex) == 0)
  452.         {
  453.             cout << "Деление на ноль, звершение работы программы" << endl;
  454.             break;
  455.         }
  456.         else if (sign_first == 1)
  457.         {
  458.             if (sign_second == 1)
  459.                 cout << "-" << one << " / (-" << two << ") = " << del_s_ost(one, two, dex) << " = " << del_s_ost(one10, two10, 10) << endl;
  460.             else
  461.                 cout << "-" << one << " / " << two << " = -" << del_s_ost(one, two, dex) << " = -" << del_s_ost(one10, two10, 10) << endl;
  462.         }
  463.         else
  464.         {
  465.             if (sign_second == 1)
  466.                 cout << one << " / (-" << two << ") = -" << del_s_ost(one, two, dex) << " = -" << del_s_ost(one10, two10, 10) << endl;
  467.             else
  468.                 cout << one << " / " << two << " = " << del_s_ost(one, two, dex) << " = " << del_s_ost(one10, two10, 10) << endl;
  469.         }
  470.         break;
  471.     }
  472.     case '?':
  473.     {
  474.         if (sign_first == 1)
  475.         {
  476.             if (sign_second == 1)
  477.                 cout << "-" << one << " " << sravn(one, two) << " -" << two << " => -" << to_ten_base(one, dex) << " " << sravn(one, two) << " -" << to_ten_base(two, dex) << endl;
  478.             else
  479.                 cout << "-" << one << " < " << two << " => -" << to_ten_base(one, dex) << " < " << to_ten_base(two, dex) << endl;
  480.         }
  481.         else
  482.         {
  483.             if (sign_second == 1)
  484.                 cout << one << " > -" << two << " => " << to_ten_base(one, dex) << " > -" << to_ten_base(two, dex) << endl;
  485.             else
  486.                 cout << one << " " << sravn(one, two) << " " << two << " => " << to_ten_base(one, dex) << " " << sravn(one, two) << " " << to_ten_base(two, dex) << endl;
  487.         }
  488.         break;
  489.     }
  490.     default:
  491.     {
  492.         cout << "¬веденной операции не существует" << endl;
  493.         break;
  494.     }
  495.     }
  496.     return 0;
  497. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement