Cod sursa(job #1355460)

Utilizator radutomaRADU TOMA radutoma Data 22 februarie 2015 18:52:14
Problema Convertor Scor 100
Compilator c Status done
Runda rosedu_cdl_2015 Marime 4.29 kb
#include <stdio.h>
#include <string.h>
#include<stdlib.h>
int main ()
{
  char *token[80000];
  char aux[1025];
  FILE *citire;
  FILE *afisare;
  char c;
  citire = fopen ("convertor.in", "r");
  afisare = fopen ("convertor.out", "w");
  unsigned int inreg = 0, poz = 0, inreg2 = 0;
  unsigned long int s = 0, i,lungime = 1;

  while (( c= fgetc (citire)) != EOF)
    {
      if (c != '"')
            {
                if (inreg == 1)
                    {
                        aux[poz] = c;
                        poz++;
                    }
                else if ((c <= '9' && c >= '0') || (c == '-'))
                    {
                        inreg2 = 1;
                        aux[poz] = c;
                        poz++;

                    }
                else if (inreg2 == 1)
                    {
                        aux[poz] = '\0';
                        token[s] = strdup (aux);
                        s++;
                        inreg2 = 0;
                    }
            }
      else if (inreg == 0)

            {

                inreg = 1;
                poz = 0;
            }
      else
            {

            if (s % 2 == 0)
                if (s != 0)
                        {
                            aux[poz] = '\0';
                            if (strcmp (token[0], aux) == 0)
                                {
                                    lungime = s;
                                    token[s] = strdup (aux);
                                    for (i = 0; i < s; i = i + 2)
                                        fprintf (afisare, "%s,", token[i]);
                                    fprintf (afisare, "\n");
                                    for (i = 1; i < s; i = i + 2)
                                        fprintf (afisare, "%s,", token[i]);
                                    fprintf (afisare, "\n");
                                    s++;
                                    poz = 0;
                                    inreg = 0;
                                    break;
                                }
                        }
            aux[poz] = '\0';
            token[s] = strdup (aux);
            inreg = 0;
            s++;
            poz = 0;
            }
    }

  while ((c = fgetc (citire)) != EOF)
            {
                if (c != '"')
                    {
                        if (inreg == 1)
                            {
                                if (s % 2 == 1)
                                    {
                                        fprintf (afisare, "%c", c);
                                            }
                            }
                        else if ((c <= '9' && c >= '0') || (c == '-'))

                        {
                            inreg2 = 1;
                            fprintf (afisare, "%c", c);
                        }
                        else if (inreg2 == 1)
                            {
                            if (s % 2 == 1)
                                    fprintf (afisare, ",");
                            if ((s + 1) % lungime == 0)
                                    fprintf (afisare, "\n");
                            s++;
                            inreg2 = 0;
                            }

                    }
                else
                    {
                            if (inreg == 0)
                                    inreg = 1;

                            else
                                {
                                    inreg = 0;
                                    if (s % 2 == 1)
                                            fprintf (afisare, ",");
                                    if ((s + 1) % lungime == 0)
                                            fprintf (afisare, "\n");
                                    s++;
                                }
                    }

            }
  if (lungime == 1)
        {
            for (i = 0; i < s; i = i + 2)
                    fprintf (afisare, "%s,", token[i]);
            fprintf (afisare, "\n");
            for (i = 1; i < s; i = i + 2)
                    fprintf (afisare, "%s,", token[i]);
        }

  fclose (afisare);
  fclose(citire);
  return 0;

}