Advertisement
Guest User

Untitled

a guest
Jun 22nd, 2013
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.22 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <ctype.h>
  4. #include <assert.h>
  5. int conta(char c, char *s){
  6.     int num_vezes=0;
  7.     while(*s!='\0'){
  8.         if(*s== '\t'){
  9.             num_vezes++;
  10.         }
  11.         s++;
  12.     }
  13.     return num_vezes;
  14. }
  15.  
  16.  
  17. int ordenar(int vetorDeNumeros[]){
  18.     int tamanhoVetor = (sizeof vetorDeNumeros)/(sizeof vetorDeNumeros[0]);
  19.     int i;
  20.     int j;
  21.     int temp;
  22.     for(i=0;i<tamanhoVetor;i++){
  23.         for(j=0;j<tamanhoVetor;j++){
  24.             if(vetorDeNumeros[i] < vetorDeNumeros[j]){
  25.                 temp = vetorDeNumeros[i];
  26.                 vetorDeNumeros[i] = vetorDeNumeros[j];
  27.                 vetorDeNumeros[j] = temp;
  28.             }
  29.         }
  30.     }
  31.  
  32.     return vetorDeNumeros[0];
  33. }
  34. void clean(char * s){
  35.      int i=0;
  36.      while(s[i]!='\0'){
  37.           if(s[i]=='\n'){
  38.              s[i]='\0';
  39.           }
  40.           i++;
  41.      }
  42. }
  43. void RemoveSpaces(char* source)
  44. {
  45.   char* i = source;
  46.   char* j = source;
  47.   while(*j != 0)
  48.   {
  49.     *i = *j++;
  50.     if(*i != ' ')
  51.       i++;
  52.   }
  53.   *i = 0;
  54. }
  55. void cleanequal(char * s){
  56.      int i=0;
  57.      while(s[i]!='\0'){
  58.           if(s[i]== '='){
  59.              s[i]=' ';
  60.           }
  61.           i++;
  62.      }
  63. }
  64. void append(char* s, char c)
  65. {
  66.         int len = strlen(s);
  67.         s[len] = c;
  68.         s[len+1] = '\0';
  69. }
  70.  
  71. char* expand_cell_references(const char* f, const char* const l, char* o); /*the magic engine*/
  72. const char* get_cell_value(const char* coordinate_b, const char* coordinate_e);
  73.  
  74. const char* cells[][4] = {
  75.     /* A       B           C        D                 */
  76.     { "the"  , "lazy"    , "cow"  , "jumped"  }, /* 1 */
  77.     { "over" , "the"     , "quick", "brown"   }, /* 2 */
  78.     { "paper", "packages", "tied" , "up"      }, /* 3 */
  79.     { "with" , "silver"  , "white", "winters" }, /* 4 */
  80.     { "that" , "melt"    , "fox" ,  "springs" }, /* 5 */
  81. };
  82.  
  83. const char* get_cell_value(const char* coordinate_b, const char* coordinate_e)
  84. {
  85. #ifdef DEBUG
  86.     static const int maxrows = (sizeof(cells)/sizeof(*cells));
  87.     static const int maxcols = (sizeof(cells[0])/sizeof(*cells[0]));
  88. #endif
  89.     size_t col = 0, row = 0;
  90.     const char* it;
  91.     for (it=coordinate_b; it != coordinate_e; ++it)
  92.     {
  93.         if (*it >= 'A' && *it <= 'Z')
  94.             col = 26*col + (*it - 'A');
  95.         if (*it >= '0' && *it <= '9')
  96.             row = 10*row + (*it - '0'); /* or use atoi and friends */
  97.     }
  98.     row--; /* 1-based row nums in Excel */
  99.  
  100. #ifdef DEBUG
  101.     assert(col>=0 && col < maxcols);
  102.     assert(row>=0 && row < maxrows);
  103. #endif
  104.  
  105.     return cells[row][col]; /* 1-based indexes in Excel */
  106. }
  107.  
  108. int main(){
  109.     //int tamanho = 0;
  110.     char vetor[302];
  111.     int m;
  112.     int k=0;
  113.     int colunas = 0;
  114.     int vetorDeNumeros[2];
  115.  
  116.     //localiza pelo numero maximo de coisas que ele vai ler
  117.     char matris[30][26][302];
  118.     //inicia-se o loop
  119.     while(1){
  120.         //Recebe os dados
  121.         fgets(vetor, 302, stdin);
  122.  
  123.         //Verifica se tem a quebra de linha
  124.         if(strcmp(vetor, "\n") == 0){
  125.             break;
  126.         }
  127.  
  128.         //Aqui eu insiro na funcao o vetor
  129.         char *s = vetor;
  130.         //Recebo a quantia de \t
  131.         m = conta('\t', s);
  132.         //Passo pra posicao do vetor, o numero de colunas
  133.         vetorDeNumeros[k] = m+1;
  134.  
  135.         //Recebo o resultado do bubble sort
  136.         colunas = ordenar(vetorDeNumeros);
  137.  
  138.         //faz a colocacao de cada elemento dentro de cada celula, tentei por funcao mas nao deu muito certo
  139.         char *ponteiro;
  140.  
  141.         int j=0;
  142.         while(j<colunas){
  143.             ponteiro = strtok(vetor, "\t");
  144.             while (ponteiro != NULL){
  145.                 strcpy(matris[k][j], ponteiro);
  146.                 ponteiro = strtok(NULL, "\t");
  147.                 //REMOVE OS \N DESNECESSARIOS
  148.                 clean(matris[k][j]);
  149.                 //REMOVE OS IGUAIS
  150.                 cleanequal((matris[k][j]));
  151.                 //REMOVE OS ESPAÇOS GERADOS PELOS ESPAÇOS
  152.                 RemoveSpaces(matris[k][j]);
  153.                 j++;
  154.             }
  155.         }
  156.         //Quantia de linhas que foram
  157.         k++;
  158.     }
  159.  
  160. int L = 0, linha=0;
  161. char col;
  162. int i, c;
  163. for(i = 0; i < k; i++){
  164.   for(c = 0; c < colunas; c++){
  165.        //printf("linha: %d coluna: %c >> |%s|", i, c, matris[i][c]);
  166.     int line;
  167.     int offset = 0, readCharCount;
  168.     char op;
  169.     char troca[301];
  170.     char buffer[32];
  171.  
  172.     char out[1024] = {0};
  173.     expand_cell_references(matris[i][c], matris[i][c]+strlen(matris[i][c]), out);
  174.     puts(out); /* "The quick brown fox jumped over the lazy dog!" */
  175.  
  176.  
  177.   // printf("%s\t", matris[i][c]);
  178.   }
  179.   printf("\n");
  180. }
  181.  
  182. /*
  183.     printf("%d lin, %d col\n\n", k, colunas);
  184.     int w,z;
  185.     for(w=0;w<k;w++){
  186.         for(z=0;z<colunas;z++){
  187.             printf("%s\t", matris[w][z]);
  188.  
  189.         }
  190.         printf("\n");
  191.     }
  192. */
  193. printf("\n");
  194. //system("pause");
  195.  
  196. return 0;
  197. }
  198. char* expand_cell_references(const char* f, const char* const l, char* o)
  199. {
  200.     enum parser_state {
  201.         other,
  202.         in_coord_col,
  203.         in_coord_row
  204.     } state = other;
  205.  
  206.     /*temporary storage for coordinates being parsed:*/
  207.     char accum[16] = {0};
  208.     char* accit = accum;
  209.     while (f!=l)
  210.     {
  211.         switch(state) /*dummy, the transitions flow in fallthrough order for now*/
  212.         {
  213.             case other:
  214.                 *(accit = accum) = 0; /*reset the accumulator*/
  215.                 while (f!=l && !(*f>='A' && *f<='Z'))
  216.                     *o++ = *f++;
  217.                 /*fallthrough*/
  218.             case in_coord_col:
  219.                 while (f!=l && *f>='A' && *f<='Z')
  220.                     *accit++ = *f++;
  221.                 /*fallthrough*/
  222.             case in_coord_row:
  223.                 {
  224.                     const char* expanded = accum;
  225.                     if (f!=l && *f>='0' && *f<='9')
  226.                     {
  227.                         while (f!=l && *f>='0' && *f<='9')
  228.                             *accit++ = *f++;
  229.                         expanded = get_cell_value(accum, accit);
  230.                     }
  231.                     else
  232.                     {
  233.                         *accit = 0;
  234.                     }
  235.                     while (*expanded)
  236.                         *o++ = *expanded++;
  237.                     continue; /*state = other;*/
  238.                 }
  239.         }
  240.     }
  241.     return o;
  242. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement