Skip to content
Related Articles

Related Articles

C++ Program to Perform Calculations in Pure Strings

View Discussion
Improve Article
Save Article
  • Last Updated : 06 Jun, 2022
View Discussion
Improve Article
Save Article

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:

  1. Pass the string into the function processOperations.
  2. Extract each word from the string using stringstream class and store all these words in a vector strarr.
  3. 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.
  4. If that operation is found returnOP then return its float answer after calculation else returns ‘-1’.
  5. 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.


#include <bits/stdc++.h>
using namespace std;
// MACROS for performing commands
#define e 2.718281828459045
#define pi 3.141592653589793
// function for comparing the type of operation
double returnOP(string& word,vector<string>&strarr,int& i)
    double num1,num2;
    // using MACROS if required
    else num1=stof(strarr[i+1]);
    else num2=stof(strarr[i+2]);
    double degree=pi*num1/180;
     // library of functions to calculate result
      // after finding the 'type of operation'
    return num1+num2;
    else if("subtract")==0)
    return num1-num2;
    else if("multiply")==0)
    return num1*num2;
    else if("divide")==0)
    return num1/num2;
    else if("percent")==0)
    return (num2/num1)*100;
    else if("log")==0)
    return log(num1)/log(num2);
    else if("exp")==0)
    return pow(num1,num2);
    else if("sqrt")==0)
    return sqrt(num1);
    else if("cbrt")==0)
    return cbrt(num1);
    else if("sin")==0)
    return sin(num1);
    else if("cos")==0)
    return cos(num1);
    else if("tan")==0)
    return tan(num1);
    else if("sec")==0)
    return 1/cos(num1);
    else if("cosec")==0)
    return 1/sin(num1);
    else if("cot")==0)
    return 1/tan(num1);
    else if("sindeg")==0)
    return sin(degree);
    else if("cosdeg")==0)
    return cos(degree);
    else if("tandeg")==0)
    return tan(degree);
    else if("secdeg")==0)
    return 1/cos(degree);
    else if("cosecdeg")==0)
    return 1/sin(degree);
    else if("cotdeg")==0)
    return 1/tan(degree);
    else if("sininv")==0)
    return asin(num1);
    else if("cosinv")==0)
    return acos(num1);
    else if("taninv")==0)
    return atan(num1);
    else if("secinv")==0)
    return acos(1/num1);
    else if("cosecinv")==0)
    return asin(1/num1);
    else if("cotinv")==0)
    return atan(1/num1);
    else if("sininvdeg")==0)
    return 180*asin(num1)/pi;
    else if("cosinvdeg")==0)
    return 180*acos(num1)/pi;
    else if("taninvdeg")==0)
    return 180*atan(num1)/pi;
    else if("secinvdeg")==0)
    return 180*acos(1/num1)/pi;
    else if("cosecinvdeg")==0)
    return 180*asin(1/num1)/pi;
    else if("cotinvdeg")==0)
    return 180*atan(1/num1)/pi;
    return -1;
// function for fetching each word from a string array of operations
// and pushing them into result after performing required operation
vector<double>processOperations(string& operations)
    string word;
  // stringtream class to extract
  // word from string
    stringstream ss(operations);
  // passing each third word to returnOP
  // to calculate final result of that query
    for(int i=0;i<strarr.size();i+=3)
    return result;
// driver's code
int main()
    string operations="add 1 1 multiply -2 -1 cosecinvdeg 1.4142135624 -1 percent 100 20";
    for(auto& x:ans)
    return 0;


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.

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!