COSC 1437 - Chapter 12.7

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

Modify LiveExample 12.12 EvaluateExpression.cpp to add operators ^ for exponent and % for modulus. For example, 3 ^ 2 is 9 and 3 % 2 is 1. The ^ operator has the highest precedence and the % operator has the same precedence as the * and / operators. For simplicity, assume the expression has at most one ^ operator.

#include <bits/stdc++.h> using namespace std; /* check prec of the operators*/ int Prec(char ch) { /*if + and - than return 1*/ if(ch == '+'||ch == '-') return 1; /*if * and 5 and / than return 2*/ if(ch == '*'||ch == '/'||ch=='%') return 2; if(ch == '^') /*if ^ than return 3*/ return 3; return 0; } /*find the values and apply operators*/ int values(int a, int b, char ch){ /*check operators and return result*/ switch(ch){ case '+': return a + b; case '-': return a - b; case '*': return a * b; case '/': return a / b; case '^': return pow(a,b); case '%': return a%b; } } /*make a function*/ int result(string input) { int i; /*make a stack for store values*/ stack <int> val; /*make a stack for store operators*/ stack <char> st; for(i = 0; i < input.length(); i++) { /*if space than ignore it*/ if(input[i] == ' ') { continue; } /*if ( than push it into stack st*/ else if(input[i] == '(') { st.push(input[i]); } else if(isdigit(input[i])) { /*declare the variabel*/ int v = 0; /*if more than one digit than find number*/ while(i < input.length() && isdigit(input[i])) { /*find number*/ v = (v*10) + (input[i]-'0'); i++; } /*push number*/ val.push(v); /*decrement i*/ i--; } /*if ) than pop value from val*/ else if(input[i] == ')') { /*do untill we get ( */ while(!st.empty() && st.top() != '(') { /*pop oprand for val*/ int val2 = val.top(); val.pop(); int val1 = val.top(); val.pop(); /*pop operator from st*/ char op = st.top(); st.pop(); /*push value into val*/ val.push(values(val1, val2, op)); } /*remove ( */ if(!st.empty()) st.pop(); } else { /*do untill we get empty st*/ while(!st.empty() && Prec(st.top())>= Prec(input[i])) { /*pop value from stack*/ int val2 = val.top(); val.pop(); int val1 = val.top(); val.pop(); char op = st.top(); st.pop(); /*push into val*/ val.push(values(val1, val2, op)); } // Push current token to 'st'. st.push(input[i]); } } while(!st.empty()) { /*pop value from stack*/ int val2 = val.top(); val.pop(); int val1 = val.top(); val.pop(); char op = st.top(); st.pop(); /*push into val*/ val.push(values(val1, val2, op)); } /*return a answer*/ return val.top(); } int main() { /*declare variabel*/ string s; /*take input from user*/ cout<<"Enter an expression: "; getline(cin,s); /*call function*/ cout <<s<<" = "<<result(s) << "\n"; return 0; }

Which of the following statements returns the smallest element in vector v? min_element(v) min_element(v, v.begin(), v.end()) min_element(v, v.begin(), v.end()) min_element(v, v.begin(), v.end() - 1)

min_element(v, v.begin(), v.end()

Which of the following statements creates a smart pointer for an array of 10 int values? int* p = new int[10] unique_ptr<int[]> p(new int[10]); unique_ptr<int> p(new int[10]); unique<int[]> p(new int[10]);

unique_ptr<int[]> p(new int[10]);

Which of the following statements deletes the first element from vector v? v.delete(0) v.erase(0) v.erase(v.begin()) v.delete(v.begin())

v.erase(v.begin())

Which of the following statements inserts number 5 to the beginning of vector v? v.add(5) v.add(0, 5) v.insert(0, 5) v.insert(v.begin(), 5)

v.insert(v.begin(), 5)


Kaugnay na mga set ng pag-aaral

Practice MCQ Questions from FINAL REVIEW + LECTURES

View Set

Coltman - Environmental Biology Test 3

View Set

A+ Guide to Managing Ch. 3 Review Questions

View Set

Iggy Chapter 32: Care of Critically Ill Patients with Respiratory Problems

View Set

Chapter 14: Main DSM-5 Categories of Mental Disorders

View Set

Lesson 3: Japan in the Interwar Years

View Set

Life Insurance Policy Provisions, Options & Riders

View Set