Cod sursa(job #1808853)

Utilizator stefanst77Luca Stefan Ioan stefanst77 Data 18 noiembrie 2016 11:44:24
Problema Paduri de multimi disjuncte Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 2.38 kb
#include <bits/stdc++.h>
#define lung 300

using namespace std;
ifstream fin ("ecuatii2.in");
ofstream fout ("ecuatii2.out");

char a[lung];
int n, xs, zs, xd, zd;

int Numar(int &i)
{
    int x=0;
    while (a[i]>='0' && a[i]<='9')
        x=x*10+a[i++]-'0';
    return x;
}

void Calcul()
{
    /*
    ax+b=0
    xs -> a din stanga
    xd -> b din stanga
    zs -> a din dreapta
    zd -> b din dreapta
*/
    int rez1, rez2;
    double sol;
    rez1=xs-xd;
    /// a
    rez2=zd-zs;
    /// b
    if (rez1==0 && rez2==0) fout << "infinit\n";
    else if (rez1==0 && rez2!=0) fout << "imposibil\n";
    else
    {
        sol=(double)rez2/rez1;
        fout << fixed<<setprecision(4)<<sol<<"\n";
    }
}


void Rezolvare()
{
    int i, x, semn=0;
    xs=zs=xd=xs=0;
    for (i=0; a[i] !='='; )
    {
        if (a[i]=='-')
        {
            semn=1;
            i++;
        }
        else if (a[i]=='+')
        {
            semn=0;
            i++;
        }
        else if (a[i]=='x')
        {
            xs++;
            i++;
        }
        else if (a[i]>='0' && a[i]<='9')
        {
            x=Numar(i);
            if (a[i]=='x')
            {
                if (semn==0) xs+=x;
                else xs-=x;
                i++;
            }
            else ///a[i+1]!='x'
            {
                if (semn==0) zs+=x;
                else zs-=x;
            }
        }
    }
    semn=0;
    i++;
    for (i ; a[i] ; )
    {
        if (a[i]=='-')
        {
            semn=1;
            i++;
        }
        else if (a[i]=='+')
        {
            semn=0;
            i++;
        }
        if (a[i]=='x')
        {
            xd++;
            i++;
        }
        if (a[i]>='0' && a[i]<='9')
        {
            x=Numar(i);
            if (a[i]=='x')
            {
                if (semn==0) xd+=x;
                else xd-=x;
                i++;
            }
            else ///a[i+1]!='x'
            {
                if (semn==0) zd+=x;
                else zd-=x;
            }
        }
    }
    Calcul();
    //fout << xs <<"x "<< zs <<"=";
    //fout << xd <<"x "<< zd << "\n";
}

void Citire()
{
    int i;
    fin >> n;
    for (i=1; i<=n; i++)
    {
        fin >> a;
        Rezolvare();
    }
}

int main()
{
    Citire();
    return 0;
}