用栈的方法运算后缀表达式的值

自己写代码的能力比较差,这段代码写了好长时间。

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#define EmptyTOS (-1)
#define MinStackSize (5)
struct StackRecord;
typedef struct StackRecord *Stack;
struct StackRecord
{
	int Capacity;
	int TopOfStack;
	char *Array;
};

/*
 * 创建一个stack
 */
Stack CreateStack(int MaxElements)
{
	Stack S;
	
	if (MaxElements < MinStackSize)
	{
		printf("Stack size is too small.\n");
	}

	S = (Stack)malloc(sizeof(struct StackRecord));
	if (S == NULL)
	{
		printf("out of space!!!\n");
	}

	S->Array = (char *)malloc(sizeof(char)*MaxElements);
	if (S->Array == NULL)
	{
		printf("out of space !!!\n");
	}

	S->TopOfStack = -1;
	S->Capacity = MaxElements;
//	printf("TopOfStack`s value = %d, S->Capacity = %d\n", S->TopOfStack, S->Capacity);
	return S;
}

/*
 * 释放栈
 */
void DisposeStack(Stack S)
{
	if (S != NULL)
	{
		free(S->Array);
		free(S);
	}
}
/*
 * 栈是否为空
 */
int IsEmpty(Stack S)
{
	return S->TopOfStack == EmptyTOS;
}

/*
 * 清空栈。
 */
void MakeEmpty(Stack S)
{
	S->TopOfStack = EmptyTOS;
}
/*
 * 判断栈是否满
 */
int IsFull(Stack S)
{
	if (S->TopOfStack >= S->Capacity)
	{
		return -1;
	}
	else
		return 0;
		
}

/*
 * push data.
 */
void Push(char X, Stack S)
{
	if (IsFull(S))
		printf("Full Stack\n");
	else
	{
		S->TopOfStack++;
		S->Array[S->TopOfStack] = X;
	}
}

/*
 * print stack.
 */
 /*
void ShowStack(Stack S)
{
	int i;
	printf("Now we can print the stack.\n");
	for (i = 0; i <= S->TopOfStack; i++)
	{
		if (i < S->TopOfStack)
		{
			printf("%c, ", S->Array[i]);			
		}
		else
		{
			printf("%c \n", S->Array[i]);
		}
	}
}
*/

/*
 * print stack.
 */
void ShowStack(Stack S)
{
	int i;
	printf("Now we can print the stack.\n");
	for (i = 0; i <= S->TopOfStack; i++)
	{
		if (i < S->TopOfStack)
		{
			printf("%d, ", S->Array[i]);			
		}
		else
		{
			printf("%d \n", S->Array[i]);
		}

	}
}
/*
 * 弹出第一个数.
 */
int Top(Stack S)
{
	if (IsEmpty(S))
	{
		return 0;		
	}
	else
	{
		return S->Array[S->TopOfStack];
	}
}
/*
 * pop data.
 */
int Pop(Stack S)
{
	if (IsEmpty(S))
	{
		printf("Empty Stack\n");
		return 0;		
	}
	else
	{
		S->TopOfStack --;		
	}
}

/*
 * top and pop
 */
int TopAndPop(Stack S)
{
	if (IsEmpty(S))
	{
		printf("The stack is empty.\n");
		return 0;		
	}
	else
	{
		return S->Array[S->TopOfStack--];
	}	
}
 
/*
int MidToAf(Stack S)
{
	printf("Please input expression.\n");
	char c;
	int trans;
	while( c != 'q')
	{
		printf("Please input character.\n");
		scanf("%c", c);	
		switch(c)
		{
			case '+':
				trans = 2;
				if (IsEmpty(S))
				{
					Push('+', S);
				}
				else if(trans < )

					
				else if (Pop(S) == '+')
					printf(" + ");
		
			case '*':
				trans = 3;
				if (Pop(S) != '(')
				{
					printf();
				}
			case '(':
				trans = 4;
			case ')':
				trans = 0;
			default;
				trans = 1;
				
		}
	}	
}
*/
// 运算后缀表达式
int CalResult(char Post[], int n, Stack S)
{
//	printf("Please input suffix expression.\n");
	//char c;
	int num =0;
	int buf;
	int i;
	//while(n--)
	for(i = 0; i < n; i ++)
	{
		
		if ( Post[i]>= '0' && Post[i]<= '9')
		{
			//printf("test = %c\n", Post[i]);
			num = Post[i] - '0';
			//printf("num = %d\n", num);
			Push(num, S);
		}
		else if (Post[i] == '+')
		{
			buf = TopAndPop(S) + TopAndPop(S);
		//	printf("buf1 is %d\n", buf);
			if(IsEmpty(S))
			{
				printf("The final result is %d\n", buf);
			}
			else
			{
			//	printf("push1\n");
				Push(buf, S);
			}	
		}
		else if (Post[i] == '*')
		{
			buf = TopAndPop(S) * TopAndPop(S);
			/*
			printf("*************\n");
			ShowStack(S);
			printf("***************\n");
			*/
		//	printf("buf2 is %d\n", buf);
			//ShowStack(S);
			if(IsEmpty(S))
			{
				printf("The final result is %d\n", buf);
			}
			else
			{
			//	printf("push**\n");
				Push(buf, S);
			}				
		}
		
	}
	
	
}


int main(void)
{
	Stack Stack1;
	Stack1 = CreateStack(20);

	char buf;
	int i = 0;
	char Post1[100];
	
	scanf("%c", &buf);
	while (buf != '#')
	{
		Post1[i] = buf;
		scanf("%c", &buf);
		i++;
	}
 	
	CalResult(Post1, strlen(Post1), Stack1);
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

qq_807315755

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值