user-0 提问: 用顺序栈判断括号是否配对

回复:
user: user-1
content: 运行程序以后，你要从键盘输入你需要测试的字符串。  现在的码农竟然99%都不会在cmd窗口中输入cd命令设置当前目录为程序所在目录，输入程序名运行程序了！

user: user-0
content: 我刚刚大一，老师，用链栈编写一个计算器，计算部分的思路是什么，您当时学习编程也是成天敲代码吗，

user: user-1
content: 仅供参考：#include<stdio.h> #include<malloc.h> #include<string.h> #define STACK_INIT_SIZE 10 #define STACK_GROW_SIZE 5 #define ELEMTYPE char #define OK 1 #define ERROR 0 typedef struct { /*建立一个栈的首结点*/     ELEMTYPE * base;     ELEMTYPE * top;     int stacksize; } SpStack; int InitStack(SpStack *s) { /*建立空的栈并返回首地址*/     s->base=((ELEMTYPE*)malloc(STACK_INIT_SIZE*sizeof(ELEMTYPE)));     if (!s->base) return ERROR;     s->top=s->base;     s->stacksize=STACK_INIT_SIZE;     return OK; } int StackEmpty(SpStack *s) { /*判断栈是否为空*/     if (s->top==s->base) return OK;     else                 return ERROR; } int Push(SpStack *s,ELEMTYPE e) { /*往栈顶插入元素即进栈*/     if (s->top-s->base>=s->stacksize) { /*判断是否栈满*/         s->base=((ELEMTYPE*)realloc(s->base,(s->stacksize+STACK_GROW_SIZE)*sizeof(ELEMTYPE)));         if (!s->base) return ERROR;         s->stacksize+=STACK_GROW_SIZE;         s->top=s->base+s->stacksize;     }     *s->top++=e;     return OK; } int Pop(SpStack *s,ELEMTYPE *e) { /*让栈顶元素依次输出即出栈*/     if (StackEmpty(s)) return ERROR;     *e=*(--s->top);     return OK; } int Comp(ELEMTYPE a,ELEMTYPE b) {     if ((a=='('&&b!=')')       ||(a=='['&&b!=']')       ||(a=='{'&&b!='}')) {         return ERROR;     } else return OK; } int Count(SpStack *s) {     ELEMTYPE e[STACK_INIT_SIZE*2];     ELEMTYPE e1;     int i;      InitStack(s);     fgets(e,STACK_INIT_SIZE*2,stdin);     if ('\n'==e[strlen(e)-1]) e[strlen(e)-1]=0;     printf("%s\n",e);     for (i=0;e[i]!='\0';i++) {         switch (e[i]) {         case '(':         case '[':         case '{':             Push(s,e[i]);             break;         case ')':         case ']':         case '}':             if (StackEmpty(s)) {                 printf("%*s↖右括号多余\n",i+1,"");                 return(ERROR);             } else Pop(s,&e1);             if (!Comp(e1,e[i])) {                 printf("%*s↖左右匹配出错\n",i+1,"");                 return(ERROR);             }         }     }     if (!StackEmpty(s)) {         printf("%*s↖左括号多余\n",i,"");         return(ERROR);     } else {         printf("匹配正确\n");         return(OK);     } } void main() {     SpStack s;     Count(&s);     free(s.base); }

user: user-0
content: 引用 9 楼 zhao4zhong1 的回复:void main是vc6可以接受的写法。‘\n’  还有‘\0’是什么意思啊   这个程序在我电脑上执行什么也没有 ，是不是需要加一个字符数组检验呢  ，我试着在main函数加一个str[100]={(3+4)*[5+6]};出现好多错误 ；是加错了吗  。需要加吗

user: user-1
content: void main是vc6可以接受的写法。

user: user-0
content: 引用 7 楼 qq_34627769 的回复:[quote=引用 6 楼 zhao4zhong1 的回复:] 仅供参考：#include<stdio.h> #include<malloc.h> #include<string.h> #define STACK_INIT_SIZE 10 #define STACK_GROW_SIZE 5 #define ELEMTYPE char #define OK 1 #define ERROR 0 typedef struct { /*建立一个栈的首结点*/     ELEMTYPE * base;     ELEMTYPE * top;     int stacksize; } SpStack; int InitStack(SpStack *s) { /*建立空的栈并返回首地址*/     s->base=((ELEMTYPE*)malloc(STACK_INIT_SIZE*sizeof(ELEMTYPE)));     if (!s->base) return ERROR;     s->top=s->base;     s->stacksize=STACK_INIT_SIZE;     return OK; } int StackEmpty(SpStack *s) { /*判断栈是否为空*/     if (s->top==s->base) return OK;     else                 return ERROR; } int Push(SpStack *s,ELEMTYPE e) { /*往栈顶插入元素即进栈*/     if (s->top-s->base>=s->stacksize) { /*判断是否栈满*/         s->base=((ELEMTYPE*)realloc(s->base,(s->stacksize+STACK_GROW_SIZE)*sizeof(ELEMTYPE)));         if (!s->base) return ERROR;         s->stacksize+=STACK_GROW_SIZE;         s->top=s->base+s->stacksize;     }     *s->top++=e;     return OK; } int Pop(SpStack *s,ELEMTYPE *e) { /*让栈顶元素依次输出即出栈*/     if (StackEmpty(s)) return ERROR;     *e=*(--s->top);     return OK; } int Comp(ELEMTYPE a,ELEMTYPE b) {     if ((a=='('&&b!=')')       ||(a=='['&&b!=']')       ||(a=='{'&&b!='}')) {         return ERROR;     } else return OK; } int Count(SpStack *s) {     ELEMTYPE e[STACK_INIT_SIZE*2];     ELEMTYPE e1;     int i;      InitStack(s);     fgets(e,STACK_INIT_SIZE*2,stdin);     if ('\n'==e[strlen(e)-1]) e[strlen(e)-1]=0;     printf("%s\n",e);     for (i=0;e[i]!='\0';i++) {         switch (e[i]) {         case '(':         case '[':         case '{':             Push(s,e[i]);             break;         case ')':         case ']':         case '}':             if (StackEmpty(s)) {                 printf("%*s↖右括号多余\n",i+1,"");                 return(ERROR);             } else Pop(s,&e1);             if (!Comp(e1,e[i])) {                 printf("%*s↖左右匹配出错\n",i+1,"");                 return(ERROR);             }         }     }     if (!StackEmpty(s)) {         printf("%*s↖左括号多余\n",i,"");         return(ERROR);     } else {         printf("匹配正确\n");         return(OK);     } } void main() {     SpStack s;     Count(&s);     free(s.base); } 最后的main是不是需要返回int 啊    用void 在coldblocks有error，我改成int是正确的 [/quote] 不   对了 刚刚我在那个错吴的text里编译的   换了一个好了，谢谢了老师

user: user-0
content: 引用 6 楼 zhao4zhong1 的回复:仅供参考：#include<stdio.h> #include<malloc.h> #include<string.h> #define STACK_INIT_SIZE 10 #define STACK_GROW_SIZE 5 #define ELEMTYPE char #define OK 1 #define ERROR 0 typedef struct { /*建立一个栈的首结点*/     ELEMTYPE * base;     ELEMTYPE * top;     int stacksize; } SpStack; int InitStack(SpStack *s) { /*建立空的栈并返回首地址*/     s->base=((ELEMTYPE*)malloc(STACK_INIT_SIZE*sizeof(ELEMTYPE)));     if (!s->base) return ERROR;     s->top=s->base;     s->stacksize=STACK_INIT_SIZE;     return OK; } int StackEmpty(SpStack *s) { /*判断栈是否为空*/     if (s->top==s->base) return OK;     else                 return ERROR; } int Push(SpStack *s,ELEMTYPE e) { /*往栈顶插入元素即进栈*/     if (s->top-s->base>=s->stacksize) { /*判断是否栈满*/         s->base=((ELEMTYPE*)realloc(s->base,(s->stacksize+STACK_GROW_SIZE)*sizeof(ELEMTYPE)));         if (!s->base) return ERROR;         s->stacksize+=STACK_GROW_SIZE;         s->top=s->base+s->stacksize;     }     *s->top++=e;     return OK; } int Pop(SpStack *s,ELEMTYPE *e) { /*让栈顶元素依次输出即出栈*/     if (StackEmpty(s)) return ERROR;     *e=*(--s->top);     return OK; } int Comp(ELEMTYPE a,ELEMTYPE b) {     if ((a=='('&&b!=')')       ||(a=='['&&b!=']')       ||(a=='{'&&b!='}')) {         return ERROR;     } else return OK; } int Count(SpStack *s) {     ELEMTYPE e[STACK_INIT_SIZE*2];     ELEMTYPE e1;     int i;      InitStack(s);     fgets(e,STACK_INIT_SIZE*2,stdin);     if ('\n'==e[strlen(e)-1]) e[strlen(e)-1]=0;     printf("%s\n",e);     for (i=0;e[i]!='\0';i++) {         switch (e[i]) {         case '(':         case '[':         case '{':             Push(s,e[i]);             break;         case ')':         case ']':         case '}':             if (StackEmpty(s)) {                 printf("%*s↖右括号多余\n",i+1,"");                 return(ERROR);             } else Pop(s,&e1);             if (!Comp(e1,e[i])) {                 printf("%*s↖左右匹配出错\n",i+1,"");                 return(ERROR);             }         }     }     if (!StackEmpty(s)) {         printf("%*s↖左括号多余\n",i,"");         return(ERROR);     } else {         printf("匹配正确\n");         return(OK);     } } void main() {     SpStack s;     Count(&s);     free(s.base); } 最后的main是不是需要返回int 啊    用void 在coldblocks有error，我改成int是正确的

user: user-0
content: 引用 2 楼 qq423399099 的回复:#include <iostream> #include <string> #include <stdlib.h> using namespace std;  class stack{ private:     int max;     int top;     char* str; public:     stack(int size)     {         max=size;         top=-1;         str=new char[max];     }     stack()     {         top=-1;     }     ~stack()     {         delete []str;     }     void clear()     {         top=-1;     }     bool push(const char item)     {         if(top==max-1)         {              return false;         }         else         {             str[++top]=item;             return true;         }     }     bool pop(char &item)     {         if(-1==top)         {              return false;         }         else         {             item=str[top--];             return true;         }     }     bool empty()     {         if(-1==top)             return true;         else             return false;     } };  int main() {     char c;     string st;     int d=1,j=1;     stack a(25);     while(j){         cout<<"请输入表达式（英文半角）："<<endl;         cin>>st;         for(int i=0;st[i]!='\0'&&d==1;i++)         {             switch(st[i])             {             case'(':             case'[':             case'{':                 a.push(st[i]);                 break;             case')':                 a.pop(c);                 if(c=='(')                     break;                 else                 {                     d=0;                     break;                 }             case']':                 a.pop(c);                 if(c=='[')                     break;                 else                 {                     d=0;                     break;                 }             case'}':                 if(c=='{')                     break;                 else                 {                     d=0;                     break;                 }              }         }         if(a.empty()&&d==1)             cout<<"配对正确！"<<endl;         else             cout<<"配对错误！"<<endl;          cout<<"******************"<<endl;         cout<<"1.继续"<<endl;         cout<<"2.退出"<<endl;         cout<<"******************"<<endl;         cin>>j;         if(2==j)         {             j=0;         }         if(1==j)         {             system("cls");             a.clear();         }     }     system("cls");     cout<<"程序结束！"<<endl;      return 0; }大神 你的程序有error

user: user-0
content: 引用 1 楼 qq423399099 的回复:怎么写看楼主的需求啊 首先栈初始化 入栈的时候要判栈满，出栈要判栈空 之后就因人而异了 引用 1 楼 qq423399099 的回复:怎么写看楼主的需求啊 首先栈初始化 入栈的时候要判栈满，出栈要判栈空 之后就因人而异了#include <stdio.h> #include <stdlib.h> #include <string.h>  #define MAX 100 typedef char ElemType; typedef struct stack {   ElemType a[MAX];   int top; } Stack;  void InitStack(Stack **p) {   *p = (Stack*)malloc(sizeof(Stack));   (*p)->top == -1; }  int StackEmpty(Stack *p) {     return (p->top==-1)?1:0; }  int StackFull(Stack *p) { return (p->top == MAX-1)?1:0; }  void Push(Stack *p, ElemType x) {     if(!StackFull(p)){     p->top++;     p->a[p->top]=x;     } }  void Pop(Stack *p, ElemType *q) {      if(!StackEmpty(p)){       *q=p->a[(p->top)--];      } }/* *q=p->a[p->top]; p->top--;*/  int StackTop(Stack *p, ElemType *q) {       if(!StackEmpty(p)){         *q=p->a[p->top];         return 1;       }else       return 0; }  void StackClear(Stack *p) {      p->a[p->top]==-1; }  int Match(char a, char b) {    if     (a=='('&&b==')'||a=='['&&b==']'||a=='{'&&b=='}')    return 1;    else    return 0; }   int main() {   //char str[100] = "()[]{}";   //char str[100] = "{[()][(()())]}";   //char str[100] = "([)]{}";   char str[100] = "{[)(]{}}";    InitStack(Stack);    int i=0;    for(i;i<100;i++)    {        if (i=='(')             Push(Stack *p,str[i]);    }     return 0; } 我这个只剩下主函数了，我想知道的是如何将str[]与栈联系上

user: user-0
content: #include <stdio.h> #include <stdlib.h> #include <string.h>  #define MAX 100 typedef char ElemType; typedef struct stack {   ElemType a[MAX];   int top; } Stack;  void InitStack(Stack **p) {   *p = (Stack*)malloc(sizeof(Stack));   (*p)->top == -1; }  int StackEmpty(Stack *p) {     return (p->top==-1)?1:0; }  int StackFull(Stack *p) { return (p->top == MAX-1)?1:0; }  void Push(Stack *p, ElemType x) {     if(!StackFull(p)){     p->top++;     p->a[p->top]=x;     } }  void Pop(Stack *p, ElemType *q) {      if(!StackEmpty(p)){       *q=p->a[(p->top)--];      } }/* *q=p->a[p->top]; p->top--;*/  int StackTop(Stack *p, ElemType *q) {       if(!StackEmpty(p)){         *q=p->a[p->top];         return 1;       }else       return 0; }  void StackClear(Stack *p) {      p->a[p->top]==-1; }  int Match(char a, char b) {    if     (a=='('&&b==')'||a=='['&&b==']'||a=='{'&&b=='}')    return 1;    else    return 0; }   int main() {   //char str[100] = "()[]{}";   //char str[100] = "{[()][(()())]}";   //char str[100] = "([)]{}";   char str[100] = "{[)(]{}}";    InitStack(Stack);    int i=0;    for(i;i<100;i++)    {        if (i=='(')             Push(Stack *p,str[i]);    }     return 0; } 我这个只剩下主函数了，我想知道的是如何将str[]与栈联系上

user: user-2
content: #include <iostream> #include <string> #include <stdlib.h> using namespace std;  class stack{ private:     int max;     int top;     char* str; public:     stack(int size)     {         max=size;         top=-1;         str=new char[max];     }     stack()     {         top=-1;     }     ~stack()     {         delete []str;     }     void clear()     {         top=-1;     }     bool push(const char item)     {         if(top==max-1)         {              return false;         }         else         {             str[++top]=item;             return true;         }     }     bool pop(char &item)     {         if(-1==top)         {              return false;         }         else         {             item=str[top--];             return true;         }     }     bool empty()     {         if(-1==top)             return true;         else             return false;     } };  int main() {     char c;     string st;     int d=1,j=1;     stack a(25);     while(j){         cout<<"请输入表达式（英文半角）："<<endl;         cin>>st;         for(int i=0;st[i]!='\0'&&d==1;i++)         {             switch(st[i])             {             case'(':             case'[':             case'{':                 a.push(st[i]);                 break;             case')':                 a.pop(c);                 if(c=='(')                     break;                 else                 {                     d=0;                     break;                 }             case']':                 a.pop(c);                 if(c=='[')                     break;                 else                 {                     d=0;                     break;                 }             case'}':                 if(c=='{')                     break;                 else                 {                     d=0;                     break;                 }              }         }         if(a.empty()&&d==1)             cout<<"配对正确！"<<endl;         else             cout<<"配对错误！"<<endl;          cout<<"******************"<<endl;         cout<<"1.继续"<<endl;         cout<<"2.退出"<<endl;         cout<<"******************"<<endl;         cin>>j;         if(2==j)         {             j=0;         }         if(1==j)         {             system("cls");             a.clear();         }     }     system("cls");     cout<<"程序结束！"<<endl;      return 0; }

user: user-2
content: 怎么写看楼主的需求啊 首先栈初始化 入栈的时候要判栈满，出栈要判栈空 之后就因人而异了