Cod sursa(job #1358452)

Utilizator Aleks10FMI - Petrache Alex Aleks10 Data 24 februarie 2015 17:02:46
Problema Convertor Scor 100
Compilator cpp Status done
Runda rosedu_cdl_2015 Marime 1.73 kb
//
//  main.cpp
//  CDL_convertor
//
//  Created by Alex Petrache on 24/02/15.
//  Copyright (c) 2015 Alex Petrache. All rights reserved.
//

#include <iostream>
#include <fstream>
using namespace std;
int main(int argc, const char * argv[]) {
    ifstream f("convertor.in");
    ofstream g("convertor.out");
//    ifstream f("/Users/alexpetrache/Documents/Programare/Xcode/CDL_convertor/CDL_convertor/convertor.in");
//    ofstream g("/Users/alexpetrache/Documents/Programare/Xcode/CDL_convertor/CDL_convertor/convertor.out");
    
    char c;
    string keys;
    string values;
    
    bool expectWhat=0; //0 key, 1 value
    bool firstTime=true;
    
    while(f>>c){
        if(c=='"'){
            //daca vreau cheie
            if(expectWhat==0){
                do{
                    f.get(c);
                    if(c!='"')
                        keys.push_back(c);
                }while(c!='"');
                keys.push_back(',');
                expectWhat=1;
            }else{
                do{
                    f.get(c);
                    if(c!='"')
                        values.push_back(c);
                }while(c!='"');
                values.push_back(',');
                expectWhat=0;
            }
        }
        else if(c>='0' && c<='9' && expectWhat){
            values.push_back(c);
            do{
                f.get(c);
                if(c>='0' && c<='9')
                    values.push_back(c);
            }while(c>='0' && c<='9');
            values.push_back(',');
            expectWhat=0;
        }
        if(c=='}'){
            if(firstTime)
                g<<keys<<'\n';
            g<<values<<endl;
            keys="";
            values="";
            firstTime=false;
        }
    }
    
    return 0;
}