0

I have been asked a question in an interview and want to know program approach to solve it.

Que: We have a text file which contain operation need to be performed in a program. User can update this text file and change the operation i.e. text file can contain + for addition or - for subtraction. Program has two variable i.e. a and b, reads text file, perform operation and display result. If text file contain +, then program should return sum of a and b and if text file has - then program should return a-b. User can put any operation in text file.

I have given two approaches:

  1. Program can have switch statement in it. If text file has +, program check switch statement and perform a+b operation as per switch statement, like wise for for other operations. This answer was rejected as we have to hard code all possible operations in switch.

  2. I can use oracle and run query for any operation i.e. if text file has +, then I can create a sql string like 'select a+b into :result from dual;' and run an embedded sql in program. Database will execute sql and return output for any valid operation and program need not to hard code all possible operation. I have given this answer as I was giving interview for C++/C and pro*c. But panel was not satisfied with this approach also.

So what is the best approach to solve this problem through a program ?

  • In any case you'll have to implement conditional logic to perform an operation selectively based on logical input (such as an arithmetic operator). Option 1 is the only way to go, unless you implement an entire virtual machine. – voodooattack Jan 26 '14 at 11:46
  • Heavy elf hack? Symbol table should contain the name you function and it is address. Take the address, put it in the pointer, and call the function. This is what you want ? – Dabo Jan 26 '14 at 11:54
  • 5
    The panel is retarded :) – Eutherpy Jan 26 '14 at 11:57
  • 2
    **Please conjugate your verbs.** Thanks. –  Jan 26 '14 at 12:01

4 Answers4

1

Probably something like this, a lookup table for functions with similar prototype

int add(int a,int b)
{
    return a+b;
}

int sub(int a,int b)
{
    return a-b;
}

typedef int (*function_cb)(int,int);
std::map<string, function_cb> callBacks;

.....
void init_lookup()
{
    callBacks["+"] = &add;
    callBacks["-"] = &sub;
}

And then use it based on your text file

 int res = callBacks["-"](8,4);

Where - , 8 , 4 are from your text file

Dabo
  • 2,371
  • 2
  • 18
  • 26
0

For reading the expression from the input file, you can use the standard reading method that Standard Library offers:

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main () {
  string line;
  ifstream myfile ("example.txt");
  if (myfile.is_open())
  {
    while ( getline (myfile,line) )
    {
      cout << line << '\n';
    }
    myfile.close();
  }

  else cout << "Unable to open file"; 

  return 0;
}

(code taken from cplusplus.com)

About translating that string you get from the file... you get very deep into expression trees. For working with the expression trees, you can take a look at the code below. This is something I wrote in the past to explain somebody about this concept:

class Token
{
public:
  enum TokenTypes { operator_token, operand_token };

  Token();
  Token( std::string token );
  ~Token(); // we are not inheriting, so no need to be virtual
  Token(const Token &in);
  Token& operator=(const Token &in);
  TokenTypes getId() const;

  // you need to set the left/right from addToken(), see class Tree
  void setLeft(Token* left);
  void setRight(Token* right);   
  const Token* getLeft();
  const Token* getRight();

private:

  // when creating the Token you need to be able to identify
  // what TokenType the string is
  TokenTypes tokenId( std::string token );

  TokenTypes m_id; // type of token
  std::string m_token; // the actual string token e.g. "+", "12", ..
  Token* m_left; // the pointers to the children left or right in a binary tree
  Token* m_right; 

};

class Tree
{
public:
  Tree();
  ~Tree(); // clean up 
  void addToken( std::string token ); // this adds a token to the tree
private:
  Token* m_root; 
};

you can then use it like so...

std::string strToken
Tree T; 
while ( getNextStringToken(strToken) )
{
  Token* newToken = new Token(strToken);
  T.addToken(newToken);
}

You can read more about this concept on Wikipedia.

I hope this helps you!

Victor
  • 13,914
  • 19
  • 78
  • 147
  • 1
    *"(code taken from cplusplus.com)"* Don't take code from cplusplus.com tutorials (ANd cplusplus in general), and don't suggest people to use it. It uses poor/old practices. For example (In your code): `.close()` call, `.is_open()` instead of boolean conversion, `using namespace std` etc. – Manu343726 Jan 26 '14 at 12:25
  • I didn't pay to much attention in selecting some good piece of code about reading from a file, since the harder part of the OP's question is the expression translation. – Victor Jan 26 '14 at 12:28
  • That was only a sidenote :) – Manu343726 Jan 26 '14 at 12:30
0

In my opinion They just want to read the character +,-,* and put this character in between the variables and execute the statement.

for how to convert the string into expression see this answerConvert string to mathematical evaluation

Community
  • 1
  • 1
smali
  • 4,687
  • 7
  • 38
  • 60
-1

I think this questions is more along the lines of operator overloading, you can define what normal operators would do when the operands are functions/other than standard variables.

noah
  • 1