Answer to Question #267698 in C++ for Anas

Question #267698

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.


1
Expert's answer
2021-11-17T17:34:33-0500
#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();
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS