C/C++ 算法 中缀转后缀表达式实现3---实现加减乘除四则算数(包括浮点数运算,包括括号)

C/C++ 算法 中缀转后缀表达式实现3---实现浮点数加减乘除四则算数(包括括号)

如:输入:12.2/2+(10+4)/7+2.5*2.5-1,输出:13.35

代码如下:

#include<iostream>
#include<stack>
using namespace std;

bool isoperator(char ch);
int priority(char ch);
void infix_to_postfix(char infix[],char postfix[]);
double getpostfix_value(char postfix[]);
int main(){
	char infix[]="12.2/2+(10+4)/7+2.5*2.5-1";
	cout<<infix<<endl;
	char postfix[1024];
	infix_to_postfix(infix,postfix);
	cout<<postfix<<endl;
	double result=getpostfix_value(postfix);
	cout<<result<<endl;

	return 0;
}
bool isoperator(char ch)
{
	switch(ch)
	{
	case '+':
	case '-':
	case '*':
	case '/':
		return true;
	default:
		return false;
	}
}
int priority(char ch)
{
	switch(ch)
	{
	case '(':
		return 0;
	case '+':
	case '-':
		return 1;
	case '*':
	case '/':
		return 2;
	default:
		return -1;
	}
}
void infix_to_postfix(char infix[],char postfix[])
{
	stack<char> s1;
	s1.push('#');
	int i=0,j=0;
	while(infix[i]!='\0')
	{
		if((infix[i]>='0'&&infix[i]<='9')||infix[i]=='.')
		{
			postfix[j++]=infix[i];
		}
		else if(infix[i]=='(')
		{
			s1.push(infix[i]);
		}
		else if(infix[i]==')')
		{
			while(s1.top()!='(')
			{
				postfix[j++]=' ';
				postfix[j++]=s1.top();
				s1.pop();
			}
			s1.pop();
		}
		else if(isoperator(infix[i]))
		{
			postfix[j++]=' ';
			if(s1.empty())
			{
				s1.push(infix[i]);
			}
			else
			{
				while(priority(infix[i])<=priority(s1.top()))
				{
					postfix[j++]=s1.top();
					postfix[j++]=' ';
					s1.pop();
				}
				s1.push(infix[i]);
			}
		}
		i++;
	}
	while(s1.size())
	{
		postfix[j++]=' ';
		postfix[j++]=s1.top();
		s1.pop();
	}
	postfix[j-1]='\0';
}
double getpostfix_value(char postfix[])
{
	stack<double> s1;
	int i=0;
	double result=0;
	double x1=0,x2=0;
	while(postfix[i]!='\0')
	{
		if(postfix[i]>='0'&&postfix[i]<='9')
		{
			double x=0;
			int n=0;//counting the number of point
			while(postfix[i]>='0'&&postfix[i]<='9')
			{
				x=x*10+(postfix[i]-'0');
				i++;
			}
			//deal point
			if(postfix[i]=='.')
			{
				i++;
				while(postfix[i]>='0'&&postfix[i]<='9')
				{
					x=x*10+(postfix[i]-'0');
					i++;
					n++;
				}
			}
			while(n)
			{
				x/=10;
				n--;
			}
			s1.push(x);
		}
		else if(postfix[i]==' ')
		{
			i++;
		}
		else if(postfix[i]=='+')
		{
			x1=s1.top();
			s1.pop();
			x2=s1.top();
			s1.pop();
			double temp=x2+x1;
			s1.push(temp);
			i++;
		}
		else if(postfix[i]=='-')
		{
			x1=s1.top();
			s1.pop();
			x2=s1.top();
			s1.pop();
			double temp=x2-x1;
			s1.push(temp);
			i++;
		}
		else if(postfix[i]=='*')
		{
			x1=s1.top();
			s1.pop();
			x2=s1.top();
			s1.pop();
			double temp=x2*x1;
			s1.push(temp);
			i++;
		}
		else if(postfix[i]=='/')
		{
			x1=s1.top();
			s1.pop();
			x2=s1.top();
			s1.pop();
			double temp=x2/x1;
			s1.push(temp);
			i++;
		}
	}
	result=s1.top();
	return result;
}



运行结果:


  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以回答这个问题。以下是一个用 C 语言编写的中缀后缀的代码,可以实现加减乘除四则运算和取余运算: #include <stdio.h> #include <stdlib.h> #include <ctype.h> #define MAX_STACK_SIZE 100 typedef enum { lparen, rparen, plus, minus, times, divide, mod, eos, operand } precedence; int isp[] = {, 19, 12, 12, 13, 13, 13, }; int icp[] = {20, 19, 12, 12, 13, 13, 13, }; precedence getToken(char *symbol, int *n) { *symbol = getchar(); switch (*symbol) { case '(': return lparen; case ')': return rparen; case '+': return plus; case '-': return minus; case '*': return times; case '/': return divide; case '%': return mod; case '\n': return eos; default: return operand; } } void postfix(void) { char symbol; precedence token; int n = ; int top = ; int stack[MAX_STACK_SIZE]; stack[] = eos; for (token = getToken(&symbol, &n); token != eos; token = getToken(&symbol, &n)) { if (token == operand) { printf("%c", symbol); } else if (token == rparen) { while (stack[top] != lparen) { switch (stack[top--]) { case plus: printf("+"); break; case minus: printf("-"); break; case times: printf("*"); break; case divide: printf("/"); break; case mod: printf("%%"); break; } } } else { while (isp[stack[top]] >= icp[token]) { switch (stack[top--]) { case plus: printf("+"); break; case minus: printf("-"); break; case times: printf("*"); break; case divide: printf("/"); break; case mod: printf("%%"); break; } } stack[++top] = token; } } while ((token = stack[top--]) != eos) { switch (token) { case plus: printf("+"); break; case minus: printf("-"); break; case times: printf("*"); break; case divide: printf("/"); break; case mod: printf("%%"); break; } } printf("\n"); } int main(void) { postfix(); return ; }

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值