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