0

I have a mathematical expression which is given via a string.For example: -4^2+3*(-23^2+5), I want the resultat to be (0-4)^2+3*((0-23)^2+5) I need to write a function which does that kind of thing for the full string. Can anyone help? thanx in advance

Rati Sharabidze
  • 69
  • 1
  • 12
  • Questions like "I need some code that will do this, can you give it to me" are generally considered off-topic here. Those are questions better suited for freelance websites. :) –  Apr 16 '15 at 07:26
  • Why? Any mathematician would prefer the former, and the other people don't count :-| And why not parentheses around the lot? and around `(0-4)^2` and `3*((0-23)^2+5)`? What's the rule? – user207421 Apr 16 '15 at 07:43
  • the problem is more serious, in reality I am calculating the expression which is given via a string,the problem is that I am having trouble on powering negative numbers. – Rati Sharabidze Apr 16 '15 at 07:48
  • the only thing I think to do is a thing like that – Rati Sharabidze Apr 16 '15 at 07:49
  • Solve the *problem.* not your first attempt at the solution. Adding parentheses in apparently arbitary places is neither implementable nor a solution. If you can work out where the parentheses appear to be needed, you don't need the parentheses. If you can't, which you can't, it's because you haven't solved the original problem. Get rid of this post and tell us what your problem with 'powering negative numbers' is. – user207421 Apr 16 '15 at 07:51
  • I am solving it dude, look at this video which I made https://www.youtube.com/watch?v=JoBcIKtb6RQ – Rati Sharabidze Apr 16 '15 at 07:57
  • it is an old video, I do not need to write spaces anymore – Rati Sharabidze Apr 16 '15 at 07:58
  • 1
    You won't solve it with this technique. The same parsing technique that tells you where to put these redundant parentheses also parses the expression correctly without the parentheses. Surely this is obvious? And nobody is going to watch your YouTube videos. Questions asked here must be complete and self-contained. – user207421 Apr 16 '15 at 08:17
  • this video is a demo,you did not believe that I was doing it – Rati Sharabidze Apr 16 '15 at 09:16
  • help me in writting the function that I described if you want or can. Nobody is forcing you. – Rati Sharabidze Apr 16 '15 at 09:17
  • 1
    Nobody is forcing you to post a proper question, but until you do you will only get irrelevant or rubbish answers, and adverse comment. Your YouTube video was, as expected, a complete waste of everybody's time. Nobody believed you weren't doing it, but everybody believed you were barking up the wrong tree. Try listening to what you're told some time. – user207421 Apr 16 '15 at 09:54
  • I agree that nobody is forcing me. If you do not want help,then stop posting comments.Good luck – Rati Sharabidze Apr 16 '15 at 11:29
  • Mathematically, -4^2 should be parsed as 0-(4^2). If you want (-4)^2, then you need to start with the parentheses there. – Teepeemm Apr 16 '15 at 11:36
  • Teepeemm,yes, I need to do (0-4)^2 everywhere in any string.I need a function which does that – Rati Sharabidze Apr 16 '15 at 12:31

1 Answers1

1

I suggest you (1) create a parser for your expressions, then (2) parse the string into an abstract syntax tree (AST), and then (3) pretty print the AST according to your needs.

(1): Writing a parser by hand gets messy quickly, so I suggest you write your grammar in some nice BNF format and use a parser generator. See for instance this question: C++ parser generator

(2): Parsing the expression should boil down to calling a parse method on your generated parser.

(3): To pretty print the AST you can either write a recursive function that traverses the tree, or use a visitor, or put the pretty print methods directly on the different nodes and rely on polymorphism.

Community
  • 1
  • 1
aioobe
  • 413,195
  • 112
  • 811
  • 826
  • the problem is more serious, in reality I am calculating the expression which is given via a string,the problem is that I am having trouble on powering negative numbers. – Rati Sharabidze Apr 16 '15 at 07:49
  • If you're *evaluating* the expression, you *definitely* should parse the expression into a proper AST. Do you have an AST representing the expression? – aioobe Apr 16 '15 at 08:05
  • Have a look at [this question](http://stackoverflow.com/questions/9329406/evaluating-arithmetic-expressions-in-c) or [this](http://stackoverflow.com/questions/9439295/convert-string-to-mathematical-evaluation) for instance. – aioobe Apr 16 '15 at 08:09
  • I use Reverse polish Notation and than I calculate. the main difficulty is that the minus can be the sign minus like -4 and can be an operator 8-5 – Rati Sharabidze Apr 16 '15 at 08:20
  • A better question to ask is then probably "How to disambiguate between unary and binary minus when transforming to reverse polish notation.". – aioobe Apr 16 '15 at 08:24
  • just help me in writting this function if you can or want, – Rati Sharabidze Apr 16 '15 at 09:18
  • Was your issue resolved? Would you mind [accepting](http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work) this answer so we get a closure, or could you elaborate on what is unresolved? – aioobe May 11 '15 at 08:36
  • this question can be closed, I do not need such thing anymore,thanx – Rati Sharabidze May 19 '15 at 09:09
  • Future readers may stumble across the same issue. If you think my answer properly addresses your question, you can mark it as accepted to indicate that you're satisfied at this point. – aioobe May 19 '15 at 09:12