📄 post_no_bintree.c
字号:
#include <stdio.h>
#include <io.h>
#define LEN 255
float stack_num[LEN]; //操作数
char stack_op[LEN]; //操作符
int top_num, top_op;
char temp[2];
char poststr[LEN];
int postp;
init_stack()//初始化
{
top_num=0;
top_op=0;
}
int push_num(float num)//入
{
if (top_num==LEN) return -1; //满
stack_num[top_num]=num;
top_num++;
return 0;
}
int push_op(char op)//入
{
if (top_op==LEN) return -1; //满
stack_op[top_op]=op;
top_op++;
return 0;
}
float pop_num() //弹
{
--top_num;
return stack_num[top_num];
}
char pop_op() //弹
{
--top_op;
return stack_op[top_op];
}
int empty_num()//空?
{
if (top_num==0) return 1; //空
return 0;
}
int empty_op()//空?
{
if (top_op==0) return 1; //空
return 0;
}
int getnum(char ch)
{
temp[0]=ch;
temp[1]=0;
return atoi(temp);
}
int comp(f_num)//比较优先级
{ char temp;
if (empty_op()) return -1; //空
temp=pop_op(); push_op(temp);
switch (temp)
{ case '+':
case '-':{if (f_num>2) return 1;break;}
case '*':
case '/':{if (f_num>3) return 1;break;}
case '(':{if (f_num>=1) return 1;break;}
case '#':{return 1;}
default:return -1;
}
return 0;
}
int compu() //计算
{ char op1;
float num1,num2;
if (empty_num()) {printf(" \nERROR INPUT");exit(0);} num1=pop_num();
if (empty_num()) {printf(" \nERROR INPUT");exit(0);} num2=pop_num();
if (empty_op()) {printf(" \nERROR INPUT");exit(0);} op1=pop_op(); sprintf(postp,"%c",op1);postp++;
switch (op1)
{ case '+':push_num(num1+num2);break;
case '-':push_num(num2-num1);break;
case '*':push_num(num1*num2);break;
case '/':push_num(num2/num1);break;
default:printf(" \nERROR INPUT");exit(0);
}
return 0;
}
main(int argc,char * argv[])
{ char ch;
int flag;
int pro;
init_stack(); push_op('#');flag=1;memset(poststr,0,LEN);postp=poststr;
if (argc!=2) {printf("请在命令行写出表达式!\n"); exit(0);}
while ( (ch=argv[1][0]) != 0 )
{
switch (ch)
{case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
case '0':{sprintf(postp,"%c",ch);postp++;push_num(getnum(ch));break;}
case '+':
case '-':{while ((pro=comp(2))==0)
if (compu()==-1)
{printf(" \nERROR INPUT");exit(0);}
if (pro==-1){printf(" \nERROR INPUT");exit(0);}
push_op(ch);
break;
}
case '*':
case '/':{while ((pro=comp(3))==0)
if (compu()==-1){printf(" \nERROR INPUT");exit(0);}
if (pro==-1){printf(" \nERROR INPUT");exit(0);}
push_op(ch);
break;
}
case '(':{push_op(ch);break;}
case ')':{while (1)
{ if (empty_op()) {printf(" \nERROR INPUT");exit(0);} ch=pop_op();
if (ch=='(') break;
push_op(ch);if (compu()==-1){printf(" \nERROR INPUT");exit(0);}
}
break;}
case '#':{while (1)
{ if (empty_op()) {printf(" \nERROR INPUT");exit(0);} ch=pop_op();
if (ch=='#') break;
push_op(ch);if (compu()==-1){printf(" \nERROR INPUT");exit(0);}
}
break;}
default :printf(" \nERROR INPUT");exit(0);
}
argv[1]++;
if ((argv[1][0]==0)&&(flag)) {flag=0;argv[1]--;argv[1][0]='#';}
}//while
if ((top_num!=1)||(top_op!=0)) {printf(" \nERROR INPUT");exit(0);}
printf(" 后序表达式为:\n %s",poststr);
printf("\n 结果为:\n %f\n",pop_num());
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -