Cod sursa(job #1348270)

Utilizator andy94Andrei Ursache andy94 Data 19 februarie 2015 16:50:48
Problema Convertor Scor 40
Compilator cpp Status done
Runda rosedu_cdl_2015 Marime 2.75 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>

using namespace std;

std::vector<std::string> &split(const std::string &s, char delim,
		std::vector<std::string> &elems) {
	std::stringstream ss(s);
	std::string item;
	while (std::getline(ss, item, delim)) {
		elems.push_back(item);
	}
	return elems;
}

std::vector<std::string> split(const std::string &s, char delim) {
	std::vector<std::string> elems;
	split(s, delim, elems);
	return elems;
}

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 printNames(string line, int &size) {

	std::vector<std::string> v = split(line, ',');
	std::vector<std::string> p;

	for (std::vector<std::string>::iterator it = v.begin(); it != v.end();
			++it) {
		p = split(*it, ':');
		if (p.size() >= 2) {
			cout<<clear(p[0])<<',';
			//fputs(clear(p[0]).c_str(), stdout);
			//fputs(",", stdout);
			size++;
		}
	}
}

void printValues(string line, int &size, int &count) {

	std::vector<std::string> v = split(line, ',');
	std::vector<std::string> p;

	for (std::vector<std::string>::iterator it = v.begin(); it != v.end();
			++it) {



		p = split(*it, ':');

		if (p.size() >= 2) {
			if (count % size == 0) {
				//fputs("\n", stdout);
				cout<<'\n';
			}
			cout<<clear(p[1])<<',';

			//fputs(clear(p[1]).c_str(), stdout);
			//fputs(",", stdout);
			count++;

		}

	}
}

void convert() {

	int size = 0, count = 0;
	char line[1024];

	//fread(line, sizeof(char), 1024, stdin);
	fgets(line, 1024, stdin);
	while (line != NULL && strchr(line, '}') == NULL) {
		printNames(line, size);
//		fread(line, sizeof(char), 1024, stdin);
		fgets(line, 1024, stdin);
	}
	printNames(string(line).substr(0, string(line).find("}")), size);

	fseek(stdin, 0, SEEK_SET);

	while (!feof(stdin)) {
//		fread(line, sizeof(char), 1024, stdin);
		fgets(line, 1024, stdin);
		printValues(line, size, count);

	}

}

int main() {

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

	convert();

	fclose(stdin);
	fclose(stdout);

	return 0;
}