【发布时间】:2020-02-22 21:05:52
【问题描述】:
我正在尝试创建一个 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 头文件的 sn-p。
#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);
}
}
【问题讨论】:
-
在构造函数中声明
int values = 0;并没有完成任何有用的事情。您似乎没有正确处理at或迭代tokens。 -
啊,好吧,所以我需要重新考虑如何处理输入。我在过去的 rpn calc 程序中使用了这种方法并且它有效,所以我认为它会在这里有效。另外,可能是因为我正在使用类,而过去没有使用类。我会再试一次。对于如何正确处理作为用户输入的字符串,您有什么建议?
标签: c++ class stack header-files rpn