Advertisement
Kostiggig

Untitled

May 20th, 2023
631
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.96 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <locale.h>
  3. #include <iostream>
  4. #include <string>
  5. #include <cmath>
  6.  
  7. using namespace std;
  8.  
  9. // todo think of removing unnecessary zeroes in the beginning of the num
  10.  
  11. bool validNumForDouble(string num) {
  12.     int countOfComma = 0;
  13.     for (char ch : num) {
  14.         if (ch == '.' || to_string(ch) == to_string(',')) {
  15.             cout << "check " << endl;
  16.             countOfComma++;
  17.         }
  18.     }
  19.  
  20.     return countOfComma <= 1;
  21. }
  22.  
  23. bool numIsValid(string num) {
  24.     bool hasDigit = false;
  25.     bool metPoints = false;
  26.  
  27.     if (num.length() == 0 || num[0] < '0' || num[0] > '9' ) return false;
  28.  
  29.     for (char c : num) {
  30.         if (c >= '0' && c <= '9') {
  31.             hasDigit = true;
  32.         } else if (c == '.' || c == ',') {
  33.             if (metPoints) return false;  
  34.             metPoints = true;
  35.         }
  36.         else return false;
  37.     }
  38.  
  39.     if (!hasDigit) {
  40.         return false;
  41.     }
  42.  
  43.     return true;
  44. }
  45.  
  46. bool isPositive(string num) {
  47.     if (num[0] == '-') {
  48.         return false;
  49.     } else if (num[0] == '+') {
  50.         return true;
  51.     } else if (num[0] >= '0' && num[0] <= '9') {
  52.         return true;
  53.     }
  54.  
  55.     return false;
  56. }
  57.  
  58. void sum() {
  59.     string firstNum;
  60.     string secondNum;
  61.  
  62.     while(true) {
  63.         cout << endl << "First num: ";
  64.         cin >> firstNum;
  65.         if(!numIsValid(firstNum)) {
  66.             cout << "Num is not valid";
  67.         } else break;
  68.     }
  69.    
  70.     while(true) {
  71.         cout << endl << "Second num: ";
  72.         cin >> secondNum;
  73.         if(!numIsValid(secondNum)) {
  74.             cout << "Num is not valid";
  75.         } else break;
  76.     }
  77.    
  78.     cout << firstNum << " + " << secondNum << " = " << stol(firstNum) + stol(secondNum);
  79.    
  80.     cout << endl;
  81.     system("pause");
  82. }
  83.  
  84. void substract() {
  85.     string firstNum;
  86.     string secondNum;
  87.  
  88.     while(true) {
  89.         cout << endl << "First num: ";
  90.         cin >> firstNum;
  91.         if(!validNumForDouble(firstNum)) {
  92.             cout << "Num is not valid";
  93.         } else break;
  94.     }
  95.    
  96.     while(true) {
  97.         cout << endl << "Second num: ";
  98.         cin >> secondNum;
  99.         if(!validNumForDouble(secondNum)) {
  100.             cout << "Num is not valid";
  101.         } else break;
  102.     }
  103.    
  104.     cout << firstNum << " - " << secondNum << " = " << stol(firstNum) - stol(secondNum);
  105.    
  106.     cout << endl;
  107.     system("pause");
  108. }
  109. void multiply() {
  110.     string firstNum;
  111.     string secondNum;
  112.  
  113.     while(true) {
  114.         cout << endl << "First num: ";
  115.         cin >> firstNum;
  116.         if(!validNumForDouble(firstNum)) {
  117.             cout << "Num is not valid";
  118.         } else break;
  119.     }
  120.    
  121.     while(true) {
  122.         cout << endl << "Second num: ";
  123.         cin >> secondNum;
  124.         if(!validNumForDouble(secondNum)) {
  125.             cout << "Num is not valid";
  126.         } else break;
  127.     }
  128.    
  129.     cout << firstNum << " * " << secondNum << " = " << stol(firstNum) * stol(secondNum);
  130.    
  131.     cout << endl;
  132.     system("pause");
  133. }
  134.  
  135. // todo check for zero
  136. void divide() {
  137.     string firstNum;
  138.     string secondNum;
  139.  
  140.     while(true) {
  141.         cout << endl << "First num: ";
  142.         cin >> firstNum;
  143.         if(!validNumForDouble(firstNum)) {
  144.             cout << "Num is not valid";
  145.         } else break;
  146.     }
  147.    
  148.     while(true) {
  149.         cout << endl << "Second num: ";
  150.         cin >> secondNum;
  151.         if(!validNumForDouble(secondNum)) {
  152.             cout << "Num is not valid";
  153.         } else break;
  154.     }
  155.    
  156.     cout << firstNum << " / " << secondNum << " = " << stol(firstNum) / stol(secondNum);
  157.    
  158.     cout << endl;
  159.     system("pause");
  160.  }
  161.  
  162. void pow() {
  163.     string firstNum;
  164.     string extent;
  165.  
  166.     while(true) {
  167.         cout << endl << "First num: ";
  168.         cin >> firstNum;
  169.         if(!validNumForDouble(firstNum)) {
  170.             cout << "Num is not valid";
  171.         } else break;
  172.     }
  173.    
  174.     while(true) {
  175.         cout << endl << "Extent: ";
  176.         cin >> extent;
  177.         if(!validNumForDouble(extent)) {
  178.             cout << "Num is not valid";
  179.         } else break;
  180.     }
  181.    
  182.     cout << firstNum << " ^ " << extent << " = " << pow(stol(firstNum), stol(extent));
  183.    
  184.     cout << endl;
  185.     system("pause");
  186.  }
  187.  
  188. int main() {
  189.  
  190.     int menuItem = -1;
  191.  
  192.     bool isExit = false;
  193.     while(!isExit) {
  194.         cout
  195.             << "\n 0 - Exit"
  196.             << "\n 1 - Sum"
  197.             << "\n 2 - Subsctract"
  198.             << "\n 3 - Miltiply"
  199.             << "\n 4 - Divide"
  200.             << "\n 5 - Pow: ";
  201.  
  202.         cin >> menuItem;
  203.         if(menuItem < 0 || menuItem > 5) continue;
  204.  
  205.         switch(menuItem) {
  206.             case 0: isExit = true; break;
  207.             case 1: sum(); break;
  208.             case 2: substract(); break;
  209.             case 3: multiply(); break;
  210.             case 4: divide(); break;
  211.             case 5: pow(); break;
  212.             default: cout << "Menu item " << menuItem << " cannot be found";
  213.         }
  214.     }
  215.  
  216.     return 0;
  217. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement