c++ its a 4 part question Background The HP-35 was the first scientific hand-hel
ID: 3743568 • Letter: C
Question
c++ its a 4 part question
Background The HP-35 was the first scientific hand-held calculator, released in 1972 and an immediate success with engineers and scientists. Amongst other features, it used an unusual way of entering numbers, called RPN. RPN (reverse Polish notation, named for Polish mathematician Jan Lukasiewicz) allows complex calculations to be entered without the use of parentheses. The key difference is that operators are entered after the operands, rather than between them The HP-35 implemented RPN using a stack of 4 registers, referred to as X, Y, Z, and T. The value in the X register is the one shown on the calculator's LED display; the other registers are used for temporary values during calculations. New values are entered into the X register, with existing values "pushed" up the register stack: the old value of X goes into Y, the old value of Y goes into Z, the old value of Z goes into T, and the old value of T is lost. Single-operand functions (such as square root) use and replace the value in the X register; other registers are unchanged. Functions that operate on 2 values (such as the arithmetic operators) use the values in Y and X, with the result going back into X. Values in the Z and T registers are "dropped" down the stack, with Z overwriting the old Y and T overwriting the old Z. The value of T is preserved, providing a simple way to do calculations with repeated values. This scheme allows calculations to be entered without using parentheses. For example, you could compute the compound expression (5+43 -2) like this SENTER 4 + 3 ENTER 2-7 Hints Youll need to define the class HPStack to represent the calculator's operand stack, with operations such as push (push all values up 1 level and store a value into X), pop (drop all values down 1 level and return the old X value), and peek (return the current X value). The simplest approach is to use an array to store the register values, with the methods manipulating the array elements The calculator should read and process tokens representing numbers and operator keys, with the current value of the X register displayed after processing each line. It should exit when all lines of input have been processed.Explanation / Answer
#include <iostream>
#include <stack>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#define MAX_LEN 200
#define PI 3.14159265359
double getNextNumber(std::stack<double>);
double ans = 0;
unsigned int line = 0;
int main(){
main_start_after_help:
std::stack<double> numstack;
double a, b;
std::cout <<line++ <<"> ";
char rpnln[MAX_LEN + 1];
std::cin.getline(rpnln, MAX_LEN);
// get first token from the input
char* p = strtok(rpnln, " ");
// empty string/whitespace input
if (p == NULL)
return main();
while (p != NULL) {
// char is a binary operator
if (((*p == '+' || *p == '-' || *p == '*' || *p == '/' || *p == '|'
|| *p == '%' || *p == '&' || *p == '^') && *(p + 1) == '')
|| !strcmp(p, "<<") || !strcmp(p, ">>") || !strcmp(p, "**")
|| !strcmp(p, "logBase") || !strcmp(p, "logBASE") || !strcmp(p, "logbase")
) {
b = getNextNumber(numstack);
a = getNextNumber(numstack);
/*
if (!numstack.empty()) {
a = numstack.top();
numstack.pop();
} else {
std::cerr <<"ERROR: Too many operators. " <<std::endl;
return main();
}
*/
switch (*p) {
case '+': numstack.push(a + b); std::cout <<a + b; break;
case '*':
if (*(p + 1) == '*')
numstack.push(pow(a, b));
else
numstack.push((double) a * (double) b);
break;
case '/': numstack.push(a / b); break;
case '-': numstack.push(a - b); break;
case '%': numstack.push((int) a % (int) b); break;
case '|': numstack.push((int) a | (int) b); break;
case '^': numstack.push((int) a ^ (int) b); break;
case '&': numstack.push((int) a & (int) b); break;
case '<': numstack.push((int) a << (int) b); break;
case '>': numstack.push((int) a >> (int) b); break;
case 'l': numstack.push(log10(b) / log10(a)); break;
}
}
// char is a unary operator
//trig functions
else if (strcmp(p, "sin") == 0)
numstack.push(sin(getNextNumber(numstack)));
else if (strcmp(p, "cos") == 0)
numstack.push(cos(getNextNumber(numstack)));
else if (strcmp(p, "tan") == 0)
numstack.push(tan(getNextNumber(numstack)));
else if (strcmp(p, "asin") == 0)
numstack.push(asin(getNextNumber(numstack)));
else if (strcmp(p, "acos") == 0)
numstack.push(acos(getNextNumber(numstack)));
else if (strcmp(p, "atan") == 0)
numstack.push(atan(getNextNumber(numstack)));
else if (strcmp(p, "sinh") == 0)
numstack.push(sinh(getNextNumber(numstack)));
else if (strcmp(p, "cosh") == 0)
numstack.push(cosh(getNextNumber(numstack)));
else if (strcmp(p, "tanh") == 0)
numstack.push(tanh(getNextNumber(numstack)));
else if (strcmp(p, "asinh") == 0)
numstack.push(asinh(getNextNumber(numstack)));
else if (strcmp(p, "acosh") == 0)
numstack.push(acosh(getNextNumber(numstack)));
else if (strcmp(p, "atanh") == 0)
numstack.push(atanh(getNextNumber(numstack)));
else if (strcmp(p, "log") == 0 || strcmp(p, "log10") == 0)
numstack.push(log10(getNextNumber(numstack)));
else if (strcmp(p, "ln") == 0)
numstack.push(log(getNextNumber(numstack)));
else if (strcmp(p, "sqrt") == 0 || strcmp(p, "sqr") == 0)
numstack.push(sqrt(getNextNumber(numstack)));
// not an operator
else {
if (*p == '#') { // comments... because I can XDDDDDDDDD
if (numstack.size() == 0)
goto main_start_after_help;
break;
} else if (strcmp(p, "pi") == 0)
numstack.push(3.14);
else if (strcmp(p, "ans") == 0) // p == "ans"
numstack.push(ans);
else if (*p == 'q' || !strcmp(p, "exit")) // p == "q"
goto exit; // exit the program
} else if (strcmp(p, "clear") == 0 || strcmp(p, "cls")){
#ifdef _WIN32
system("cls");
#else
system("cear");
#endif
return main();
} else if (strcmp(p, "reset") == 0 ) {
ans = line = 0;
goto main_start_after_help;
} else if (*p == '~' && *(p + 1) != '')
numstack.push(~atoi(p + 1));
else { // number constant (pushes 0 if not)
double number = atof(p);
if (number == 0 && *p != '0') {
std::cerr <<"SYNTAX ERROR " <<std::endl;
return main();
}
numstack.push(number);
}
}
// get next token
p = strtok(NULL, " ");
}
std::cout <<"ans = " <<(ans = numstack.top()) <<' ' <<std::endl;
return main(); //next line...
exit:
return 0;
}
inline double getNextNumber(std::stack<double> numberStack){
double topNum;
if (!numberStack.empty()) {
topNum = numberStack.top();
numberStack.pop();
return topNum;
} else {
std::cerr <<"ERROR: Too many operators. " <<std::endl;
main();
return 0;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.