Need help with my code please!!!!

I'm using a test driver to run my program and Part II (convert to infix to postfix )failed!!! Student gives the right values.
1
2
3
4
5
6
7
8
9
10
11
12
13
infixToPostfix() returned incorrect response for: 2 + a
	Student: 2 a 
	Expected: invalid
infixToPostfix() returned incorrect response for: 40 * ( 2 + 4 - ( 2 + 2 ) ) - 4 / 5 / 6
	Student: invalid
	Expected: 40 2 4 + 2 2 + - * 4 5 / 6 / -
infixToPostfix() returned incorrect response for: ( 3 + 2 + 13 ) / 4
	Student: invalid
	Expected: 3 2 + 13 + 4 /
3+ Test's Failed. 

		Proceeding to next Test.
Part 2 Failed 





1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/*
 * ExpressionManager.h
 *
 *  Created on: May 21, 2014
 *      Author: claudita
 */

#ifndef EXPRESSIONMANAGER_H_
#define EXPRESSIONMANAGER_H_
#include <iostream>
#include <stack>
#include <string>
#include <sstream>
#include "Syntax_Error.h"
using namespace std;

class ExpressionManager : public  ExpressionManagerInterface{
public:
	ExpressionManager(){}
	~ExpressionManager(){}

 /* Checks whether two characters aren opening and closing of the same type */
    bool CharPair(char opening, char closing){
        if (opening == '(' && closing == ')')return true;
        else if (opening == '[' && closing == ']')return true;
        else if (opening == '{' && closing == '}') return true;
        return false;
    }

    bool isBalanced(string expression){

    	stack<char> charstack;
    	for (unsigned int i=0; i<expression.length(); i++)
        {
            if(expression[i] == '(' || expression[i] == '{' || expression[i] == '[')
                charstack.push(expression[i]);
            else if(expression[i] == ')' || expression[i] == '}' || expression[i] == ']')
            {
                if(charstack.empty() || !CharPair(charstack.top(),expression[i]))
                    return false;
                else
                    charstack.pop();
            }
        }
        return charstack.empty() ? true:false;
    }

    /*check if operater is a valid value*/
    bool ev_operator(char op){
    	if (op == '+')
    		return true;
    	else if(op == '-')
    		return true;
    	else if(op == '*')
    		return true;
    	else if(op == '/')
    		return true;
    	else if (op == '%')
    		return true;
    	return false;
    }
    /*check precedence of operator*/
    int precedence(char p) {
    	if (p == '+' || p == '-')
    	    return 1;
    	else if (p == '*' || p == '/'|| p == '%')
       		return 2;
    	return 0;
    }

    void process_operator(char op){
            stack<int> operator_stack;
            string postfix;

            if (operator_stack.empty() || (op == '(') || (op == '[') || (op=='{')) {//if the operator stack is empty
            	if ((op == ')') || (op == ']') || (op=='}')) {
            			throw Syntax_Error("Unmatched close parenthesis");
            	}
            	operator_stack.push(op);//push the current operator onto the stack

            }else {

            	if (precedence(op) > precedence(operator_stack.top())){//if the precende of the current operator is greaten than the precedence of stack.top()
                            operator_stack.push(op);//push the current operator onto stack
                    }else{
                            while (!operator_stack.empty()&& (precedence(op) <= precedence(operator_stack.top()))){
                            postfix += operator_stack.top();
                            postfix += " ";
                            operator_stack.pop();
                            }
                            operator_stack.push(op);
                    }
            }
    }

    string postfixToInfix(string postfixExpression){
    	return string();
    	}




    string infixToPostfix(string infixExpression){
            stack<int> operator_stack; /*initialize the operator stak to an empty stack*/
            string postfix = ""; /*initialize postfix string to an empty string*/
            while (!operator_stack.empty()){ /*while the operator_stack is not empty*/
                    operator_stack.pop();}	/*put the operator onto the operator_stack*/
            istringstream infix_tokens(infixExpression); /*crea un istringstream llamado infix_token*/
            string next_token; //* creates a string called next_token*/
            while(infix_tokens >>next_token){/**/
                    if(isalnum(next_token[0])){/*if next_token is a number*/
                            postfix += next_token;/*postfix = postfix+ next_token*/
                            postfix += " ";
                            }
                    else if (ev_operator(next_token[0])){/*else if the next token is an operator*/
                            process_operator(next_token[0]);/*call process_operator*/
                    }else { /*indicate syntax error*/
                    	return "invalid";}
            }
            while (!operator_stack.empty()){/*pop remaining operators off the operator stack and append them to postfix*/

            		char op = operator_stack.top();
                    operator_stack.pop();
                    postfix += op;
                    postfix += " ";
                    //cout<<"op"<<op;
            }
            return postfix;
            		//cout<<"postfix result"<<postfix;
            }




    string postfixEvaluate(string postfixExpression){
    	return string();
    }

    };

#endif /* EXPRESSIONMANAGER_H_ */ 
> I'm using a test driver to run my program
¿may you post it so we could build and run your program?


loop at line 110, you do not consider parenthesis as valid tokens
> if(isalnum(next_token[0])){/*if next_token is a number*/
wrong isalnum(c) is equivalent to isalpha(c) or isdigit(c), that's why you are considering 'a' as valid.
Last edited on
Yeah, your right. I fixed that.
Topic archived. No new replies allowed.