Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <locale.h>
- #include <iostream>
- #include <string>
- #include <cmath>
- using namespace std;
- // todo think of removing unnecessary zeroes in the beginning of the num
- bool validNumForDouble(string num) {
- int countOfComma = 0;
- for (char ch : num) {
- if (ch == '.' || to_string(ch) == to_string(',')) {
- cout << "check " << endl;
- countOfComma++;
- }
- }
- return countOfComma <= 1;
- }
- bool numIsValid(string num) {
- bool hasDigit = false;
- bool metPoints = false;
- if (num.length() == 0 || num[0] < '0' || num[0] > '9' ) return false;
- for (char c : num) {
- if (c >= '0' && c <= '9') {
- hasDigit = true;
- } else if (c == '.' || c == ',') {
- if (metPoints) return false;
- metPoints = true;
- }
- else return false;
- }
- if (!hasDigit) {
- return false;
- }
- return true;
- }
- bool isPositive(string num) {
- if (num[0] == '-') {
- return false;
- } else if (num[0] == '+') {
- return true;
- } else if (num[0] >= '0' && num[0] <= '9') {
- return true;
- }
- return false;
- }
- void sum() {
- string firstNum;
- string secondNum;
- while(true) {
- cout << endl << "First num: ";
- cin >> firstNum;
- if(!numIsValid(firstNum)) {
- cout << "Num is not valid";
- } else break;
- }
- while(true) {
- cout << endl << "Second num: ";
- cin >> secondNum;
- if(!numIsValid(secondNum)) {
- cout << "Num is not valid";
- } else break;
- }
- cout << firstNum << " + " << secondNum << " = " << stol(firstNum) + stol(secondNum);
- cout << endl;
- system("pause");
- }
- void substract() {
- string firstNum;
- string secondNum;
- while(true) {
- cout << endl << "First num: ";
- cin >> firstNum;
- if(!validNumForDouble(firstNum)) {
- cout << "Num is not valid";
- } else break;
- }
- while(true) {
- cout << endl << "Second num: ";
- cin >> secondNum;
- if(!validNumForDouble(secondNum)) {
- cout << "Num is not valid";
- } else break;
- }
- cout << firstNum << " - " << secondNum << " = " << stol(firstNum) - stol(secondNum);
- cout << endl;
- system("pause");
- }
- void multiply() {
- string firstNum;
- string secondNum;
- while(true) {
- cout << endl << "First num: ";
- cin >> firstNum;
- if(!validNumForDouble(firstNum)) {
- cout << "Num is not valid";
- } else break;
- }
- while(true) {
- cout << endl << "Second num: ";
- cin >> secondNum;
- if(!validNumForDouble(secondNum)) {
- cout << "Num is not valid";
- } else break;
- }
- cout << firstNum << " * " << secondNum << " = " << stol(firstNum) * stol(secondNum);
- cout << endl;
- system("pause");
- }
- // todo check for zero
- void divide() {
- string firstNum;
- string secondNum;
- while(true) {
- cout << endl << "First num: ";
- cin >> firstNum;
- if(!validNumForDouble(firstNum)) {
- cout << "Num is not valid";
- } else break;
- }
- while(true) {
- cout << endl << "Second num: ";
- cin >> secondNum;
- if(!validNumForDouble(secondNum)) {
- cout << "Num is not valid";
- } else break;
- }
- cout << firstNum << " / " << secondNum << " = " << stol(firstNum) / stol(secondNum);
- cout << endl;
- system("pause");
- }
- void pow() {
- string firstNum;
- string extent;
- while(true) {
- cout << endl << "First num: ";
- cin >> firstNum;
- if(!validNumForDouble(firstNum)) {
- cout << "Num is not valid";
- } else break;
- }
- while(true) {
- cout << endl << "Extent: ";
- cin >> extent;
- if(!validNumForDouble(extent)) {
- cout << "Num is not valid";
- } else break;
- }
- cout << firstNum << " ^ " << extent << " = " << pow(stol(firstNum), stol(extent));
- cout << endl;
- system("pause");
- }
- int main() {
- int menuItem = -1;
- bool isExit = false;
- while(!isExit) {
- cout
- << "\n 0 - Exit"
- << "\n 1 - Sum"
- << "\n 2 - Subsctract"
- << "\n 3 - Miltiply"
- << "\n 4 - Divide"
- << "\n 5 - Pow: ";
- cin >> menuItem;
- if(menuItem < 0 || menuItem > 5) continue;
- switch(menuItem) {
- case 0: isExit = true; break;
- case 1: sum(); break;
- case 2: substract(); break;
- case 3: multiply(); break;
- case 4: divide(); break;
- case 5: pow(); break;
- default: cout << "Menu item " << menuItem << " cannot be found";
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement