Using a do-while loop, re-implement the calculator program that we implemented in class. But, instead of using switch cases to detect the operand, you should use vector<char> to store operands(e.i ‘+’, ‘-’ etc) and process it when the user enters an expression to be evaluated. Your program should be able to process the following inputs: 5+5, 12*13, 12/6, etc.
#include<iostream>
#include<vector>
#include<cmath>
using namespace std;
class Calculator{
public:
vector<char>operand;
void display(){
if(operand.at(1) =='+'){
cout<<"The sum of "<<operand.at(0)<<" and "<<operand.at(2)<<" is :"<<(operand.at(0) - 48) + (operand.at(2) - 48)<<endl;
}
if(operand.at(1) =='-'){
cout<<"The difference of "<<operand.at(0)<<" and "<<operand.at(2)<<" is :"<<abs((operand.at(0) - 48) - (operand.at(2) - 48))<<endl;
}
if(operand.at(1) =='*'){
cout<<"The product of "<<operand.at(0)<<" and "<<operand.at(2)<<" is :"<<(operand.at(0) - 48) * (operand.at(2) - 48)<<endl;
}
if(operand.at(1) =='/'){
cout<<"The difference of "<<operand.at(0)<<" and "<<operand.at(2)<<" is :"<<(operand.at(0) - 48) / (operand.at(2) - 48)<<endl;
}
}
};
int main(){
Calculator c;
c.operand ={'5','-', '2'};
c.display();
}
Comments
Leave a comment