自己写代码的能力比较差,这段代码写了好长时间。
#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;
}