0

I always asked user to input x and y and then calculate the result. This is a very simple example :

int main()
{
int x,y;
cout << "Please enter x" <<endl;
cin >> x ;
cout << "please enter y" <<endl;
cin >> y;

int result = x + y ;
cout << result << endl;
return 0;
}

but what if the user inputs the full expression ? for example asking the user to input an expression and the user input : 4+5, can we calculate this directly and give the result?

Andy M
  • 167
  • 1
  • 2
  • 7
  • possible duplicate of [What is the best way to evaluate mathematical expression in C++?](http://stackoverflow.com/questions/5115872/what-is-the-best-way-to-evaluate-mathematical-expression-in-c) and several other questions. – lhf Feb 19 '13 at 00:30
  • thanks for the link, I looked at it before posting this question but it wasn't helpful so I decided to post a new question – Andy M Feb 22 '13 at 07:02

3 Answers3

1

I would not spend time reinventing the wheel. I'd use an existing scripting language for the users input, and my personal choice there would be lua, though many others are feasible.

This is roughly what your application would look like using lua as an interpreter.

#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"

#include <string>
#include <iostream>


int main()
{
  using namespace std;
  string x,y;
  cout << "Please enter x" <<endl;
  cin >> x ;
  cout << "please enter y" <<endl;
  cin >> y;

  //TODO: You may want to check that they've entered an expression
  //
  lua_State * L = lua_open();

  // We only import the maths library, rather than all the available
  // libraries, so the user can't do i/o etc.

  luaopen_math(L);
  luaopen_base(L);

  // We next pull the math library into the global namespace, so that
  // the user can use sin(x) rather than math.sin(x).
  if( luaL_dostring(L,"for k,v in pairs(math) do _G[k] = v end") != 0)
  {
    std::cout<<"Error :"<<lua_tostring(L,-1)<<std::endl;
    return 1;
  }

  // Convert x to an integer
  x = "return "+x;
  if( luaL_dostring(L,x.c_str()) != 0 )
  {
    std::cout<<"Error in x :"<<lua_tostring(L,-1)<<std::endl;
    return 1;
  }
  int xv = lua_tointeger(L,-1);
  lua_pop(L,1);

  // Convert y to an integer
  y = "return "+y;
  if( luaL_dostring(L,y.c_str()) != 0 )
  {
    std::cout<<"Error in y :"<<lua_tostring(L,-1)<<std::endl;
    return 1;
  }
  int yv = lua_tointeger(L,-1);
  lua_pop(L,1);

  int result = xv + yv ;
  cout << result << endl;
  return 0;
}

With this you can enter things like 1+sin(2.5)*3.

Michael Anderson
  • 70,661
  • 7
  • 134
  • 187
  • In that case, the OP would be better off using my [ae](http://www.tecgraf.puc-rio.br/~lhf/ftp/lua/#ae). – lhf Feb 20 '13 at 13:20
  • @lhf Quite likely - but I have no experience with ae. Maybe you could/should contribute an example that shows what the code would look like using ae? – Michael Anderson Feb 21 '13 at 09:06
1

Following up on Michael Anderson's answer, using my ae front-end for the Lua engine, the heart of the code would be simply

ae_open();
ae_set("x",x);
ae_set("y",y);
int result = ae_eval("x + y");
ae_close();

Of course, the fun part is when ae_eval gets a strings containing an arbitrary expression input from the user.

lhf
  • 70,581
  • 9
  • 108
  • 149
0

You should implement infix notation parsing algorithm.

For example; http://en.wikipedia.org/wiki/Shunting-yard_algorithm

mert
  • 920
  • 3
  • 15
  • 24
  • not sure how can I use that, I want to calculate this expression : `4+5/3` with condition that the user inputs this expression directly. if we use string, can we do a read for the operations? – Andy M Feb 18 '13 at 23:16
  • Yeah,you can read but Infix notation a little difficult.I said,Shunting-yard algorithm for this job.You can research Postfix Notation for your brain clear. – mert Feb 18 '13 at 23:29