#include #include using namespace std; //Order of operations: parenthases functions, mult/devide, add/subtract //negative numbers wont really work I geuss. //----Whenever there is a "(-" or "[start]-" replace '-' with "0-" //Prototypes string replacesubstr(string modifyString, const string replaceString, const unsigned short start, const unsigned short length); string math(string mathstring); string floattostring(const float in); float stringtofloat(const string in); float solve(string operationString); //Main int main() { string solvestring; cout << "Welcome to calculator! Type -help for commands." << endl; while(solvestring != "-exit") { cin >> solvestring; if(solvestring == "-help") cout << "-exit -- Exit the program\n-save -- Start saveing output in a text file.\n-stopsave -- Stop saving output\n-changesave -- Change the output file is saved too \n-functions -- List avilible functions\n"; else if(solvestring == "-exit") return 0; else if(solvestring == "-save") cout << "Not Yet Implanted\n"; else if(solvestring == "-stopsave") cout << "Not Yet Implanted\n"; else if(solvestring == "-functions") cout << "sin, cos, tan, asin, acos, and atan, sqrt\n"; else if(solvestring == "-changesave") cout << "Not Yet Implanted\n"; else if(solvestring[0] == '-') cout << "Command not reconized\n"; else if(/*Valid Math Entry*/true) cout << solve(solvestring) << endl; else cout << "Syntax error\n"; } } //Uses string sream to convert a float to a string string floattostring(const float in) { stringstream ss (stringstream::in | stringstream::out); ss << in; return ss.str(); } //Convert a string to a float using atof with a C style string float stringtofloat(const string in) { return atof(in.c_str()); } //Returns an equation fully solved float solve(string operationString) { unsigned short pernthases = 0; for(unsigned short i = 0; operationString[i] != 0; i++) if(operationString[i] == '(') pernthases++; unsigned short start, end, foundPernth; //For Each pernthases, set the start of the perenthesis in start, and end in end, then operat on it and replace the substring. while(pernthases > 0) { //Find rightmost '(' start = 0; foundPernth = 0; while(foundPernth != pernthases) { start++; if(operationString[start] == '(') foundPernth++; } //Find First closeing ')' for(end = start; operationString[end] != ')'; end++); //Solve Inside the Perenthisis operationString = replacesubstr(operationString, math(operationString.substr(start+1, end-start)), start, end-start+1); pernthases--; } //Compleat return stringtofloat(math(operationString)); } //Replace a substring in a string with another string string replacesubstr(string modifyString, const string replaceString, const unsigned short start, const unsigned short length) { //Mofify modifystring so that substring between start and length is replaced with replace string return modifyString.substr(0, start) + replaceString + modifyString.substr(length+start+1, modifyString.length()-(length+start+1)); } //Compleates a simple equation (No perantheses) string math(string mathstring) { unsigned short operatorChar, LeftNumber, RightNumber, functions = 0, multaple = 0, power = 0, addition = 0; for(unsigned short i = 0; mathstring[i] != 0; i++) //Count Each operation { if(mathstring[i] == '*' || mathstring[i] == '/') multaple++; if(mathstring[i] == '+' || mathstring[i] == '-') addition++; if(mathstring[i] == '^') power++; if(mathstring[i+1] != 0 && mathstring[i+2] != 0 && mathstring[i+3] != 0) if( mathstring[i] == 's' && mathstring[i+1] == 'i' && mathstring[i+2] == 'n' || mathstring[i] == 'c' && mathstring[i+1] == 'o' && mathstring[i+2] == 's' || mathstring[i] == 't' && mathstring[i+1] == 'a' && mathstring[i+2] == 'n' || mathstring[i] == 'a' && mathstring[i+1] == 's' && mathstring[i+2] == 'i' && mathstring[i+3] == 'n' || mathstring[i] == 'a' && mathstring[i+1] == 'c' && mathstring[i+2] == 'o' && mathstring[i+3] == 's' || mathstring[i] == 's' && mathstring[i+1] == 'q' && mathstring[i+2] == 'r' && mathstring[i+3] == 't' || mathstring[i] == 'a' && mathstring[i+1] == 't' && mathstring[i+2] == 'a' && mathstring[i+3] == 'n') functions++; } //sin, cos, tan, asin, acos, and atan, sqrt //for each function //Power '^' while(multaple > 0) { for(operatorChar = 0; mathstring[operatorChar] != '*' && mathstring[operatorChar] != '/'; operatorChar++); for(LeftNumber = operatorChar-1; LeftNumber != 0 && isdigit(mathstring[LeftNumber]) || mathstring[LeftNumber] == '.'; LeftNumber--); for(RightNumber = operatorChar+1; RightNumber != mathstring.length()-1 && isdigit(mathstring[RightNumber]) || mathstring[RightNumber] == '.'; RightNumber++); if(mathstring[operatorChar] == '*') mathstring = replacesubstr(mathstring, floattostring(stringtofloat(mathstring.substr(LeftNumber,operatorChar-LeftNumber)) * stringtofloat(mathstring.substr(operatorChar+1,operatorChar-RightNumber))), LeftNumber, RightNumber); else mathstring = replacesubstr(mathstring, floattostring(stringtofloat(mathstring.substr(LeftNumber,operatorChar-LeftNumber)) / stringtofloat(mathstring.substr(operatorChar+1,operatorChar-RightNumber))), LeftNumber, RightNumber); multaple--; } while(addition>0) { for(operatorChar = 0; mathstring[operatorChar] != '+' && mathstring[operatorChar] != '-'; operatorChar++); for(LeftNumber = operatorChar-1; LeftNumber != 0 && isdigit(mathstring[LeftNumber]) || mathstring[LeftNumber] == '.'; LeftNumber--); for(RightNumber = operatorChar+1; RightNumber != mathstring.length()-1 && isdigit(mathstring[RightNumber]) || mathstring[RightNumber] == '.'; RightNumber++); if(mathstring[operatorChar] == '+') mathstring = replacesubstr(mathstring, floattostring(stringtofloat(mathstring.substr(LeftNumber,operatorChar-LeftNumber)) + stringtofloat(mathstring.substr(operatorChar+1,operatorChar-RightNumber))), LeftNumber, RightNumber); else mathstring = replacesubstr(mathstring, floattostring(stringtofloat(mathstring.substr(LeftNumber,operatorChar-LeftNumber)) - stringtofloat(mathstring.substr(operatorChar+1,operatorChar-RightNumber))), LeftNumber, RightNumber); addition--; } return mathstring; }