0

Not Yet Answered # Variable equation solver

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

drkybelk Discussion Starter thecoolman5 8

0

0

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.

0

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;
}
}
```

This article has been dead for over six months. Start a new discussion instead.

Recommended Articles

Help! I want to create a java program that finds the highest even integer among the values entered by the user. Stop asking values when a value less than 1 have been entered. If no even integer is entered, display "No Even Integer"

Here is the sample output that I ...

Hello All ...

Iam Getting An Error With try to excecute the stored procedure .

I have Have Sql database , the stored procedure like so :

```
USE [MPRS]
GO
/****** Object: StoredProcedure [dbo].[Search_Licenses_By_Number] Script Date: 26-Nov-16 8:06:52 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE ...
```

I don’t want at this stage work on a big separate project as I've already got plenty ...