Mr. Potato Head has been promoted and now is a math professor at the UNAL.

For his first course he is willing to teach hard subjects, so at the moment he is teaching how to add and subtract fractions.

To complete his course the students have to do a long series of exercises, each exercise corresponds to a valid formula containing only additions and subtractions of fractions.

Formally a valid formula is one of the following:

  • A fraction
  • F1+F2F1+F2
  • F1−F2F1−F2
  • (F1)(F1)

where F1F1 and F2F2 are also valid formulas.

Mr. Potato Head knows that the exam would be impossible if fractions are too large or if they are negative, so he decides that for every fraction a/ba/b, 0≤a≤1000≤a≤100 and 0<b≤200<b≤20.

Can you pass the course of Mr. Potato Head?

Input

The input consists of several lines, each line contains a valid formula without spaces.

It is guaranteed that all lines contains valid formulas and the total number of characters in all formulas does not exceed 2∗1052∗105

Output

For each formula output a line with an irreducible fraction a/ba/b, b>0b>0 −− The solution of the corresponding formula

Example

Input
1/2+1/3
1/5-2/10
1/2+(1/2-2/1)
Output
5/6
0/1
-1/1

Note

A fraction is irreducible if its numerator and denominator do not have common divisors greater than 1

题解:只有加减法和括号的运算,可直接从前往后挨个计算,只需判断当前的符号是+或-即可.

#include <bits/stdc++.h>
#define met(a, b) memset(a, b, sizeof(a))
#define ll long long
#define ull unsigned long long
#define rep(i,a,b) for(int i=a;i<=b;i++)
using namespace std;
ll gcd(ll a,ll b){return b==?a:gcd(b,a%b);}
ll lcm(ll a,ll b){return a*b/gcd(a,b);}
typedef pair<ll,ll>P;
const int maxn=;
const double eps=1e-;
const double pi=acos(-);
char s[maxn];
int len;
P ans;
int f,ff;
P cal(P ans,P now)
{
ll son=ans.first*now.second+f*ff*now.first*ans.second;
ll mon=ans.second*now.second;
ll g=gcd(abs(son),abs(mon));
son/=g;
mon/=g;
return P(son,mon);
}
P ok(int pos)
{
ll son=,mon=,l=-;
for(int i=pos-;i>=;i--){
if(s[i]<''||s[i]>''){
l=i;
break;
}
}
for(int i=l+;i<len;i++){
if(s[i]>=''&&s[i]<='')son=son*+s[i]-'';
else break;
}
for(int i=pos+;i<len;i++){
if(s[i]>=''&&s[i]<='')mon=mon*+s[i]-'';
else break;
}
return P(son,mon);
}
int main()
{
while(cin>>s){
stack<int>sta;
len=strlen(s);
ans=P(,);
f=ff=;
for(int i=;i<len;i++){
if(s[i]=='+'){
f=;
}
else if(s[i]=='-'){
f=-;
}
else if(s[i]=='('){
f=;
if(s[i-]=='-'){
ff*=-;
sta.push(-);
}
else{
sta.push();
}
}
else if(s[i]==')'){
int w=sta.top();
ff*=w;
sta.pop();
}
else if(s[i]=='/'){
P now=ok(i);
ans=cal(ans,now);
}
}
cout<<ans.first<<"/"<<ans.second<<endl;
}
return ;
}

最新文章

  1. ios 弹出不同的键盘
  2. 用jdbc访问大段文本数据
  3. java多线程系类:JUC线程池:01之线程池架构
  4. JQurey
  5. XXX esx.problem.syslog.nonpersistent.formatOnHost not found XXX
  6. 20个漂亮 CSS3 按钮效果及优秀的制作教程
  7. CorelDRAW 文件实用工具 CDRTools 2
  8. 问题-关于SizeOf在Delphi7和Delphi2009下结果分别是16/32
  9. lesson5:Condition的原理分析及demo展示
  10. RabbitMQ入门-从HelloWorld开始
  11. libev学习代码
  12. Java 对IP请求进行限流.
  13. 认识一下margin
  14. Elasticsearch 集群和索引健康状态及常见错误说明
  15. Linux的Shell练习--个人笔记
  16. HDU 4311 Meeting point-1(曼哈顿距离最小)
  17. Codeforces 686 D - Kay and Snowflake
  18. Numpy知识(二)
  19. django drf 动态权限配置和动态seriaizer_class配置
  20. Linux快速查看某条命令的版本和存放的位置(ls -l `which mvn`)

热门文章

  1. Java算法练习——回文数
  2. idea的简单使用
  3. C++基础--引用做函数参数
  4. 基于云开发开发 Web 应用(四):引入统计及 Crash 收集
  5. java题求代码,4、现在有如下的一个数组: int oldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5} 要求将以上数组中值为0的项去掉,将不为0的值存入一个新的数组,生成的新数组为: int newArr[]={1,3,4,5,6,6,5,4,7,6,7,5}
  6. day24(024-多线程(上))
  7. DRF源码-serializers
  8. POJ 3258:River Hopscotch 二分的好想法
  9. find_element_by_xpath()的6种方法
  10. clonezilla使用说明