Cod sursa(job #1348503)

Utilizator andy94Andrei Ursache andy94 Data 19 februarie 2015 18:51:56
Problema Convertor Scor 70
Compilator cpp Status done
Runda rosedu_cdl_2015 Marime 2.1 kb
//============================================================================
// Name        : Converter.cpp
// Author      : andrei
// Version     :
// Copyright   : Your copyright notice
// Description :,stdinfoArena problem
//============================================================================

#include <iostream>
#include <string>
#include <string.h>
#include <vector>
#include <sstream>
#include <algorithm>
#include <stdio.h>
#include <iterator>

using namespace std;

string clear(string &val) {

	if (val == "\"\"") {
		return "";
	}
	if (val.find('"', 0) != string::npos) {
		int a = val.find_first_of('"');
		int b = val.find_last_of('"');
		return val.substr(a + 1, b - a - 1);
	}
	val.erase(remove_if(val.begin(), val.end(), ::isspace), val.end());

	if (val.find('?', 0) != string::npos) {
		return val.substr(1, val.size());
	}

	if (val.find('}', 0) != string::npos) {
		return val.substr(0, val.find('}'));
	}

	return val;
}


void setValue(string &slot, string &value) {
	value = slot.substr(slot.find_first_of(":") + 1, slot.size());
	value = clear(value);
}

void setNameValue(string &slot, string &name, string &value) {
	int a = slot.find_first_of('"');
	int b = slot.substr(a + 1, slot.size()).find_first_of("\"");
	name = slot.substr(a + 1, b);

	setValue(slot, value);
}

void convert2() {

	string slot;
	string name;
	string value;

	int size = 0;
	int count = 0;

	vector<string> tobe;

	getline(cin, slot, ',');

	while (slot.find("}") == string::npos) {
		setNameValue(slot, name, value);
		cout << name << ',';
		size++;
		tobe.push_back(value);
		getline(cin, slot, ',');
	}

	setNameValue(slot, name, value);
	cout << name << ',' << '\n';
	size++;
	tobe.push_back(value);
	value = size;
	for (std::vector<std::string>::iterator it = tobe.begin(); it != tobe.end();
			++it) {
		cout << *it << ',';
	}

	while (getline(cin, slot, ',')) {
		setValue(slot, value);
		if (count % size == 0) {
			cout << '\n';
		}
		cout << value << ',';
		count++;
	}

}

int main() {

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

	convert2();

	fclose(stdin);
	fclose(stdout);

	return 0;
}