http://poj.org/problem?id=1400

题意:给出一个表达式可能含有多余的括号,去掉多余的括号,输出它的最简形式。

思路:先将表达式转化成后缀式,因为后缀式不含括号,然后再转化成中缀式,并根据运算符的优先级添加括号。

 #include <stdio.h>
#include <string.h>
#include <string>
#include <stack>
#include <iostream>
using namespace std;
char ss[];
int f(char ch)
{
if(ch=='(')
return ;
else if(ch=='+'||ch=='-')
return ;
else if (ch=='*'||ch=='/')
return ;
}
void change1(string s)
{
int l = ;
stack<char>p;
while(!p.empty()) p.pop();
for (int i = ; i < s.size(); i++)
{
if(s[i]>='a'&&s[i]<='z')
ss[l++]=s[i];
else
{
if (s[i]=='(')
p.push(s[i]);
else if (s[i]==')')
{
while(!p.empty())
{
char ch = p.top();
p.pop();
if(ch=='(')
break;
ss[l++] = ch;
}
}
else
{
while(!p.empty()&&f(p.top())>=f(s[i]))
{
char ch = p.top();
p.pop();
ss[l++] = ch;
}
p.push(s[i]);
} }
}
}
while(!p.empty())
{
ss[l++] = p.top();
p.pop();
}
ss[l++]='\0';
}
void change2(char a[])
{
string s1, s2, s3, fl, fs;
string s[];
char f[];
int top = ;
s1 = a;
for(int i = ; i < (int)s1.length(); i++)
{
if(s1[i]>='a' && s1[i]<='z')
{
fl = s1[i];
top++;
s[top] = s1[i];
}
else if(s1[i]=='+')
{
s2 = s[top];
top--;
s3 = s[top];
top--;
top++;
s[top] = s3+'+'+s2;
f[top] = '+';
}
else if(s1[i]=='-')
{
s2 = s[top];
top--;
s3 = s[top];
top--;
if(f[top+]=='+' || f[top+]=='-')
{
if(s2.length()>) s2 = '('+s2+')';
}
top++;
s[top] = s3+'-'+s2;
f[top] = '-';
}
else if(s1[i]=='*')
{
s2 = s[top];
top--;
s3 = s[top];
top--;
if(f[top+]=='+' || f[top+]=='-')
{
if(s3.length()>) s3 = '('+s3+')';
}
if(f[top+]=='+' || f[top+]=='-')
{
if(s2.length()>) s2 = '('+s2+')';
}
top++;
s[top] = s3+'*'+s2;
f[top] = '*';
}
else if(s1[i]=='/')
{
s2 = s[top];
top--;
s3 = s[top];
top--;
if(f[top+]=='+' || f[top+]=='-')
{
if(s3.length()>) s3 = '('+s3+')';
}
if(f[top+]=='+' || f[top+]=='-' || f[top+]=='*' || f[top+]=='/')
{
if(s2.length()>) s2='('+s2+')';
}
top++;
s[top] = s3+'/'+s2;
f[top] = '/';
}
}
cout<<s[]<<endl;
}
int main()
{ //freopen("expr.in", "r", stdin);
//freopen("ss.out", "w", stdout);
int t;
string s;
cin>>t;
while(t--)
{
cin>>s;
change1(s);
change2(ss);
}
return ;
}

