一种括号的情况
#include <iostream></span>
#include <stdlib.h>
#include <stdbool.h>
using namespace std;
typedef struct node
{
char AL;
struct node* next;
}qnode,*pnode;
typedef struct stack
{
pnode top;
pnode botton;
}qstack,*pstack;
void creat_(pstack s)
{
s->top=(pnode)malloc(sizeof(node));
if(s->top==NULL)
{
cout<<"fault";
return;
}
s->botton=s->top;
s->botton->next=NULL;
}
void push(pstack s,char e)
{
pnode new_=(pnode)malloc(sizeof(qnode));
new_->AL=e;
new_->next=s->top;
s->top=new_;
}
bool empty(pstack s)
{
if(s->top==s->botton)
{
return true;
}
else
{
return false;
}
}
void pop(pstack s)
{
if(empty(s))
{
cout<<"出栈失败";
}
else
{
pnode r;
r=s->top;
s->top=r->next;
free(r);
}
}
int lengh(pstack s)
{
int i=0;
pnode q=s->top;
while(q!=s->botton)
{
q=q->next;
i++;
}
return i;
}
int check()
{
pstack s;
creat_(s);
char ch;
ch=getchar();
while(ch!='\n')//当等于换行的时候跳出循环
{
if(ch=='(')//等于(入栈
{
push(s,ch);
}
else if(ch==')')//等于)如果为空则缺少(返回负一
{
if(empty(s))
{
return -1;
}
else
{
pop(s);//否则的话栈顶出栈
}
}
ch=getchar();//再次输入
}
return lengh(s);//返回最后栈的长度
}
int main()
{
int result;
result=check();
if(result==0)
{
cout<<"right";
}
else if(result>0)
{
cout<<"min";
}
else if(result<0)
{
cout<<"min(";
}
return 0;
}
------------------------
两种括号的情况
#include <iostream>
#include <stdlib.h>
#include <stdbool.h>
using namespace std;
typedef struct node
{
char AL;
struct node* next;
}qnode,*pnode;
typedef struct stack
{
pnode top;
pnode botton;
}qstack,*pstack;
void creat_(pstack s)
{
s->top=(pnode)malloc(sizeof(node));
if(s->top==NULL)
{
cout<<"fault";
return;
}
s->botton=s->top;
s->botton->next=NULL;
}
void push(pstack s,char e)
{
pnode new_=(pnode)malloc(sizeof(qnode));
new_->AL=e;
new_->next=s->top;
s->top=new_;
}
bool empty(pstack s)
{
if(s->top==s->botton)
{
return true;
}
else
{
return false;
}
}
void pop(pstack s)
{
if(empty(s))
{
cout<<"出栈失败";
}
else
{
pnode r;
r=s->top;
s->top=r->next;
free(r);
}
}
int lengh(pstack s)
{
int i=0;
pnode q=s->top;
while(q!=s->botton)
{
q=q->next;
i++;
}
return i;
}
int check()
{
char l;
pstack s;
creat_(s);
char ch;
ch=getchar();//getchar略过空格
while(ch!='\n')//当等于换行的时候跳出循环
{
if(ch=='(')//等于(入栈
{
push(s,ch);
}
else if(ch==')')//等于)如果为空则缺少(返回负一
{
if(empty(s))
{
return -1;
}
else
{ if(s->top->AL=='(')
pop(s);//否则的话栈顶出栈
}
}
else if(ch=='{')
{
push(s,ch);
}
else if(ch=='}')
{
if(empty(s))
{
return -1;
}
else
{
if(s->top->AL=='{')
{
pop(s);
}
}
}
ch=getchar();//再次输入
}
return lengh(s);//返回最后栈的长度
}
int main()
{
int result;
result=check();
if(result==0)
{
cout<<"right";
}
else if(result>0)
{
cout<<"min";
}
else if(result<0)
{
cout<<"short ( or {";
}
return 0;
}