Advertisement
Guest User

Untitled

a guest
Feb 20th, 2019
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.90 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <sys/stat.h>
  3. #include <dirent.h>
  4. #include <string.h>
  5. #include <stdlib.h>
  6.  
  7.  
  8. long long int counter(const char* dirPath, char* lastDir){
  9. printf("%sn", dirPath);
  10. long long int tmp;
  11. long long int res = 0;
  12. char* dirCopy;
  13. DIR *dir = opendir(dirPath);
  14. if (strcmp(lastDir, "add") == 0){
  15. res = 0;
  16. struct dirent *de = readdir(dir);
  17. while (de){
  18. if (strcmp(de->d_name, "add") == 0){
  19. dirCopy = (char*)malloc((strlen(dirPath) + 5) * sizeof(char));
  20. strcpy(dirCopy, dirPath);
  21. strcat(dirCopy, "/");
  22. strcat(dirCopy, "add");
  23. res += counter(dirCopy, "add");
  24. }
  25. else if(strcmp(de->d_name, "mul") == 0){
  26. dirCopy = (char*)malloc((strlen(dirPath) + 5) * sizeof(char));
  27. strcpy(dirCopy, dirPath);
  28. strcat(dirCopy, "/");
  29. strcat(dirCopy, "mul");
  30. res += counter(dirCopy, "mul");
  31. }
  32. else if(strstr(de->d_name, ".txt") != NULL){
  33. dirCopy = (char*)malloc((strlen(dirPath) + strlen(de->d_name) + 2) * sizeof(char));
  34. strcpy(dirCopy, dirPath);
  35. strcat(dirCopy, "/");
  36. strcat(dirCopy, de->d_name);
  37. FILE* file = fopen(dirCopy, "r");
  38. while(fscanf(file, "%lld", &tmp) != EOF){
  39. printf("%lldn", tmp);
  40. res += tmp;
  41. }
  42. }
  43. de = readdir(dir);
  44. }
  45. }
  46. else if (strcmp(lastDir, "mul") == 0){
  47. res = 1;
  48. struct dirent *de = readdir(dir);
  49. while (de){
  50. if (strcmp(de->d_name, "add") == 0){
  51. dirCopy = (char*)malloc((strlen(dirPath) + 5) * sizeof(char));
  52. strcpy(dirCopy, dirPath);
  53. strcat(dirCopy, "/");
  54. strcat(dirCopy, "add");
  55. res *= counter(dirCopy, "add");
  56. }
  57. else if(strcmp(de->d_name, "mul") == 0){
  58. strcpy(dirCopy, dirPath);
  59. strcat(dirCopy, "/");
  60. strcat(dirCopy, "mul");
  61. res *= counter(dirCopy, "mul");
  62. }
  63. else if(strstr(de->d_name, ".txt") != NULL){
  64. dirCopy = (char*)malloc((strlen(dirPath) + strlen(de->d_name) + 2) * sizeof(char));
  65. strcpy(dirCopy, dirPath);
  66. strcat(dirCopy, "/");
  67. strcat(dirCopy, de->d_name);
  68. FILE* file = fopen(dirCopy, "r");
  69. while(fscanf(file, "%lld", &tmp) != EOF){
  70. printf("%lldn", tmp);
  71. res *= tmp;
  72. }
  73. }
  74. de = readdir(dir);
  75. }
  76. }
  77. else if (strcmp(lastDir, "tmp") == 0){
  78. if(dir) {
  79. res = 0;
  80. struct dirent *de = readdir(dir);
  81. while (de) {
  82. if (strcmp(de->d_name, "add") == 0){
  83. dirCopy = (char*)malloc((strlen(dirPath) + 5) * sizeof(char));
  84. strcpy(dirCopy, dirPath);
  85. strcat(dirCopy, "/");
  86. strcat(dirCopy, "add");
  87. res += counter(dirCopy, "add");
  88. }
  89. else if(strcmp(de->d_name, "mul") == 0){
  90. dirCopy = (char*)malloc((strlen(dirPath) + 5) * sizeof(char));
  91. strcpy(dirCopy, dirPath);
  92. strcat(dirCopy, "/");
  93. strcat(dirCopy, "mul");
  94. res += counter(dirCopy, "mul");
  95. }
  96. de = readdir(dir);
  97. }
  98. }
  99. }
  100. return res;
  101. }
  102.  
  103.  
  104.  
  105.  
  106. int main(){
  107. char* path = (char*)malloc(4 * sizeof(char));
  108. long long int c;
  109. FILE* file_ptr;
  110. strcpy(path,"tmp");
  111. c = counter(path, "tmp");
  112. file_ptr = fopen("result.txt", "w");
  113. fprintf(file_ptr, "%lld", c);
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement