Cod sursa(job #1674061)

Utilizator ionutpop118Pop Ioan Cristian ionutpop118 Data 4 aprilie 2016 12:41:11
Problema A+B Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 4.03 kb
#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;
}