Cod sursa(job #1343357)

Utilizator TrixerAdrian Dinu Trixer Data 15 februarie 2015 13:30:00
Problema Convertor Scor 90
Compilator cpp Status done
Runda rosedu_cdl_2015 Marime 2.1 kb
#include <iostream>
#include <fstream>
#include <string.h>
#include <stdlib.h>

using namespace std;

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

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

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

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

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

    i = 0;

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

    value[i] = '\0';
}

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

    i = 0;

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

            len = strlen(aux);
            if (aux[len-2] == '}') aux[len-2] = '\0';
            else if (aux[len-1] == ',') aux[len-1] = '\0';

            strcat(value, aux);
            break;
        }
    }
}

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

    keys    = 0;
    ok      = 1;

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

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

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

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

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

    ok = 1;

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

            value[1] = '\0';
            read_value2(value, '"');

            printf("%s,", value);
        }
        if (!ok) break;
        printf("\n");
    }

    return 0;
}