您的位置:首页 > 其它

中缀转后缀&后缀求值

2013-03-08 22:02 204 查看
/*
*			Infix to Postfix, and get the value for Postfix.
*			Date: 2013/3/8
*													NolanJian
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct stacknode;
typedef struct stacknode StackNode;
typedef StackNode *StackPointer;

int ValueOfPostfix(char *postfix);
void InfixToPostfix(char *infix, char *postfix);
char GetTop(StackPointer top);
void Push(StackPointer *top, int ch);
void Pop(StackPointer *top, char *postfix, int *i, int ch);
void StringToInt(char *postfix);
struct stacknode {
int Num;
StackPointer Next;
};

int main() {
char infix[100], postfix[100];
while(scanf("%s", infix)) {
memset(postfix, '\0', 100);
InfixToPostfix(infix, postfix);
printf("Infix:     %s\n", infix);
printf("Postfix:   %s\n", postfix);
StringToInt(postfix);
printf("Result:    %d\n", ValueOfPostfix(postfix));
}
return 0;
}

void StringToInt(char *postfix) {
while(*postfix) {
if(*postfix >= '0' && *postfix <= '9')
*postfix -= '0';
postfix++;
}
}

void Push(StackPointer *top, int ch) {
StackPointer tmp;
if(*top == NULL) {
tmp = (StackPointer)malloc(sizeof(StackNode));
tmp->Next = NULL;
tmp->Num = ch;
(*top) = tmp;
return ;
}
tmp = (StackPointer)malloc(sizeof(StackNode));
tmp->Next = (*top);
tmp->Num = ch;
(*top) = tmp;
}

void Pop(StackPointer *top, char *postfix, int *i, int ch) {
StackPointer tmp;
if(ch == '+' || ch == '-' || ch == '*' || ch == '/') {
postfix[(*i)++] = (*top)->Num;
tmp = *top;
*top = (*top)->Next;
free(tmp);
return ;
}
if(ch == ')') {
while((*top)->Num != '(') {
postfix[(*i)++] = (*top)->Num;
tmp = *top;
*top = (*top)->Next;
free(tmp);
}
tmp = *top;
*top = (*top)->Next;
free(tmp);
return ;
}
if(ch == 3) {
postfix[(*i)++] = (*top)->Num;
tmp = *top;
*top = (*top)->Next;
free(tmp);
return ;
}
tmp = *top;
*top = (*top)->Next;
free(tmp);
}

void InfixToPostfix(char *infix, char *postfix) {
StackPointer top = NULL;
int i = 0, flag;
char ch;
while(*infix) {
if(*infix >= '0' && *infix <= '9')
postfix[i++] = *infix;
else if(*infix == ')')
Pop(&top, postfix, &i, *infix);
else if(*infix == '*' || *infix == '/' || *infix == '(')
Push(&top, *infix);
else if(*infix == '+' || *infix == '-') {
ch = 0;
if(!top) {
Push(&top, *infix);
infix++;
continue;
}
while(top){
flag = 0;
ch = GetTop(top);
if(ch == '(') {
Push(&top, *infix);
flag = 1;
break;
}
else
Pop(&top, postfix, &i, *infix);
}
if(!flag)
Push(&top, *infix);
}
infix++;
}
while(top)
Pop(&top, postfix, &i, 3);
postfix[i] = '\0';
}

char GetTop(StackPointer top) {
return top->Num;
}

int ValueOfPostfix(char *postfix) {
StackPointer top = NULL;
int a, b;
while(*postfix) {
if(*postfix >= 0 && *postfix <= 9) {
Push(&top, *postfix);
postfix++;
continue;
}
if(*postfix == '+') {
b = GetTop(top);
Pop(&top, 0, 0, 0);
a = GetTop(top);
Pop(&top, 0, 0, 0);
Push(&top, a + b);
postfix++;
continue;
}
if(*postfix == '-') {
b = GetTop(top);
Pop(&top, 0, 0, 0);
a = GetTop(top);
Pop(&top, 0, 0, 0);
Push(&top, a - b);
postfix++;
continue;
}
if(*postfix == '*') {
b = GetTop(top);
Pop(&top, 0, 0, 0);
a = GetTop(top);
Pop(&top, 0, 0, 0);
Push(&top, a * b);
postfix++;
continue;
}
if(*postfix == '/') {
b = GetTop(top);
Pop(&top, 0, 0, 0);
a = GetTop(top);
Pop(&top, 0, 0, 0);
Push(&top, a / b);
postfix++;
continue;
}
}
return top->Num;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: