Just started learning data structures. Wrote a classic application with buyers, with parentheses matching.
Algorithm ideas:
When you enter a string. Will ' (', ' [') press into the buyers. Encountered ') ', then the stack is buyers. to match parentheses. Assume a successful match. will continue. Otherwise. End of program, input mismatch information,
Assuming ') ' is matched, then the inferred buyers is NULL, assuming null, then the input matches. Otherwise, the number of output symbols does not match.
#include <iostream>using namespace std; #define Max Stack{int top;int Maxsize;char *element;}; typedef struct STACK stack;void Init (stack *s,int N) {if (n>0) {s->top=-1; s->maxsize=n; S->element=new Char [s->maxsize];}} BOOL Isfull (Stack *s) {return (s->top==s->maxsize-1);} BOOL IsEmpty (Stack *s) {return (s->top==-1);} void push (Stack *s,char x) {if (! Isfull (S)) s->element[++s->top]=x;else{cout<< "Buyers full" <<ENDL;} Char pop (Stack *s) {if (! IsEmpty (S)) return s->element[s->top--];else{cout<< "Buyers Empty" <<endl;return-1;} int main () {Stack *s=new stack;init (S,max); bool Flag=true;bool flag_1=true;bool flag_2=true;cout<< "Please enter string:" < <endl;char ch;while ((ch = getchar ()) = ' \ n ') {switch (ch) {case ' (': Push (S,CH); Case ' [': Push (S,CH); Case ') ': {if (pop (S)! = ' (' && flag_2) {cout<< "') '" << "does not match" <<endl; Flag_1=false; Flag=false; Break } else {continue; }} Case ': {if (pop (S)! = ' [' && flag_1) {cout<< "'] '" << "symbol does not match" <<endl; Flag_2=false; Flag=false; Break } else {continue; }}}}if (! IsEmpty (S) && flag) {cout<< "Quantity mismatch" <<ENDL;} else if (flag) {cout<< "Match" <<ENDL;} System ("pause"); return 0;}
is a simple definition. Simple function.
Application of buyers in C + +---bracket matching