RPN计算器使用头文件进行计算操作和堆栈;用于堆栈的矢量



我试图创建一个rpn计算器,但必须使用带有类声明和计算器函数和堆栈定义的头文件。我决定为堆栈使用一个向量,它是我在stack.h文件中创建的。每当我测试程序时,当输入一个不是给定运算符的令牌时,它总是给我一个无效令牌错误。我试着让它处理rpn中值的向量,我试着为向量创建一个函数,以返回堆栈头文件中的向量,以便在我的rpn头中实例化和调用它。出于某种原因,这些对我都不起作用。感谢您的帮助。

这是我收到的结果:

Please enter numbers and operators:
1 2 + =
Invalid token entered.

这是我的堆栈头文件,名为stack8.h

#ifndef STACK8
#define STACK8
#include <iostream>
#include <vector>
using namespace std;
class stack8 {
std::vector<double> rpn_stack;
public:
stack8();
void push(double vals);
double pop();
void dump_op();
int getsize();
int i;
string message;
};
stack8::stack8() {
int values = 0;
}
void stack8::push(double vals) {
rpn_stack.push_back(vals);  //pushing any double values found into the stack
return; 
}
double stack8::pop() {
double nums; //declaration for operands
if (rpn_stack.size() <= 0) {  //checking for stack underflow
std::cerr << "Stack underflow." << endl;
//use_info.usage_info(message);
exit(2);
}
else {
nums = rpn_stack.back(); //if no stack underflow, values are user's input
rpn_stack.pop_back();     //will pop the values at the top of the stack until the end based on user's input
return nums;              //returning the result of values after popping
}
}
int stack8::getsize() {
std::cout<< rpn_stack.size() << " values remain on the stack." << endl; //number of vals in stack
exit(3);
}
void stack8::dump_op() {
for (i = 0; i < rpn_stack.size(); ++i) {  //iterating through stack to print values for dump operation
cout << rpn_stack.at(i) << " ";
}
}
#endif

下面是我为rpn.h头文件所做的一个片段。

#ifndef RPN8
#define RPN8
#include <iostream>
#include <string>
#include <vector>
#include <cmath>
#include <sstream>
using namespace std;
#include "stack8.h"
stack8 st;    //calling a stack class object to use for the calculator operations
class rpn8 {
public:
rpn8();
double process();
int getstacksize();
double values;                 //declaration to use for our calculator operand values
double tempo;                  //declaration to be used for temporary calc values for certain pop operations
double r1, r2;                 //same as above, for use with certain pop operations
string tokens;
stringstream at;
private:
double result;
};
rpn8::rpn8() {
result = 0;
}
double rpn8::process() {
cout << "Please enter numbers and operators: " << endl;
while (std::cin >> tokens) {
//asking for user input for calculator operations
at.str("");         //Looking for "" indicators
at.clear();         //Removing the "" tokens
at.str(tokens);     //Setting stringstream to user input entered
at >> values;
if (!at) {
if (tokens.compare("+") == 0) {     //finding + in the input and popping two operands for addition
st.push(st.pop() + st.pop());
}
else if (tokens.compare("-") == 0){ //finding - in the input and popping two operands for subtraction
tempo = st.pop();       //using temporary value for subtracting operators
st.push(st.pop() - tempo);
}
else if (tokens.compare("*") == 0) //finding * in the input and popping two operands for multiplication
{
st.push(st.pop() * st.pop());
} 
else if (tokens.compare("/")== 0) //finding / in the input and popping two operands for division
{
tempo = st.pop();
if (tempo == 0) {  //if temp value = 0, division is undefined.  
std::cerr<< "This results in division by 0, which is undefined.";
exit(2);
}
st.push(st.pop() / tempo); //otherwise, carry out division from pop
}
else if (tokens.compare("'") == 0)
{
st.push(1.0 / st.pop());
}
else if (tokens.compare("~")== 0) //finding ~ to reverse sign of operand
{
st.push(-st.pop()); 
}
else if (tokens.compare("**") == 0) //finding ** for power function
{
tempo = st.pop();
st.push(pow(st.pop(), tempo));
}
else if (tokens.compare("=") == 0) //time to print the final answer with the equal sign
{
std::cout<< "Result = "<< st.pop() << endl;  //printing results
std::cout<< st.getsize() << " values remain on the stack." << endl; //# of vals in stack
exit(3);
}
else if (tokens.compare("dump") == 0) {      //if dump is an operator entered
st.dump_op();
cout << endl;
exit(4);
}
else {  //pushing values
st.push(values);
}
}
else {   //statement for if any invalid tokens are entered
std::cerr << "Invalid token entered." << endl;  //error message printed before usage info and termination of program
//help.usage_info(helps);
exit(7);
}       
}
}

这是我的cpp文件

#include <iostream>
#include <string>
//adding a call for the rpn8.h header file
#include "rpn8.h"
int main (int argc, char* argv[]) {
string help_info = "--help";  //string declaration for --help command-line arg
string begin_prog = "-rpn";    //string declaration for -rpn command-line arg
string usage_inf;              //string declaration to be used for argument when calling usage info function
rpn8 calc;                      //instantiation of the rpn8 class

if (argc == 1) {
cerr << "Not enough arguments.";
//help message...
}
else if (argc == 2 && (argv[1] == help_info)) { //if command-line arg is --help
//usage_message(usage_inf);  //printing usage info
}
else if (argc == 2 && (argv[1] == begin_prog)) {  //if command-line arg is -rpn, starting calculator operations
calc.process(); //calling the process function from rpn8 using call
}
else {
cerr << "Too many command-line arguments or invalid arguments." << endl; //
//usage_message(help_usage);
exit(5);
}
}

rpn8::process中的else分支可能是错误的。每当您输入数字时(当st >> values成功时(,if (!at)将转到else分支。但是,您不处理这个数字,而是将其作为无效令牌进行处理。

其他一些注意事项:

  • std::cout<< st.getsize() << " values remain on the stack." << endl;有点奇怪,因为stack8::getsize不返回任何内容,而是打印出值并退出整个程序
  • at.str("");不查找任何指示符,只是用空字符串替换字符串流的内容

最新更新