Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <sys/stat.h>
- #include <dirent.h>
- #include <string.h>
- #include <stdlib.h>
- long long int counter(const char* dirPath, char* lastDir){
- printf("%sn", dirPath);
- long long int tmp;
- long long int res = 0;
- char* dirCopy;
- DIR *dir = opendir(dirPath);
- if (strcmp(lastDir, "add") == 0){
- res = 0;
- struct dirent *de = readdir(dir);
- while (de){
- if (strcmp(de->d_name, "add") == 0){
- dirCopy = (char*)malloc((strlen(dirPath) + 5) * sizeof(char));
- strcpy(dirCopy, dirPath);
- strcat(dirCopy, "/");
- strcat(dirCopy, "add");
- res += counter(dirCopy, "add");
- }
- else if(strcmp(de->d_name, "mul") == 0){
- dirCopy = (char*)malloc((strlen(dirPath) + 5) * sizeof(char));
- strcpy(dirCopy, dirPath);
- strcat(dirCopy, "/");
- strcat(dirCopy, "mul");
- res += counter(dirCopy, "mul");
- }
- else if(strstr(de->d_name, ".txt") != NULL){
- dirCopy = (char*)malloc((strlen(dirPath) + strlen(de->d_name) + 2) * sizeof(char));
- strcpy(dirCopy, dirPath);
- strcat(dirCopy, "/");
- strcat(dirCopy, de->d_name);
- FILE* file = fopen(dirCopy, "r");
- while(fscanf(file, "%lld", &tmp) != EOF){
- printf("%lldn", tmp);
- res += tmp;
- }
- }
- de = readdir(dir);
- }
- }
- else if (strcmp(lastDir, "mul") == 0){
- res = 1;
- struct dirent *de = readdir(dir);
- while (de){
- if (strcmp(de->d_name, "add") == 0){
- dirCopy = (char*)malloc((strlen(dirPath) + 5) * sizeof(char));
- strcpy(dirCopy, dirPath);
- strcat(dirCopy, "/");
- strcat(dirCopy, "add");
- res *= counter(dirCopy, "add");
- }
- else if(strcmp(de->d_name, "mul") == 0){
- strcpy(dirCopy, dirPath);
- strcat(dirCopy, "/");
- strcat(dirCopy, "mul");
- res *= counter(dirCopy, "mul");
- }
- else if(strstr(de->d_name, ".txt") != NULL){
- dirCopy = (char*)malloc((strlen(dirPath) + strlen(de->d_name) + 2) * sizeof(char));
- strcpy(dirCopy, dirPath);
- strcat(dirCopy, "/");
- strcat(dirCopy, de->d_name);
- FILE* file = fopen(dirCopy, "r");
- while(fscanf(file, "%lld", &tmp) != EOF){
- printf("%lldn", tmp);
- res *= tmp;
- }
- }
- de = readdir(dir);
- }
- }
- else if (strcmp(lastDir, "tmp") == 0){
- if(dir) {
- res = 0;
- struct dirent *de = readdir(dir);
- while (de) {
- if (strcmp(de->d_name, "add") == 0){
- dirCopy = (char*)malloc((strlen(dirPath) + 5) * sizeof(char));
- strcpy(dirCopy, dirPath);
- strcat(dirCopy, "/");
- strcat(dirCopy, "add");
- res += counter(dirCopy, "add");
- }
- else if(strcmp(de->d_name, "mul") == 0){
- dirCopy = (char*)malloc((strlen(dirPath) + 5) * sizeof(char));
- strcpy(dirCopy, dirPath);
- strcat(dirCopy, "/");
- strcat(dirCopy, "mul");
- res += counter(dirCopy, "mul");
- }
- de = readdir(dir);
- }
- }
- }
- return res;
- }
- int main(){
- char* path = (char*)malloc(4 * sizeof(char));
- long long int c;
- FILE* file_ptr;
- strcpy(path,"tmp");
- c = counter(path, "tmp");
- file_ptr = fopen("result.txt", "w");
- fprintf(file_ptr, "%lld", c);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement