C++ Program to Perform Calculations in Pure Strings
Given a string of operations containing three operands for each operation “type of command“, “first operand“, and “second operand“. Calculate all the commands given in this string format.
In other words, you will be given a pure string that will ask you to perform an operation and you have to perform it and print the result in order of the commands given.
- add – For addition (+) accepts two arguments num1 and num2. (similar with subtract (-), multiply (x), divide (/) ).
- percent – For percentage (%) with 100 accepts one argument num1 and num2 as ‘-1’ always.
- log – For logarithm of num1 with base num2 accepts two arguments. (similar with exp (^) ).
- sqrt – For the square root of num1, accept one argument num1 and num2 as ‘-1’ always.
- cbrt – For cube root of num1, accepts one argument num1 and num2 as ‘-1’ always.
- All trigonometric functions – accepts one argument num1 and num2 as ‘-1’ always.
Note: Print “inf” in case of any exception like divide by zero (0) and for functions that input one argument, add ‘-1’ as the second.
Input: "add 4 5" Output: 9 Explanation: 4+5=9 which is to be printed.
Input: "subtract 7 6" Output: 1 Explanation: 7-6=1 which is to be printed.
Input: "divide 2 0 multiply 6 6" Output: inf 36 // in this case it will only print 36 Explanation: 2/0=infinity and 6*6=36 which is to be printed.
Input: "percent 50 -1" Output: 0.5 Explanation: 50/100=0.5 which is to be printed.
Approach: It is a simple case of string handling where we need to extract each word and then calculate the operands output based on the ‘type of operation’. It can be done by creating a series of if-else ladder of functions or using switch cases. If any function is invalid or not found in the returnOP function then return ‘-1’.
For example: “add 5 6” can be broken down as three strings using stringstream “add”, “5”, and “6”. We will compare add with our library of functions and perform 5+6 after converting them to float. So if “add” is at position i then 5 and 6 would be at positions i+1 and i+2.
Follow the below steps to implement the above approach:
- Pass the string into the function processOperations.
- Extract each word from the string using stringstream class and store all these words in a vector strarr.
- Then check for every third word (3*k) to see the ‘type of operation’ being performed and compare it with the library of operations available in returnOP function.
- If that operation is found returnOP then return its float answer after calculation else returns ‘-1’.
- Calculate the output values based on the operation and return them to the original function processOperations.
All these operation results that we get from returnOP will then be pushed into a result vector of type float. The result vector will be our final answer which is to be returned after performing all operations.
2 // adding of 1 and 1 2 // multiplying of -2 and -1 45 // cosecinvdeg 1.4142135624 and -1 20 // percentage of 100 and 20
Time Complexity: O(N)
Auxiliary Space: O(N), where N is the number of characters in string operations.