fork(2) 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 value[1024] = {0};
  135. char out[1024] = {0};
  136.  
  137. strncpy(value, matris[i][c], sizeof(value));
  138.  
  139. int iterations_left = 10;
  140. while(iterations_left--)
  141. {
  142. expand_cell_references(value, value+strlen(value), out);
  143.  
  144. #ifdef DEBUG
  145. printf("[i,c]=[%d,%d]\titerations_left: %d\tcell: '%s'\tformula: '%s'\texpansion: '%s'\n",
  146. i, c, iterations_left, matris[i][c], value, out);
  147. #endif
  148.  
  149. if (!strcmp(value, out))
  150. break; // stop when no more expansions occur
  151.  
  152. strncpy(value, out, sizeof(value));
  153. }
  154.  
  155. printf("%s\t", out);
  156. // printf("%s\t", matris[i][c]);
  157. }
  158. printf("\n");
  159. }
  160. /*
  161.   printf("%d lin, %d col\n\n", k, colunas);
  162.   int w,z;
  163.   for(w=0;w<k;w++){
  164.   for(z=0;z<colunas;z++){
  165.   printf("%s\t", matris[w][z]);
  166.  
  167.   }
  168.   printf("\n");
  169.   }
  170.   */
  171. printf("\n");
  172. //system("pause");
  173. return 0;
  174. }
  175.  
  176. const char* get_cell_value(const char* coordinate_b, const char* coordinate_e)
  177. {
  178. #ifdef DEBUG
  179. static const size_t maxrows = (sizeof(matris)/sizeof(*matris));
  180. static const size_t maxcols = (sizeof(matris[0])/sizeof(*matris[0]));
  181. #endif
  182. size_t col = 0, row = 0;
  183. const char* it;
  184. for (it=coordinate_b; it != coordinate_e; ++it)
  185. {
  186. if (*it >= 'A' && *it <= 'Z')
  187. col = 26*col + (*it - 'A');
  188. if (*it >= '0' && *it <= '9')
  189. row = 10*row + (*it - '0'); /* or use atoi and friends */
  190. }
  191. row--; /* 1-based row nums in Excel */
  192.  
  193. #ifdef DEBUG
  194. assert(col>=0 && col < maxcols);
  195. assert(row>=0 && row < maxrows);
  196. #endif
  197.  
  198. return matris[row][col]; /* 1-based indexes in Excel */
  199. }
  200.  
  201. char* expand_cell_references(const char* f, const char* const l, char* o)
  202. {
  203. enum parser_state {
  204. other,
  205. in_coord_col,
  206. in_coord_row
  207. } state = other;
  208.  
  209. /*temporary storage for coordinates being parsed:*/
  210. char accum[16] = {0};
  211. char* accit = accum;
  212. while (f!=l)
  213. {
  214. switch(state) /*dummy, the transitions flow in fallthrough order for now*/
  215. {
  216. case other:
  217. *(accit = accum) = 0; /*reset the accumulator*/
  218. while (f!=l && !(*f>='A' && *f<='Z'))
  219. *o++ = *f++;
  220. /*fallthrough*/
  221. case in_coord_col:
  222. while (f!=l && *f>='A' && *f<='Z')
  223. *accit++ = *f++;
  224. /*fallthrough*/
  225. case in_coord_row:
  226. {
  227. const char* expanded = accum;
  228. if (f!=l && *f>='0' && *f<='9')
  229. {
  230. while (f!=l && *f>='0' && *f<='9')
  231. *accit++ = *f++;
  232. expanded = get_cell_value(accum, accit);
  233. }
  234. else
  235. {
  236. *accit = 0;
  237. }
  238. while (*expanded)
  239. *o++ = *expanded++;
  240. continue; /*state = other;*/
  241. }
  242. }
  243. }
  244. *o = '\0';
  245. return o;
  246. }
  247.  
Success #stdin #stdout 0s 2020KB
stdin
1	2	3	4	5	6
A1	A2+B1	C1	D1	B2+C2	E1
stdout
1	2	3	4	5	6	
1	1+2	3	4	1+2+3	5