Advertisement
Guest User

Untitled

a guest
Nov 14th, 2019
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.97 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include "extra.h"
  5.  
  6. #define TEMP_FILE "temp"
  7.  
  8. typedef struct word {
  9.     char key[128];
  10.     int freq;
  11. } word;
  12.  
  13. int new_record(char *filename, int curr_line) {
  14.     char input[128];
  15.  
  16.     FILE *f = fopen(filename, "a");
  17.     if(!f) {
  18.         printf("Could not open \"%s\".\n", filename);
  19.         return 0;
  20.     }
  21.  
  22.     getchar();
  23.     printf("New Record: ");
  24.     scanf("%[^\n]", input);
  25.  
  26.     fprintf(f, "%s\n", input);
  27.    
  28.     fclose(f);
  29.     printf("Record added successfully.\n");
  30.  
  31.     return 1;
  32. }
  33.  
  34. int remove_record(char *filename, int file_size) {
  35.     FILE *f = fopen(filename, "r");
  36.     FILE *temp_f = fopen(TEMP_FILE, "w");
  37.    
  38.     if(!f) {
  39.         printf("Could not open \"%s\".\n", f);
  40.         return 0;
  41.     }
  42.     if(!temp_f) {
  43.         printf("There was an error opening a temp file.\n");
  44.         return 0;
  45.     }
  46.  
  47.     char line[128];
  48.     int line_num, i = 0;
  49.  
  50.     printf("Line: ");
  51.     scanf("%d", &line_num);
  52.    
  53.     if(line_num <= 0 || line_num > file_size) {
  54.         printf("Invalid input.\n");
  55.         return 0;
  56.     }
  57.  
  58.     i = 1;
  59.     while(fgets(line, 128, f) != NULL) {
  60.         if(i != line_num) fprintf(temp_f, "%s", line);
  61.         i++;
  62.     }
  63.  
  64.     fclose(f);
  65.     fclose(temp_f);
  66.  
  67.     remove(filename);
  68.     rename(TEMP_FILE, filename);
  69.  
  70.     printf("Record removed successfully.\n");
  71.  
  72.     return 1;
  73. }
  74.  
  75. void seperate_records(int l, char *line, char *search_item) {
  76.     char temp_str[128] = "";
  77.     int j = 0, column = 1;
  78.  
  79.     for(int i = 0; i <= strlen(line); i++) {
  80.         if(line[i] == ',' || line[i] == '\0' || line[i] == '\n') {
  81.             if(strcmp(trim_spaces(temp_str), search_item) == 0) printf("At the register that was in line %d and column %d.\n", l, column);
  82.             if(line[i] == '\0') break;
  83.  
  84.             memset(temp_str, 0, sizeof(temp_str));
  85.  
  86.             j = 0;
  87.             column++;
  88.         }
  89.         else {
  90.             temp_str[j] = line[i];
  91.             j++;
  92.         }
  93.     }
  94. }
  95.  
  96. void search_record(char *filename) {
  97.     char line[128], search_item[128];
  98.     int l = 1;
  99.     FILE *f = fopen(filename, "r");
  100.     if(!f) {
  101.         printf("Could not open \"%s\".\n", filename);
  102.         return;
  103.     }
  104.  
  105.     getchar();
  106.     printf("Search item: ");
  107.     scanf("%[^\n]", search_item);
  108.  
  109.     printf("The word was found:\n");
  110.     while(fgets(line, 128, f) != NULL) {
  111.         seperate_records(l, line, search_item);
  112.         l++;
  113.     }
  114.     printf("\n");
  115. }
  116.  
  117. word *get_most_frequent_words(FILE *f) {
  118.     word* temp_arr = (word*)malloc(0);
  119.     int arr_size = 0;
  120.  
  121.     char temp_str[128] = "";
  122.     int j = 0, z = 0;
  123.     char line[128];
  124.  
  125.     while(fgets(line, 128, f) != NULL) {
  126.         for(int i = 0; i <= strlen(line); i++) {
  127.             if(line[i] == ',' || line[i] == '\0' || line[i] == '\n') {
  128.                 strcpy(temp_str, trim_spaces(temp_str));
  129.                 for(z = 0; z < arr_size; z++) {
  130.                     if(strcmp(temp_str, temp_arr[z].key) == 0) {
  131.                         temp_arr[z].freq++;
  132.                         break;
  133.                     }
  134.                 }
  135.                 //strcmp is bandage.
  136.                 if(z == arr_size && strcmp(temp_str, "") != 0) {
  137.                     temp_arr = (word*)realloc(temp_arr, sizeof(word) * (++arr_size));
  138.                     strcpy(temp_arr[arr_size - 1].key, temp_str);
  139.                     temp_arr[arr_size - 1].freq = 1;
  140.                    
  141.                 }
  142.                 memset(temp_str, 0, sizeof(temp_str));
  143.                 j = 0;
  144.             }
  145.             else {
  146.                 temp_str[j] = line[i];
  147.                 j++;
  148.             }
  149.         }
  150.     }
  151.  
  152.     temp_arr = bubble_sort(temp_arr, arr_size);
  153.  
  154.     word *top5 = (word*)malloc(sizeof(word) * 5);
  155.     int tmp = 5;
  156.     if(arr_size < 5) tmp = arr_size;
  157.     for(int i = 0; i < tmp; i++) top5[i] = temp_arr[i];
  158.  
  159.     return top5;
  160. }
  161.  
  162. void print_stats(char *filename) {
  163.     int *lines_columns = count_lines_and_columns(filename);
  164.     double average_column_per_line = 0.0;
  165.  
  166.     if(lines_columns[0] != 0) average_column_per_line = (double)(lines_columns[1]) / (double)(lines_columns[0]);
  167.  
  168.     printf("Total Lines: %d.\n"
  169.            "Total Columns: %d.\n"
  170.            "Average Columns Per Line: %f.\n", lines_columns[0], lines_columns[1], average_column_per_line);
  171.  
  172.     FILE *f = fopen(filename, "r");
  173.     if(!f) {
  174.         printf("Could not open \"%s\".", filename);
  175.         return;
  176.     }
  177.  
  178.     word *top = get_most_frequent_words(f);
  179.  
  180.     for(int i = 0; i < 5; i++) if(top[i].freq > 0) printf("Word \"%s\". Appearance frequency: %d.\n", top[i].key, top[i].freq);
  181.  
  182.     printf("\n");
  183. }
  184.  
  185. void print_stats_file(char *filename) {
  186.     char stats_filename[128];
  187.  
  188.     strcpy(stats_filename, get_filename());
  189.  
  190.     FILE *f = fopen(stats_filename, "w");
  191.     if(!f) {
  192.         printf("Could not open \"%s\".\n", stats_filename);
  193.         return;
  194.     }
  195.  
  196.     int *lines_columns = count_lines_and_columns(filename);
  197.     double average_column_per_line = 0.0;
  198.  
  199.     if(lines_columns[0] != 0) average_column_per_line = (double)(lines_columns[1]) / (double)(lines_columns[0]);
  200.  
  201.     fprintf(f, "Total Lines: %d.\n"
  202.                "Total Columns: %d.\n"
  203.                "Average Columns Per Line: %f.\n", lines_columns[0], lines_columns[1], average_column_per_line);
  204.  
  205.     FILE *f2 = fopen(filename, "r");
  206.     if(!f2) {
  207.         printf("Could not open \"%s\".\n", filename);
  208.         return;
  209.     }
  210.  
  211.     word *top = get_most_frequent_words(f2);
  212.     fclose(f2);
  213.     for(int i = 0; i < 5; i++) if(top[i].freq > 0) fprintf(f, "Word \"%s\". Appearance frequency: %d.\n", top[i].key, top[i].freq);
  214.  
  215.     fclose(f);
  216.  
  217.     printf("Export to \"%s\" successfull.\n\n", stats_filename);
  218. }
  219.  
  220. int main() {
  221.     int input, curr_line = 0;;
  222.     char filename[128] = "";
  223.  
  224.     do {
  225.         printf("1. Enter the name of the file you want to insert data.\n"
  226.                "2. Insert new record.\n"
  227.                "3. Delete existing record.\n"
  228.                "4. Search record.\n"
  229.                "5. View statistics.\n"
  230.                "6. Export statistics to a file.\n"
  231.                "Press any other nymber to exit.\n");
  232.  
  233.         scanf("%d", &input);
  234.         switch(input) {
  235.             case 1:
  236.                 strcpy(filename, get_filename());
  237.                 break;
  238.             case 2:
  239.                 curr_line = count_lines(filename);
  240.                 if(curr_line != -1) if(new_record(filename, curr_line) == 1) curr_line++;
  241.                 break;
  242.             case 3:
  243.                 curr_line = count_lines(filename);
  244.                 if(curr_line != -1) if(remove_record(filename, curr_line) == 1) curr_line--;
  245.                 break;
  246.             case 4:
  247.                 search_record(filename);
  248.                 break;
  249.             case 5:
  250.                 print_stats(filename);
  251.                 break;
  252.             case 6:
  253.                 print_stats_file(filename);
  254.                 break;
  255.         }
  256.  
  257.     } while(input > 0 && input < 7);
  258. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement