栈的应用(四则运算中中缀和后缀的转化,包含源代码)

在这里插入图片描述


源代码:

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

typedef struct node
{
	char *nValue;
	struct node *pNext;
}Node;

typedef struct queue
{
	int nCount;
	Node *pTop;
}Stack;

void s_Init(Stack **pStack)
{
	*pStack = (Stack *)malloc(sizeof(Stack));
	(*pStack)->nCount = 0;
	(*pStack)->pTop = NULL;
}

void s_Push(Stack *pStack,char *pStr)
{
	Node *pNode = (Node *)malloc(sizeof(Node));
	if(pStack == NULL)exit(1);
	pNode->nValue = pStr;
	pNode->pNext = pStack->pTop;
	pStack->pTop = pNode;
	pStack->nCount++;
}

char *s_Pop(Stack *pStack)
{
	char *nValue;
	Node *pNode = NULL;
	if(pStack == NULL || pStack->pTop == NULL)exit(1);
	nValue = pStack->pTop->nValue;
	pNode = pStack->pTop;
	pStack->pTop = pNode->pNext;
	free(pNode);
	pNode = NULL;
	pStack->nCount--;
	return nValue;
}

int s_IsEmpty(Stack *pStack)
{
	if(pStack == NULL)exit(1);
	return pStack->nCount == 0;	
}

char *GetString()
{
	int size = 5;
	int nCount = 0;
	char ch;
	char *str = (char *)malloc(sizeof(char)*size);
	while((ch = getchar()) != '\n'){
		str[nCount++] = ch;
		if(ch == size-1){
			char *NewStr = NULL;
			str[ch] = '\0';
			size += 5;
			NewStr = (char *)malloc(sizeof(char)*size);
			strcpy_s(NewStr, size, str);
			free(str);
			str = NewStr;
		}
	}
	str[nCount] = '\0';
	return str;
}

char *ResoleString(char **pStr)
{
	int nFlag = 0, nCount = 0;
	char *pNewStr = NULL;
	if((*pStr) == NULL)return NULL;
	pNewStr = (char *)malloc(sizeof(char)*strlen((*pStr)) + 1);
	while(strlen((*pStr))){
		if((*(*pStr) == ' ' || *(*pStr) == '+' || *(*pStr) == '-' || 
			*(*pStr) == '*' || *(*pStr) == '/' || *(*pStr) == '(' || *(*pStr) == ')' )
			&& nFlag){
			pNewStr[nCount] = '\0';
			return pNewStr;
		}else{
			if(*(*pStr) == ' ')(*pStr)++;
			else if(*(*pStr) == '+' || *(*pStr) == '-' || *(*pStr) == '*' || 
				*(*pStr) == '/' || *(*pStr) == '(' || *(*pStr) == ')'){
				pNewStr[0] = *(*pStr)++;
				pNewStr[1] = '\0';
				return pNewStr;
			}
			else if(*(*pStr)>='0' && *(*pStr)<='9'){
				nFlag = 1;
				pNewStr[nCount++] = *(*pStr)++; 
			}
			else return "";
		}
		
	}
	pNewStr[nCount] = '\0';
	return pNewStr;

}


char *ArithmeticChange(Stack *pStack)
{
	char *pStr = NULL;
	char *pSumStr = NULL;
	char *pTemp = NULL;
	char *pMark = NULL;
	int nLen = 0;
	int nLength = 0;
	int nFlag = 0;
	if(pStack == NULL)exit(1);
	pStr = GetString();
	nLength = strlen(pStr)*2+1;
	pSumStr = (char *)malloc(sizeof(char)*nLength);
	*pSumStr = '\0';
	while(strlen( (pTemp = ResoleString(&pStr)) ))
	{
		if(*pTemp >= '0' && *pTemp <= '9')
			strcat_s(pSumStr,nLength,pTemp);
		else if(*pTemp == ')')
		{
			nFlag = 0;
			while(!s_IsEmpty(pStack))
			{
				pMark = s_Pop(pStack);
				if(*pMark == '(')break;
				strcat_s(pSumStr,nLength,pMark);
			}
		}else if(*pTemp == '(')
		{
			nFlag = 1;
			s_Push(pStack,pTemp);
		}else if(nFlag)
		{
			nFlag = 0;
			s_Push(pStack,pTemp);
		}else{
			if(s_IsEmpty(pStack))
			{
				s_Push(pStack,pTemp);
			}else if(*pTemp == '+' || *pTemp == '-')
			{
				while(!s_IsEmpty(pStack))
				{
					pMark = s_Pop(pStack);
					strcat_s(pSumStr,nLength,pMark);
				}
				s_Push(pStack,pTemp);
			}else if(*pTemp == '*' || *pTemp == '/')
			{
				if(*(pStack->pTop->nValue) == '+' || *(pStack->pTop->nValue) == '-')
				{
					s_Push(pStack,pTemp);
				}else{
					while(!s_IsEmpty(pStack))
					{
						if(*(pStack->pTop->nValue) == '+' || *(pStack->pTop->nValue) == '-')
						{
							break;
						}
						pMark = s_Pop(pStack);
						strcat_s(pSumStr,nLength,pMark);
					}
					s_Push(pStack,pTemp);
				}
			}
		}
		nLen = strlen(pSumStr);
		pSumStr[nLen+1] = '\0';
		pSumStr[nLen] = ' ';
	}
	while(!s_IsEmpty(pStack)){
		pMark = s_Pop(pStack);
		strcat_s(pSumStr,nLength,pMark);
		nLen = strlen(pSumStr);
		pSumStr[nLen+1] = '\0';
		pSumStr[nLen] = ' ';
	}
	return pSumStr;
}

char *ArithmeticReduction(Stack *pStack,char *pStr)
{
	char *pTemp = NULL;
	char *pMark1 = NULL;
	char *pMark2 = NULL;
	int nLen = 0;
	int nLength = 0;
	int nFlag = 0;
	if(pStack == NULL || pStr == NULL)exit(1);
	nLength = strlen(pStr)*2+1;
	while(strlen( (pTemp = ResoleString(&pStr)) ))
	{
		if(*pTemp >= '0' && *pTemp <= '9')
			s_Push(pStack,pTemp);
		if(*pTemp == '+' || *pTemp == '-' || *pTemp == '*' || *pTemp == '/')
		{
			char *pSumStr = (char *)malloc(sizeof(char)*nLength);
			*pSumStr = '\0';
			pMark2 = s_Pop(pStack);
			pMark1 = s_Pop(pStack);
			nLen = strlen(pSumStr);
			pSumStr[nLen+1] = '\0';
			pSumStr[nLen] = '(';
			strcat_s(pSumStr,nLength,pMark1);
			strcat_s(pSumStr,nLength,pTemp);
			strcat_s(pSumStr,nLength,pMark2);
			nLen = strlen(pSumStr);
			pSumStr[nLen+1] = '\0';
			pSumStr[nLen] = ')';
			s_Push(pStack,pSumStr);
		}
	}
	return pStack->pTop->nValue;
}

int main()
{
	Stack *pStack = NULL;
	char *pStr1 = NULL;
	char *pStr2 = NULL;
	s_Init(&pStack);
	pStr1 = ArithmeticChange(pStack);
	pStr2 = ArithmeticReduction(pStack,pStr1);
	printf("%s\n",pStr1);
	printf("%s\n",pStr2);
	return 0;
}


//234 + 324 * (234 +234) -234 /234 +324

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值