Pagini recente » Cod sursa (job #930116) | Cod sursa (job #2831038) | Cod sursa (job #1867686) | Cod sursa (job #2870965) | Cod sursa (job #1808853)
#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;
}