表达式求值--进阶-求小数(建议先看我之前写的表达式求值)

#include<stdio.h>
#include<stdbool.h>
#include<string.h>
#include<assert.h>
#include<stdlib.h>
#include<ctype.h>
#include<math.h>
typedef float SLTDataType;

typedef struct Stack
{
	SLTDataType data;
	struct Stack* next;
}ST;

//创建节点
ST* BuyListNode(SLTDataType x)
{
	ST* newnode = (ST*)malloc(sizeof(ST));
	if (newnode == NULL)
	{
		perror("节点创建失败");
		return NULL;
	}
	newnode->next = NULL;
	newnode->data = x;
	return newnode;
}

//在栈顶插入
void StackPush(ST** phead, SLTDataType x)
{
	ST* newnode = BuyListNode(x);
	assert(newnode);
	newnode->next = *phead;
	*phead = newnode;
}


//栈是否为空
bool StackEmpty(ST* phead)
{
	return phead == NULL;
}

//在栈顶删除
SLTDataType StackPop(ST** phead)
{
	assert(phead);
	assert(!StackEmpty(*phead));
	ST* prev = *phead;
	SLTDataType x = prev->data;
	*phead = (*phead)->next;
	free(prev);
	return x;
}

//查看栈顶元素
SLTDataType StackTop(ST* phead)
{
	assert(phead);
	assert(!StackEmpty(phead));
	return phead->data;
}


//销毁栈
void StackDestroy(ST** phead)
{
	assert(phead);
	while (*phead)
	{
		ST* cur = *phead;
		*phead = (*phead)->next;
		free(cur);
	}
}

//求栈的长度
int StackLength(ST* phead)
{
	assert(phead);
	//assert(!StackEmpty(phead));
	int count = 0;
	ST* cur = phead;
	while (cur)
	{
		count++;
		cur = cur->next;
	}
	return count;
}


//表达式求值
//法一:两个栈,直接计算法
ST* s1 = NULL;//存放符号
ST* s2 = NULL;//存放数字
int prior(char ch)
{
	if (ch == '*' || ch == '/')
		return 3;
	else if (ch == '+' || ch == '-')
		return 2;
	else if (ch == '(' || ch == ')')
		return 1;
	else if (ch == '#')
		return 0;
}

void Cal()
{
	char tmp = StackTop(s1);
	SLTDataType x1, x2;
	x1 = StackPop(&s2);
	x2 = StackPop(&s2);
	switch (tmp)
	{
	case '+':
		StackPush(&s2, x1 + x2);
		break;
	case '-':
		StackPush(&s2, x2 - x1);//注意顺序
		break;
	case '*':
		StackPush(&s2, x1 * x2);
		break;
	case '/':
		if (fabs(x1) < 1e-6)
		{
			printf("除数不能为零\n");
			return;
		}
		StackPush(&s2, x2 / x1);//注意顺序
		break;
	}
}


SLTDataType GetValue(char p[])
{
	int i = 0;
	StackPush(&s1, '#');
	while (p[i])
	{
		SLTDataType sum = 0;
		//将数字整个放入s2栈中
		while (isdigit(p[i])||p[i]=='.')
		{
			//计算小数位
			if (p[i] == '.')
			{
				i++;
				int j = 0;
				SLTDataType decimal = 0;
				while (isdigit(p[i]))
				{
					decimal = decimal * 10 + p[i] - '0';
					i++;
					j++;
				}
				decimal = decimal / pow(10,j);
				sum += decimal;
			}
			else if(isdigit(p[i]))
			{
				//计算整数位
				sum = sum * 10 + p[i] - '0';
				i++;	
			}
			if (!isdigit(p[i]) && p[i] != '.')
			{
				StackPush(&s2,sum);
				break;
			}
		}
		
		if (!p[i])
			break;
		char tmp;
		switch (p[i])
		{
		case '(':
			StackPush(&s1, '(');
			break;
		case ')':
			tmp = StackTop(s1);
			while (tmp != '(')
			{
				Cal();
				StackPop(&s1);
				tmp = StackTop(s1);
			}
			StackPop(&s1);//删除'('
			break;
		default:
			//处理数据
			tmp = StackTop(s1);
//注意我们现在的算法是随时运算,故只要后面的符号不大于前面的符号,就可以直接进行运算,
//否则会出现逆序的运算
			if (prior(p[i]) > prior(tmp))
			{
				StackPush(&s1, p[i]);
			}
			else
			{
				while (prior(p[i]) <= prior(tmp))
				{
					Cal();
					StackPop(&s1);
					tmp = StackTop(s1);
				}
				StackPush(&s1, p[i]);
			}
			break;
		}
		i++;
	}
	char tmp;
	while ((tmp = StackTop(s1)) != '#')
	{
		Cal();
		StackPop(&s1);
		tmp = StackTop(s1);
	}
	return StackTop(s2);
}
int main()
{
	char ch[100] = { 0 };
	gets(ch);
	SLTDataType ret = GetValue(ch);
	printf("%f", ret);
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值