1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
|
#include<string>
#include<iostream>
#include<sstream>
using namespace std;
int isnumber(string mystring, int loc) {
const int NoConstSt = 49;
const int NoConstEn = 58;
cout << "The operation is: " << (int) mystring.at(loc) << "\n";
if ((int)mystring.at(loc) >= NoConstSt && (int)mystring.at(loc) <= NoConstEn) return 1; //This returns one, which to the program will mean "this container holds a number".
else if ((int)mystring.at(loc) == 45) return 2; //Means we do subtraction.
else if ((int)mystring.at(loc) == 43) return 3; //Means we do addition.
else if ((int)mystring.at(loc) == 42) return 4; //Means we do multiplication.
else if ((int)mystring.at(loc) == 47) return 5; //Means we do division.
else if ((int)mystring.at(loc) == 40) return 6; //Means we have an opening parantheses.
else if ((int)mystring.at(loc) == 41) return 7; //Means we have a closing parantheses.
else if ((int)mystring.at(loc) == 46) return 8; //Means we have a decimal.
else return 0;
}
double compute ( double var1, double var2, int operand) {
switch ( operand )
{
case 2:
return var1 - var2;
case 3:
return var1 + var2;
case 4:
return var1 * var2;
case 5:
return var1 / var2;
default:
return 0;
}
}
double extractnumber (int startloc, string &mathexpression) {
int b;
for (int unsigned c = startloc; c <= mathexpression.length() - 1; c++ ) {
static string mystring;
static int iteration = 0; //Counts the number of iterations.
double result;
iteration++;
b = isnumber(mathexpression,c);
if (b >= 2 && b <= 7 || c == mathexpression.length() - 1) {
cout << "Mathexpression before number deletion: " << mathexpression << " Iteration is: " << iteration << "\n";
stringstream (mystring) >> result;
mathexpression.erase(startloc, c + 1);
cout << "Mathexpression after number deletion: " << mathexpression<< "\n";
return result;
}
else if (b == 8) {
continue;
}
else if (b == 1){
mystring = mystring + mathexpression.substr(c);
}
}
}
int main ()
{
double var1 = 0;
double var2 = 0;
double var3 = 0;
int initvar = 0;
int closevar = 0;
int endvar;
bool parantbool = false;
bool usebool = false;
int operand = 0;
int operation = 0;
double result = 0;
string OO;
string mystr;
cout << "Enter a series of mathematical operations: \n";
getline (cin, OO); //Enter "5+5("
endvar = OO.length() - 1;
for (int signed c = 0; c <= endvar; c++ ) {
operand = isnumber(OO, c);
endvar = OO.length() -1;
if (operand == 6 && parantbool == false) {
initvar = c;
cout << "initvar is: " << initvar << " \n";
}
else if (operand == 7 && parantbool == false) {
closevar = c;
cout << "closevar is: " << closevar << "\n";
parantbool = true;
}
else if (c == endvar && parantbool == true) {
c = initvar;
endvar = closevar;
}
else if (operand == 1 && parantbool = true) {
if (usebool == false) {
var1 = extractnumber(c + 1, OO);
}
else if (usebool == true) {
var2 = extractnumber(c, OO);
result = compute(var1,var2,operation);
cout << "Result is: " << result;
}
else if (operand >= 2 && operand <= 5) {
operation = operand;
}
}
}
cin >> OO;
return 0;
}
//Just for a note: / = 47, * = 42, + = 43, - = 45, ( = 40, ) = 40, and . = 46 . These are the operators (in ASCII)
|