ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
hey i'm doing a project in c++ for one of my classes where you have to read some infix expressions from a file change them into postfix than evaluate them and output to the file however i keep getting this error when i run it:
*** glibc detected *** double free or corruption (fasttop): 0x0804d270 ***
Aborted
it occurs after the code has done what i want it to i have it set up to just output the data it read from the file
this is the main for my code
Code:
#include <iostream>
#include <fstream>
#include <string>
#include <cassert>
#include <cctype>
#include "Queue.h"
#include "LinkedList.h"
#include "Stack.h"
using namespace std;
Queue<string> readerQueue;
Stack<char> opStack;
Stack<char> evalStack;
LinkedList<int> varList;
string toPostfix(string exp);
void evaluate(string exp);
int main() {
ifstream fin;
fin.open("myfile.txt");
assert(fin.is_open());
string reading;
for ( ;; ) {
getline(fin, reading);
if (fin.eof()) {
break;
}
readerQueue.push(reading);
}
fin.close();
string temp;
while (!readerQueue.isEmpty()) {
temp = readerQueue.peek();
cout << readerQueue.getSize() << " " << temp << endl;
readerQueue.pull();
}
/*while (!readerQueue.isEmpty()) {
temp = toPostfix(readerQueue.peek());
evaluate(temp);
readerQueue.pull();
}
ofstream fout("output.txt");
assert(fout.is_open());
fout << varList.print();
fout.close();
cout << "Processing comlete." << endl;*/
return 0;
}
/* Converts an infix expression to postfix
pre - infix expression is passed
post - postfix expression is returned */
inline string toPostfix(string exp) {
char token, topToken;
string postfixExp;
for (int i = 0 ; i < exp.size() ; i ++) {
token = exp[i];
switch (token) {
case ' ' : break;
case '=' : opStack.push(token);
break;
case '(' : opStack.push(token);
break;
case ')' : for ( ;; ) {
assert (!opStack.isEmpty());
topToken = opStack.peek();
opStack.pop();
if (topToken == '(') {
break;
}
postfixExp.append(" " + topToken);
}
break;
case '+' :
case '-' :
case '*' :
case '/' :
case '%' : for ( ;; ) {
if (opStack.isEmpty() || opStack.peek() == '(' ||
(token == '*' || token == '/' || token == '%') && (opStack.peek() == '+' || opStack.peek() == '-')) {
opStack.push(token);
break;
}
else {
topToken = opStack.peek();
opStack.pop();
postfixExp.append(" " + topToken);
}
}
break;
default: //operand
postfixExp.append(" " + token);
for ( ;; ) {
if (!isalnum(exp[i+1])) {
break;
}
i++;
token = exp[i];
postfixExp.append(1, token);
}
}
}
//Pop remaining operators on the stack
for ( ;; ) {
if (opStack.isEmpty()) {
break;
}
topToken = opStack.peek();
opStack.pop();
if (topToken != '(') {
postfixExp.append(" " + topToken);
}
else {
cerr << "***ERROR IN INFIX EXPRESSION***" << endl;
break;
}
}
return postfixExp;
}
/* Evaluates a postfix expression
pre - postfix expression is passed
post - after evaluation, values inserted into a list */
inline void evaluate(string exp) {
char token, upperTok, lowerTok;
int holder, upper, lower;
for (int i = 0 ; i < exp.size() ; i ++) {
token = exp[i];
switch (token) {
case ' ' : break;
case '+' :
case '-' :
case '*' :
case '/' :
case '%' : upperTok = evalStack.peek();
evalStack.pop();
lowerTok = evalStack.peek();
evalStack.pop();
if (isalpha(lowerTok)) {
lower = varList.getValue(lowerTok);
}
else {
lower = (int)lowerTok;
}
if (isalpha(upperTok)) {
upper = varList.getValue(upperTok);
}
else {
upper = (int)upperTok;
}
switch (token) {
case '+' : holder = lower + upper;
break;
case '-' : holder = lower - upper;
break;
case '*' : holder = lower * upper;
break;
case '/' : holder = lower / upper;
break;
case '%' : holder = lower % upper;
break;
}
evalStack.push((char)holder);
break;
case '=' : upperTok = evalStack.peek();
evalStack.pop();
lowerTok = evalStack.peek();
evalStack.pop();
if (isalpha(upperTok) && !isalpha(lowerTok)) {
varList.insert(upperTok, (int)lowerTok);
break;
}
else {
varList.insert(lowerTok, (int)upperTok);
break;
}
default: //operand
evalStack.push(token);
break;
}
}
}
this is queue.h i think this is where the problems lies because all im trying to do is pull() the data from the queue
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.