Advertisement
Kostiggig

mega calculator

May 20th, 2023
1,000
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.53 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <locale.h>
  3. #include <iostream>
  4. #include <string>
  5. #include <cmath>
  6. #include <iomanip>
  7.  
  8. using namespace std;
  9.  
  10. #define PRECISION 5
  11.  
  12. // todo think of removing unnecessary zeroes in the beginning of the num
  13.  
  14. bool validNumForDouble(string num) {
  15.     return true;
  16. }
  17.  
  18. enum NumSign {
  19.     POSITIVE,
  20.     NEGATIVE,
  21.     NOT_CORRECTED
  22. };
  23.  
  24. NumSign getNumSign(string num) {
  25.     int countMinusInTheBeginning = 0;
  26.     int countPliusInTheBeginning = 0;
  27.  
  28.     for(int i = 0; i < num.length(); i++) {
  29.         if(num[i] == '-') countMinusInTheBeginning++;
  30.         if(num[i] == '+') countPliusInTheBeginning++;
  31.     }
  32.  
  33.     if(countMinusInTheBeginning > 1 || countPliusInTheBeginning > 1) return NOT_CORRECTED;
  34.  
  35.     if (num.length() == 0) {
  36.         return NOT_CORRECTED;
  37.     }
  38.  
  39.     if (num[0] == '-') {
  40.         return NEGATIVE;
  41.     } else if (num[0] == '+') {
  42.         return POSITIVE;
  43.     } else if (num[0] >= '0' && num[0] <= '9') {
  44.         return POSITIVE;
  45.     } else {
  46.         return NOT_CORRECTED;
  47.     }
  48. }
  49.  
  50. bool numIsValid(string num, bool *isPositive, bool *isReal) {
  51.     bool hasDigit = false;
  52.     bool metPoints = false;
  53.  
  54.     NumSign numSign = getNumSign(num);
  55.     if(numSign == NOT_CORRECTED) return false;
  56.  
  57.     string numWithoutSign = num;
  58.     if(num[0] == '-' || num[0] == '+') numWithoutSign = num.substr(1, num.length());
  59.  
  60.     if(numWithoutSign[0] < '0' || numWithoutSign[0] > '9') return false;
  61.    
  62.     (*isPositive) = numSign == POSITIVE;
  63.  
  64.     for (char c : numWithoutSign) {
  65.         if (c >= '0' && c <= '9') {
  66.             hasDigit = true;
  67.         } else if (c == '.' || c == ',') {
  68.             if (metPoints) return false;  
  69.             metPoints = true;
  70.         }
  71.         else return false;
  72.     }
  73.  
  74.     if (!hasDigit) {
  75.         return false;
  76.     }
  77.  
  78.     (*isReal) = metPoints;
  79.  
  80.     return true;
  81. }
  82.  
  83. bool isPositive(string num) {
  84.     if (num[0] == '-') {
  85.         return false;
  86.     } else if (num[0] == '+') {
  87.         return true;
  88.     } else if (num[0] >= '0' && num[0] <= '9') {
  89.         return true;
  90.     }
  91.  
  92.     return false;
  93. }
  94.  
  95. void sum() {
  96.     bool firstIsPositive = false;
  97.     bool secondIsPositive = false;
  98.  
  99.     bool firstIsReal = false;
  100.     bool secondIsReal = false;
  101.  
  102.     string firstNum;
  103.     string secondNum;
  104.  
  105.     while(true) {
  106.         cout << endl << "First num: ";
  107.         cin >> firstNum;
  108.         if(!numIsValid(firstNum, &firstIsPositive, &firstIsReal)) {
  109.             cout << "Num is not valid";
  110.         } else break;
  111.     }
  112.  
  113.     while(true) {
  114.         cout << endl << "Second num: ";
  115.         cin >> secondNum;
  116.         if(!numIsValid(secondNum, &secondIsPositive, &secondIsReal)) {
  117.             cout << "Num is not valid";
  118.         } else break;
  119.     }
  120.    
  121.     cout.precision(PRECISION);
  122.     cout << firstNum << " + " << secondNum << " = " << fixed <<(stold(firstNum) + stold(secondNum));
  123.    
  124.     cout << endl;
  125.     system("pause");
  126. }
  127.  
  128. void substract() {
  129.     bool firstIsPositive = false;
  130.     bool secondIsPositive = false;
  131.  
  132.     bool firstIsReal = false;
  133.     bool secondIsReal = false;
  134.  
  135.     string firstNum;
  136.     string secondNum;
  137.  
  138.     while(true) {
  139.         cout << endl << "First num: ";
  140.         cin >> firstNum;
  141.         if(!numIsValid(firstNum, &firstIsPositive, &firstIsReal)) {
  142.             cout << "Num is not valid";
  143.         } else break;
  144.     }
  145.  
  146.     while(true) {
  147.         cout << endl << "Second num: ";
  148.         cin >> secondNum;
  149.         if(!numIsValid(secondNum, &secondIsPositive, &secondIsReal)) {
  150.             cout << "Num is not valid";
  151.         } else break;
  152.     }
  153.    
  154.     cout.precision(PRECISION);
  155.     cout << firstNum << " - " << secondNum << " = " << fixed << (stold(firstNum) - stold(secondNum));
  156.    
  157.     cout << endl;
  158.     system("pause");
  159. }
  160. void multiply() {
  161.     bool firstIsPositive = false;
  162.     bool secondIsPositive = false;
  163.  
  164.     bool firstIsReal = false;
  165.     bool secondIsReal = false;
  166.  
  167.     string firstNum;
  168.     string secondNum;
  169.  
  170.     while(true) {
  171.         cout << endl << "First num: ";
  172.         cin >> firstNum;
  173.         if(!numIsValid(firstNum, &firstIsPositive, &firstIsReal)) {
  174.             cout << "Num is not valid";
  175.         } else break;
  176.     }
  177.  
  178.     while(true) {
  179.         cout << endl << "Second num: ";
  180.         cin >> secondNum;
  181.         if(!numIsValid(secondNum, &secondIsPositive, &secondIsReal)) {
  182.             cout << "Num is not valid";
  183.         } else break;
  184.     }
  185.    
  186.     cout.precision(PRECISION);
  187.     cout << firstNum << " * " << secondNum << " = " << fixed << (stold(firstNum) * stold(secondNum));
  188.    
  189.     cout << endl;
  190.     system("pause");
  191. }
  192.  
  193. void divide() {
  194.     bool firstIsPositive = false;
  195.     bool secondIsPositive = false;
  196.  
  197.     bool firstIsReal = false;
  198.     bool secondIsReal = false;
  199.  
  200.     string firstNum;
  201.     string secondNum;
  202.  
  203.     while(true) {
  204.         cout << endl << "First num: ";
  205.         cin >> firstNum;
  206.         if(!numIsValid(firstNum, &firstIsPositive, &firstIsReal)) {
  207.             cout << "Num is not valid";
  208.         } else break;
  209.     }
  210.  
  211.     while(true) {
  212.         cout << endl << "Second num: ";
  213.         cin >> secondNum;
  214.         if(!numIsValid(secondNum, &secondIsPositive, &secondIsReal)) {
  215.             cout << "Num is not valid";
  216.         } else {
  217.             if(stold(secondNum) == 0.0) {
  218.                 cout << endl << "Divider cannot be equal to zero";
  219.                 continue;
  220.             }
  221.             break;
  222.         }
  223.     }
  224.    
  225.     int precision = -1;
  226.     while(true) {
  227.         cout << endl << "Enter precision: ";
  228.         cin >> precision;
  229.         if(precision >= 0) break;
  230.     }
  231.  
  232.     cout.precision(precision);
  233.  
  234.     cout << endl << "precision is " << precision;
  235.     cout << endl << firstNum << " / " << secondNum << " = " << fixed << (stold(firstNum) / stold(secondNum));
  236.    
  237.     cout << endl;
  238.     system("pause");
  239.  }
  240.  
  241. bool validExtent(string extent) {
  242.     for (char c : extent) {
  243.         if (!isdigit(c)) {
  244.             return false;
  245.         }
  246.     }
  247.     return true;
  248. }
  249.  
  250. void pow() {
  251.     bool firstIsPositive = false;
  252.     bool secondIsPositive = false;
  253.  
  254.     bool firstIsReal = false;
  255.     bool secondIsReal = false;
  256.  
  257.     string firstNum;
  258.     string extent;
  259.  
  260.     while(true) {
  261.         cout << endl << "Num: ";
  262.         cin >> firstNum;
  263.         if(!numIsValid(firstNum, &firstIsPositive, &firstIsReal)) {
  264.             cout << "Num is not valid";
  265.         } else break;
  266.     }
  267.  
  268.     while(true) {
  269.         cout << endl << "extent: ";
  270.         cin >> extent;
  271.         if(!validExtent(extent)) {
  272.             cout << endl << "The extent is not valid";
  273.         } else break;
  274.     }
  275.    
  276.     cout.precision(PRECISION);
  277.  
  278.     cout << endl << firstNum << " ^ " << extent << " = " << fixed << (pow(stold(firstNum),stold(extent)));
  279.    
  280.     cout << endl;
  281.     system("pause");
  282.  }
  283.  
  284. int main() {
  285.  
  286.     int menuItem = -1;
  287.  
  288.     bool isExit = false;
  289.     while(!isExit) {
  290.         system("clear");
  291.         cout
  292.             << "\n 0 - Exit"
  293.             << "\n 1 - Sum"
  294.             << "\n 2 - Subsctract"
  295.             << "\n 3 - Miltiply"
  296.             << "\n 4 - Divide"
  297.             << "\n 5 - Pow: ";
  298.  
  299.         cin >> menuItem;
  300.         if(menuItem < 0 || menuItem > 5) continue;
  301.  
  302.         switch(menuItem) {
  303.             case 0: isExit = true; break;
  304.             case 1: sum(); break;
  305.             case 2: substract(); break;
  306.             case 3: multiply(); break;
  307.             case 4: divide(); break;
  308.             case 5: pow(); break;
  309.             default: cout << "Menu item " << menuItem << " cannot be found";
  310.         }
  311.     }
  312.  
  313.     return 0;
  314. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement