Pagini recente » Cod sursa (job #2689211) | Cod sursa (job #2187293) | Cod sursa (job #549438) | Cod sursa (job #12572) | Cod sursa (job #1674061)
#include <cstdio>
#include <cstring>
using namespace std;
struct numar
{
int a, b;
};
numar num[200];
char semn[200], s[200];
void calc(int x, int y, char semnn)
{
numar aux;
if (semnn == '*')
{
if (num[x].a == 0)
{
aux.a = num[x].b * num[y].a;
aux.b = num[x].b * num[y].b;
}
else
{
aux.a = num[y].b * num[x].a;
aux.b = num[y].b * num[x].b;
}
num[x] = aux;
}
else if (semnn == '-')
{
num[x].a -= num[y].a;
num[x].b -= num[y].b;
}
else if (semnn == '+')
{
num[x].a += num[y].a;
num[x].b += num[y].b;
}
}
int main()
{
freopen("date.in", "r", stdin);
freopen("date.out", "w", stdout);
int a1, a2, b1, b2, a, b, nr, n, top1, top2;
for (register int test = 1; gets(s + 1); ++test)
{
n = strlen(s + 1);
top1 = top2 = 0; nr = 0;
for (register int i = 1; i <= n; ++i)
{
if (s[i] == '=')
{
if (nr != 0){
num[top1 + 1].a = 0;
num[top1 + 1].b = nr;
++top1; nr = 0;
}
/// golim stiva ///
while (top2 > 0)
{
calc(top1 - 1, top1, semn[top2]);
--top2; --top1;
}
a1 = num[1].a;
b1 = num[1].b;
}
else if (s[i] == '(')
{
semn[++top2] == '(';
}
else if (s[i] == ')')
{
if (nr != 0)
{
num[top1 + 1].a = 0;
num[top1 + 1].b = nr;
++top1; nr = 0;
}
while (semn[top2] != '(' && top2 > 0)
{
calc(top1 - 1, top1, semn[top2]);
--top2; --top1;
}
semn[top2] = 0; --top2;
}
else if (s[i] == '+')
{
if (nr != 0)
{
num[top1 + 1].a = 0;
num[top1 + 1].b = nr;
++top1; nr = 0;
}
while (semn[top2] != '(' && top2 > 0)
{
calc(top1 - 1, top1, semn[top2]);
--top2; --top1;
}
semn[++top2] = '+';
}
else if (s[i] == '-')
{
if (nr != 0){
num[top1 + 1].a = 0;
num[top1 + 1].b = nr;
++top1; nr = 0;
}
while (semn[top2] != '(' && top2 > 0)
{
calc(top1 - 1, top1, semn[top2]);
--top2;
}
semn[++top2] = '-';
}
else if (s[i] == '*')
{
if (nr != 0)
{
num[top1 + 1].a = 0;
num[top1 + 1].b = nr;
++top1; nr = 0;
}
semn[++top2] = '*';
}
else if (s[i] == 'x')
{
num[top1 + 1].a = 1;
num[top1 + 1].b = 0;
++top1; nr = 0;
}
else /// cifra
nr = nr * 10 + s[i] - '0';
}
/// am ajuns la a1 * x + b1 = a2 * x + b2 ///
while (top2 > 0)
{
calc(top1 - 1, top1, semn[top2]);
--top2;
}
a2 = num[1].a;
b2 = num[1].b;
a = a1 - a2; b = b1 - b2;
printf("Equation #%d\n", test);
if (a == 0 && b == 0)
printf("Infinitely many solutions.\n");
else if (a == 0 && b != 0)
printf("No solution.");
else printf("x = %lf\n", b / a);
}
return 0;
}