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

1、题目:

2、代码:

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
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<length; i++)
	{
		if(str[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<length; i++)
	{
		if(str[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 <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <queue>
#include <vector>
using namespace std;
char c[1005]; 
__int64 ma,mi,sum;
stack<int>p1;
stack<char>p2;
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;
}
posted @ 2016-09-27 21:46  laixl  阅读(343)  评论(0编辑  收藏  举报