栈——数据结构——day4-栈的应用

四则运算表达式求值

头文件

#ifndef __STACK_H__
#define __STACK_H__

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


typedef struct node
{
	int data;
	struct node *pnext;
}STACK_NODE;

typedef struct list
{
	STACK_NODE *phead;
	int clen;
}STACK_LIST;

extern STACK_LIST *CreatStackList();
extern int PushHeadSTACK(STACK_LIST *plist,int data);
extern int PopHeadSTACK(STACK_LIST *plist,int *data);
extern int GetTopOfStack(STACK_LIST *plist,int *num);
extern int ClearStack(STACK_LIST *plist);
extern void DestoryStack(STACK_LIST *plist);
extern int ErgodicOfStack(STACK_LIST *plist);
extern int StackLength(STACK_LIST *plist);
extern int IsEmptyStack(STACK_LIST *plist);

#endif

main.c

#include "stack.h"

int IsNumChar(char ch)
{
	if(ch >= '0' && ch <= '9')
	{
		return 1;
	}
	return 0;
}

int GetOptLevel(char opt)
{
	switch(opt)
	{
		case '+':
		case '-':
			return 1;
		case '*':
		case '/':
			return 2;
		default:
			printf("opt error\n");
			exit(1);
	}
}

int GetValue(int num1,int num2,int opt)
{
	switch(opt)
	{
	case '+':
		return num1 + num2;
	case '-':
		return num1 - num2;
	case '*':
		return num1 * num2;
	case '/':
		return num1 / num2;
	}
}

int main(void)
{
	char str[1024] = {0};
	
	STACK_LIST *pstacknum = CreatStackList();
	STACK_LIST *pstackopt = CreatStackList();
	if(NULL == pstacknum || NULL == pstackopt)
	{
		return -1;
	}


	printf("Please enter four operations:");
	fgets(str,sizeof(str),stdin);
	str[strlen(str)-1] = '\0';

	char *p = str;
	int num = 0;
	int popOpt;
	int num1,num2,opt;

	while(1)
	{
		if(*p == '\0' && IsEmptyStack(pstackopt))
		{
			break;
		}

		while(IsNumChar(*p))
		{
			num = num * 10 + (*p - '0') ;
			p++;
			if(!IsNumChar(*p))
			{
				PushHeadSTACK(pstacknum,num);
				num = 0;
			}
		}
		

		if(IsEmptyStack(pstackopt))
		{
			PushHeadSTACK(pstackopt,*p);
			p++;
			continue;
		}
		
		GetTopOfStack(pstackopt,&popOpt);
		if(*p != '\0' && GetOptLevel(*p) > GetOptLevel(popOpt))
		{
			PushHeadSTACK(pstackopt,*p);
			p++;
		}
		else
		{
			PopHeadSTACK(pstackopt,&opt);
			PopHeadSTACK(pstacknum,&num2);
			PopHeadSTACK(pstacknum,&num1);
			int res = GetValue(num1,num2,opt);
			PushHeadSTACK(pstacknum,res);
		}

	}
	
	ErgodicOfStack(pstacknum);

	DestoryStack(pstacknum);
	DestoryStack(pstackopt);

	return 0;
}

opera.c

#include "stack.h"

STACK_LIST *CreatStackList()
{
	STACK_LIST *p = malloc(sizeof(STACK_LIST));

	p->phead = NULL;
	p->clen = 0;

	return p;
}

int IsEmptyStack(STACK_LIST *plist)
{
	return NULL == plist->phead;
}

int PushHeadSTACK(STACK_LIST *plist,int data)
{
	STACK_NODE *p = malloc(sizeof(STACK_NODE));


	p->pnext = plist->phead;
	p->data = data;

	plist->phead = p;
	plist->clen++;

	return 0;
}

int PopHeadSTACK(STACK_LIST *plist,int *data)
{
	if(IsEmptyStack(plist))
	{
		return -1;
	}
	
	STACK_NODE *p = plist->phead;
	plist->phead = p->pnext;
	if(data != NULL)
	{
		*data = p->data;
	}
		free(p);
	plist->clen--;

	return 0;
}

int GetTopOfStack(STACK_LIST *plist,int *num)
{
	if(IsEmptyStack(plist))
	{
		return 0;
	}
	
	STACK_NODE *p = plist->phead;

	*num = p->data;

	return 0;
}

int ClearStack(STACK_LIST *plist)
{
	if(IsEmptyStack(plist))
	{
		return 0;
	}

	STACK_NODE *p = plist->phead;

	while(p)
	{
		PopHeadSTACK(plist,NULL);
		p = plist->phead;
	}

	return 0;
}

void DestoryStack(STACK_LIST *plist)
{
	ClearStack(plist);
		free(plist);
		return ;
}

int StackLength(STACK_LIST *plist)
{
	return plist->clen;
}

int ErgodicOfStack(STACK_LIST *plist)
{
	if(IsEmptyStack(plist))
	{
		return 0;
	}
	
	STACK_NODE *p = plist->phead;

	while(p)
	{
		printf("%d ",p->data);
		p = p->pnext;
	}
	
	putchar('\n');

	return 0;
}

###结果:
在这里插入图片描述
以上就是今天内容!

上一篇:基于SSM框架的酒店预订系统


下一篇:3dmax效果图云渲染平台哪个好?