Cod sursa(job #1343852)

Utilizator SebiSebiPirtoaca George Sebastian SebiSebi Data 15 februarie 2015 23:23:34
Problema Convertor Scor 100
Compilator cpp Status done
Runda rosedu_cdl_2015 Marime 2.7 kb
#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
 
#define NMAX 10130
#define DIM 100000
#define INF (1<<30)
 
char buff[DIM];
 
int findfirst(char ch, int poz)
{
    int i,ok;
    do {
        if(poz>=DIM) {
            ok=fread(buff,sizeof(char),DIM,stdin);
            i=0;
        }
        else i=poz;
        while(i<=DIM-1) {
            if(buff[i]==ch)
                return i;
            if(buff[i]=='}')
                return -i-1;
            if(buff[i]==']')
                return -INF;
            i++;
        }
        poz=DIM;
    } while(i>=DIM);
    return -1;
}

void go_next(int &i)
{
	int ok;
	i++;
	if(i>=DIM) {
		ok=fread(buff,sizeof(char),DIM,stdin);
		i=0;
	}
}
int main ()
{
    int i,ok;
    freopen("convertor.in","r",stdin);
    freopen("convertor.out","w",stdout);
    i=DIM;
    while(1) {
        i=findfirst('"',i);
        if(i<0)
            break;
        go_next(i);
        while(buff[i]!='"') {
            fwrite(buff+i,sizeof(char),1,stdout);
            go_next(i);
        }
        printf(",");
		go_next(i);
        while(buff[i]!='"' && isdigit(buff[i])==0) {
            if(buff[i]=='}')
                break;
            go_next(i);
        }
        if(buff[i]=='"') {
            i=findfirst('"',i+1);
            if(i<0)
                break;
            go_next(i);
        }
        else {
            while(isdigit(buff[i])) 
                go_next(i);
        }
        if(i>=DIM) {
            ok=fread(buff,sizeof(char),DIM,stdin);
            i=0;
        }
    }
    printf("\n");
    fseek(stdin,0,SEEK_SET);
    for(i=findfirst('"',DIM);;i=findfirst('"',i)) {
        if(i==-INF)
            break;
        go_next(i);
        while(1) {
            i=findfirst('"',i);
            go_next(i);
            while(1) {
                if(buff[i]=='"' || isdigit(buff[i]))
                    break;
                go_next(i);
            }
            if(buff[i]=='"') {
                go_next(i);
                while(buff[i]!='"') {
                    fwrite(buff+i,sizeof(char),1,stdout);
                    go_next(i);
                }
				go_next(i);
            }
            else {
                while(isdigit(buff[i])) {
                    fwrite(buff+i,sizeof(char),1,stdout);
                    go_next(i);
                }
            }
            printf(",");
            i=findfirst('"',i);
            if(i<0) {
                i=-i-1;
                go_next(i);
                break;
            }
            else go_next(i);
        }
        printf("\n");
    }
    fclose(stdin);
    fclose(stdout);
    return 0;
}