⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 shiyan3.c

📁 后缀表达式计算 可以计算多位 可带括号
💻 C
字号:
#include<stdio.h>
#include<String.h>
#include<malloc.h>
#include<stdlib.h>
#define M 100
typedef struct{
   int top;
   int elem[M];
}sqstack;
sqstack *s;
initstack(sqstack *s){
    (*s).top=-1;
}
int pop(sqstack *s){
   int x;
   if((*s).top==-1)
   return 0;
   else
   {x=(*s).elem[(*s).top];(*s).top--;return(x);}
}
int push(sqstack *s,int x){
   if((*s).top==M-1)
   return 0;
   else
   {(*s).top++;(*s).elem[(*s).top]=x;return 1;}
}
void suffix_value(char a[M]){
  int i=0;
  int w;
  initstack(&s);
  while(a[i]!='#'){
     switch(a[i]){
        case '0':push(&s,0);break;
        case '1':push(&s,1);break;
        case '2':push(&s,2);break;
        case '3':push(&s,3);break;
        case '4':push(&s,4);break;
        case '5':push(&s,5);break;
        case '6':push(&s,6);break;
        case '7':push(&s,7);break;
        case '8':push(&s,8);break;
        case '9':push(&s,9);break;
        case '+':w=pop(&s);push(&s,pop(&s)+w);break;
        case '-':w=pop(&s);push(&s,pop(&s)-w);break;
        case '*':w=pop(&s);push(&s,pop(&s)*w);break;
        case '/':w=pop(&s);push(&s,pop(&s)/w);break;
     }
     i++;
  }
   printf("%d",pop(&s));
}
main()
{ char a[M];
  int i=0;
  printf("Please input the oper:\n");
  scanf("%c",&(a[i]));
  while(a[i]!='#')
  a[++i]=getchar();
  suffix_value(a);
  getch();
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -