栈的应用

来源:互联网 发布:super在java中的位置 编辑:程序博客网 时间:2024/06/05 10:05

//括号匹配

#include<stdio.h>#include<string.h>#include<stdlib.h>#define MAXSIZE 100typedef int datatype;typedef struct{datatype a[MAXSIZE];int top;}sequence_stack;void init(sequence_stack *st){st->top=0; } datatype read(sequence_stack *st)    //读取栈顶的元素 { if(st->top==0) { printf("栈是空的\n"); exit(1); } return st->a[st->top-1]; }int empty(sequence_stack *st){return st->top ? 0:1;}void push(sequence_stack *st,datatype x) //将元素压入栈{if(st->top==MAXSIZE){printf("栈是满的\n");exit(1);}st->a[st->top]=x;st->top++;}void pop(sequence_stack *st)  //弹出栈顶元素{if(!st->top){printf("栈是空的\n");exit(1);}st->top--;}int match_kuohao(char s[]){int i=0;sequence_stack s1;init(&s1);while(s[i]!='#'){    switch(s[i]){   case '(' :   case '[':   case '{' :       push(&s1,s[i]);       break;   case ')':        if(!empty(&s1)&&read(&s1)=='(') //每次匹配成功,删除匹配成功的括号        {           pop(&s1);           break; } else return 0;   case '}':        if(!empty(&s1)&&read(&s1)=='}')        {           pop(&s1);break; } else return 0;   case ']':         if(!empty(&s1)&&read(&s1)=='[')         {           pop(&s1);           break;  }}i++;}return (empty(&s1));//当栈中没有元素时括号匹配成功 }int main(){char s[100];int i=0;int res=0;while(scanf("%c",&s[i])&&s[i]!='#')  //从左到右将字符串压入栈{     // getchar();      i++;}res=match_kuohao(s);if(res)   printf("yes\n");else printf("no\n");return 0;}



后缀表达式

#include<stdio.h>#include<stdlib.h>#define maxn 11111double readnum(char f[],int *i){double x=0;int k=0;  //记录小数位数 while(f[*i]>='0'&&f[*i]<='9'){x=x*10+(f[*i]-'0');(*i)++;}if(f[*i]=='.') (*i)++;while(f[*i]>='0'&&f[*i]<='9'){x=x*10+(f[*i]-'0');(*i)++;k++; }while(k){x/=10;k--;} return x;}double evalpost(char f[]){double obst[100];int top=0;int i=0;double x1,x2;while(f[i]!='#'){if(f[i]>='0'&&f[i]<='9')obst[top++]=readnum(f,&i);else if(f[i]==' ') i++;      else if(f[i]=='+')           {              x1=obst[--top];              x2=obst[--top];              obst[top++]=x1+x2;              i++;   }   else        if(f[i]=='-')         {         x1=obst[--top];         x2=obst[--top];         obst[top++]=x2-x1;         i++; }else  if(f[i]=='*') {         x1=obst[--top];         x2=obst[--top];         obst[top++]=x2*x1;         i++; }else  if(f[i]=='/')   {          x1=obst[--top];         x2=obst[--top];         obst[top++]=x2/x1;         i++;   }}return obst[0];}int main(){char s[maxn];int i=0;while(scanf("%c",&s[i])&&s[i]!='#')  i++;printf("%lf",evalpost(s));return 0;} 



中缀表达式

#include<stdio.h>#include<stdlib.h>#include<string.h>#define maxn 11111double readnum(char s[],int *i){    int k=0;    double sum=0;    while(s[*i]>='0'&&s[*i]<='9')    {        sum=sum*10+(s[*i]-'0');        (*i)++;    }    if(s[*i]=='.'){    (*i)++;while(s[*i]>='0'&&s[*i]<='9')    {        sum=sum*10+(s[*i]-'0');        k++;        (*i)++;    }}    while(k)    {        sum/=10;        k--;    }    return sum;}double evalpost(char s[]){    int i=0;    double obst[maxn];    int top=0;    double x,y;    while(s[i]!='#')    {        if(s[i]>='0'&&s[i]<='9')            obst[top++]=readnum(s,&i);        else if(s[i]==' ') i++;             else if(s[i]=='+')             {                 x=obst[--top];                 y=obst[--top];                 obst[top++]=y+x;                 i++;             }else if(s[i]=='-')               {                   x=obst[--top];                   y=obst[--top];                   obst[top++]=y-x;                   i++;               }else if(s[i]=='*')                {                    x=obst[--top];                    y=obst[--top];                    obst[top++]=y*x;                    i++;                }else if(s[i]=='/')                 {                     x=obst[--top];                     y=obst[--top];                     obst[top++]=y/x;                     i++;                 }    }    return obst[0];}int is_operator(char c){    switch(c)    {      case '+':      case '-':      case '*':      case '/':  return 1;      default :  return 0;    }}int priority(char c){    switch(c)    {       case '#':           return -1;       case '(':           return 0;       case '+':       case '-':           return 1;       case '*':       case '/':           return 2;       default :           return -1;    }}void postfix(char s[],char f[]){    int i=0;    int top=0;    int j=0;    char opst[maxn];    opst[top++]='#';    while(s[i]!='#')    {        if(s[i]>='0'&&s[i]<='9'||s[i]=='.') f[j++]=s[i];        else if(s[i]=='(') opst[top++]=s[i];            else if(is_operator(s[i]))            {                 f[j++]=' ';                 while(priority(opst[top-1])>=priority(s[i]))                 {                    f[j++]=opst[--top];                 }                 opst[top++]=s[i];            }else if(s[i]==')')              {                  int t=top-1;                  while(opst[t]!='(')                  {                    f[j++]=opst[top-1];                    top--;  //pop一个值,减一                    t=top-1;//栈顶的下标                  }                  top--; //弹出’)‘              }        i++;    }    while(top) f[j++]=opst[--top];}int main(){    char s[maxn];    char f[maxn];    int i=0;    double sum=0;    while(scanf("%c",&s[i])&&s[i]!='#') i++;    postfix(s,f);    int len =strlen(f);    for(i=0;i<len;i++) printf("%c",f[i]);    sum=evalpost(f);    printf("%lf\n",sum);    return 0;}


0 0
原创粉丝点击