表达式求值 nyistoj +BNUoj

来源:互联网 发布:gnu linux 编辑:程序博客网 时间:2024/06/01 08:58

BNUOJ Resistors

#include<stdio.h>#include<iostream>#include<algorithm>#include<string.h>#include<stack>using namespace std;struct date{    long long fz,fm;};long long j;char ch[1000];long long  maxy(long long  a,long long  b){    while(b)    {        long long t=b;        b=a%b;        a=t;    }    return a;}long long minb(long long  a,long long b){    long long  x;    x=a*b;    return x/maxy(a,b);}date yu(date a,date b){    long long a1,b1;    date sum1;    a1=minb(a.fm,b.fm);    sum1.fm=a1;    b1=a1/b.fm;    a1=a1/a.fm;    sum1.fz=a.fz*a1+b.fz*b1;    long long  l=maxy(sum1.fz,sum1.fm);    sum1.fz/=l;    sum1.fm/=l;    return sum1;}date huo(date a,date b){    long long  a1,b1;    date sum1;    a1=minb(a.fz,b.fz);    sum1.fz=a1;    b1=a1/b.fz;    a1=a1/a.fz;    sum1.fm=a.fm*a1+b.fm*b1;    long long  l=maxy(sum1.fz,sum1.fm);    sum1.fz/=l;    sum1.fm/=l;    return sum1;}int main(){    while(gets(ch)!=NULL)    {        date sum,temp,t;         stack<date>my;        j=strlen(ch);        long long i=0;        while(i<j)        {            if(ch[i]==' ')                i++;            if(ch[i]=='(')            {                i++;                temp.fz=0;                temp.fm=0;                my.push(temp);            }             if(ch[i]>='0'&&ch[i]<='9')            {                long long a=0;                while(ch[i]>='0'&&ch[i]<='9')                {                    a*=10;                    a+=ch[i++]-'0';                }                temp.fz=a;                i++;                a=0;                while(ch[i]>='0'&&ch[i]<='9')                {                    a*=10;                    a+=ch[i++]-'0';                }                temp.fm=a;                my.push(temp);            }             if(ch[i]=='&')            {                i++;                temp.fz=-2;                temp.fm=0;                my.push(temp);            }             if(ch[i]=='|')            {                i++;                temp.fz=-1;                temp.fm=0;                my.push(temp);            }             if(ch[i]==')')            {                while(1)                {                    t=my.top();                    my.pop();                    if(my.empty()||my.top().fz==0)                    {                        if(my.empty())                        {                            my.push(t);                            break;                        }                        else                        {                            my.pop();                            my.push(t);                            break;                        }                    }                    temp=my.top();                    my.pop();                    sum=my.top();                    if(temp.fz==-1)                        sum=huo(sum,t);                    else if(temp.fz==-2)                        sum=yu(sum,t);                        my.pop();                    my.push(sum);                }                i++;            }        }        while(1)        {            t=my.top();            my.pop();            if(my.empty()||my.top().fz==0)            {                if(my.empty())                {                    my.push(t);                    break;                }                else                {                    my.pop();                    my.push(t);                    break;                }            }            temp=my.top();            my.pop();            sum=my.top();            if(temp.fz==-1)                sum=huo(sum,t);           if(temp.fz==-2)                sum=yu(sum,t);            my.pop();            my.push(sum);        }         cout<<my.top().fz<<'/'<<my.top().fm<<endl;    }    return 0;}
0 0
原创粉丝点击