F - Fraction Formula Gym - 102307F

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
  • F1F2F1−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, 0a1000≤a≤100 and 0<b200<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 21052∗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==0?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=200010;
const double eps=1e-10;
const double pi=acos(-1);
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=0,mon=0,l=-1;
  for(int i=pos-1;i>=0;i--){
    if(s[i]<'0'||s[i]>'9'){
      l=i;
      break;
    }
  } 
  for(int i=l+1;i<len;i++){
    if(s[i]>='0'&&s[i]<='9')son=son*10+s[i]-'0';
    else break;
  }
  for(int i=pos+1;i<len;i++){
    if(s[i]>='0'&&s[i]<='9')mon=mon*10+s[i]-'0';
    else break;
  }
  return P(son,mon);
}
int main()
{
  while(cin>>s){
    stack<int>sta;
    len=strlen(s);
    ans=P(0,1);
    f=ff=1;
    for(int i=0;i<len;i++){
      if(s[i]=='+'){
        f=1;
      }
      else if(s[i]=='-'){
        f=-1;
      }
      else if(s[i]=='('){
        f=1;
        if(s[i-1]=='-'){
          ff*=-1;
          sta.push(-1);
        }
        else{
          sta.push(1);
        }
      }
      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 0;
}

 

转载于:https://www.cnblogs.com/cherish-lin/p/11516917.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值