Complicated Expressions(表达式转换)的更多相关文章

  1. lambda表达式转换sql

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; usin ...

  2. 计算器类(C++&JAVA——表达式转换、运算、模板公式)

    运行: (a+b)*c 后缀表达式:ab+c* 赋值: Enter the a : 10 Enter the b : 3 Enter the c : 5 结果为:65 代码是我从的逻辑判断系统改过来的 ...

  3. ZH奶酪:Python 中缀表达式转换后缀表达式

    实现一个可以处理加减乘数运算的中缀表达式转换后缀表达式的程序: 一个输入中缀表达式inOrder 一个输出池pool 一个缓存栈stack 从前至后逐字读取inOrder 首先看一下不包含括号的: ( ...

  4. SDUT-2132_数据结构实验之栈与队列二:一般算术表达式转换成后缀式

    数据结构实验之栈与队列二:一般算术表达式转换成后缀式 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 对于一个基于二元运 ...

  5. Linq to Entity经验:表达式转换

    http://www.cnblogs.com/ASPNET2008/archive/2012/10/27/2742434.html 最近一年的项目,我主要负责一些小型项目(就是指企业内部的小项目),在 ...

  6. Angularjs –– Expressions(表达式)

    点击查看AngularJS系列目录 转载请注明出处:http://www.cnblogs.com/leosx/ Angular的表达式 Angular的表达式和JavaScript代码很像,不过通常A ...

  7. 3-06. 表达式转换(25)(中缀表达式转后缀表达式ZJU_PAT)

    题目链接:http://pat.zju.edu.cn/contests/ds/3-06 算术表达式有前缀表示法.中缀表示法和后缀表示法等形式. 日常使用的算术表达式是採用中缀表示法,即二元运算符位于两 ...

  8. PTA 7-20 表达式转换

    转自:https://www.cnblogs.com/yuxiaoba/p/8399934.html 算术表达式有前缀表示法.中缀表示法和后缀表示法等形式.日常使用的算术表达式是采用中缀表示法,即二元 ...

  9. uva 11234 Expressions 表达式 建树+BFS层次遍历

    题目给出一个后缀表达式,让你求从下往上的层次遍历. 思路:结构体建树,然后用数组进行BFS进行层次遍历,最后把数组倒着输出就行了. uva过了,poj老是超时,郁闷. 代码: #include < ...

随机推荐

  1. DBDA

    <?php class DBDA{ public $host = "localhost"; //服务器地址 public $uid = "root"; / ...

  2. Word 格式优化

    Word 格式优化. Word 支持 VBA 意味着,可以编程实现自己想要的格式拓展. Word 代码布局

  3. Linux---shell基本指令

    1. 显示当前目录 pwd wangzhengchao@ubuntu:~$ cd /home/wangzhengchao/Desktop/ wangzhengchao@ubuntu:~/Desktop ...

  4. db2记录去重

    --查出二码,归档日期,借据号重复的数据的条数 select default_index_item_id,record_date,index_yxdk_dkjjh,min(sys_org_id),ma ...

  5. Spring MVC学习总结(12)——Spring MVC集成Swagger时报错{"schemaValidationMessages":[

    在springmvc结合swagger的时候,如果将项目部署到服务器上就会出现问题出现下面的图标: 点开会报错误信息: schemaValidationMessages":[{"l ...

  6. C#中的定制特性(Attributes)

    C#中的定制特性(Attributes) 介绍 Attributes是一种新的描述信息,我们既可以使用attributes来定义设计期信息(例如:帮助文件.文档的URL),还可能用attributes ...

  7. hdu 5037 模拟网选1006

    /* 模拟 实例: 33 1 10 5 5 2 10 3 3 6 1 3 2 1 1 4 2 1 1 5 2 1 1 6 2 1 1 7 2 1 5 20 8 1 2 3 4 5 1 20 8 5 0 ...

  8. 【BZOJ2342】双倍回文(manacher,并查集)

    题意: 思路:From http://blog.sina.com.cn/s/blog_8d5d2f04010196bh.html 首先我可以看出: (1)我们找到的串的本身也是一个回文串(显然) (2 ...

  9. codevs4343 找回密码

    题目描述 Description jrMz 很喜欢动漫<叛逆的鲁鲁修>(额= =不知道是不是因为他盯上了动画片里的 MM),他准备以一种神奇的方式降临<叛逆的鲁鲁修>世界,所以 ...

  10. kendo grid 点击新增没有反映

    在datasource中缺少 editable: "inline",这一行