Cod sursa(job #1361229)

Utilizator roots4Irimia Alexandru Gabriel roots4 Data 25 februarie 2015 20:16:52
Problema Convertor Scor 70
Compilator cpp Status done
Runda rosedu_cdl_2015 Marime 2.52 kb
#include <fstream>
#include<string>
#include<iostream>

#define MAX_LINE_LENGTH 1100
#define MAX_OUTPUT_LINE_LENGTH 1000000

//Problema are doar 70 de puncte dar am aflat foarte tarziu de acest proiect si nu am avut doar o ora si jumatate la dispozitie sa o fac
//Imi cer scuze pentru codul urat

using namespace std;

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

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

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

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

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

void goToValue(){

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

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

}

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

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

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

int main()
{

    char output[MAX_OUTPUT_LINE_LENGTH] , header[MAX_OUTPUT_LINE_LENGTH];
    int lastPosInHeader = 0 , lastPosInOutput = 0;
    bool readFirstElement = false;

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

    while( true ){

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

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

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

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

        posInStr++;
    }


    return 0;
}