Advertisement
Guest User

Untitled

a guest
Dec 12th, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.88 KB | None | 0 0
  1. //
  2. // main.c
  3. // h.w.8
  4. //
  5. // Created by Темирхан Мамаев on 05/12/2019.
  6. // Copyright © 2019 Темирхан Мамаев. All rights reserved.
  7. //
  8.  
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <ctype.h>
  13. #include <limits.h>
  14.  
  15. char *min_alpha(char **s, int size_arr){
  16. char *p_min_alpha = NULL;
  17. int min_count = INT_MAX;
  18. for (char **ps = s; ps < s + size_arr; ps++){
  19. int count = 0;
  20. for (char *sub = *ps; sub < *ps + strlen(*ps); sub++){
  21. if (isalpha(*sub)) {
  22. count++;
  23. }
  24. }
  25. if (count < min_count) {
  26. p_min_alpha = *ps;
  27. min_count = count;
  28. }
  29. }
  30. return p_min_alpha;
  31. }
  32.  
  33. char **podstr_split(char **s, int size_arr, char fs, char ls){
  34. int n = 0;
  35. char **podstr_arr = malloc(sizeof(char*) * 5);
  36. for(char **str = s; str < s + size_arr; str++){
  37. char *q = *str, *p = *str;
  38. while((q = strchr(q, fs)) && (p = strchr(q, ls)))
  39. if (strchr(q, '*')) {
  40. *(podstr_arr + n) = malloc(sizeof(char) * (p - q));
  41. strncpy(*(podstr_arr + n), (q + 1), (p - q - 1));
  42. n++;
  43. q++;
  44. // *(*(podstr_arr + n) + (p - q)) = '\0';
  45. }
  46. if (n % 5 == 0) {
  47. podstr_arr = realloc(podstr_arr, sizeof(char*) * (n + 5));
  48. }
  49. }
  50. *(podstr_arr + n) = NULL;
  51. return podstr_arr;
  52. }
  53.  
  54. void vstav_char(char *s, char ps, char h){
  55. char *p = s, *q;
  56. while((p = strchr(p, h))){
  57. q = strchr(p, '\0');
  58. while(q > p){
  59. *(q + 1) = *q;
  60. q--;
  61. }
  62. *(p + 1) = ps;
  63. p++;
  64. }
  65. }
  66.  
  67.  
  68. int main(int argc, const char * argv[]) {
  69. FILE *f;
  70. char buf[80], **p = NULL, **str = NULL, **s, *p_max, **podstr;
  71. int n = 0;
  72. if (!(f = fopen("data.txt", "rb"))) {
  73. printf("file don't exist");
  74. return 1;
  75. }
  76. else{
  77. while (!feof(f)) {
  78. str = (char**)malloc(sizeof(char*) * 5);
  79. p = str;
  80. while (!feof(f)) {
  81. fgets(buf, 79, f);
  82. *p = (char*)malloc((strlen(buf) + 1) * sizeof(char));
  83. strcpy(*p, buf);
  84. p++;
  85. n++;
  86. if (n % 5 == 0) {
  87. str = (char**)realloc(str, sizeof(char*) * (n + 5));
  88. p = str + n;
  89. }
  90. }
  91. }
  92. }
  93. podstr = podstr_split(str, n, '(', ')');
  94. p_max = min_alpha(str, n);
  95. puts(p_max);
  96. for(s = str; s < str + n; s++){
  97. vstav_char(*s, '0', '1');
  98. puts(*s);
  99. free(*s);
  100. }
  101. printf("\n__________________________________\n");
  102. for(s = podstr; *s; s++){
  103. puts(*s);
  104. free(*s);
  105. }
  106. free(podstr);
  107. free(str);
  108. fclose(f);
  109. printf("\n");
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement