数据结构01利用栈来编写计算器

利用栈来编写计算器

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

typedef struct snode {
	char data;
	struct snode* next;
}snode, * linkstack;

typedef struct snode_ {
	float data;
	struct snode_* next;
}snode_, * linkstack_;

void init(linkstack& s);
void pushstack(linkstack& s, char x);
void popstack(linkstack& s, char& x);
void pushstack_(linkstack_& s, char x);
void popstack_(linkstack_& s, char& x);
void nifsuf(char* nif, char* suf);//将中缀表达式转为后缀式
float cal(char* suf);

void main()
{
	char nif[100], suf[100];
	linkstack s;
	init(s);
	scanf("%s", nif);
	nifsuf(nif, suf);
	cal(suf);
}

void init(linkstack& s)
{
	s = (linkstack)malloc(sizeof(snode));
	s->data = '#';
	s->next = NULL;
	//s = NULL;
}

void pushstack(linkstack& s, char x)
{
	linkstack p = (linkstack)malloc(sizeof(snode));
	p->data = x;
	p->next = s;
	s = p;
}
void pushstack_(linkstack_& s, float x)
{
	linkstack_ p = (linkstack_)malloc(sizeof(snode_));
	p->data = x;
	p->next = s;
	s = p;
}

void nifsuf(char* nif, char* suf)
{
	int i = 0, j = 0;
	char c, t;
	linkstack s;
	init(s);
	c = nif[i];
	while (c != '#')
	{
		while (isdigit(c) || c == '.')
		{
			suf[j++] = c;
			c = nif[++i];
			if (c < '0' || c>'9')
			{
				if(c != '.')
				{
					suf[j++] = ' ';
				}
			}
			suf[j] = '\0';
		}
		//pushstack(s, '#');

		if (c == '+' || c == '-')
		{
			if (s->data=='#')
			{
				pushstack(s, c);
			}
			else
			{
				popstack(s, t);
				if (t == '(')
				{
					pushstack(s,t);
					pushstack(s,c);
				}
				else
				{
					suf[j++] = t;
					suf[j++] = ' ';
					suf[j] = '\0';
				}
			}
			c = nif[++i];

		}
		if(c == '*' || c == '/')
		{
			if (s->data=='#')
			{
				pushstack(s, c);
			}
			else
			{
				if(s->data=='*'||s->data=='/')
				{
					popstack(s, t);
					suf[j++] = t;
					suf[j++] = ' ';
					suf[j] = '\0';
					pushstack(s,c);
				}
				else{
					if(s->data=='+'||s->data=='-')
					{
                        pushstack(s,c);
					}

				}
			}
			c = nif[++i];

		}
		if (c == '(')
		{
			pushstack(s, c);
			c = nif[++i];
		}
		if (c == ')')
		{
			popstack(s, t);
			while (t != '(')
			{
				suf[j++] = t;
				suf[j++] = ' ';
				popstack(s, t);
			}
			suf[j] = '\0';
			c = nif[++i];
		}


	}
	if (c == '#')
	{
		while (s->next)
		{
			popstack(s, t);
			suf[j++] = t;
			suf[j++] = ' ';
		}
		suf[j++] = '#';
		suf[j] = '\0';
		free(s);

	}
}

void popstack(linkstack& s, char& x)
{
	linkstack p = s;
	x = p->data;
	if(s->next==NULL)
	{
		s->next = NULL;
	}
	else {
		s = s->next;
	}
	free(p);
}
void popstack_(linkstack_& s, float& x)
{
	linkstack_ p = s;
	x = p->data;
	if (s->next == NULL)
	{
		s->next = NULL;
	}
	else {
		s = s->next;
	}
	free(p);
}

float cal(char* suf)
{
	int i = 0, j = 0;
	char c;
	float f, r, d1, d2;
	linkstack_ s;
	s = (linkstack_)malloc(sizeof(snode_));
	s = NULL;
	char t[100];
	c = suf[j++];
	while (c != '#')
	{
		while (isdigit(c) || c == '.')
		{
			t[i++] = c;
			t[i] = '\0';
			c = suf[j++];
			if (c == ' ')
			{
				f = atof(t);
				pushstack_(s, f);
				i = 0;
			}
		}
		switch (c) {
		case'+':
			popstack_(s, d2);
			popstack_(s, d1);
			pushstack_(s, d1 + d2);
			break;
		case'-':
			popstack_(s, d2);
			popstack_(s, d1);
			pushstack_(s, d1 - d2);
			break;
		case'*':
			popstack_(s, d2);
			popstack_(s, d1);
			pushstack_(s, d1 * d2);
			break;
		case'/':
			popstack_(s, d2);
			if (d2 == 0)
			{
				printf("error");
				return -1;
			}
			popstack_(s, d1);
			pushstack_(s, d1 / d2);
			break;
		}
		c = suf[j++];
	}
	popstack_(s, r);
	printf("result:%f", r);
	free(s);
	return r;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值