hello, I am writing a variable equation solver. Now to get it to work, i just need your calculator to be able to detect random strings instead of numbers. and when it does, i need to assign a random number to that string and have the calculator solve the problem again with that new number. I am also wondering how to get your calculator to detect ( and ) so it can solve problems like (1+1)*(1+1). thanks
thecoolman5 8 Posting Whiz in Training
drkybelk 0 Junior Poster in Training
have a look at the boost Spirit library, there is an example where they do exactly that. basically you create a tree where operators are nodes and expressions subtrees with literals as leaves. It's a parse job of a Dyck-language basically (more or less).
http://www.boost.org/doc/libs/1_46_1/libs/spirit/doc/html/index.html
Can't find the exact location now, but it's one of the examples.
thecoolman5 8 Posting Whiz in Training
here, i finished a normal calculator that can handle paranthesis, order of operations, and unlimited operators.
#include<iostream>
#include<string>
#include<sstream>
#include<windows.h>
#include<conio.h>
#include<ctime>
using namespace std;
int main ()
{
cout << "No variables." << endl;
cout << "Type in cls to clear the screen, and type exit to exit." << endl;
SetConsoleTitle("Calculator");
for(;;)
{
re:
string equation = "";
string equation2 = "";
string equation3 = "";
string equation4 = "";
string equation5 = "";
int non_digit5 = 0;
int non_digit2 = -1;
int non_digit6 = 0;
int c = 0;
int i = 0;
float n1 = 0;
string operators = "";
float n2 = 0;
cin >> equation;
DWORD ticks = GetTickCount();
if(equation == "exit")
{
return 0;
}
if(equation == "cls")
{
system("cls");
goto re;
}
while(true)
{
c = equation.find('(', c);
if(c == -1)
{
break;
}
//PARENTHESIS SOLVING CODE
do
{
non_digit2 = equation.find('(', non_digit2+1);
if(non_digit2 == -1)
{
break;
}
non_digit5 = non_digit2;
}while(non_digit2 != -1);
non_digit5 = non_digit5 + 1;
int non_digit3 = 0;
//DOUBLE PARANTHESIS EQUATIONS
equation4 = equation.substr(non_digit5, 999999999);
int l = equation.length();
int l1 = equation4.length();
non_digit3 = equation4.find(")", non_digit3+1);
l1 = l1 - non_digit3;
l = l - l1;
non_digit3 = l;
equation3 = equation.substr(non_digit3+1, 999999999);
equation2 = equation.substr(non_digit5, non_digit3);
do
{
non_digit2 = equation2.find(')', non_digit2+1);
if(non_digit2 == -1)
{
break;
}
non_digit6 = non_digit2;
}while(non_digit2 != -1);
equation2 = equation2.substr(0, non_digit6);
equation = equation.substr(0, non_digit5-1);
n1 = 0;
operators = "";
n2 = 0;
int non_digit = equation2.find_first_not_of("12345677890.");
string part = equation2.substr(0, non_digit);
stringstream(part) >> n1;
operators = equation2[non_digit];
equation2 = equation2.substr(non_digit + 1);
while(true)
{
n2 = 0;
non_digit = equation2.find_first_not_of("1234567890.");
part = equation2.substr (0, non_digit);
stringstream(part) >> n2;
if(int(non_digit) == 0)
{
string part = equation2.substr (0, non_digit+3);
stringstream(part) >> n2;
equation2 = equation2.substr(non_digit + 1);
}
if(operators == "+")
{
n1 = n1 + n2;
}
if(operators == "-")
{
n1 = n1 - n2;
}
if(operators == "*")
{
n1 = n1 * n2;
}
if(operators == "/")
{
n1 = n1 / n2;
}
if(non_digit == -1)
{
break;
}
non_digit = equation2.find_first_not_of("0123456789.");
operators = equation2[non_digit];
equation2 = equation2.substr(non_digit + 1);
}
stringstream out;
out << n1;
equation = equation + out.str();
equation = equation + equation3;
}
//ORDER OF OPERATIONS
while(true)
{
c = equation.find_first_not_of("1234567890.-+");
if(c == -1)
{
break;
}
equation2 = "";
equation3 = "";
equation4 = "";
operators = "";
float n1 = 0;
float n2 = 0;
float n3 = 0;
int non_digit = equation.find_first_not_of("1234567890.-+");
operators = equation[non_digit];
equation2 = equation.substr(non_digit+1, 999999999);
stringstream(equation2) >> n1;
equation3 = equation.substr(0,non_digit);
reverse(equation3.begin(), equation3.end());
//BEGIN FIXING REVERSED EQUATION
non_digit = equation3.find_first_not_of("1234567890.");
if(non_digit == -1)
{
reverse(equation3.begin(), equation3.end());
stringstream(equation3) >> n2;
}
else
{
equation5 = equation3.substr(0, non_digit);
reverse(equation5.begin(), equation5.end());
stringstream(equation5) >> n2;
}
stringstream out;
out << n2;
equation4 = equation4 + out.str();
equation4 = equation4 + operators;
stringstream out1;
out1 << n1;
equation4 = equation4 + out1.str();
//END OF FIND OPERATOR
int l = equation4.length();
non_digit = equation.find(equation4);
equation2 = equation.substr(0, non_digit);
equation4 = equation.substr(non_digit+l, 999999999);
if(operators == "/")
{
n3 = n2 / n1;
}
if(operators == "*")
{
n3 = n2 * n1;
}
stringstream out2;
out2 << n3;
equation2 = equation2 + out2.str();
equation2 = equation2 + equation4;
equation = equation2;
}
//START FIXED EQUATION SOLVING
n1 = 0;
operators = "";
n2 = 0;
size_t non_digit = equation.find_first_not_of("12345677890.");
string part = equation.substr (0, non_digit);
stringstream(part) >> n1;
operators = equation[non_digit];
equation = equation.substr(non_digit + 1);
while (true)
{
n2 = 0;
size_t non_digit = equation.find_first_not_of("1234567890.");
string part = equation.substr (0, non_digit);
stringstream(part) >> n2;
if(int(non_digit) == 0)
{
string part = equation.substr (0, non_digit+3);
stringstream(part) >> n2;
equation = equation.substr(non_digit + 1);
}
if(operators == "+")
{
n1 = n1 + n2;
}
if(operators == "-")
{
n1 = n1 - n2;
}
if(operators == "*")
{
n1 = n1 * n2;
}
if(operators == "/")
{
n1 = n1 / n2;
}
if (int(non_digit) == -1)
{
break;
}
non_digit = equation.find_first_not_of("0123456789.");
operators = equation[non_digit];
equation = equation.substr(non_digit + 1);
}
cout << "Speed of calculation: " << GetTickCount() - ticks << " milliseconds." << endl;
cout << "Result : " << n1 << endl;
system("pause");
goto re;
}
}
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.