Mathematical grouping

i have a program going that is supposed to take an equation say (8+9) and tell weather it has the proper grouping. Where as (8+9} is not proper nor is {(8+9}).
my pseudo code for the last part i need is
if(array[i] != to a corresponding right symbol) then break out of the loop.
i just don't really know exactly how to code this part
here is what i have:
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
{

char array[50];
cout<<"Enter your function"<<endl;
cin>>array;
int i=0;
stack<char> b;
while(array[i] != '\0'){

        if(array[i] == '(')
              b.push('(');
        else if(array[i] == ')')
              b.pop();
              
        if(array[i] == '{')
              b.push('{');
        else if(array[i] == '}')
              b.pop();
          
    i++;    
}


cout << endl;

b.view();
system("PAUSE");
}
i think i need to return what i am popping and compare it to something but im still fuzzy on it
here is my pop function but im not sure how to make it return what it is popping.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
template<class type>
bool stack<type> :: pop ()
{

node<type>* cur;
cur = head;
if(cur == NULL)
{
return false;
}

else
{
head = cur -> next;
delete cur;
return true;
}
}
would i just return cur and then delete it?
As to that question: You would change the return value to type, create a new local type variable, assign *head to it, and return it at the end of the function after deleting the old head.

As to the other one, if you encounter an ')', you check whether pop returns '(', if you encounter a '}' you do the same thing with '{'.

Oh, and this forum has an edit button. Please don't quadruple post, it's not forbidden but it's very annoying.
i only did it because they were separate thought
That's what paragraphs are for.
yes i know it was sloppy i do apologize
ok so here is what i have so far... it kind of works but its not perfect if you could take a quick glance and look at it let me know what you think.
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
template<class type>
type stack<type> :: pop ()
{

node<type>* cur;
cur = head;
if(cur == NULL)
{
cout<<"There is nothing to pop"<<endl;
}

else
{
head = cur -> next;
return cur->data;
delete cur;
}
}
// Accessor functions.
// Function to see whether an element is on the list.
template<class type>
void stack<type> :: view ()
{
node<type>* tmp = head;

while(tmp != NULL){
cout << tmp -> data;
tmp = tmp -> next;
}
cout<<endl;
}

int main()
{

char array[50];
cout<<"Enter your function"<<endl;
cin>>array;
int i=0;
stack<char> b;
while(array[i] != '\0'){

        if(array[i] == '(')
              b.push('(');
        else if(array[i] == ')')
              b.pop();
        if(array[i]==')' && b.pop()=='(')
                          cout<<"correct grouping"<<endl;  
        else if(array[i]==')' && b.pop() != '('){
            cout<<"Incorrect grouping"<<endl;  
            break;
            } 
        if(array[i] == '{')
              b.push('{');
        else if(array[i] == '}')
              b.pop();
        
        if(array[i]=='}' && b.pop()=='{')
                          cout<<"correct grouping"<<endl;   
         
        else if(array[i]=='}' && b.pop() != '{')
        {
            cout<<"Incorrect grouping"<<endl;  
            break;
            }                 
    i++;    
}


cout << endl;

b.view();
system("PAUSE");
}


here is the out put

Enter your function
({})
correct grouping
There is nothing to pop
There is nothing to pop
There is nothing to pop
Incorrect grouping


is my problem that i am adding the items onto my stack from the end... i think thats what it is
Last edited on
How does this work? If pop is implemented properly (which it isn't... you are causing memory leaks again...) this would crash your program with a pretty much 100% chance. Or rather than crash, it would pretty much give you that result. Please look at your pop function again and look at what you do in your code, and figure out for yourself what you are doing wrong. Think about what pop does, and what the return statement does.
Last edited on
well it runs just not perfectly.

it is supposed to add the '(' or '{' to the stack when it encounters one. and then when it encounters the closing form of those it will pop from the stack and if what it is popping is the corresponding closing brace then the grouping is correct so for example
({})
would push the first ( and the { to the stack then when it encounters the closing it will pop it off so then it reaches } and should pop { but what i have now the push function adds from the back but i changed it to push from the beginning so the stack looks like {( after words
Last edited on
Nope, it's a complete mess.
would you like me to post the entire program?

at the moment my program works with using () or {} but once i mix the two for ex. ({}) it doesn't work

in the pop function for some reason its always saying cur is null even when i know im putting something in so i just put a cout; for now just to get rid of the extra output
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
#include <iomanip>
#include <iostream>
#include <typeinfo>
#include <cstdlib>
#include <string>

using namespace std;

// Specification file for a linked list abstract data type class.
// This linked list is actually a stack, since all additions and
// removals are at the head.
template<class type>
struct node // Each node has two fields:
{
type data; // a data field,
node<type> *next; // and a pointer field.
};

template<class type>
class stack
{
private:
node<type> *head; // Pointer to the first cell.

public:

stack();
void push(type item);
type pop();
void view();

};

template<class type>
stack<type>::stack()
{
head=NULL;
}

template<class type>
void stack<type> :: push (type item)
{
node<type> *t = new node<type>;
t -> data = item;
t -> next = head;
head = t;

}


// Function to remove the first element from the stack and
// return it to the caller.
template<class type>
type stack<type> :: pop ()
{

node<type>* cur;
cur = head;
if(cur == NULL)
{
cout;
}

else
{
head = cur -> next;
return cur->data;
delete cur;
}
}
// Accessor functions.
// Function to see whether an element is on the list.
template<class type>
void stack<type> :: view ()
{
node<type>* tmp = head;

while(tmp != NULL){
cout << tmp -> data;
tmp = tmp -> next;
}
cout<<endl;
}

int main()
{

char array[50];
cout<<"Enter your function"<<endl;
cin>>array;
int i=0;
stack<char> b;
while(array[i] != '\0'){

        if(array[i] == '(')
              b.push('(');
        else if(array[i] == ')')
              b.pop();
        if(array[i]==')' && b.pop()=='(')
        {
        cout<<"correct grouping"<<endl; 
        } 
        else if(array[i]==')' && b.pop() != '('){
            cout<<"Incorrect grouping"<<endl;  
            break;
            } 
            
        if(array[i] == '{')
              b.push('{');
        else if(array[i] == '}')
              b.pop();
        
        if(array[i]=='}' && b.pop()=='{')
        {
        cout<<"correct grouping"<<endl;   
        }
        else if(array[i]=='}' && b.pop() != '{')
        {
            cout<<"Incorrect grouping"<<endl;  
            break;
            }                 
    i++;    
}



b.view();

system("PAUSE");
}

i keep writing it down on paper and it seems to work fine but its not working
Last edited on
never mind i got it
Topic archived. No new replies allowed.