算法与数据结构实验题 3.3 最大最小

1、题目:

算法与数据结构实验题 3.3 最大最小_第1张图片

2、代码:

#include
#include
#include
typedef struct astack *Stack;
typedef struct astack
{
    __int64 top;
    __int64 *data;

} Astack;
typedef struct stack *Operator;
typedef struct stack
{
    __int64 topOperator;
    char *ch;
} sta;

void PushNumber(Stack S,__int64 x)
{
    S->data[++S->top]=x%870764322;
}

void PushOperator(Operator o,char c)
{
    o->ch[++o->topOperator]=c;
}
int StackEmpty(Stack S)
{
    return S->top<0;
}

int OperatorEmpty(Operator o)
{
    return o->topOperator<0;
}

char PopOperator(Operator o)
{
    return o->ch[o->topOperator--];
}

int PopNumber(Stack S)
{
    return S->data[S->top--]%870764322;
}
int main()
{
    char str[201];
    gets(str);
    int length=strlen(str);

    Stack S=(Stack)malloc(sizeof*S);
    S->data=(__int64*)malloc(length*sizeof(__int64));
    S->top=-1;

    Operator o=(Operator)malloc(sizeof*o);
    o->ch=(char*)malloc(length*sizeof(char));
    o->topOperator=-1;

    int  i;
    __int64 a1,a2,sum,max,min;
//计算最小值,先算乘法
    while (!StackEmpty(S)) PopNumber(S);
    while (!OperatorEmpty(o)) PopOperator(o);

    for(i=0; i='0'&&str[i]<='9')
        {
            PushNumber(S,str[i]-48);
        }
        else
        {
            if(OperatorEmpty(o))
            {
                PushOperator(o,str[i]);
            }
            else
            {
                while(str[i] == '+' && o->ch[o->topOperator]== '*')
                {
                    a1=(__int64)S->data[S->top]%870764322;
                    PopNumber(S);
                    a2=(__int64)S->data[S->top]%870764322;
                    PopNumber(S);
                    sum=(a1*a2)%870764322;
                    PushNumber(S,sum);
                    PopOperator(o);
                    if(OperatorEmpty(o))
                    {
                        break;
                    }
                }
                PushOperator(o,str[i]);
            }
        }
    }

    while(!OperatorEmpty(o))
    {
        a1=(__int64)S->data[S->top]%870764322;
        PopNumber(S);
        a2=(__int64)S->data[S->top]%870764322;
        PopNumber(S);
        if(o->ch[o->topOperator]=='+')
        {
            sum=(a1+a2)%870764322;
        }
        else
        {
            sum=(a1*a2)%870764322;
        }

        PushNumber(S,sum);
        PopOperator(o);
    }
    min=S->data[S->top];
//计算最大值,先做加法运算
//存数字和符号的栈清空,重新存
    while (!StackEmpty(S)) PopNumber(S);
    while (!OperatorEmpty(o)) PopOperator(o);

    for(i=0; i='0'&&str[i]<='9')
        {
            PushNumber(S,str[i]-48);
        }
        else
        {
            if(OperatorEmpty(o))
            {
                PushOperator(o,str[i]);
            }
            else
            {
                while(str[i] == '*' && o->ch[o->topOperator]== '+')
                {
                    a1=(__int64)S->data[S->top]%870764322;
                    PopNumber(S);
                    a2=(__int64)S->data[S->top]%870764322;
                    PopNumber(S);
                    sum=(a1+a2)%870764322;
                    PushNumber(S,sum);
                    PopOperator(o);
                    if(OperatorEmpty(o))
                    {
                        break;
                    }
                }
                PushOperator(o,str[i]);
            }
        }
    }

    while(!OperatorEmpty(o))
    {
        a1=(__int64)S->data[S->top]%870764322;
        PopNumber(S);
        a2=(__int64)S->data[S->top]%870764322;
        PopNumber(S);
        if(o->ch[o->topOperator]=='*')
        {
            sum=(a1*a2)%870764322;
        }
        else
        {
            sum=(a1+a2)%870764322;
        }

        PushNumber(S,sum);
        PopOperator(o);
    }
    max=S->data[S->top];
    printf("%d\n",max);
    printf("%d\n",min);
    return 0;
}

3、大神代码:

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;
char c[1005]; 
__int64 ma,mi,sum;
stackp1;
stackp2;
int main()
{
    scanf("%s",c);
    int len = strlen(c);
    int i;
    sum = 0;
    __int64 a1,a2;
    while (!p1.empty()) p1.pop();
    while (!p2.empty()) p2.pop();
    for (i = 0; i < len; i++)
    {
        if (c[i] >= '0' && c[i] <= '9')
        {
            p1.push(c[i]-'0');
        }
        else
        {
            if (p2.empty()) p2.push(c[i]);
            else
            {
                while (c[i] == '+' && p2.top() == '*')
                {
                    a1 = (__int64)p1.top(); p1.pop();
                    a2 = (__int64)p1.top(); p1.pop();
                    sum = (a1*a2)%870764322;
                    p1.push(sum);
                    p2.pop();
                    if (p2.empty()) break;
                }
                p2.push(c[i]);
            }
        }
    }
    while (!p2.empty())
    {
        a1 = (__int64)p1.top(); p1.pop();
        a2 = (__int64)p1.top(); p1.pop();
        if (p2.top() == '+')sum = (a1+a2)%870764322;
        else sum = (a1*a2)%870764322;
        p1.push(sum);       
        p2.pop();
    }
    mi = p1.top();
    
    while (!p1.empty()) p1.pop();
    while (!p2.empty()) p2.pop();
    for (i = 0; i < len; i++)
    {
        if (c[i] >= '0' && c[i] <= '9')
        {
            p1.push(c[i]-'0');
        }
        else
        {
            if (p2.empty()) p2.push(c[i]);
            else
            {
                while (c[i] == '*' && p2.top() == '+')
                {
                    a1 = (__int64)p1.top(); p1.pop();
                    a2 = (__int64)p1.top(); p1.pop();
                    sum = (a1+a2)%870764322;
                    p1.push(sum);
                    p2.pop();
                    if (p2.empty()) break;
                }
                p2.push(c[i]);
            }
        }
    }
    while (!p2.empty())
    {
        a1 = (__int64)p1.top(); p1.pop();
        a2 = (__int64)p1.top(); p1.pop();
        if (p2.top() == '+')sum = (a1+a2)%870764322;
        else sum = (a1*a2)%870764322;
        p1.push(sum);   
        p2.pop();   
    }
    ma = p1.top();
    printf("%I64d\n",ma);
    printf("%I64d\n",mi);
    return 0;
}

转载于:https://www.cnblogs.com/laixiaolian/p/5914425.html

你可能感兴趣的:(算法与数据结构实验题 3.3 最大最小)