我在c ++中有关于堆栈的问题。我尝试访问stack.top()但它崩溃,因为它说我正在访问一个无效的地址。虽然我已经检查以确保堆栈在访问之前包含元素。
注意:我正在尝试为postfix创建一个中缀,然后计算值的后缀。
这里是我的main.cpp
//
// main.cpp
// testing
//
// Created by Tan Der Shen on 11/17/15.
// Copyright (c) 2015 Tan Der Shen. All rights reserved.
//
#include <iostream>
#include "main.h"
#include <vector>
#include <string>
using namespace std;
int main() {
// insert code here...
std::string st1 = "2 * y + x * (8 + 3) - y";
std::string newstring;
for (int k = 0; k < st1.size(); k++) {
if (st1[k] == '(') {
newstring = newstring + "( ";
}else if (st1[k] == ')') {
newstring = newstring + " )";
}
else {
newstring = newstring + st1[k];
}
}
cout<< newstring;
cout<< "\n";
vector<string> test1 = tokenize(newstring, " ");
for (int i = 0; i < test1.size(); i++) {
cout<< test1.at(i);
cout<< "\n";
cout<< whatIsIt(test1.at(i));
cout<< "\n";
}
for (int j = 0; j < test1.size(); j++) {
if (operators.size() != 0) {
cout<< operators.top();
}
if (postfix.size() != 0) {
cout<< postfix.back();
}
switch (whatIsIt(test1.at(j))) {
case 0:
postfix.push_back("1");
cout<< "done";
break;
case 1:
postfix.push_back(test1.at(j));
break;
case 2:
operators.push(test1.at(j));
break;
case 3:
while (operators.top() != "(") {
postfix.push_back(operators.top());
operators.pop();
}
operators.pop();
break;
case 4:
if (operators.size() == 0) {
operators.push(test1.at(j));
} else {
while (whatIsIt(operators.top()) > 4) {
postfix.push_back(operators.top());
operators.pop();
}
operators.push(test1.at(j));
}
break;
case 5:
if (operators.size() == 0) {
operators.push(test1.at(j));
} else {
while (whatIsIt(operators.top()) > 5) {
postfix.push_back(operators.top());
operators.pop();
}
}
operators.push(test1.at(j));
break;
case 6:
operators.push(test1.at(j));
break;
default:
break;
}
}
while (operators.size() != 0) {
postfix.push_back(operators.top());
operators.pop();
}
for (int v = 0; v < postfix.size(); v++) {
cout<< postfix.at(v);
cout<< " ";
}
cout<< "\n";
for (int n = 0; n < postfix.size(); n++) {
cout<< "whatisit: " << whatIsIt(postfix.at(n));
cout<< "\n";
switch (whatIsIt(postfix.at(n))) {
case 1:
answer.push(stod(postfix.at(n)));
cout<< "postfix(n): " << postfix.at(n);
cout<< "\n";
break;
case 4:
cout<< postfix.at(n);
cout<< "\n";
val1 = answer.top();
cout<< "val1: " << val1;
cout<< "\n";
answer.pop();
val2 = answer.top();
cout<< "val2: " << val2;
cout<< "\n";
answer.pop();
if (postfix.at(n) == "+") {
aVal = val1 + val2;
cout<< "aVal(+): " << aVal;
cout<< "\n";
} else {
aVal = val2 - val1;
cout<< "aVal(-): " << aVal;
cout<< "\n";
}
answer.push(aVal);
break;
case 5:
cout<< postfix.at(n);
cout<< "\n";
val1 = answer.top();
answer.pop();
cout<< "val1: " << val1;
cout<< "\n";
val2 = answer.top();
answer.pop();
cout<< "val2: " << val2;
cout<< "\n";
aVal = (val2)/(val1);
answer.push(aVal);
cout<< "aVal(/): " << aVal;
cout<< "\n";
break;
case 6:
cout<< postfix.at(n);
cout<< "\n";
val1 = answer.top();
answer.pop();
cout<< "val1: " << val1;
cout<< "\n";
val2 = answer.top();
answer.pop();
cout<< "val2: " << val2;
cout<< "\n";
aVal = (val2)*(val1);
answer.push(aVal);
cout<< "aVal(*): " << aVal;
cout<< "\n";
break;
default:
break;
}
}
cout<< answer.top();
cout<< "\n";
return 0;
}
vector<string>& tokenize(string line, string delimiter){
vector<string>* allSplits = new vector<string>;
string lineCopy = line;
while(lineCopy.size() != 0){
int location = lineCopy.find(delimiter);
if(location == string::npos){
allSplits->push_back(lineCopy);
break;
}
allSplits->push_back(lineCopy.substr(0, location));
lineCopy.erase(0,location + delimiter.size());
}
return *allSplits;
}
int whatIsIt(string& inputOp) {
if (isNumber(inputOp) == 1) {
return 1;
} else if (inputOp == "(") {
return 2;
} else if (inputOp == ")") {
return 3;
} else if (inputOp == "+") {
return 4;
} else if (inputOp == "-") {
return 4;
} else if (inputOp == "/") {
return 5;
} else if (inputOp == "*") {
return 6;
} else {
return 0;
}
}
int isNumber(const string& input){
if(input.size() == 0){
return -1;
}
bool foundDecimal = false;
for(int i=0; i<input.size(); i++){
if(!isdigit(input.at(i))){
if(input.at(i) == '.'){
if(foundDecimal){
return 0;
}else{
foundDecimal = true;
// allows trailing and leading decimal. ex: .1, 10.
}
}else{
return 0;
}
}
}
return 1;
}
这里是我的main.h
//
// main.h
// testing
//
// Created by Tan Der Shen on 11/17/15.
// Copyright (c) 2015 Tan Der Shen. All rights reserved.
//
#ifndef testing_main_h
#define testing_main_h
#include <iostream>
#include <string>
#include <vector>
#include <sstream>
#include <stack>
std::vector<std::string>& tokenize(std::string line, std::string delimiter);
int whatIsIt(std::string& inputOp);
int isNumber(const std::string&);
std::vector<std::string> postfix;
std::stack<std::string> operators;
std::stack<double> answer;
double val1;
double val2;
double aVal;
#endif
请帮助我理解为什么我会收到这个错误。
答案 0 :(得分:0)
对于
std::stack<double /*, std::deque<double>*/> stack;
stack.top();
effectively calls [container].back().,恰好是您std::deque::back
的情况,依次为
未定义回调空容器。
成立; stack::pop
和deque::pop_back
相同。
因此你应该检查你是否真的把东西推到了堆栈上,如果没有,为什么不呢,当你调用pop()
时,堆栈上实际上是pop
ped。