2 Replies - 827 Views - Last Post: 09 July 2013 - 08:42 AM Rate Topic: -----

#1 mrcalzones  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 22-January 12

Variable Substitution function

Posted 09 July 2013 - 08:22 AM

Hey everyone! I am trying to make a program in DevC++ where the user types in an equation with an 'x' variable (example: 2+3x).

Using MATLAB, i can easily substitute the x with whatever number i want using "subs()". Then afterwards i can find the answer to the equation.

I've searched and searched but couldn't find a similar function that could do that for me in C++. Does anyone know how or am i just searching for something that doesn't exist? Its really my choice on doing it in C++, i could do MATLAB but i feel more accomplished if i do it in C++. heheh thanks for your help;

Is This A Good Question/Topic? 0
  • +

Replies To: Variable Substitution function

#2 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2296
  • View blog
  • Posts: 3,553
  • Joined: 21-June 11

Re: Variable Substitution function

Posted 09 July 2013 - 08:41 AM

In C++ 11 you can use regex_replace from <regex>.

In prior versions of C++ there is no find-and-replace function, so you'll need to find all occurrences of the variable using find in a loop and replace each one using replace.

That said, parsing your expression into a tree and then walking that tree might be a better way to do what you want than working on the expression using string operations. But of course that depends on what exactly you're planning to do with your expression.

This post has been edited by sepp2k: 09 July 2013 - 08:42 AM

Was This Post Helpful? 0
  • +
  • -

#3 BetaWar  Icon User is offline

  • #include "soul.h"
  • member icon

Reputation: 1369
  • View blog
  • Posts: 7,873
  • Joined: 07-September 06

Re: Variable Substitution function

Posted 09 July 2013 - 08:42 AM

It is possible to do in C++, but it requires a lot of work. Basically matlab already has all of the functionality in place to take the equations your write and parse them down into something a computer can work with. In C++ you can type in the equation, but it will just see a string until you parse it into an equation, and even then it won't know how to solve that equation until you tell it how to work with the operators and variables.

Basically, what you are attempting to do in C++ is the same thing that needs to be done when creating a programming language.
First you have to lexically analyze the input string (program) to ensure that the string works out into your lexical groups (such as keywords, operators, variables, numbers, etc.)
Second you have to take that lexical information and parse it into an expression tree and from there you can ensure that the syntax makes sense in your language (you don't have any syntax errors)
Third, at this point things diverge a bit between a compiled and interpreted language, you can either (a ) begin getting the assembly working for your variables and functionality in the program to compile into an executable later, or (b ) interpret the expression tree and execute any additional code needed to run the program.

Obviously there are some more interesting elements that go into the creation of a language, but that should give you a feel for how difficult it can be. You can always attempt to spoof the processes by going character by character through your input string and attempting to execute things on the way, but that gets a bit more difficult once again.

For a quick example, I created a calculator application in Java which allowed you to type in an equation using mathematical functions (no variables) and get the result, or see the errors that were found while parsing it and that program was 889 lines long.

If you just had to create a calculator, you could do that with stacks and queues and not worry about all the steps labeled above, but from the sounds of it you need variables, and while you may be able to accomplish that with maps thrown in there too I believe it would get confusing quickly.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1