Hi there ever1. I have written a bracket checker program using stack, seems to me its working ok, but still I have one problem with it.Well, it compiles ok,but when you are entering value, it just doesn't show you any results, but it supposed to give you the results.Any help in finding the problem will be appreciaed.Thanks.
Heres the code:
#include<iostream>
using namespace std;
typedef struct Node
{ char data;
struct Node *next;
}node;
void push(char);
void display_stack();
node *top;
int main()
{
char check();
char temp[20];
int i=0,flag=1;
system("CLS");
cout << "\nPlease Enter Math Expression:\n" << endl;
top=NULL;
temp[i]= cin.get();
if ( (temp[i]==')') || (temp[i]==']') || (temp[i]=='}') )
{
cout << "\n Bad Expression! Please Run it Again... \n" << endl;
cin.get();
return 1;
}
else
{
while ( (int)temp[i]!=13 && (flag==1))
{
switch (toupper(temp[i]))
{
case '(': { push(temp[i]); break; }
case '[': { push(temp[i]); break; }
case '{': { push(temp[i]); break; }
case ')': { push(temp[i]); break; }
case ']': { push(temp[i]); break; }
case '}': { push(temp[i]); break; }
//case ')': { flag=check(temp[i]);}
//case ']': { flag=check(temp[i]);}
//case '}': { flag=check(temp[i]);}
}
i+=1;
temp[i] = cin.get();
}
}
if (flag==1) cout << "\nOK\n" << endl;
else cout << "\nNO!\n" << endl;
return 0;
};
void push(char y)
{
node *ptr;
ptr= new node;
ptr->data = y;
ptr->next = top;
top = ptr;
}
void display_stack()
{
int i =0;
node * temp;
temp = top;
while(temp!=NULL)
{
cout << "\nNode : Value = next " << endl;
i++,temp->data,temp->next;
temp=temp->next;
}
}
/* REMOVES TOP NODE FROM THE STACK AND RETURNS ITS VALUE*/
char pop()
{
char a;
if(top==NULL)
{
cout << ("\n\t\tSTACK EMPTY...\n\n") << endl; return 0;
}
else
{
a=top->data;
cout << ("\n\n\n Pop: value to be returned : c ",a) << endl;
free(top);
top=top->next;
return a;
}
}
int check(char x)
{
char tmp;
tmp = pop();
switch (tmp)
{
case ')': { if( tmp-x!=1 ) return 0; break;}
case ']': { if( tmp-x!=2 ) return 0; break;}
case '}': { if( tmp-x!=2 ) return 0; break;}
}
return 1;
}