Question

In: Computer Science

can someone translate this pseudo code to actual c++ code while (not the end of the...

can someone translate this pseudo code to actual c++ code

while (not the end of the input)

If the next input is a number read it and push it on the stack

else If the next input is an operator, read it

pop 2 operands off of the stack

apply the operator

push the result onto the stack

When you reach the end of the input:

if there is one number on the stack, print it else error

Solutions

Expert Solution

// Here is your pseudo code in c++ as per all instruction

#include <iostream>
#include <stack>
using namespace std;

int stack_solver(string str) // Here is function to solve input string
{
        int i=0,length,a,b;
        stack<int> s;            // Here we make instance s of stack predefined class in C++ library
        length=str.length();      // Here we input the length of strung via length()
        while(i<length)          // This will run upto end of the string
        {
        if(str[i]>='0'&&str[i]<='9')  // Here we check for operand
                        s.push(str[i]-'0');   // if oprand push in stack

           else if((str[i]>='a'&&str[i]<='z')||(str[i]>='A'&&str[i]<='Z'))  // Here we check for any input other than operator and operand
            return -1;

                else                     // here else for operator
                {
            a= s.top();         //here we store the top most element in a
                        s.pop();           // Here we pop the topmost element
            b= s.top();        // here we store the next top most element in b
                        s.pop();          //Here we pop the next topmost element
            if(str[i]=='-')    // Here we check for operation to do on operand and push the result on stack
                                s.push(b-a);
                        else if(str[i]=='+')  // Here we check for operation to do on operand and push the result on stack
                                s.push(b+a);
                        else if (str[i]=='/')  // Here we check for operation to do on operand and push the result on stack
                                s.push(b/a);
                        else if(str[i]=='*')   // Here we check for operation to do on operand and push the result on stack
                                s.push(b*a);
         }
                i++;
        }
        if(s.size()==1)   // here we check for only one element in stack if true then return
        return s.top();
        else
        return -1;   // else return -1 to print the ERROR
}

int main()
{
        string str;   // Here we declare the string str
        cout<<"Enter the string of operator and operands ";   // Here we alert the user for input the string
        getline(cin,str);     // Here we input the string
        if(stack_solver(str)==-1)   // Here we check for any invalid input
        cout<<"ERROR";
    else
        cout<<stack_solver(str);   // Here we print our result if input is correct
    return 0;
}

Output for correct input:

Output for stack has left with more than one number

Output for incorrect input:

Please hit the like button if you find this helpful for you THANK YOU AND HAPPY LEARNING:)


Related Solutions

can someone change this code so that timestandard can be calculated at the end of the...
can someone change this code so that timestandard can be calculated at the end of the code using the inputs n,RF,PFD, and the measured cycles, instead of being called from the beginning of the code using namespace std; float timestandard(float time, float rf, float pfd) { return((time / 100) * rf * (1 + pfd)); /* calculating the time standard using the given formula*/ } int main() { int n, rf, pfd, x; /* inputs are taken*/ cout << "****...
Translate the following Code into MIPS (.asm) Arrays and Reentrant Subprograms CallingSubroutinesDemo.asm Implement the following pseudo...
Translate the following Code into MIPS (.asm) Arrays and Reentrant Subprograms CallingSubroutinesDemo.asm Implement the following pseudo code in a program called SubRoutinePractice.asm. Be sure to follow proper protocol for calling subroutines. Implement a prolog and epilog. // Main routine asks for three numbers from the user. Stores them in $s0, $s1 and $s2 // It then calls a subroutine that determines the largest and sum. // Program then prints the results main() { // $s0, $s1 and $s2 hold input...
Translate following pseudo-code to MIPS assembly language cout << “\n Please input a number for $s0”;...
Translate following pseudo-code to MIPS assembly language cout << “\n Please input a number for $s0”; cin >> $s0; cout << “\n Please input a number for $s1”; cin >> $s1; cout << “\n Please input a number for $s2”; cin >> $s2; $t0 = $s0 / 8 - 2 * $s1 + $s2; cout << “\n the Value of the expression “$s0 / 8 - 2 * $s1 + $s2” is ”; cout >> $t0; return;
Write a pseudo code for an O (n7log3n) algorithm. Please write in C++.
Write a pseudo code for an O (n7log3n) algorithm. Please write in C++.
2. Translate the following C/Java code to MIPS assembly code. Assume that the values of a,...
2. Translate the following C/Java code to MIPS assembly code. Assume that the values of a, i, and j are in registers $s0, $t0, and $t1, respectively. Assume that register $s2 holds the base address of the array A (add comments to your MIPS code). j = 0; for(i=0 ; i<a ; i++) A[i]=i+j++;
4.Translate the following C code to MIPS assembly code. Assume that the value of i is...
4.Translate the following C code to MIPS assembly code. Assume that the value of i is in register $t0, and $s0 holds the base address of the integer MemArray if (i > 10) MemArray[i] = 0; else MemArray[i] = -MemArray[i]; 6.Translate the following C code to MIPS assembly code. Use a minimum number of instructions. Assume that the values of a, b, i, and j are in registers $s0, $s1, $t0, and $t1, respectively. Also, assume that register $s2 holds...
I have to translate C++ code into MIPS. It is expected to have an output of:...
I have to translate C++ code into MIPS. It is expected to have an output of: Value of a: 25 Value of b: 31 Value of c: 18 Value of d: 49 Here is the C++ code: I need to translate this C++ into MIPS code. #include using namespace std; int main(void) { int a = 5; int b = 6; int c = 7; int d; d = -1; if ( a < 10){ a++; }else{ a--; } d...
Can you translate this C code into MIPS assembly? #include <stdio.h> #include <math.h> #include <stdlib.h> double...
Can you translate this C code into MIPS assembly? #include <stdio.h> #include <math.h> #include <stdlib.h> double fact (double); void main () { int angle_in_D; double term, angle_in_R; float sine = 0; unsigned int i = 1; double sign = 1; int n = 1000; printf ("Please enter an angle (Unit: Degree): "); scanf ("%d", &angle_in_D); angle_in_R = angle_in_D * M_PI / 180.0; do { term = pow(-1,(i-1)) * pow (angle_in_R, (2*i - 1)) / fact (2*i - 1); sine =...
1.) Translate the following C code to MIPS assembly code. Assume that the variables f, g,...
1.) Translate the following C code to MIPS assembly code. Assume that the variables f, g, h, i, and j are assigned to registers $s0, $s1, $s2, $s3, and $s4, respectively. Assume that the base address of the arrays A and B are in registers $s6 and $s7, respectively   B[8] = A[i-j]; 2.Translate the following C code to MIPS assembly code. Assume that the values of v in $a0, k in $a1, temp in $t0.    // leaf procedure that...
Translate the following C code into MIPS Assembly code, assuming Loop Variable k is in $s0...
Translate the following C code into MIPS Assembly code, assuming Loop Variable k is in $s0 and initially containing 0 . Also assume base of array Arr is in $s3 while ( k < = 10 ) { Arr[k] = k ; k = k + 1; }
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT