Loading

题解 | Wendy的程序(栈)

原题来自 BUAA数据结构期末复习各路模拟题

Show Code
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

struct array{
    int *a;
    int cnt;
};

struct array calculate(char str[]);
int getNum(char str[],int pos,int *leap);
void shuffle(int *mylist,int len);
int cmp(const void *e1,const void *e2)
{
    int a1=*(int *)e1;
    int a2=*(int *)e2;
    if(a1<a2)
        return -1;
    if(a1>a2)
        return 1;
    return 0;
}

struct array nStack[205];
int ntop=-1;
int opstack[205];   //操作栈,0——merge,1——sort,2——shuffle
int otop=-1;

int main(){

    char str1[205],str2[205];
    struct array res1,res2;

    scanf("%s",str1);
    res1=calculate(str1);

    ntop=-1;
    otop=-1;

    scanf("%s",str2);
    res2=calculate(str2);

    if(res1.cnt!=res2.cnt)
    {
        puts("Failed");
    }
    else 
    {
        int t,flag=1;
        for(t=0;t<res1.cnt;t++)
        {
            if(res1.a[t]!=res2.a[t])
            {
                flag=0;
                break;
            }
        }
        if(flag==1)
            puts("TobyWendy");
        else
            puts("Failed");
    }
    return 0;
}

struct array calculate(char str[])
{
    int i=0,j,k,w,l;
    int temp[205];
    while(str[i]!='\0')
    {
        if(isalpha(str[i]))
        {
            if(str[i]=='s' && str[i+1]=='h')
            {
                opstack[++otop]=2;
                i+=8;
            }
            else if(str[i]=='s')
            {
                opstack[++otop]=1;
                i+=5;
            }
            else
            {
                opstack[++otop]=0;
                i+=6;
            }
        }
        else if(str[i]=='[')
        {
            j=0;
            while(str[i]!=']')     
            {
                i++;
                temp[j++]=getNum(str,i,&w);
                i+=w;
            }
            struct array p;
            p.a=(int *)malloc(sizeof(int)*j);
            for(k=0;k<j;k++)
            {
                p.a[k]=temp[k];
            }
            p.cnt=j;
            nStack[++ntop]=p;
            i++;
        }
        else if(str[i]==',')
        {
            i++;
        }
        else
        {
            if(opstack[otop]==0)
            {
                struct array p,q,r;
                p=nStack[ntop--];
                q=nStack[ntop--];
                r.a=(int *)malloc(sizeof(int)*(p.cnt+q.cnt));
                for(j=0;j<q.cnt;j++)
                {
                    r.a[j]=q.a[j];
                }
                for(j=0;j<p.cnt;j++)
                {
                    r.a[q.cnt+j]=p.a[j];
                }
                r.cnt=p.cnt+q.cnt;
                free(p.a);
                free(q.a);
                nStack[++ntop]=r;
            }
            else if(opstack[otop]==1)
            {
                qsort(nStack[ntop].a,nStack[ntop].cnt,sizeof(int),cmp);
            }
            else
            {
                shuffle(nStack[ntop].a,nStack[ntop].cnt);
            }
            otop--;
            i++;
        }
    }
    //debug
    /* 
    for(i=0;i<nStack[0].cnt;i++)
    {
        printf("%d ",nStack[0].a[i]);
    }
    */
    return nStack[0];
}
int getNum(char str[],int pos,int *leap)
{
    int l=0,sign=1,ans=0;
    if(str[pos]=='-')
    {
        sign=-1;
        l++;
    }
    while(str[pos+l]>='0' && str[pos+l]<='9')
    {
        ans=ans*10+str[pos+l]-'0';
        l++;
    }
    *leap=l;
    return ans*sign;
}
/*mylist是欲打乱的数组的首地址(或数组名),len是数组长度*/
void shuffle(int* mylist, int len)
{
    static unsigned lucky_number = 520;
    while(len > 1)
    {
        int temp = *mylist;
        *mylist = *(mylist + lucky_number % len);
        *(mylist + lucky_number % len) = temp;
        mylist++; len--;
        lucky_number *= 113344;
        lucky_number += 993311;
    }
}
posted @ 2024-06-22 19:55  Chase_Tsai  阅读(9)  评论(0)    收藏  举报