Help With Simple Equation Parsing

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Nov 2007
Posts: 390
Reputation: skatamatic will become famous soon enough skatamatic will become famous soon enough 
Solved Threads: 39
skatamatic skatamatic is offline Offline
Posting Whiz

Help With Simple Equation Parsing

 
0
  #1
Nov 5th, 2008
I'm writing a class for simple equation parser. I've completed the - and + operators, and am currently working on implementing brackets. They seem to work most of the time, except I occassionally get strange results, such as with 10 - ((10 - 20) + (10 - 20)) returns 40! It should only be 30 >.< ! I've tried stepping through the code to see what was going wrong, and I know for sure that I'm handling brackets incorrectly. I think I'm on the right track for this, but any help would be much appreciated.

This is the function cpp:
  1. #include "Function.h"
  2.  
  3.  
  4. CFunction::~CFunction(void)
  5. {
  6. }
  7.  
  8. int CFunction::GetNextValue(int & i)
  9. {
  10. string sBuffer;
  11. char szFormula[255];
  12. strcpy_s(szFormula, 255, _sFunc.c_str());
  13. bool numStream(false);
  14. for (i = i; i < strlen(szFormula); ++i)
  15. {
  16. if (isdigit(szFormula[i]))
  17. {
  18. //add the digit to the buffer
  19. numStream = true;
  20. sBuffer += szFormula[i];
  21. }
  22. if (numStream && (!isdigit(szFormula[i]) || i == strlen(szFormula) - 1))
  23. {
  24. return atoi(sBuffer.c_str()); //return the buffer, converted to int
  25. }
  26. if (szFormula[i] == '(')
  27. {//solve what's inside brackets first!
  28. if (numStream)
  29. throw exception ("Bracket error");
  30. string tmp(ToCloseBracket(i));
  31. return CFunction(tmp).Solve();
  32. }
  33. }
  34. //error flag
  35. return -1;
  36. }
  37.  
  38. string CFunction::ToCloseBracket(int & iIndex)
  39. {
  40. //problem function!!!***********************
  41. string sBuffer = "";
  42. int iCount(0), iCount2(0);
  43. for (int i(iIndex); i < strlen(_sFunc.c_str()); ++i)
  44. {
  45. if (_sFunc[i] == '(')
  46. iCount++;
  47. }
  48. while (iCount2 < iCount)
  49. {
  50. if (_sFunc[iIndex++] == ')')
  51. iCount2++;
  52. else
  53. sBuffer += _sFunc[iIndex];
  54. }
  55. return sBuffer;
  56. }
  57.  
  58. int CFunction::Solve(char var)
  59. {
  60. int i(0),
  61. iTmpVal(0),
  62. iReturn(0);
  63. char op(0);
  64. iReturn = GetNextValue(i);
  65. while (i < strlen(_sFunc.c_str()) - 1)
  66. {
  67. op = GetNextOp(i);
  68. iTmpVal = GetNextValue(i);
  69. switch (op)
  70. {
  71. case '+': iReturn += iTmpVal; break;
  72. case '-': iReturn -= iTmpVal; break;
  73. default: break;
  74. }
  75. }
  76. return iReturn;
  77. }
  78.  
  79. char CFunction::GetNextOp(int & i)
  80. {
  81. for (i = i; i < strlen(_sFunc.c_str()); ++i)
  82. if (_sFunc[i] == '+' || _sFunc[i] == '-' || _sFunc[i] == '*' || _sFunc[i] == '/')
  83. return _sFunc[i];
  84. //error flag
  85. return -1;
  86. }

This is the header:
  1. #pragma once
  2. #pragma warning(disable: 4018)
  3. #include <iostream>
  4. #include <string.h>
  5. using namespace std;
  6.  
  7. class CFunction
  8. {
  9. protected:
  10. string _sFunc;
  11. int _iCursor;
  12. int GetNextValue(int &);
  13. char GetNextOp(int &);
  14. string ToCloseBracket(int &);
  15. public:
  16. bool SetFunc(string);
  17. int Solve(char = 0);
  18. CFunction(string s): _iCursor(0)
  19. {
  20. _sFunc = s;
  21. }
  22. ~CFunction(void);
  23. };

and here is the main() implementation

  1. #include <string>
  2. #include <iostream>
  3. using namespace std;
  4. #include "Function.h"
  5.  
  6. int main()
  7. {
  8. char szBuffer[255];
  9. string sBuffer = "";
  10. cout << "Enter formula: ";
  11. cin.getline(szBuffer, 254);
  12. sBuffer = szBuffer;
  13. CFunction myFunc(sBuffer);
  14. cout << sBuffer << " = " << myFunc.Solve() << endl;
  15. cin.get();
  16. return 0;
  17. }
Last edited by skatamatic; Nov 5th, 2008 at 6:34 pm.
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 275
Reputation: dougy83 is on a distinguished road 
Solved Threads: 45
dougy83 dougy83 is offline Offline
Posting Whiz in Training

Re: Help With Simple Equation Parsing

 
0
  #2
Nov 6th, 2008
I wrote an equation parser a few years back, don't have it on me, but I'll try to dig it up for you.

I think the idea I used was to pass through the equation string a few times - the first time assigns a numerical priority to each operator (based on 'BODMAS' - i.e. bracket, orders (powers/exponents), division, multiplication, addition, subtraction). The higher the priority, the sooner the operation is done. I think I increased the base priority (a value I added to the relative priority value given by 'BODMAS') by say 10 when I ran into an open bracket, and decreased it by 10 when I hit a close bracket, that way the operators inside the deepest set of brackets will have the highest priority.

Then running through again to evaluate the highest priority operator, replacing the operator and 2 operands with the result. This loop continues until only a single value is left, which is the answer to the equation.

It was really quick and simple to write (and I was a beginner back then), so if you want to try the above method, have a go. I'll see if I can find the original in the mean time.
Last edited by dougy83; Nov 6th, 2008 at 3:50 am.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,273
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 378
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Help With Simple Equation Parsing

 
0
  #3
Nov 6th, 2008
Google rpn/shunting yard algorithm.

http://www.daniweb.com/code/snippet599.html
Last edited by iamthwee; Nov 6th, 2008 at 5:08 am.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC