Cod sursa(job #1357538)

Utilizator vld7Campeanu Vlad vld7 Data 23 februarie 2015 22:58:01
Problema Convertor Scor 100
Compilator cpp Status done
Runda rosedu_cdl_2015 Marime 1.39 kb
#include <fstream>
#include <vector>

using namespace std;

ifstream fin ("convertor.in");
ofstream fout ("convertor.out");

vector <string> Values;

int main() {
	int op = 0;
	/*
	/ - 0, citesc cheie
	/ - 1, citesc valoare
	/
	*/
	
	bool keysFirst = true;			// tratez cazul cand afisez prima linie
	char c;
	
	fin.get (c);
	do {
		if (c == '"') {
			if (op == 0) {
				// trebuie sa citesc o cheie
				
				string key;
				fin.get (c);
				while (c != '"') {
					key.push_back (c);
					fin.get (c);
				}
				
				if (keysFirst)
					fout << key << ",";
				
				op = 1;		// trebuie sa citesc valoare acum
				
				fin.get (c);
			} else {
				// trebuie sa citesc o valoare
				
				string value;
				fin.get (c);
				while (c != '"') {
					value.push_back (c);
					fin.get (c);
				}
				
				Values.push_back (value);
				op = 0;		// trebuie sa citesc cheie
				
				fin.get (c);
			}
		} else if (c >= '0' && c <= '9') {
			string value;
			while (c >= '0' && c <= '9') {
				value.push_back (c);
				fin.get (c);
			}
			
			Values.push_back (value);
			op = 0;
		} else {
			if (c == '}') {
				if (keysFirst)
					fout << '\n';
				keysFirst = false;
				for (int i = 0; i < Values.size(); i++)
					fout << Values[i] << ',';
				fout << '\n';
				
				Values.clear();
			}
			fin.get (c);
		}
	} while (c != ']');
	
	return 0;
}