C++ #include <iostream> #include <sstream> using namespace std; const int MAXSIZ
ID: 3738810 • Letter: C
Question
C++
#include <iostream>
#include <sstream>
using namespace std;
const int MAXSIZE = 5;
class Stack {
public:
Stack();
bool Empty();
void Push(int item);
void Pop();
int Top();
void displayStack();
private:
int stk[MAXSIZE];
int top;
};
bool isNumber(string, int &);
int calculate(int, int, string);
int main() {
Stack expStack;
string inToken;
int num, oprd1, oprd2, result;
// *** enter your code here - ref to the steps
// *** in the lab sheet.
system("Pause");
return 0;
}
Stack::Stack() {
top = 0;
}
bool Stack::Empty() {
if (top == 0)
return true;
else
return false;
}
void Stack::Push(int item) {
stk[top] = item;
top++;
}
void Stack::Pop() {
top--;
}
int Stack::Top() {
return stk[top];
}
void Stack::displayStack() {
for (int i = 0; i < top; i++)
cout << stk[i] << " ";
cout << endl << endl << top << endl;
}
/* this function will take a string and determine if it is
an integer. The function will return true and store the
integer value to number (a reference parameter). it will
return false otherwise.
*/
bool isNumber(string inStr, int & number){
stringstream ss;
ss << inStr;
number = 0;
ss >> number;
if (ss.good())
return false;
else if (number == 0 && inStr[0] != '0')
return false;
else
return true;
}
/*
This function will use switch statement check the
first charactor in op to see if it is "=".
Then it will calculate and return the result. Assume
there are only 4 operators +, -, *, /
*/
int calculate(int num1, int num2, string op) {
// *** write your code here
}
Explanation / Answer
#include #include using namespace std; const int MAXSIZE = 5; class Stack { public: Stack(); bool Empty(); void Push(int item); void Pop(); int Top(); void displayStack(); private: int stk[MAXSIZE]; int top; }; bool isNumber(string, int &); int calculate(int, int, string); int main() { Stack expStack; string inToken; int num, oprd1, oprd2, result; cout > inToken; if(inToken == "=") { break; } else if(isNumber(inToken, num)) { expStack.Push(num); } else { oprd1 = expStack.Top(); expStack.Pop(); oprd2 = expStack.Top(); expStack.Pop(); result = calculate(oprd1, oprd2, inToken); expStack.Push(result); } coutRelated Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.