Advertisement
Kostiggig

Untitled

May 20th, 2023
625
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.96 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. bool validNumSign(string num) {
  96.  
  97. }
  98.  
  99. void sum() {
  100.     bool firstIsPositive = false;
  101.     bool secondIsPositive = false;
  102.  
  103.     bool firstIsReal = false;
  104.     bool secondIsReal = false;
  105.  
  106.     string firstNum;
  107.     string secondNum;
  108.  
  109.     while(true) {
  110.         cout << endl << "First num: ";
  111.         cin >> firstNum;
  112.         if(!numIsValid(firstNum, &firstIsPositive, &firstIsReal)) {
  113.             cout << "Num is not valid";
  114.         } else break;
  115.     }
  116.  
  117.     while(true) {
  118.         cout << endl << "Second num: ";
  119.         cin >> secondNum;
  120.         if(!numIsValid(secondNum, &secondIsPositive, &secondIsReal)) {
  121.             cout << "Num is not valid";
  122.         } else break;
  123.     }
  124.    
  125.     cout << endl << "first is positive " << firstIsPositive << " is real " << firstIsReal;
  126.     cout << endl << "second is positive " << secondIsPositive << " is real " << secondIsReal;
  127.     cout << endl;
  128.  
  129.     setprecision(PRECISION);
  130.     cout << firstNum << " + " << secondNum << " = " << stod(firstNum) + stod(secondNum);
  131.    
  132.     cout << endl;
  133.     system("pause");
  134. }
  135.  
  136. void substract() {
  137.     bool firstIsPositive = false;
  138.     bool secondIsPositive = false;
  139.  
  140.     bool firstIsReal = false;
  141.     bool secondIsReal = false;
  142.  
  143.     string firstNum;
  144.     string secondNum;
  145.  
  146.     while(true) {
  147.         cout << endl << "First num: ";
  148.         cin >> firstNum;
  149.         if(!numIsValid(firstNum, &firstIsPositive, &firstIsReal)) {
  150.             cout << "Num is not valid";
  151.         } else break;
  152.     }
  153.  
  154.     while(true) {
  155.         cout << endl << "Second num: ";
  156.         cin >> secondNum;
  157.         if(!numIsValid(secondNum, &secondIsPositive, &secondIsReal)) {
  158.             cout << "Num is not valid";
  159.         } else break;
  160.     }
  161.    
  162.     cout << endl << "first is positive " << firstIsPositive << " is real " << firstIsReal;
  163.     cout << endl << "second is positive " << secondIsPositive << " is real " << secondIsReal;
  164.     cout << endl;
  165.  
  166.     setprecision(PRECISION);
  167.     cout << firstNum << " - " << secondNum << " = " << stod(firstNum) - stod(secondNum);
  168.    
  169.     cout << endl;
  170.     system("pause");
  171. }
  172. void multiply() {
  173.     string firstNum;
  174.     string secondNum;
  175.  
  176.     while(true) {
  177.         cout << endl << "First num: ";
  178.         cin >> firstNum;
  179.         if(!validNumForDouble(firstNum)) {
  180.             cout << "Num is not valid";
  181.         } else break;
  182.     }
  183.    
  184.     while(true) {
  185.         cout << endl << "Second num: ";
  186.         cin >> secondNum;
  187.         if(!validNumForDouble(secondNum)) {
  188.             cout << "Num is not valid";
  189.         } else break;
  190.     }
  191.    
  192.     cout << firstNum << " * " << secondNum << " = " << stol(firstNum) * stol(secondNum);
  193.    
  194.     cout << endl;
  195.     system("pause");
  196. }
  197.  
  198. // todo check for zero
  199. void divide() {
  200.     string firstNum;
  201.     string secondNum;
  202.  
  203.     while(true) {
  204.         cout << endl << "First num: ";
  205.         cin >> firstNum;
  206.         if(!validNumForDouble(firstNum)) {
  207.             cout << "Num is not valid";
  208.         } else break;
  209.     }
  210.    
  211.     while(true) {
  212.         cout << endl << "Second num: ";
  213.         cin >> secondNum;
  214.         if(!validNumForDouble(secondNum)) {
  215.             cout << "Num is not valid";
  216.         } else break;
  217.     }
  218.    
  219.     int signsAfterComma = -1;
  220.     while(signsAfterComma < 0) {
  221.         cout << endl << "Enter signs after comma: ";
  222.         cin >> signsAfterComma;
  223.     }
  224.  
  225.     cout << endl << "Signs after comma: " << signsAfterComma;
  226.  
  227.     cout << firstNum << " / " << secondNum << " = " << stol(firstNum) / stol(secondNum);
  228.    
  229.     cout << endl;
  230.     system("pause");
  231.  }
  232.  
  233. void pow() {
  234.     string firstNum;
  235.     string extent;
  236.  
  237.     while(true) {
  238.         cout << endl << "First num: ";
  239.         cin >> firstNum;
  240.         if(!validNumForDouble(firstNum)) {
  241.             cout << "Num is not valid";
  242.         } else break;
  243.     }
  244.    
  245.     while(true) {
  246.         cout << endl << "Extent: ";
  247.         cin >> extent;
  248.         if(!validNumForDouble(extent)) {
  249.             cout << "Num is not valid";
  250.         } else break;
  251.     }
  252.    
  253.     cout << firstNum << " ^ " << extent << " = " << pow(stol(firstNum), stol(extent));
  254.    
  255.     cout << endl;
  256.     system("pause");
  257.  }
  258.  
  259. int main() {
  260.  
  261.     int menuItem = -1;
  262.  
  263.     bool isExit = false;
  264.     while(!isExit) {
  265.         cout
  266.             << "\n 0 - Exit"
  267.             << "\n 1 - Sum"
  268.             << "\n 2 - Subsctract"
  269.             << "\n 3 - Miltiply"
  270.             << "\n 4 - Divide"
  271.             << "\n 5 - Pow: ";
  272.  
  273.         cin >> menuItem;
  274.         if(menuItem < 0 || menuItem > 5) continue;
  275.  
  276.         switch(menuItem) {
  277.             case 0: isExit = true; break;
  278.             case 1: sum(); break;
  279.             case 2: substract(); break;
  280.             case 3: multiply(); break;
  281.             case 4: divide(); break;
  282.             case 5: pow(); break;
  283.             default: cout << "Menu item " << menuItem << " cannot be found";
  284.         }
  285.     }
  286.  
  287.     return 0;
  288. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement