Advertisement
Kostiggig

Untitled

May 20th, 2023
759
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.15 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 <<(stod(firstNum) + stod(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 << (stod(firstNum) - stod(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 << (stod(firstNum) * stod(secondNum));
  188.    
  189.     cout << endl;
  190.     system("pause");
  191. }
  192.  
  193. // todo check for zero
  194. void divide() {
  195.     bool firstIsPositive = false;
  196.     bool secondIsPositive = false;
  197.  
  198.     bool firstIsReal = false;
  199.     bool secondIsReal = false;
  200.  
  201.     string firstNum;
  202.     string secondNum;
  203.  
  204.     while(true) {
  205.         cout << endl << "First num: ";
  206.         cin >> firstNum;
  207.         if(!numIsValid(firstNum, &firstIsPositive, &firstIsReal)) {
  208.             cout << "Num is not valid";
  209.         } else break;
  210.     }
  211.  
  212.     while(true) {
  213.         cout << endl << "Second num: ";
  214.         cin >> secondNum;
  215.         if(!numIsValid(secondNum, &secondIsPositive, &secondIsReal)) {
  216.             cout << "Num is not valid";
  217.         } else {
  218.             if(stod(secondNum) == 0.0) {
  219.                 cout << endl << "Divider cannot be equal to zero";
  220.                 continue;
  221.             }
  222.             break;
  223.         }
  224.     }
  225.    
  226.     int precision = -1;
  227.     while(true) {
  228.         cout << endl << "Enter precision: ";
  229.         cin >> precision;
  230.         if(precision >= 0) break;
  231.     }
  232.  
  233.     cout.precision(precision);
  234.  
  235.     cout << endl << "precision is " << precision;
  236.     cout << endl << firstNum << " / " << secondNum << " = " << fixed << (stod(firstNum) / stod(secondNum));
  237.    
  238.     cout << endl;
  239.     system("pause");
  240.  }
  241.  
  242. void pow() {
  243.     string firstNum;
  244.     string extent;
  245.  
  246.     while(true) {
  247.         cout << endl << "First num: ";
  248.         cin >> firstNum;
  249.         if(!validNumForDouble(firstNum)) {
  250.             cout << "Num is not valid";
  251.         } else break;
  252.     }
  253.    
  254.     while(true) {
  255.         cout << endl << "Extent: ";
  256.         cin >> extent;
  257.         if(!validNumForDouble(extent)) {
  258.             cout << "Num is not valid";
  259.         } else break;
  260.     }
  261.    
  262.     cout << firstNum << " ^ " << extent << " = " << pow(stol(firstNum), stol(extent));
  263.    
  264.     cout << endl;
  265.     system("pause");
  266.  }
  267.  
  268. int main() {
  269.  
  270.     int menuItem = -1;
  271.  
  272.     bool isExit = false;
  273.     while(!isExit) {
  274.         cout
  275.             << "\n 0 - Exit"
  276.             << "\n 1 - Sum"
  277.             << "\n 2 - Subsctract"
  278.             << "\n 3 - Miltiply"
  279.             << "\n 4 - Divide"
  280.             << "\n 5 - Pow: ";
  281.  
  282.         cin >> menuItem;
  283.         if(menuItem < 0 || menuItem > 5) continue;
  284.  
  285.         switch(menuItem) {
  286.             case 0: isExit = true; break;
  287.             case 1: sum(); break;
  288.             case 2: substract(); break;
  289.             case 3: multiply(); break;
  290.             case 4: divide(); break;
  291.             case 5: pow(); break;
  292.             default: cout << "Menu item " << menuItem << " cannot be found";
  293.         }
  294.     }
  295.  
  296.     return 0;
  297. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement