Cod sursa(job #1361836)

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

string firstFunc ();

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

	ofstream file2 ("conv.txt");
	file2 << str;

	file2.close();
	i = 0;

	string st1[MAX], st2[MAX];

	ifstream file1 ("conv.txt");
	while (!file1.eof()) {
		getline (file1, st1[i]);
		getline (file1, st2[i]);
		i++;
	}
	n = i;

//HERE!
	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][2])
			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);
	}

	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] << ",";
	}
}

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;
}