Cod sursa(job #1343526)

Utilizator TrixerAdrian Dinu Trixer Data 15 februarie 2015 16:15:28
Problema Convertor Scor 100
Compilator cpp Status done
Runda rosedu_cdl_2015 Marime 2.08 kb
#include <iostream>
#include <fstream>
#include <string.h>

using namespace std;

void goto_next(char c, int &ok, char stop)
{
    char x;

    while (x = getc(stdin))
    {
        if (x == stop) {ok = 0; break;}
        if (x == c) break;
    }
}

void read_value1(char value[], char stop)
{
    char x;
    int i = 0;

    while (x = getc(stdin))
    {
        if (x == stop) break;
        value[i] = x;
        i++;
    }
    value[i] = '\0';
}

void read_value2(char value[], char stop, int &ok)
{
    char x, aux[1024] = {0};
    int len;

    while (x = getc(stdin))
    {
        if (x == stop)
        {
            read_value1(value, stop);
            printf("%s,", value);
            break;
        }
        else if (x != ' ' && x != '\n')
        {
            scanf("%s", aux);

            len = strlen(aux);
            if (aux[len-2] == '}') aux[len-2] = '\0';
            else if (aux[len-1] == ',' || aux[len-1] == '}') aux[len-1] = '\0';
            if (aux[len-1] == ']')
            {
                aux[len-2] = '\0';
                printf("%c%s,", x, aux);
                ok = 0;
                break;
            }

            printf("%c%s,", x, aux);
            break;
        }
    }
}

int main()
{
    int keys = 0, ok = 1, i;
    char value[1024] = {0};

    freopen("convertor.in", "r", stdin);
    freopen("convertor.out", "w", stdout);

    // get number of keys and first line
    while (true)
    {
        goto_next('"', ok, '}');
        if (!ok) break;

        keys++;
        read_value1(value, '"');
        printf("%s,", value);

        goto_next(',', ok, '}');
        if (!ok) break;
    }
    printf("\n");

    // get values
    freopen("convertor.in", "r", stdin);

    ok = 1;
    while (true)
    {
        for (i = 0; i < keys; i++)
        {
            goto_next(':', ok, ']');
            if (!ok) break;

            read_value2(value, '"', ok);
            if (!ok) break;
        }
        if (!ok) break;
        printf("\n");
    }

    return 0;
}