Cod sursa(job #1363120)

Utilizator MetalbrainAndreea Dobroteanu Metalbrain Data 26 februarie 2015 18:45:29
Problema Convertor Scor 0
Compilator cpp Status done
Runda rosedu_cdl_2015 Marime 2.31 kb
#include <iostream>
#include <fstream>
#include <algorithm>
#include <string.h>
#include <stdio.h>
#include <cstddef>
#include <sstream>
#define MAX 500
using namespace std;

string firstFunc ();

int main () {
	int i, n;
	string str = firstFunc();

	string st1[MAX], st2[MAX];
	istringstream iss(str);

	int count1 = 0, count2 = 0;
	while (getline(iss, st1[i])) {		
		i++;
		getline(iss, st2[i-1]);	
	}
	n = i;

	string st3[MAX];

	for (i = 0; i < n; i++) {
		st3[i] = st1[i];
	}

	for (i = 0; i < n; i++) {
		st1[i].erase(remove(st1[i].begin(), st1[i].end(), ' '), st1[i].end());
	}

	string firstLine[MAX] = {};


	int count = 1	;

	firstLine[0] = st1[0];
	for (i = 1; i < n; i++) {
		if (st1[0] == st1[i])
			break;
		else {
			firstLine[count] = st1[i];
			count++;
		}
	}

	for (i = 0; i < count; i++) {
		st3[i].erase(0,0);
	}

	for (i = 0; i < n; i++) {
		if (isspace(st2[i][0]))
			st2[i].erase(0,1);
		if (isspace(st2[i][0]) && st2[i][1])
			st2[i].erase(0, 3);
	}

	for (i = 0; i < count; i++) {
		if (isspace(st3[i][0]))
			st3[i].erase(0, 1);
	}

	for (i = 0; i < count; i++) {
		if (isspace(st3[i][0]))
			st3[i].erase(0, 1);
		if (isspace(st3[i][st3[i].size()-1]))
			st3[i].erase(st3[i].size()-1, st3[i].size()-1);
	}

	for (i = 0; i < n; i++) {
		if (isspace(st2[i][st2[i].size()-1]))
			st2[i].erase(st2[i].size()-1, st2[i].size()-1);
	}

	for (i = 0; i < n; i++) {
		if (isspace(st2[i][st2[i].size()-1]))
			st2[i].erase(st2[i].size()-1, st2[i].size()-1);
	}

	ofstream finFile ("convertor.out");

	for (i = 0; i < count; i++) {
		finFile << st3[i] << ",";
	}

	for (i = 0; i < n; i++) {
		if (i % count == 0)
			finFile << endl;
		finFile << st2[i] << ",";
	}

	finFile.close();
}

string firstFunc() {
	FILE *file = fopen ("convertor.in", "r");

	char line[MAX] = {};
	char text[MAX][MAX];
	int i = 0;
	char first[MAX][MAX] = {};

	while (fgets (text[i], MAX, file) != NULL) {
		i++;
	}
	int n = i;

	fclose (file);

	for (i = 0; i < n; i++)
		strcat (line, text[i]);

	i = 0;
	char *p = strtok (line, "\"[{]}\n");
	while (p) {
		strcat(first[i], p);
		i++;
		p = strtok (NULL, "\"[{]}\n");
	}
	n = i;
	char txt[MAX];
	for (i = 0; i < n; i++)
		strcat (txt, first[i]);

	string str(txt);

	size_t found = str.find_first_of (":,");
	while (found != string::npos) {
		str[found] = '\n';
		found = str.find_first_of (":,");
	}

	return str;
}