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:
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.
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 ?