Evaluate a short algebraic expression using code with three-operand instructions
ID: 667406 • Letter: E
Question
Evaluate a short algebraic expression using code with three-operand instructions. The expression should have a minimum of three operands and 2 operators. Operands may be alpha or numeric.
You may only use registers A through F, plus X and T.
Registers A through F may not be changed, i.e. their values are fixed.
Register T may be used as a temporary register, and Register X must contain the final answer.
Show the postfix for the expression, and then use a stack to evaluate the expression.
As an example, consider X=A +B + C * D
The three-operand instructions would be:
ADD X, A, B
MUL T, C, D
ADD X, X, T
The Postfix would be AB+CD*+
The stack would be:
PUSH / A onto the stack
PUSH / B onto the stack
ADD /pop A & B, add them, and push the answer back on the stack
PUSH /C
PUSH / D
MUL /pop C & D, multiply them, and push the answer back on the stack
ADD / pop X & T, add them, and push the answer back on the stack
POP / X off the stack
It is important to note that operational hierarchy dictates that
we first perform all arithmetic inside inner parentheses;
then inside outer parentheses;
then do multiplication and division operations before addition and subtraction operations.
Explanation / Answer
#include <iostream>
#include <vector>
C++ program for calculating X=A +B + C * D or any such equations
#include <stdlib.h>
#include <string>
#include <sstream>
using namespace std;
bool correct = true;
void check(string s){
int j=0;
int cbr=0;
int cop=0; char ch,pr,nc;
if (s.length()==0){
cout << "NO EXPRESSION ENTERED " << endl;
correct = false;
return;
}
else {
ch = s[j];
if (ch == '('){
cbr++;
j++;
while (j<s.length()){
ch = s[j];
if ((int)ch >= 48 && (int)ch <=57){
j++;
}
else
break;
}
cop++;
}
else if ((int)ch >= 48 && (int)ch <=57){
j++;
while (j<s.length()){
ch = s[j];
if ((int)ch >= 48 && (int)ch <=57){
j++;
}
else
break;
}
cop++;
}
}
for (int i=j; i<s.length(); i++){
if (i == 0) {
cout << "Invalid start of the expression " << endl;
correct = false;
break;
}
ch = s[i];
pr = s[i-1];
if (ch=='('){
cbr++;
if (((int)pr >= 48 && (int)pr <= 57) || pr == ')'){
cout << "Missing Operator " << pr << ch << endl;
correct = false;
break;
}
if (i+1 < s.length()){
nc = s[i+1];
if (((int)nc < 48 || (int)nc > 57) && nc != ')'){
cout << "Missing number " << ch << nc << endl;
correct = false;
break;
}
else {
if (nc == ')');
else {
i++;
while (i<s.length()){
ch = s[i];
if ((int)ch >= 48 && (int)ch <=57){
i++;
}
else {
cop++;
i--;
break;
}
}
}
}
}
}
else if (ch == ')'){
cbr--;
if (((int)pr<48 || (int)pr>57) && pr!='(' ){
cout << "Missing number " << pr << ch << endl;
correct = false;
break;
}
if (i+1 < s.length()){
char nc = s[i+1];
if (((int)nc >=48 && (int)nc <=57) || ch=='('){
cout << "Missing operator " << ch << nc << endl;
correct = false;
break;
}
}
}
else if (ch=='+' || ch=='-' || ch=='*'){
cop--;
if (i+1 < s.length()){
nc = s[i+1];
if (nc=='+' || nc=='-' || nc=='*' ){
cout << "double operator " << ch << nc << endl;
correct = false;
break;
}
else if ((int)nc>=48 && (int)nc<=57){
i++;
while (i<s.length()){
ch = s[i];
if ((int)ch >= 48 && (int)ch <=57){
i++;
}
else {
i--;
break;
}
}
cop++;
}
}
}
else {
cout << "INVALID CHARACTER " << s[i] <<" IN THE STRING " << endl;
correct = false;
break;
}
}
if (cbr!=0) {
if (cbr > 0)
cout << "Opening brackets are more than closing brackets." << endl;
else
cout << "closing brackets are more than opening brackets." << endl;
correct = false;
}
else if (cop!=1){
if (cop>1)
cout << "Operands are more than Operators" << endl;
else
cout << "Operators are more than Operands" << endl;
correct = false;
}
}
string convert(string s){
string output="";
char ch;
vector<char> vect;
for (int i=0; i<s.length(); i++){
ch = s[i];
if (ch=='('){
vect.push_back(ch);
}
else if (ch==')'){
int j = vect.size()-1;
while (vect[j]!='('){
char ch = vect[j];
j--;
output = output+ch+" ";
vect.pop_back();
}
vect.pop_back();
}
else if (ch=='*' || ch=='+' || ch=='-'){
if (ch=='+' || ch=='-'){
int j = vect.size()-1;
while (vect.size()!=0 && vect[j]!='('){
char ch = vect[j];
j--;
output = output+ch+" ";
vect.pop_back();
}
vect.push_back(ch);
}
else if (ch=='*'){
int j = vect.size()-1;
if (j>0){
while (vect[j]=='*'){
char ch = vect[j];
j--;
output = output+" "+ch;
vect.pop_back();
}
}
vect.push_back('*');
}
}
else {
string ss;
while ((int)ch>=48 && (int)ch<=57){
ss = ss+ch;
i++;
if (i < s.length())
ch = s[i];
else {
break;
}
}
output = output+ss+" ";
i--;
}
}
while (vect.size()!=0){
int j = vect.size()-1;
char ch = vect[j];
j--;
output = output+ch+" ";
vect.pop_back();
}
output = output.substr(0,output.length()-1);
return output;
}
int calculate(string s){
vector<int> vect;
for (int i=0; i<s.length(); i++){
char ch=s[i];
if (ch=='+' || ch=='-' || ch=='*'){
int l = 0;
int j = vect.size()-1;
int m = vect[j];
int n = vect[j-1];
vect.pop_back();
vect.pop_back();
if (ch=='+'){
l = m+n;
}
else if (ch=='-'){
l = n-m;
}
else {
l = m*n;
}
vect.push_back(l);
}
else if ((int)ch >=48 && (int)ch<=57) {
string ss;
while (ch!=' '){
ss=ss+ch;
i++;
ch = s[i];
}
int m = atoi(ss.c_str());
vect.push_back(m);
}
}
return vect[0];
}
int main(){
int option;
string get;
while(true){
cout << "Choose Desire Option " << endl;
cout << "For Calculating value of the expression : 1 " << endl;
cout << "QUIT : 2 " << endl;
cin >> option;
if (option==1){
cout << " Enter an arithmetic expression: " << " ";
cin >> get;
check(get);
if (correct==true){
string got = convert(get);
cout << " The RPN form of the expression " << got << endl;
cout << endl;
int result = calculate(got);
cout << " The Value of The Expression is " << result << endl << endl;
}
}
else
break;
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.