NYOJ 35表达式求值

题目链接:http://acm.nyist.edu.cn/JudgeOnline/problem.php?pid=35

//将中缀表达式转换成后缀表达式计算结果,将代码中注释的输出语句取消注释 ,可输出后缀表达式。

参考代码;

#include<iostream>
#include<cstdio>
#include<cstring>
#include<stack>
#include<sstream>
#include<algorithm>
#include<cstdlib>
using namespace std;

char pri[7][7] = {  // 判断符号优先级
{'>','>','<','<','<','>','>'},
{'>','>','<','<','<','>','>'},
{'>','>','>','>','<','>','>'},
{'>','>','>','>','<','>','>'},
{'<','<','<','<','<','=',' '},
{'>','>','>','>',' ','>','>'},
{'<','<','<','<','<',' ','='}};

int shine(char ch)  //映射符号到整型
{
	switch(ch)
	{
	case '+':return 0;
	case '-':return 1;
	case '*':return 2;
	case '/':return 3;
	case '(':return 4;
	case ')':return 5;
	case '=':return 6;
	default:return 7;
	}
}
char judge(char a,char b) //判断两个字符的优先级
{
	return pri[shine(a)][shine(b)];
}
char str1[3000];
int pos = 0,p = 0;
int main()
{
	int T;
	scanf("%d",&T);
	while(T--)
	{
		memset(str1,0,sizeof(str1));
        stack<char> ope;//操作符
        stack<double> ope_num;//操作数
        string str;
		cin>>str; getchar();
		double res = 0; pos = 0;
		int n; p = 0;
		while(pos < str.size()-1)
		{
			double midnum = 0;
			if(str[pos] <= '9' && str[pos] >= '0')
			{
				//for(int i = pos;shine(str[i]) == 7;++i) //输出数据
					//printf("%c",str[i]);
				sscanf(&str[pos],"%lf%n",&midnum,&n);  //读取数据	
				sprintf(&str1[p],"%lf",midnum);  //将数据输入到STR1数组中,STR1数组存的是后缀表达式
				p += n;str1[p++] = '@'; // 每个数据后符号后加个特殊字符,方便以后计算后缀表达式
				pos += n; ope_num.push(midnum);
			}
			else 
			{
				char ch = str[pos];  pos++;
				if(ope.empty()) ope.push(ch);
				else 
				{
                    loop:if(!ope.empty())
					switch(judge(ope.top(),ch))  //判断新读取的运算符与栈顶运算符的优先级
					{
					case '>':{                //如果优先级小于等于栈顶元素,弹出栈顶运算符,直至大于栈顶运算符的优先级
						//printf("%c",ope.top());
						sprintf(&str1[p++],"%c",ope.top());
						str1[p++] = '@';
						ope.pop();goto loop;
							 }
					case '<':ope.push(ch);break;  // 将新加入的运算符压入操作符栈内
					case '=':ope.pop();break;
					default:printf("error\n");
					}
				}
				if(ope.empty() && shine(ch) != 5) ope.push(ch); //将新加入的运算符压入操作符栈内
			}
		}
		while(!ope.empty()) //弹出操作符栈内的操作符
		{
			//printf("%c",ope.top());
			str1[p++] = ope.top();
			str1[p++] = '@';
			ope.pop();
		}str1[--p] = '\0';
		while(!ope_num.empty()) ope_num.pop();
		double num1 = 0,num2 = 0;
		char op;
		for(int i = 0;i < p;)   //计算后缀表达式
		{
			if(str1[i] <= '9' && str1[i] >= '0')
			{
				sscanf(&str1[i],"%lf%n",&num1,&n);
				ope_num.push(num1); i += n;
			}
			else if(str1[i] == '@') i++;
			else 
			{
				num2 = ope_num.top(); ope_num.pop();
				num1 = ope_num.top(); ope_num.pop();
				switch(str1[i])
				{
				case '+':num1 += num2;break;
				case '-':num1 -= num2;break;
				case '*':num1 *= num2;break;
				case '/':num1 /= num2;break;
				}
				++i;
				ope_num.push(num1);
			}
		}
		//printf("=\n");
		printf("%.2lf\n",ope_num.top());
	}
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值