这篇看学长的思路用C写了一遍
目录
结构定义
typedef struct Stack{
int numdata[MAXSIZE];
char chdata[MAXSIZE];
int numtop;
int chtop;
}*StackPtr;
初始化
StackPtr iniStack(){
StackPtr p = (StackPtr)malloc(sizeof(struct Stack));
p->chtop = -1;
p->numtop = -1;
return p;
}
压栈
void numpush(StackPtr pStack,int pnum){
if(pStack->numtop >= MAXSIZE-1){
printf("Cannot push element: stack full.\n");
return;
}
pStack->numtop++;
pStack->numdata[pStack->numtop] = pnum;
}
void chpush(StackPtr pStack,char pch){
if(pStack->chtop >= MAXSIZE-1){
printf("Cannot push element: stack full.\n");
return;
}
pStack->chtop++;
pStack->chdata[pStack->chtop] = pch;
}
弹栈
int numpop(StackPtr pStack){
if(pStack->numtop < 0){
printf("Cannot pop element: stack empty.\r\n");
return 0;
}
pStack->numtop--;
return pStack->numdata[pStack->numtop+1];
}
char chpop(StackPtr pStack){
if(pStack->chtop < 0){
printf("Cannot pop element: stack empty1.\r\n");
return '\0';
}
pStack->chtop--;
return pStack->chdata[pStack->chtop+1];
}
优先级判断
int judgePriority(char pch){
if(pch == '+' || pch == '-'){
return 1;
}else if(pch == '*' || pch == '/'){
return 2;
}
}
计算
void eval(StackPtr pStack){
int b = numpop(pStack);
int a = numpop(pStack);
char c = chpop(pStack);
int x;
if(c == '+'){
x = a + b;
}else if((c == '-')){
x = a - b;
}else if (c == '*'){
x = a * b;
}else{
x = a / b;
}
numpush(pStack,x);
}
主函数
int main(){
StackPtr tempStack = iniStack();
char a[MAXSIZE];
gets(a);
int size = strlen(a);
int i;
for(i = 0;i < size;i++){
char c = a[i];
if(isdigit(c)){
int x = 0,j = i;
while(j < size && isdigit(a[j])){
x = x * 10 + a[j++] - '0';
}
i = j - 1;
numpush(tempStack,x);
}else if(c == '('){
chpush(tempStack,c);
}else if(c == ')'){
while (tempStack->chdata[tempStack->chtop] != '('){
eval(tempStack);
}
chpop(tempStack);
}else{
while( tempStack->chtop != -1 && tempStack->chdata[tempStack->chtop] != '(' && judgePriority(tempStack->chdata[tempStack->chtop]) >= judgePriority(c)){
eval(tempStack);
}
chpush(tempStack,c);
}
}
while(tempStack->chtop != -1){
eval(tempStack);
}
printf("%d",tempStack->numdata[tempStack->numtop]);
return 0;
}
完整代码
#include<stdio.h>
#include<malloc.h>
#include<string.h>
#define MAXSIZE 100
typedef struct Stack{
int numdata[MAXSIZE];
char chdata[MAXSIZE];
int numtop;
int chtop;
}*StackPtr;
StackPtr iniStack(){
StackPtr p = (StackPtr)malloc(sizeof(struct Stack));
p->chtop = -1;
p->numtop = -1;
return p;
}
void numpush(StackPtr pStack,int pnum){
if(pStack->numtop >= MAXSIZE-1){
printf("Cannot push element: stack full.\n");
return;
}
pStack->numtop++;
pStack->numdata[pStack->numtop] = pnum;
}
void chpush(StackPtr pStack,char pch){
if(pStack->chtop >= MAXSIZE-1){
printf("Cannot push element: stack full.\n");
return;
}
pStack->chtop++;
pStack->chdata[pStack->chtop] = pch;
}
int numpop(StackPtr pStack){
if(pStack->numtop < 0){
printf("Cannot pop element: stack empty.\r\n");
return 0;
}
pStack->numtop--;
return pStack->numdata[pStack->numtop+1];
}
char chpop(StackPtr pStack){
if(pStack->chtop < 0){
printf("Cannot pop element: stack empty1.\r\n");
return '\0';
}
pStack->chtop--;
return pStack->chdata[pStack->chtop+1];
}
int judgePriority(char pch){
if(pch == '+' || pch == '-'){
return 1;
}else if(pch == '*' || pch == '/'){
return 2;
}
}
void eval(StackPtr pStack){
int b = numpop(pStack);
int a = numpop(pStack);
char c = chpop(pStack);
int x;
if(c == '+'){
x = a + b;
}else if((c == '-')){
x = a - b;
}else if (c == '*'){
x = a * b;
}else{
x = a / b;
}
numpush(pStack,x);
}
int main(){
StackPtr tempStack = iniStack();
char a[MAXSIZE];
gets(a);
int size = strlen(a);
int i;
for(i = 0;i < size;i++){
char c = a[i];
if(isdigit(c)){
int x = 0,j = i;
while(j < size && isdigit(a[j])){
x = x * 10 + a[j++] - '0';
}
i = j - 1;
numpush(tempStack,x);
}else if(c == '('){
chpush(tempStack,c);
}else if(c == ')'){
while (tempStack->chdata[tempStack->chtop] != '('){
eval(tempStack);
}
chpop(tempStack);
}else{
while( tempStack->chtop != -1 && tempStack->chdata[tempStack->chtop] != '(' && judgePriority(tempStack->chdata[tempStack->chtop]) >= judgePriority(c)){
eval(tempStack);
}
chpush(tempStack,c);
}
}
while(tempStack->chtop != -1){
eval(tempStack);
}
printf("%d",tempStack->numdata[tempStack->numtop]);
return 0;
}
运行结果示例
(1+2*6)-12+9/3
4
总结:
一味地照着修改让我走了一些弯路,主要是在运算是否完成的判断上。另外,dev c++居然会有逐步debug能出正确答案而直接编译运行不能出正确答案的情况,令我百思不得其解。