C語言 實現中綴表示式轉字尾表示式並求值
阿新 • • 發佈:2019-01-08
大一菜鳥,初學程式設計,這是我的第一篇部落格,希望能用部落格記錄我的成長之路。
初學資料結構,剛接觸連結串列和棧,看到有中綴表示式轉字尾的題就試著實現了一下。下面貼上程式碼。因為使用的是字元型變數,所以只能計算個位數。
/* 堆疊練習——中綴表示式轉字尾表示式 */
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<stdbool.h>
#define MAX 25
typedef struct node{
char date;
struct node *next;
} LinkStake;
typedef struct node2{
float date;
struct node *next;
} Stake;
typedef struct body{
char date[MAX];
int j;
} Body;
LinkStake *CreatStake();//建立空棧
int IsEmpty(LinkStake *s);//判斷空棧
void Push(char fuhao, LinkStake *s, Body *p);//壓棧
int Judge(bool flag, char c);//判斷優先順序
LinkStake *CreatStake()
{
LinkStake *s;
s=malloc (sizeof(struct node));
s->next = NULL;
return s;
}
int IsEmpty(LinkStake *s)
{
return(s->next == NULL);
}
void Push(char fuhao, LinkStake *s, Body *p)
{
while(s->next != NULL && Judge(false,fuhao)<Judge(true,s->next->date)){
LinkStake *t;
printf ("%c", s->next->date);
p->date[p->j] = s->next->date;
p->j++;
t = s->next;
s->next = t->next;
free(t);
}
if(IsEmpty(s)){
LinkStake *tmp;
tmp = malloc(sizeof(struct node));
tmp->date = fuhao;
tmp->next = s->next;
s->next = tmp;
}
else if(Judge(false,fuhao)>Judge(true,s->next->date)){
LinkStake *tmp;
tmp = malloc(sizeof(struct node));
tmp->date = fuhao;
tmp->next = s->next;
s->next = tmp;
}
else if(Judge(false,fuhao) == Judge(true,s->next->date)){
LinkStake *t;
t = s->next;
s->next = t->next;
free(t);
}
}
int Judge(bool flag, char c)
{
if(c == '+' || c == '-')
if(flag) return 3;
else return 2;
else if(c == '*' || c == '/')
if(flag) return 5;
else return 4;
else if(c == '(')
if(flag) return 1;
else return 6;
else if(c == ')')
if(flag) return 6;
else return 1;
else return 0;
}
void PushStake(float c, LinkStake* s)
{
Stake *tmp;
tmp = malloc(sizeof(struct node2));
tmp->date = c;
tmp->next = s->next;
s->next = tmp;
}
float PopStake(LinkStake *s)
{
Stake *t;
float c;
t = s->next;
c = t->date;
s->next = t->next;
free(t);
return c;
}
int main()
{
int i = 0;
LinkStake *head;
Body *p = malloc(sizeof(struct body));
char s[MAX];
head = CreatStake();
p->j = 0;
printf("請輸入中綴表示式;\n");
gets(s);
printf("轉換為字尾表示式: \n");
while(s[i]){
if(s[i]>='0' && s[i]<='9'){
printf("%c", s[i]);
p->date[p->j] = s[i];
p->j++;
}
else Push(s[i],head,p);
i++;
}
while(head->next != NULL){
printf("%c", head->next->date);
p->date[p->j++] = head->next->date;
p->j++;
LinkStake *t;
t = head->next;
head->next = t->next;
free(t);
}
printf("\n");
printf("計算結果為:\n");
Stake *stake;
float sum = 0.0, a, b;
stake = (Stake*)malloc(sizeof(struct node2));
stake->next = NULL;
for(int i = 0; i < p->j; i++){
if(p->date[i]>='0' && p->date[i]<='9')
PushStake((float)(p->date[i]-'0'), stake);
else if(p->date[i]=='+'){
b = PopStake(stake);
a = PopStake(stake);
sum = a+b;
PushStake(sum, stake);
}
else if(p->date[i]=='-'){
b = PopStake(stake);
a = PopStake(stake);
sum = a-b;
PushStake(sum, stake);
}
else if(p->date[i]=='*'){
b = PopStake(stake);
a = PopStake(stake);
sum = a*b;
PushStake(sum, stake);
}
else if(p->date[i]=='/'){
b = PopStake(stake);
a = PopStake(stake);
sum = a/b;
PushStake(sum, stake);
}
}
printf("%.2f", sum);
return 0;
}