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>
- #include <iomanip>
- using namespace std;
- #define PRECISION 5
- // todo think of removing unnecessary zeroes in the beginning of the num
- bool validNumForDouble(string num) {
- return true;
- }
- enum NumSign {
- POSITIVE,
- NEGATIVE,
- NOT_CORRECTED
- };
- NumSign getNumSign(string num) {
- int countMinusInTheBeginning = 0;
- int countPliusInTheBeginning = 0;
- for(int i = 0; i < num.length(); i++) {
- if(num[i] == '-') countMinusInTheBeginning++;
- if(num[i] == '+') countPliusInTheBeginning++;
- }
- if(countMinusInTheBeginning > 1 || countPliusInTheBeginning > 1) return NOT_CORRECTED;
- if (num.length() == 0) {
- return NOT_CORRECTED;
- }
- if (num[0] == '-') {
- return NEGATIVE;
- } else if (num[0] == '+') {
- return POSITIVE;
- } else if (num[0] >= '0' && num[0] <= '9') {
- return POSITIVE;
- } else {
- return NOT_CORRECTED;
- }
- }
- bool numIsValid(string num, bool *isPositive, bool *isReal) {
- bool hasDigit = false;
- bool metPoints = false;
- NumSign numSign = getNumSign(num);
- if(numSign == NOT_CORRECTED) return false;
- string numWithoutSign = num;
- if(num[0] == '-' || num[0] == '+') numWithoutSign = num.substr(1, num.length());
- if(numWithoutSign[0] < '0' || numWithoutSign[0] > '9') return false;
- (*isPositive) = numSign == POSITIVE;
- for (char c : numWithoutSign) {
- if (c >= '0' && c <= '9') {
- hasDigit = true;
- } else if (c == '.' || c == ',') {
- if (metPoints) return false;
- metPoints = true;
- }
- else return false;
- }
- if (!hasDigit) {
- return false;
- }
- (*isReal) = metPoints;
- 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;
- }
- bool validNumSign(string num) {
- }
- void sum() {
- bool firstIsPositive = false;
- bool secondIsPositive = false;
- bool firstIsReal = false;
- bool secondIsReal = false;
- string firstNum;
- string secondNum;
- while(true) {
- cout << endl << "First num: ";
- cin >> firstNum;
- if(!numIsValid(firstNum, &firstIsPositive, &firstIsReal)) {
- cout << "Num is not valid";
- } else break;
- }
- while(true) {
- cout << endl << "Second num: ";
- cin >> secondNum;
- if(!numIsValid(secondNum, &secondIsPositive, &secondIsReal)) {
- cout << "Num is not valid";
- } else break;
- }
- cout << endl << "first is positive " << firstIsPositive << " is real " << firstIsReal;
- cout << endl << "second is positive " << secondIsPositive << " is real " << secondIsReal;
- cout << endl;
- setprecision(PRECISION);
- cout << firstNum << " + " << secondNum << " = " << stod(firstNum) + stod(secondNum);
- cout << endl;
- system("pause");
- }
- void substract() {
- bool firstIsPositive = false;
- bool secondIsPositive = false;
- bool firstIsReal = false;
- bool secondIsReal = false;
- string firstNum;
- string secondNum;
- while(true) {
- cout << endl << "First num: ";
- cin >> firstNum;
- if(!numIsValid(firstNum, &firstIsPositive, &firstIsReal)) {
- cout << "Num is not valid";
- } else break;
- }
- while(true) {
- cout << endl << "Second num: ";
- cin >> secondNum;
- if(!numIsValid(secondNum, &secondIsPositive, &secondIsReal)) {
- cout << "Num is not valid";
- } else break;
- }
- cout << endl << "first is positive " << firstIsPositive << " is real " << firstIsReal;
- cout << endl << "second is positive " << secondIsPositive << " is real " << secondIsReal;
- cout << endl;
- setprecision(PRECISION);
- cout << firstNum << " - " << secondNum << " = " << stod(firstNum) - stod(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;
- }
- int signsAfterComma = -1;
- while(signsAfterComma < 0) {
- cout << endl << "Enter signs after comma: ";
- cin >> signsAfterComma;
- }
- cout << endl << "Signs after comma: " << signsAfterComma;
- 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