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;
- }
- 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.precision(PRECISION);
- cout << firstNum << " + " << secondNum << " = " << fixed <<(stold(firstNum) + stold(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.precision(PRECISION);
- cout << firstNum << " - " << secondNum << " = " << fixed << (stold(firstNum) - stold(secondNum));
- cout << endl;
- system("pause");
- }
- void multiply() {
- 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.precision(PRECISION);
- cout << firstNum << " * " << secondNum << " = " << fixed << (stold(firstNum) * stold(secondNum));
- cout << endl;
- system("pause");
- }
- void divide() {
- 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 {
- if(stold(secondNum) == 0.0) {
- cout << endl << "Divider cannot be equal to zero";
- continue;
- }
- break;
- }
- }
- int precision = -1;
- while(true) {
- cout << endl << "Enter precision: ";
- cin >> precision;
- if(precision >= 0) break;
- }
- cout.precision(precision);
- cout << endl << "precision is " << precision;
- cout << endl << firstNum << " / " << secondNum << " = " << fixed << (stold(firstNum) / stold(secondNum));
- cout << endl;
- system("pause");
- }
- bool validExtent(string extent) {
- for (char c : extent) {
- if (!isdigit(c)) {
- return false;
- }
- }
- return true;
- }
- void pow() {
- bool firstIsPositive = false;
- bool secondIsPositive = false;
- bool firstIsReal = false;
- bool secondIsReal = false;
- string firstNum;
- string extent;
- while(true) {
- cout << endl << "First num: ";
- cin >> firstNum;
- if(!numIsValid(firstNum, &firstIsPositive, &firstIsReal)) {
- cout << "Num is not valid";
- } else break;
- }
- while(true) {
- cout << endl << "extent: ";
- if(!validExtent(extent)) {
- cout << endl << "The extent is not valid";
- } else break;
- }
- cout.precision(PRECISION);
- cout << endl << firstNum << " ^ " << extent << " = " << fixed << (pow(stold(firstNum),stold(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