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

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

如,输入:1+((2+3)*4)-5,输出前缀表达式:- + 1 * + 2 3 4 5,计算结果等于:16

代码如下:

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

void reverse(char polish[]);
bool isoperator(char ch);
int priority(char ch);
double getpolish_value(char polish[]);
void infix_to_polish(char infix[],char polish[]);

int main(){
	char infix[]="1+((2+3)*4)-5";
	char polish[1024];
	cout<<infix<<endl;
	infix_to_polish(infix,polish);
	cout<<polish<<endl;
	double result=getpolish_value(polish);
	cout<<result<<endl;
	return 0;
}

void reverse(char str[])
{
	for(int i=0,j=strlen(str)-1;i<j;i++,j--)
	{
		char ch=str[i];
		str[i]=str[j];
		str[j]=ch;
	}
}
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_polish(char infix[],char polish[])
{
	//从右往左扫描
	reverse(infix);
	stack<char> s1;
	s1.push('#');
	int i=0,j=0;
	while(infix[i]!='\0')
	{
		if((infix[i]>='0'&&infix[i]<='9')||infix[i]=='.')
		{
			polish[j++]=infix[i];
		}
		else if(infix[i]==')')
		{
			s1.push(infix[i]);
		}
		else if(infix[i]=='(')
		{
			while(s1.top()!=')')
			{
				polish[j++]=' ';
				polish[j++]=s1.top();
				s1.pop();
			}
			s1.pop();
		}
		else if(isoperator(infix[i]))
		{
			polish[j++]=' ';
			if(s1.empty())
			{
				s1.push(polish[i]);
			}
			else
			{
				if(priority(infix[i])<priority(s1.top()))
				{
					polish[j++]=s1.top();
					polish[j++]=' ';
					s1.pop();
				}
				s1.push(infix[i]);
			}
		}
	
		i++;
	}
	while(s1.size())
	{
		polish[j++]=' ';
		polish[j++]=s1.top();
		s1.pop();
	}
	polish[j-1]='\0';
	reverse(polish);

}

double getpolish_value(char polish[]){
	reverse(polish);
	stack<double> s1;
	int i=0;
	double result=0;
	double x1=0,x2=0;
	while(polish[i]!='\0')
	{
		if(polish[i]>='0'&&polish[i]<='9')
		{
			double x=0;
			int n=0;
			while(polish[i]>='0'&&polish[i]<='9')
			{
				x=x*10+(polish[i]-'0');
				i++;
			}
			if(polish[i]=='.')
			{
				i++;
				while(polish[i]>='0'&&polish[i]<='9')
				{
					x=x*10+(polish[i]-'0');
					i++;
					n++;
				}
			}
			while(n)
			{
				x/=10;
				n--;
			}
			s1.push(x);
		}
		else if(polish[i]==' ')
		{
			i++;
		}
		else if(polish[i]=='+')
		{
			x1=s1.top();
			s1.pop();
			x2=s1.top();
			s1.pop();
			double temp=x1+x2;
			s1.push(temp);
			i++;
		}
		else if(polish[i]=='-')
		{
			x1=s1.top();
			s1.pop();
			x2=s1.top();
			s1.pop();
			double temp=x1-x2;
			s1.push(temp);
			i++;
		}
		else if(polish[i]=='*')
		{
			x1=s1.top();
			s1.pop();
			x2=s1.top();
			s1.pop();
			double temp=x1*x2;
			s1.push(temp);
			i++;
		}
		else if(polish[i]=='+')
		{
			x1=s1.top();
			s1.pop();
			x2=s1.top();
			s1.pop();
			double temp=x1/x2;
			s1.push(temp);
			i++;
		}
	}
	result=s1.top();
	return result;
}
			

运行结果:


  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实现中缀表达式前缀表达式,可以采用以下步骤: 1. 将中缀表达式翻,得到一个反向的表达式,这样左右括号的位置就会发生变化。 2. 将反向表达式中的左右括号互换,得到一个新的表达式。 3. 对新表达式进行前缀表达式的求解。 例如,将中缀表达式"5+(6-3)*2/3"换为前缀表达式的过程如下: 1. 将中缀表达式翻得到反向表达式"3/2*(3-6)+5"。 2. 将反向表达式中的左右括号互换得到新表达式"5+(6-3)*2/3". 3. 对新表达式进行前缀表达式的求解,得到前缀表达式"+ 5 / * - 6 3 2 3"。 下面是一个C++实现代码: ```cpp #include <iostream> #include <stack> #include <string> #include <algorithm> using namespace std; bool isOperator(char c) { return (c == '+' || c == '-' || c == '*' || c == '/'); } int priority(char op) { if (op == '+' || op == '-') return 1; if (op == '*' || op == '/') return 2; return 0; } string infixToPrefix(string infix) { reverse(infix.begin(), infix.end()); stack<char> s; string prefix; for (int i = 0; i < infix.length(); i++) { char c = infix[i]; if (isOperator(c)) { while (!s.empty() && priority(s.top()) > priority(c)) { prefix += s.top(); s.pop(); } s.push(c); } else if (c == ')') { s.push(c); } else if (c == '(') { while (!s.empty() && s.top() != ')') { prefix += s.top(); s.pop(); } s.pop(); } else { prefix += c; } } while (!s.empty()) { prefix += s.top(); s.pop(); } reverse(prefix.begin(), prefix.end()); return prefix; } int main() { string infix = "5+(6-3)*2/3"; string prefix = infixToPrefix(infix); cout << prefix << endl; // output: + 5 / * - 6 3 2 3 return 0; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值