Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include "extra.h"
- #define TEMP_FILE "temp"
- typedef struct word {
- char key[128];
- int freq;
- } word;
- int new_record(char *filename, int curr_line) {
- char input[128];
- FILE *f = fopen(filename, "a");
- if(!f) {
- printf("Could not open \"%s\".\n", filename);
- return 0;
- }
- getchar();
- printf("New Record: ");
- scanf("%[^\n]", input);
- fprintf(f, "%s\n", input);
- fclose(f);
- printf("Record added successfully.\n");
- return 1;
- }
- int remove_record(char *filename, int file_size) {
- FILE *f = fopen(filename, "r");
- FILE *temp_f = fopen(TEMP_FILE, "w");
- if(!f) {
- printf("Could not open \"%s\".\n", f);
- return 0;
- }
- if(!temp_f) {
- printf("There was an error opening a temp file.\n");
- return 0;
- }
- char line[128];
- int line_num, i = 0;
- printf("Line: ");
- scanf("%d", &line_num);
- if(line_num <= 0 || line_num > file_size) {
- printf("Invalid input.\n");
- return 0;
- }
- i = 1;
- while(fgets(line, 128, f) != NULL) {
- if(i != line_num) fprintf(temp_f, "%s", line);
- i++;
- }
- fclose(f);
- fclose(temp_f);
- remove(filename);
- rename(TEMP_FILE, filename);
- printf("Record removed successfully.\n");
- return 1;
- }
- void seperate_records(int l, char *line, char *search_item) {
- char temp_str[128] = "";
- int j = 0, column = 1;
- for(int i = 0; i <= strlen(line); i++) {
- if(line[i] == ',' || line[i] == '\0' || line[i] == '\n') {
- if(strcmp(trim_spaces(temp_str), search_item) == 0) printf("At the register that was in line %d and column %d.\n", l, column);
- if(line[i] == '\0') break;
- memset(temp_str, 0, sizeof(temp_str));
- j = 0;
- column++;
- }
- else {
- temp_str[j] = line[i];
- j++;
- }
- }
- }
- void search_record(char *filename) {
- char line[128], search_item[128];
- int l = 1;
- FILE *f = fopen(filename, "r");
- if(!f) {
- printf("Could not open \"%s\".\n", filename);
- return;
- }
- getchar();
- printf("Search item: ");
- scanf("%[^\n]", search_item);
- printf("The word was found:\n");
- while(fgets(line, 128, f) != NULL) {
- seperate_records(l, line, search_item);
- l++;
- }
- printf("\n");
- }
- word *get_most_frequent_words(FILE *f) {
- word* temp_arr = (word*)malloc(0);
- int arr_size = 0;
- char temp_str[128] = "";
- int j = 0, z = 0;
- char line[128];
- while(fgets(line, 128, f) != NULL) {
- for(int i = 0; i <= strlen(line); i++) {
- if(line[i] == ',' || line[i] == '\0' || line[i] == '\n') {
- strcpy(temp_str, trim_spaces(temp_str));
- for(z = 0; z < arr_size; z++) {
- if(strcmp(temp_str, temp_arr[z].key) == 0) {
- temp_arr[z].freq++;
- break;
- }
- }
- //strcmp is bandage.
- if(z == arr_size && strcmp(temp_str, "") != 0) {
- temp_arr = (word*)realloc(temp_arr, sizeof(word) * (++arr_size));
- strcpy(temp_arr[arr_size - 1].key, temp_str);
- temp_arr[arr_size - 1].freq = 1;
- }
- memset(temp_str, 0, sizeof(temp_str));
- j = 0;
- }
- else {
- temp_str[j] = line[i];
- j++;
- }
- }
- }
- temp_arr = bubble_sort(temp_arr, arr_size);
- word *top5 = (word*)malloc(sizeof(word) * 5);
- int tmp = 5;
- if(arr_size < 5) tmp = arr_size;
- for(int i = 0; i < tmp; i++) top5[i] = temp_arr[i];
- return top5;
- }
- void print_stats(char *filename) {
- int *lines_columns = count_lines_and_columns(filename);
- double average_column_per_line = 0.0;
- if(lines_columns[0] != 0) average_column_per_line = (double)(lines_columns[1]) / (double)(lines_columns[0]);
- printf("Total Lines: %d.\n"
- "Total Columns: %d.\n"
- "Average Columns Per Line: %f.\n", lines_columns[0], lines_columns[1], average_column_per_line);
- FILE *f = fopen(filename, "r");
- if(!f) {
- printf("Could not open \"%s\".", filename);
- return;
- }
- word *top = get_most_frequent_words(f);
- 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);
- printf("\n");
- }
- void print_stats_file(char *filename) {
- char stats_filename[128];
- strcpy(stats_filename, get_filename());
- FILE *f = fopen(stats_filename, "w");
- if(!f) {
- printf("Could not open \"%s\".\n", stats_filename);
- return;
- }
- int *lines_columns = count_lines_and_columns(filename);
- double average_column_per_line = 0.0;
- if(lines_columns[0] != 0) average_column_per_line = (double)(lines_columns[1]) / (double)(lines_columns[0]);
- fprintf(f, "Total Lines: %d.\n"
- "Total Columns: %d.\n"
- "Average Columns Per Line: %f.\n", lines_columns[0], lines_columns[1], average_column_per_line);
- FILE *f2 = fopen(filename, "r");
- if(!f2) {
- printf("Could not open \"%s\".\n", filename);
- return;
- }
- word *top = get_most_frequent_words(f2);
- fclose(f2);
- 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);
- fclose(f);
- printf("Export to \"%s\" successfull.\n\n", stats_filename);
- }
- int main() {
- int input, curr_line = 0;;
- char filename[128] = "";
- do {
- printf("1. Enter the name of the file you want to insert data.\n"
- "2. Insert new record.\n"
- "3. Delete existing record.\n"
- "4. Search record.\n"
- "5. View statistics.\n"
- "6. Export statistics to a file.\n"
- "Press any other nymber to exit.\n");
- scanf("%d", &input);
- switch(input) {
- case 1:
- strcpy(filename, get_filename());
- break;
- case 2:
- curr_line = count_lines(filename);
- if(curr_line != -1) if(new_record(filename, curr_line) == 1) curr_line++;
- break;
- case 3:
- curr_line = count_lines(filename);
- if(curr_line != -1) if(remove_record(filename, curr_line) == 1) curr_line--;
- break;
- case 4:
- search_record(filename);
- break;
- case 5:
- print_stats(filename);
- break;
- case 6:
- print_stats_file(filename);
- break;
- }
- } while(input > 0 && input < 7);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement