【发布时间】:2021-06-27 19:20:56
【问题描述】:
我的任务是创建一个后缀计算器,但问题是我无法将 Token 与任何东西进行比较。我知道评估后修复的伪代码是这样的,我正在尝试基于它来执行我的程序:
来自制作文件:
测试:build/infix_calculator build/postfix_calculator build/tokenize
./build/tokenize "2 3 4 + *"
./build/postfix_calculator "2 3 4 + *"
./build/tokenize "2 * (3 + 4)"
./build/infix_calculator "2 * (3 + 4)"
for ( each character ch in the string)
{
if (ch is an operand)
Push the value of the operand ch onto the stack
else // ch is an operator named op
{
// Evaluate and push the result
operand2 = top of stack
Pop the stack
operand1 = top of stack
Pop the stack
result = operand1 op operand2
Push result onto the stack
}
}
感谢任何反馈
这里是在线gdb的链接,可以亲自查看:
https://onlinegdb.com/BkIkaPzBO
错误:
src/PostfixCalculator.cpp:15:19: error: no match for 'operator>=' (操作数类型是 '__gnu_cxx::__alloc_traitsstd::allocator src/PostfixCalculator.cpp:15:39: error: no match for 'operator::value_type {aka Token}' and 'char' )
if (tokens[i] >= '0' && tokens[i]
src/PostfixCalculator.cpp:16:24: error: no match for 'operator-' (操作数类型是 '__gnu_cxx::__alloc_traitsstd::allocator
/**
* @file PostfixCalculator.cpp
*/
#include "PostfixCalculator.h"
#include<stack>
#include<iostream>
#include<stack>
#include<string>
double PostfixCalculator::eval(std::vector<Token> tokens) {
std::stack<double>s;
for (unsigned int i = 0; i < tokens.size(); i++){
if (tokens[i] >= '0' && tokens[i] <= '9' ){
s.push(tokens[i] - '0');
}
}
return 0;
}
/**
* @file PostfixCalculator.h
*/
#ifndef POSTFIX_CALCULATOR_H
#define POSTFIX_CALCULATOR_H
#include<vector>
#include "EvalInterface.h"
class PostfixCalculator : public EvalInterface<double>
{
public:
double eval(std::vector<Token> expr);
};
#endif
/**
* @file Token.h
*/
#ifndef TOKEN_H
#define TOKEN_H
#include <iostream>
/**
* Tag represents a categories of token types
*/
enum Tag { OPERATOR, OPERAND, LPAREN, RPAREN };
/**
* @brief Operator represents types of binary operations
*/
enum Operator { ADD, SUBTRACT, MULTIPLY, DIVIDE };
/**
* @brief A data structure to represent a token parsed from a string.
*/
struct Token {
/** a numeric value -- only valid if the tag is OPERAND */
double d;
/** an operator type -- only valid if the tag is OPERATOR */
Operator o;
/** the category of the token */
Tag tag;
};
/**
* @brief convert an Operator to a std::string
* @param o the operator
* @return a string representation of the operator
*/
std::string opToString(Operator o);
/**
* @brief An overloaded stream insertion operator for the Token type.
* @param os the output stream object
* @param t the Token to be inserted into the stream
* @return the same ostream that was passed in
*/
std::ostream& operator<<(std::ostream& os, const Token& t);
/**
* @brief parse a string representing a simple arithmetic expression
* into sequence of tokens.
* @param s the string to parse
* @return the result of parsing the string into tokens
*/
std::vector<Token> tokenize(std::string s);
#endif
/**
* @file Token.cpp
*/
#include <sstream>
#include <string>
#include <vector>
#include <stdexcept>
#include "Token.h"
std::string opToString(Operator o) {
std::string result;
switch(o) {
case ADD:
result = "ADD";
break;
case SUBTRACT:
result = "SUBTRACT";
break;
case MULTIPLY:
result = "MULTIPLY";
break;
case DIVIDE:
result = "DIVIDE";
break;
}
return result;
}
std::ostream& operator<<(std::ostream& os, const Token& t) {
std::string type;
switch (t.tag) {
case OPERATOR:
os << "OPERATOR: " << opToString(t.o) << std::endl;
break;
case OPERAND:
os << "OPERAND: " << t.d << std::endl;
break;
case LPAREN:
os << "LPAREN" << std::endl;
break;
case RPAREN:
os << "RPAREN" << std::endl;
break;
default:
throw std::logic_error("Invalid token");
break;
}
return os;
}
std::vector<Token> tokenize(std::string s) {
std::stringstream ss(s);
std::vector<Token> result;
while (ss) {
Token t;
if (ss.peek() == ' ') {
ss.get();
continue;
}
if (isalnum(ss.peek())) {
ss >> t.d;
t.tag = OPERAND;
}
else {
char op;
ss >> op;
t.d = 0;
if (op == '(') {
t.tag = LPAREN;
}
else if (op == ')') {
t.tag = RPAREN;
}
else if (op == '*') {
t.o = MULTIPLY;
t.tag = OPERATOR;
}
else if (op == '/') {
t.o = DIVIDE;
t.tag = OPERATOR;
}
else if (op == '+') {
t.o = ADD;
t.tag = OPERATOR;
}
else if (op == '-') {
t.o = SUBTRACT;
t.tag = OPERATOR;
}
else {
throw std::logic_error("Invalid token");
}
}
result.push_back(t);
}
result.pop_back();
return result;
}
【问题讨论】:
-
(tokens[i] >= '0'tokens[i]是什么?是char吗?从发布的代码来看,tokens[i]是Token。 -
我明白了,但我的问题是如何将该令牌转换为字符。
-
请发帖
Token。 -
哦,好的,token.h 和 cpp,等一下。
-
也更新了gdb链接
标签: c++ data-structures stack