Cod sursa(job #1357766)

Utilizator edigiorgiEdi Georgi edigiorgi Data 24 februarie 2015 08:28:27
Problema Convertor Scor 100
Compilator cpp Status done
Runda rosedu_cdl_2015 Marime 2.3 kb
#include <iostream>
#include <fstream>
#include <cmath>
#include <string>
#include<stdio.h>

using namespace std;

int main()
{
	FILE *g;
	g = fopen("convertor.out", "w");
	FILE *f;
	f = fopen("convertor.in", "r");
	string s;
	char c;
	int cont2 = 0;
	c = fgetc(f);
	string chei;
	while (c != '}')
	{
		if (c == '"')
		{
			cont2 = (cont2 + 1) % 2;
			if (cont2)
			{
				char x;
				x = fgetc(f);
				while (x != '"')
				{
					chei += x;
//					fputc(x, g);
					x = fgetc(f);
				} 
//				fputc(',', g);
				chei += ',';
			}
			else
			{
				char x;
				x = fgetc(f);
				while (x != '"')
				{
					s += x;
					x = fgetc(f);
				}
				s += ',';

			}

		}
		else if (((c - '0') < 10) && (c - '0' >= 0))
		{
			cont2 = (cont2 + 1) % 2;
			s += c;
			char s3[1000];
			fscanf(f, "%s", s3);
			string s2 (s3);
			int e = 0;
			if (s2.find("}") != string::npos)
				e = 1;
			while  ((((s2.back() - '0') > 9) || (s2.back() - '0' < 0)))
			{
				s2.pop_back();
				if (s2.size() == 0)
					break;
			}
			s += s2;
			s += ',';
			if (e)
				break;
		}
		c = fgetc(f);
	}

	fputs(chei.c_str(), g);
	fputc('\n', g);

	fputs(s.c_str(), g);
	fputc('\n', g);
	c = fgetc(f);
	while (c != EOF)
	{
		if (c == '{')
		{
			s.clear();
			cont2 = 0;
			while (c != '}')
			{
				if (c == '"')
				{
					cont2 = (cont2 + 1) % 2;
					if (cont2 == 0)
					{
						char x;
						x = fgetc(f);
						while (x != '"')
						{
							s += x;
							x = fgetc(f);
						}
						s += ',';

					}
					else
					{
						char x;
						do
						{
							x = fgetc(f);
						} while (x != '"');
					}

				}
				else if (((c - '0') < 10) && (c - '0' >= 0))
				{
					cont2 = (cont2 + 1) % 2;
					if (cont2 == 0)
					{

						s += c;
						char s3[1000];
						fscanf(f, "%s", s3);
						string s2 (s3);
						int e = 0;
						if (s2.find("}") != string::npos)
							e = 1;
						while (((s2.back() - '0') > 9) || (s2.back() - '0' < 0))
						{
							s2.pop_back();
							if (s2.size() == 0)
								break;
						}
						s += s2;
						s += ',';
						if (e)
							break;

					}

				}
				c = fgetc(f);
			}
			fputs(s.c_str(), g);
			fputc('\n', g);
		}
		c = fgetc(f);
	}

	fclose(f);
	fclose(g);
	return 0;
}