C++ 栈 实验

5 篇文章 2 订阅
1 篇文章 0 订阅

1.实验目的:

(1)理解栈的定义、特点及与线性表的异同;
(2)熟悉顺序栈的组织方法,栈满、栈空的判断条件及其描述;
(3)掌握栈的基本操作(进栈、退栈等)。

2.实验内容:

(1)设计一个算法,将一般算术表达式转化为逆波兰表达式,并求逆波兰表达
式的值。
(2)设计两个栈 S1、S2 都采用顺序栈方式,并且共享一个存储区[0,MaxLen-1],
为了尽量利用空间,减少溢出的可能,可采用栈顶相向、迎面增长的存储方式,如图
2-1 所示。设计一个有关栈的入栈和出栈算法。

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

const int StackSize = 30;
char result[30];
char num;
 
typedef struct Stack
{
	char data[StackSize];
	int top;
}StackType;
 
char Pop(StackType *p)
{
	char x;
	if(p->top == -1) 
	{
		printf("under flow\n");
		exit(0);
	}
	x = p->data[p->top--];
	return x;
}
void Push(StackType *p, char dat)
{
	if(p->top == StackSize -1)
	{
		printf("over flow\n");
		exit(0);
	}
	p->data[++p->top] = dat;
}
 
bool Emtry(StackType *p)
{
	if(p->top == -1)
		return 1;
	return 0;
} 

char GetTop(StackType *p)
{
	if(p->top != -1)
		return p->data[p->top];
	return 0;
}
 
 bool isNumber(char op)
 {
	 switch(op)
	 { 	
		case ' ':
		case '(':
		case ')':
		case '+':    	
		case '-':    	
		case '*':    	
		case '/':
		case '#':		return 0;    
		default :       return 1;	 
	}
 }

 int priority(char ch)
 {
	int value= 10; 
	switch(ch)
	{
		case '(':
		case ')':	value = 4;	break;
		case '*':	value = 2;	break;
		case '/':	value = 2;	break;
		case '+':	value = 3;  break;
		case '-':	value = 3;	break;
		case '#':	value = 5;	break;
		default:  break;
	}
	return value;
 }
 


void Nifix2Postfix(char arr[])
{
	StackType* pStack = new StackType;
	pStack->top = -1;
	int i = 0;
	char ch;
	for(i=0;arr[i] != '\0';i++)
	{
		if(isNumber(arr[i]))
		{
			result[num++] = arr[i];
		}
	
		else
		{
		
			if(Emtry(pStack) || arr[i] == '(')
			{
				Push(pStack, arr[i]);
				continue;
			}
			if(arr[i] == '#')
				break;
			if(arr[i] == ')')
			{
				result[num++] = ' ';
				while((ch = Pop(pStack)) != '(' )
				{
					result[num++] = ch;
					result[num++] = ' ';
				}
				continue;
			}
			result[num++] = ' ';
			ch = priority(GetTop(pStack)) - priority(arr[i]);
			if(ch > 0)
			{
				Push(pStack, arr[i]);
			}
			else if(ch <= 0)
			{
				
				while( priority(arr[i]) >= priority(GetTop(pStack)))
				{
//					printf("%c",ch = Pop(pStack));
					result[num++] = Pop(pStack);
//					printf(" ");
					result[num++] = ' ';
				}
				Push(pStack, arr[i]);							
			}
 
		}
 
	}
	while(GetTop(pStack) != '#')
	{
		result[num++] = ' ';
		result[num++] = Pop(pStack);
	}
	for(i=0;i<num;i++)
		printf("%c",result[i]);
}
 
int Calculate(char arr[])
{
	int i, cal[10],top=-1;
	memset(cal,0,sizeof(cal));
	for(i=0;i<num;i++)
	{
		if(isNumber(arr[i]))
		{
			
			top++;
			while(arr[i] != ' ')
			{
				cal[top] = cal[top]*10 + arr[i++]-48;
			}
		}
		else
		{
			switch(arr[i])
			{
				//模拟出栈
				case '+':	cal[top-1] = cal[top-1] + cal[top]; cal[top--] = 0; break;
				case '-':	cal[top-1] = cal[top-1] - cal[top]; cal[top--] = 0; break;
				case '*':	cal[top-1] = cal[top-1] * cal[top]; cal[top--] = 0; break;
				case '/':	cal[top-1] = cal[top-1] / cal[top]; cal[top--] = 0; break;
				default: break;
			}
		}
	}
	return cal[0];
}
 
 
int main(void)
{
	char  express[] = "#3*(4+2)/2-5#";
	
//	gets(express);
 
	printf("中缀表达式:");
	printf("%s\n", express);
 
	printf("后缀表达式:");
	Nifix2Postfix(express);
	printf("\n");
	
	printf("运算结果为:%d\n", Calculate(result));
 
	return 0;
}
#include<stdio.h>
#include<stdlib.h>
#define  MaxSize 100

typedef int ElemType;
typedef struct{
    ElemType data[MaxSize];
    int top[2];
}SqStack;

void InitStack(SqStack*);
int Push(SqStack*,ElemType,int);
int Pop(SqStack*,ElemType*,int);

int main(int argc,char* argv[])
{
    SqStack s;
    InitStack(&s);

    ElemType x=5;
    int n=0;
    int flagPush;
    flagPush=Push(&s,x,n);
    if(flagPush){
        printf("Push false!\n");
    }else{
        printf("入栈 %d 成功啦!\n",x);
    }

    int flagPop;
    flagPop=Pop(&s,&x,n);
    if(flagPop){
        printf("Pop false!\n");
    }else{
        printf("出栈 %d  成功啦!\n",x);
    }
    return 0;
}
//初始化共享栈
void InitStack(SqStack *s)
{
    s->top[0]=-1;
    s->top[1]=MaxSize;
}
//入栈操作
int Push(SqStack*s, ElemType x, int n)
{
    if(n<0||n>1){
        printf("The stack number is false!\n");
        return -1;
    }
    if(s->top[1]-s->top[0]==1){
        printf("The stack is full!\n");
        return -1;
    }
    switch(n){
        case 0:s->data[++s->top[0]]=x;break;
        case 1:s->data[--s->top[1]]=x;break;
    }
    return 0;
}
//出栈操作
int Pop(SqStack *s, ElemType* x,int n)
{
    if(n<0||n>1){
        printf("The stack number is false!\n");
        return -1;
    }
    switch(n){
        case 0:
            if(s->top[0]==-1){
                printf("The stack[0] is empty!\n");
            }
            *x=s->data[s->top[0]--];
            break;
        case 1:
            if(s->top[1]==MaxSize){
                printf("The stack[1] is empty!\n");
            }
            *x=s->data[s->top[1]++];
            break;
    }
    return 0;
}
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值