关闭

NYOJ 35表达式求值

标签: stringc
487人阅读 评论(1) 收藏 举报
分类:

题目链接: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

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:44753次
    • 积分:1354
    • 等级:
    • 排名:千里之外
    • 原创:74篇
    • 转载:6篇
    • 译文:0篇
    • 评论:25条
    最新评论
    好友博客