stacks

Program goal: user input for 2 equations to evaluate the operator signs. i.e. 1 * 2 + 4 - 4
and 5 - 2 * 6 + 9 would be the same --> " * + - " in both, order does not matter.

Output
: cout saying match or not match.

I'm not sure how to go about checking the operators in the best/easiest fashion, in the main(), or in my cpp file where 90% of the "work" is happening. I've tried all "i" know to do. Obviously I'm missing something, and/or went about this program totally abstract. But, I am basically teaching myself and slightly struggling. So.. he's the code: ( any help is GREATLY appreciated !)

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
int main() {

	stack s1;
	stack s2;
    char ch1=NULL,
		 ch2=NULL;

	if(s1.empty())
		cout << "stack currently empty...\n" << endl;  // check if stack is empty

    cout << "enter first set: ";
	while ((ch1 = cin.get())!= '\n'){
		if (!s1.full()) s1.push(ch1,ch2);
	while (!s1.empty())
		cout << s1.pop();
	}//end whiles s1
	
    cout << "\nenter second set: ";
	while ((ch2 = cin.get())!= '\n'){
		if (!s2.full()) s2.push(ch1,ch2);}
	while (!s2.empty()){
		cout <<  s2.pop();
	}//end whiles s2
	
// prob not the best place to check for equality here...

	s1.~stack(); // delete s1 stack
	s2.~stack(); // delete s2 stack
	
	cin.sync();
	cin.peek();
}


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
void stack::push(int a,int b) {
	
	char p ='+',
	s= '-',
	m = '*',
	d = '/',
	e = '=';
   char pp ='+',
	ss = '-',
	mm = '*',
	dd = '/',
	ee = '=';

	if(a == p){
		cout << p;
		p++;}

	if(a == s){
		cout << s;
		s++;}

	if(a == m){
		cout << m;
		m++;}

	if(a == d){
		cout << d;
		d++;}

	if(a == e){
		cout << e;
		e++;}
/*--------------------------------------
   if (int a, int b)  seperator
---------------------------------------*/
	if(b == pp){
		cout << pp; 
		pp++;}

	if(b == ss){
		cout << ss;
		ss++;}

	if(b == mm){
		cout << mm;
		mm++;}

	if(b == dd){
		cout << dd;
		dd++;}

	if(b == ee){
		cout << ee;
		ee++;}

//thinking this is the better place...but HOW is the problem ..

}// end stack::push 
Your code is quite obfuscated.
1
2
3
while ((ch1 = cin.get())!= '\n'){
  if (!s1.full()) 
    s1.push(ch1,ch2); //ch2 never changes 

What do you want to do here?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
void stack::push(int a,int b) { //why mixing the types?
//An enum in your class would be a lot better that this constants 
//(that are not const) repeating all over the code
	char p ='+',
	s= '-',
	m = '*',
	d = '/',
	e = '=';
   char pp ='+', //why repeat the info?
	ss = '-',
	mm = '*',
	dd = '/',
	ee = '=';

//a switch will be clearer
	if(a == p){
		cout << p;
		p++; //eh? That local variable will die when the method ends
//...
}
Your push method does not modify your object. That doesn't make sense. And why is it recieving 2 parameters?

1
2
s1.~stack(); // delete s1 stack
s2.~stack(); // delete s2 stack 
There is no need for you to call the destructors (in fact, can be dangerous)
char ch1=NULL; it is not a pointer, use '\0' or 0. (the result is the same)
Last edited on
Topic archived. No new replies allowed.