Advertisement
Dalacul

Untitled

Dec 8th, 2019
347
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.01 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. int compare(const void* a, const void* b)
  6. {
  7. return (*(int*)a - * (int*)b);
  8. }
  9.  
  10. char* read_del(char* pchar)
  11. {
  12. char temp[15]; //array temporar pentru a citi o linie
  13. fgets(temp, 15, stdin); //citirea liniei
  14. int lg = strlen(temp);
  15. pchar = (char*)malloc(lg); //alocam memorie dinamic cu lungimea lui temp
  16. strcpy(pchar, temp); //copiem in array-ul alocat dinamic
  17. return strdup(pchar); //returnam linia citita
  18. }
  19.  
  20. char* read_out_del(char *pchar)
  21. {
  22. char temp[15]; //array temporar pentru a citi o linie
  23. fgets(temp, 15, stdin); //citirea liniei
  24. int lg = strlen(temp) - 1;
  25. temp[lg] = 0; //scapam de \n din final
  26. pchar = (char*)malloc(lg); //alocam memorie dinamic cu lungimea lui temp
  27. strcpy(pchar, temp); //copiem in array-ul alocat dinamic
  28. return strdup(pchar); //returnam linia citita
  29. }
  30.  
  31. char* read_line(char* pchar)
  32. {
  33. char temp[200]; //array temporar pentru a citi o linie
  34. fgets(temp, 200, stdin); //citirea liniei
  35. int lg = strlen(temp) - 1;
  36. temp[lg] = 0; //scapam de \n din final
  37. pchar = (char*)malloc(lg); //alocam memorie dinamic cu lungimea lui temp
  38. strcpy(pchar, temp); //copiem in array-ul alocat dinamic
  39. return strdup(pchar); //returnam linia citita
  40. }
  41.  
  42. void read_column(int *arr, int *n)
  43. {
  44. int i, k = 0;
  45. for(i = 0; i < (*n); ++i)
  46. scanf("%d,", &arr[i]);
  47. qsort(arr, (*n), sizeof(int), compare);
  48. for(i = 1; i < (*n); ++i)
  49. if(arr[k] != arr[i])
  50. arr[++k] = arr[i];
  51. *n = k + 1;
  52. }
  53.  
  54.  
  55. int main()
  56. {
  57. int *column, n, m, i, lgout_del;
  58. char *del, *out_del, temp[2000];
  59. //
  60. del = 0;
  61. del = read_del(del);
  62. //
  63. scanf("%d\n", &n);
  64. column = malloc(n * sizeof(int));
  65. read_column(column, &n);
  66. scanf("\n");
  67. //
  68. out_del = 0;
  69. out_del = read_out_del(out_del);
  70. lgout_del = strlen(out_del);
  71. //
  72. scanf("%d\n", &m);
  73. char **text = malloc(m * sizeof(*text));
  74. for(i = 0; i < m; ++i)
  75. text[i] = read_line(text[i]);
  76. //
  77. for(i = 0; i < m; ++i)
  78. {
  79. int nrcuv = 0, k = 0;
  80. strcpy(temp, "");
  81. char *pchar = strtok(text[i], del);
  82. while(pchar != NULL)
  83. {
  84. if(++nrcuv == column[k])
  85. {
  86. strcat(temp, pchar);
  87. strcat(temp, out_del);
  88. ++k;
  89. }
  90. pchar = strtok(NULL, del);
  91. }
  92. temp[strlen(temp) - lgout_del] = 0;
  93. printf("%s\n", temp);
  94. }
  95. free(del);
  96. free(out_del);
  97. free(text);
  98. free(column);
  99. return 0;
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement