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. 利用CMD 創建新文件的機種方法

    用 CMD 創建新文件 説明一下: 是在Windows的 CMD命令行模式下,或者在PowerShell命令行模式下創建新文件的機種方法. 創建空文件 cd.>a.txt cd.表示改变当前目录 ...

  2. 阿里云ECS屏蔽25端口,官方建议使用465 SSL端口发送邮件

    阿里云ECS  VPC网络,搭建了zabbix,想通过三方邮件系统发送邮件,本机开虚拟机测试发邮件一切正常,到阿里ECS的时候邮件各种发不出去,到处找原因,最后度娘告诉了我真想,原来阿里把25端口屏蔽 ...

  3. Vim常用快捷键--正常的学习曲线

    vim可能对于初学者不太友好,学习曲线有点陡,特此整理了较为平滑的学习曲线的学习快捷键的方式,包含最常用的快捷键,让初学者领悟vim的优点,想要进阶学习请查找其它更好的教程 正常模式:可以使用快捷键命 ...

  4. NumPy 学习笔记(四)

    NumPy 算术函数: 1.numpy.reciprocal(arr) 返回参数逐个元素的倒数 2.numpy.power(one, two) 将第一个输入数组中的元素作为底数,计算它与第二个输入数组 ...

  5. 00 大王警语--be_a_new_gentleman

    大王博客:https://www.cnblogs.com/alex3714/ # 表面层次# 1,着装特体(服饰的牌子中高端)# 2,每天洗澡# 3,适当用香水# 4,女士优先# 5,不随地吐痰.不乱 ...

  6. SHA256兼容性

    SHA-2是一个加密哈希(Cryptographic Hash)函数的一个集合,包括SHA-224,SHA256和SHA-512.在SHA-256中的256代表哈希(Hash)输出或者摘要的位尺寸(即 ...

  7. Linear and Logistic Regression in TensorFlow

    Linear and Logistic Regression in TensorFlow Graphs and sessions TF Ops: constants, variables, funct ...

  8. MongoDB增加用户、删除用户、修改用户读写权限及只读权限(注:转载于http://www.2cto.com/database/201203/125025.html)

    MongoDB  增加用户 删除用户  修改用户  读写权限 只读权限,   MongoDB用户权限分配的操作是针对某个库来说的.--这句话很重要.   1. 进入ljc 数据库:       use ...

  9. Codeforces Round #232 (Div. 2) C

    C. On Number of Decompositions into Multipliers time limit per test 1 second memory limit per test 2 ...

  10. poj 3164 最小树形图模板!!!

    /* tle十几次,最后发现当i从1开始时,给环赋值时要注意啊! 最小树形图 */ #include<stdio.h> #include<string.h> #include& ...