C言語は接尾辞式を実現して接尾辞式を転換してそして値を求める


初心者が書いたコードなので、構造が混乱して再構築されたコードはここで参考にします.http://blog.csdn.net/wait_nothing_alone/article/details/70184819
    ,    ,         ,              。
      ,       ,                     。      。           ,         。
/*     ——            */
#include
#include
#include
#include
#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)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("        ;
"
); gets(s); printf(" :
"
); 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("
"
); printf(" :
"
); 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; }