-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
135 lines (127 loc) · 4.2 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/*
* File: main.c
* Author: Bryan Daniel
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "sorting.h"
#include "view.h"
#include "fileutil.h"
#include "baseutil.h"
#include "testfile.h"
#include "validate.h"
/*
* The main method
*/
int main(int argc, char** argv)
{
int exit_main = 0;
int exit_file_selection = 0;
int exit_algorithm_selection = 0;
int exit_program = 0;
int main_selection = 0;
int algorithm_selection = 0;
int file_selection = 0;
int selected_file_size = 0;
char selected_file_name[ProgramValues.file_name_max_length];
char path_to_file[ProgramValues.path_max_length];
ProgramOptions *options = malloc(sizeof (ProgramOptions));
MenuOption *menu_options = malloc(ProgramValues.max_menu_options * sizeof (MenuOption));
if (options == NULL || menu_options == NULL)
{
printf("Unable to allocate memory.\n");
return 1;
}
options->PrintList = ON;
do
{
exit_file_selection = 0;
exit_algorithm_selection = 0;
exit_program = 0;
while (exit_main == 0 && exit_program == 0)
{
print_main_menu();
char message[ProgramValues.max_message_size];
snprintf(message, sizeof message, "%s\n", "Select an option from the menu.");
main_selection = validate_numeric_input(message);
switch (main_selection)
{
case 1:
exit_main = 1;
break;
case 2:
create_test_file();
break;
case 3:
print_options_menu(options);
break;
case 4:
printf("Have a nice day.\n");
exit_program = 1;
break;
default: printf("Invalid entry.\n");
}
}
exit_main = 0;
while (exit_file_selection == 0 && exit_program == 0)
{
int count = print_file_menu(menu_options, ProgramValues.test_directory_name);
char message[ProgramValues.max_message_size];
snprintf(message, sizeof message, "%s\n", "Select a file from the menu.");
file_selection = validate_numeric_input(message);
if (file_selection < count)
{
strcpy(selected_file_name, menu_options[file_selection - 1].selection);
snprintf(path_to_file, sizeof path_to_file, "%s%s",
ProgramValues.test_directory_name, selected_file_name);
selected_file_size = count_lines(path_to_file);
exit_file_selection = 1;
}
else if (file_selection == count)
{
printf("Returning to main menu...\n");
exit_file_selection = 1;
exit_algorithm_selection = 1;
}
else
{
printf("Invalid entry.\n");
}
}
while (exit_algorithm_selection == 0 && exit_program == 0)
{
print_algorithm_menu();
char message[ProgramValues.max_message_size];
snprintf(message, sizeof message, "%s\n", "Enter an algorithm selection from the menu.");
algorithm_selection = validate_numeric_input(message);
switch (algorithm_selection)
{
case 1:
do_bubble_sort(path_to_file, selected_file_size, options);
exit_algorithm_selection = 1;
exit_main = 1;
break;
case 2:
do_quick_sort(path_to_file, selected_file_size, options);
exit_algorithm_selection = 1;
exit_main = 1;
break;
case 3:
do_merge_sort(path_to_file, selected_file_size, options);
exit_algorithm_selection = 1;
exit_main = 1;
break;
case 4:
printf("Returning to main menu...\n");
exit_algorithm_selection = 1;
break;
default: printf("Invalid entry.\n");
}
}
}
while (exit_program == 0);
free(options);
free(menu_options);
return (EXIT_SUCCESS);
}