Cod sursa(job #1361201)

Utilizator roots4Irimia Alexandru Gabriel roots4 Data 25 februarie 2015 20:08:47
Problema Convertor Scor 40
Compilator cpp Status done
Runda rosedu_cdl_2015 Marime 2.28 kb
#include <fstream>
#include<string>
#include<iostream>

#define MAX_LINE_LENGHT 1100

using namespace std;

ifstream f("convertor.in");
ofstream g("convertor.out");

char line[MAX_LINE_LENGHT];
char *posInStr = line;

bool isNumber(){
    if( '0' <= (*posInStr) && (*posInStr) <= '9' )
        return true;
    return false;
}

void readKey( char *header , int &endHeader ){

    while( (*posInStr) != '"' ){
        header[endHeader++] = (*posInStr);
        posInStr++;
    }
    header[endHeader++] = ',';
}

void goToValue(){

    while( (*posInStr) != '"' )
        posInStr++;
    posInStr++;

    while( (*posInStr) != '"' && (!isNumber()) )
        posInStr++;

}

void readValue( char *output , int &endOutput ){
    if( isNumber() ){
        while( isNumber() ){
            output[endOutput++] = (*posInStr);
            posInStr++;
        }
    }
    if( (*posInStr) == '"' ){
        posInStr++;
        while( (*posInStr) != '"' ){
            output[endOutput++] = (*posInStr);
            posInStr++;
        }
    }
    output[endOutput++] = ',';
}

void print( char *str , int &last ){

    str[last] = '\0';
    g<<str<<"\n";
    last = 0;
}

int main()
{

    char output[MAX_LINE_LENGHT] , header[MAX_LINE_LENGHT];
    int endHeader = 0 , endOutput = 0;
    bool readFirstElement = false;

    f.getline( line , MAX_LINE_LENGHT );
    posInStr = line;

    while( true ){

        if( (*posInStr) == '\0' ){
            f.getline( line , MAX_LINE_LENGHT );
            posInStr = line;
        }

        if( (*posInStr) == ']' ){
            if( !readFirstElement ){
                print( header , endHeader );
            }
            print( output , endOutput );
            break;
        }

        if( (*posInStr) == '}' ){
            if( !readFirstElement ){
                print( header , endHeader );
            }
            readFirstElement = true;
            print( output , endOutput );
        }

        if( (*posInStr) == '"' ){
                posInStr++;
            if( !readFirstElement ){
                readKey( header , endHeader );
            }
            goToValue();
            readValue( output , endOutput );
        }

        posInStr++;
    }


    return 0;
}