Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
3.2 kB
2
Indexable
Never
#include<bits/stdc++.h>
using namespace std;

struct phanso{
    long long tu,mau;
};

vector<phanso>ps,pso;
vector<char>dau,d;

string s;
long long n=s.size();

phanso res;

phanso rutgon(phanso a)
{
    phanso c;
    long tup=a.tu,maup=a.mau;
    long long ucln=__gcd(tup,maup);
    tup=tup/ucln;
    maup=maup/ucln;
    c.tu=tup;
    c.mau=maup;
    return c;
}

phanso nhan(phanso a,phanso b)
{
    phanso c;
    long long tua=a.tu,maua=a.mau,tub=b.tu,maub=b.mau;
    long long tun=tua*tub,maun=maua*maub;
    c.tu=tun;
    c.mau=maun;
    return c;
}

phanso chia(phanso a,phanso b)
{
    phanso c;
    long long tua=a.tu,maua=a.mau,tub=b.tu,maub=b.mau;
    long long tun=tua*maub,maun=maua*tub;
    c.tu=tun;
    c.mau=maun;
    return c;
}

phanso cong(phanso a,phanso b)
{
    phanso c;
    long long tua=a.tu,maua=a.mau,tub=b.tu,maub=b.mau;
    long long tu1=tua*maub,tu2=tub*maua,maun=maua*maub;
    c.tu=tu1+tu2;
    c.mau=maun;
    rutgon(c);
    return c;
}

phanso tru(phanso a,phanso b)
{
    phanso c;
    long long tua=a.tu,maua=a.mau,tub=b.tu,maub=b.mau;
    long long tu1=tua*maub,tu2=tub*maua,maun=maua*maub;
    c.tu=tu1-tu2;
    c.mau=maun;
    rutgon(c);
    return c;
}

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    long long index=0,tup=0,maup=0;
    string t="";
    cin>>s;
    for(int i=0;i<s.size();i++)
    {
        if(s[i]>='0'&&s[i]<='9') t+=s[i];
        if(s[i]=='+'||s[i]=='-'||s[i]=='*'||s[i]==':') dau.push_back(s[i]);
        if(s[i]=='+'||s[i]=='-') d.push_back(s[i]);
        if(s[i]=='/')
        {
            tup=stoll(t);
            t="";
        }
        if(s[i]=='+'||s[i]=='-'||s[i]=='*'||s[i]==':'||i==s.size()-1)
        {
            maup=stoll(t);
            ps.push_back(phanso());
            ps[index].tu=tup;
            ps[index].mau=maup;
            index++;
            t="";
        }
    }
    for(int i=0;i<ps.size();i++)
    {
        ps[i]=rutgon(ps[i]);
    }
    long long id=1;
    pso.push_back(phanso());
    pso[0].tu=ps[0].tu;
    pso[0].mau=ps[0].mau;
    for(int i=1;i<dau.size();i++)
    {
        if(dau[i]=='-')
        {
            ps[i+1].tu=-ps[i+1].tu;
        }
        if((dau[i]=='+'||dau[i]=='-')&&(dau[i-1]=='+'||dau[i-1]=='-'))
        {
            pso.push_back(phanso());
            pso[id].tu=ps[i].tu;
            pso[id].mau=ps[i].mau;
            id++;
        }
        if(dau[i]=='*')
        {
            ps[i]=nhan(ps[i],ps[i+1]);
            rutgon(ps[i]);
            pso.push_back(phanso());
            pso[id].tu=ps[i].tu;
            pso[id].mau=ps[i].mau;
            id++;
        }
        if(dau[i]==':')
        {
            ps[i]=chia(ps[i],ps[i+1]);
            rutgon(ps[i]);
            pso.push_back(phanso());
            pso[id].tu=ps[i].tu;
            pso[id].mau=ps[i].mau;
            id++;
        }
    }
    for(int i=0;i<pso.size();i++)
    {
        cout<<pso[i].tu<<" "<<pso[i].mau<<" "<<d[i]<<"\n";
    }
    phanso res;
    res.tu=pso[0].tu;
    res.mau=pso[0].mau;
    for(int i=1;i<pso.size();i++)
    {
        res=cong(res,pso[i]);
        cout<<res.tu<<"/"<<res.mau<<"\n";
    }
    cout<<res.tu<<"/"<<res.mau<<"\n";
    return 0;
}