表示式求解(非一位數)

酷行僧發表於2019-05-14
#include < stdio.h>
#include < stdlib.h>
#include < string.h>

typedef struct dd{
int i;
struct dd * next;
}woca;

typedef struct aa{
char i;
struct aa* next;   
}dota;


void create(woca**fist)
{
    *fist = (woca*)malloc(sizeof(woca));
    (*fist)->i = -1;
    (*fist)->next = NULL;  
}


void create_1(dota**fist)
{
    *fist = (dota*)malloc(sizeof(dota));
    (*fist)->i = `#`;
    (*fist)->next = NULL;
}


void push(woca*fist,int i)
{
    woca*p = (woca*)malloc(sizeof(woca));
    if(!p)printf("error");
    p->i = i;

    if(fist->next == NULL)
    {
        fist->next = p;
        p->next=NULL;

    }
    else{
        p->next = fist->next;
        fist->next = p;
        }
}




void push_zifu(dota*fist,char i)
{
    dota*p = (dota*)malloc(sizeof(dota));
    if(!p)printf("error");
    p->i = i;
    if(fist->next == NULL)
    {
        fist->next = p;
        p->next=NULL;

    }
    else{
        p->next = fist->next;
        fist->next = p;    
        }
}


void look(dota*fist,char*i)
{
    if(fist->next == NULL)
        *i = `#`;
    else *i = fist->next->i;
}


int opera(int i,char c,int j)
{
    if(c==`+`)
        return i+j;
    if(c==`-`)
        return i-j;
    if(c==`*`)
        return i*j;
    if(c==`/`)
        return i/j;
return 0;
}

int bijiao(char i,char j)
{   
    if((i==`*`||i == `/`)&&(j==`-`||j==`+`||j ==`#`))return 1;
    if(i ==`(`)return 1;
    if((i == `+`||i == `-`)&&j == `#`)return 1;
    if((i==`+`||i==`-`)&&(j==`+`||j == `-`))return -1;
    if((i==`*`||i==`/`)&&(j==`*`||j == `/`))return -1;
    if((i==`+`||i==`-`)&&(j==`*`||j == `/`))return -1;
    if(j == `(`&&(i ==`+`||i == `-`||i == `/`||i ==`*`))return 1;
    if(i == `)`&&j==`(`)return 0;
    if(i == `)`&&j!=`(`)return -1;
    else return 2;
 }


void pop_1(dota*fist,char *i)
{
    if(fist->next == NULL)
        *i = `#`;
    else
    {dota* p = fist->next;
        *i= fist->next->i;
        fist->next=p->next;
        free(p);}
}

void suan(char a[],dota*fist,woca*hehe)
{
    int i = 0;
    int shu[100];
    int j = 0;
    int sum = 0;

    char i_1;
    char i_2;
    long t =strlen(a);
    while (a[i]!=` `)
    {
        j = 0;
        sum = 0;
        if(a[i]>=`0`&&a[i]<=`9`)
        {
            while(a[i]>=`0`&&a[i]<=`9`)
            {
                shu[j] = a[i]-`0`;
                i++;
                j++;

            }
            for(int jj = 0;jj<j;jj++)
            {
                for(int ww = 0;ww<(j-jj-1);ww++)
                    shu[jj] = shu[jj]*10;


                sum=sum+shu[jj];


            }
             printf("%d
",sum);
            push(hehe, sum);
        }

        else
        {
            look(fist,&i_1);
            /*  if(a[i]==`)`&&i_1 == `(`)
             {
             pop_1(fist, &i_2);

             }
             */
            if(bijiao(a[i],i_1) == 1)
            {

                push_zifu(fist, a[i]);
                putchar(fist->next->i);
            }
            if(a[i] == `)`)
            {
                //pop_1(fist, &i_2);

                while(i_2 !=`(`)
                {pop_1(fist, &i_2);
                    if(i_2!=`(`)
                    {woca*q;
                        woca*qq;
                        q = hehe->next;
                        qq = q->next;
                        hehe->next->i = opera(hehe->next->next->i, i_2, hehe->next->i);
                        hehe->next->next = qq->next;
                        //printf("1   %d     %d
",hehe->next->i,qq->i);
                        free(qq);
                        //  printf("1   %d     %d
",hehe->next->i,hehe->next->next->i);


                    }
                }

            }

            if(bijiao(a[i], i_1)==-1&&a[i]!=`)`)
            {

                woca*q;
                woca*qq;
                pop_1(fist, &i_2);
                q = hehe->next;
                qq = q->next;
                hehe->next->i = opera(hehe->next->next->i, i_2,hehe->next->i );
                hehe->next->next = qq->next;
                printf("1   %d     %d
",hehe->next->i,qq->i);

                free(qq);//printf("1   %d     %d
",hehe->next->i,hehe->next->next->i);

                push_zifu(fist, a[i]);
                  putchar(fist->next->i);
}
        i++;
        }
        if(i == t&&hehe->next->next!=NULL)
        {
            woca*j = hehe->next;
            woca*p;
            for(;;)
            {pop_1(fist, &i_2);
                //if(i_2 == `(`)pop_1(fist, &i_2);
                if(i_2 == `#`)break;
                p=j->next;
                hehe->next->i = opera(hehe->next->next->i, i_2, hehe->next->i);
                hehe->next->next= p->next;
                if(j->next!=NULL) printf("2    %d     %d
",j->i,j->next->i);
                //printf("2    %d     %d
",j->i,j->next->i);
                free(p);

            }
         }     
    }   
}


int main(int argc, const char * argv[])
{

    // insert code here...

    char a[100];
    woca*fist;
    dota*hehe;
    create(&fist);
    create_1(&hehe);
    scanf("%s",a);
    suan(a,hehe,fist);
    printf("%d
",fist->next->i); 
    return 0;
}

相關文章