Submission - 2023-08-20T155044.694

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 13

Contents

1 Basic Test Results 2

2 ex2/README.md 4

3 ex2/main.c 5

4 ex2/sort bus lines.h 9

5 ex2/sort bus lines.c 10

6 ex2/test bus lines.h 11

7 ex2/test bus lines.c 12

1
1 Basic Test Results
1
2 Running presubmission script...
3
4
5 Opening tar file
6 OK
7 Tar extracted O.K.
8 For your convenience, the MD5 checksum for your submission is 1db8541a92cf17a6186e08452705928d
9 Checking files...
10 OK
11 Making sure files are not empty...
12 OK
13 Checking CodingStyle...
14 Checking file sort_bus_lines.c...
15 Checking file test_bus_lines.c...
16 Checking file main.c...
17 Checking file sort_bus_lines.h...
18 Checking file test_bus_lines.h...
19 Passed codingStyle check.
20 Compilation check...
21 Compiling...
22
23 Compilation looks good!
24
25
26 =====================
27 Public test cases
28 =====================
29
30
31 =====================
32 Running AST Test Number 1...
33 Passed Test.
34 =====================
35
36
37 =====================
38 Running AST Test Number 2...
39 Passed Test.
40 =====================
41
42
43 =====================
44 Running AST Test Number 3...
45 Passed Test.
46 =====================
47
48
49 =====================
50 Running IO Test Number 1
51 Running test...
52 OK
53 Passed Test.
54 =====================
55
56
57 =====================
58 Running IO Test Number 17
59 Running test...

2
60 OK
61 Passed Test.
62 =====================
63
64
65 =====================
66 Running IO Test Number 18
67 Running test...
68 OK
69 Passed Test.
70 =====================
71
72
73 =====================
74 Running IO Test Number 6
75 Running test...
76 OK
77 Passed Test.
78 =====================
79
80
81 =====================
82 Running IO Test Number 24
83 Running test...
84 OK
85 Passed Test.
86 =====================
87
88
89 =====================
90 Running IO Test Number 9
91 Running test...
92 OK
93 Passed Test.
94 =====================
95
96
97 =====================
98 Running IO Test Number 15
99 Running test...
100 OK
101 Passed Test.
102 =====================
103
104 *****************************************
105 * *** *
106 * Passed all tests!! *
107 * Good Job! *
108 * *** *
109 *****************************************

3
2 ex2/README.md
1 # ex2-kingmoamin

4
3 ex2/main.c
1 #include "sort_bus_lines.h"
2 #include "test_bus_lines.h"
3
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <ctype.h>
7
8 #define TWO 2
9 #define THREE 3
10 #define FOUR 4
11 #define FIVE 5
12 #define SIX 6
13 #define ONE_THOUSAND 1000
14 #define TEN 10
15 #define ONE_HUNDRED 100
16 #define LENGTH 61
17 #define TWENTY_ONE 21
18 #define ONE 1
19 #define ZERO 0
20
21 int check_file_args (int argc, char *argv[])
22 {
23 if (argc != TWO)
24 {
25 fprintf (stdout, "Number of arguments should be 2");
26 return EXIT_FAILURE;
27 }
28 if ((strcmp (argv[ONE], "by_duration") != ZERO) &&
29 (strcmp (argv[ONE], "by_distance") != ZERO)
30 && (strcmp (argv[ONE], "by_name") != ZERO)
31 && (strcmp (argv[ONE], "test") != ZERO))
32 {
33 fprintf (stdout, "argument is not defined");
34 return EXIT_FAILURE;
35 }
36 return EXIT_SUCCESS;
37 }
38
39 int get_num_of_buses ()
40 {
41 char lines_num_char[LENGTH];
42 while (ONE)
43 {
44 fprintf (stdout, "Enter number of lines. Then enter\n");
45 fgets (lines_num_char, LENGTH, stdin);
46 int lines_number = strtol (lines_num_char, NULL, TEN);
47 if (lines_number <= ZERO)
48 {
49 fprintf (stdout, "ERROR: give a positive number king\n");
50 }
51 else
52 {
53 return lines_number;
54 }
55 }
56 }
57
58 int check_arguments (char *name_of_bus, int *distance, int *duration)
59 {

5
60 char args[LENGTH];
61 int arg_checked = ONE;
62 while (arg_checked != ZERO) {
63 printf ("Enter line info. Then enter\n");
64 fgets (args, LENGTH, stdin);
65 int int_args = sscanf (args,\
66 "%[^,],%d,%d", name_of_bus, distance,duration);
67 // check number of args given
68 if (int_args != THREE) {
69 fprintf (stdout,\
70 "ERROR: Invalid input format. name,distance,duration\n");
71 arg_checked = ONE;
72 continue;}
73 // check duration input
74 if (*duration < TEN || *duration > ONE_HUNDRED) {
75 fprintf (stdout,\
76 "ERROR: Duration should be an integer between 10 and 100\n");
77 arg_checked = ONE;
78 continue;}
79 // check distance
80 if (*distance < ZERO || *distance > ONE_THOUSAND) {
81 fprintf (stdout,\
82 "ERROR: Distance should be an integer between 0 and "
83 "1000\n");
84 arg_checked = ONE; // Update arg_checked
85 continue;}
86 // check the name given for the bus
87 long len = strlen (name_of_bus);
88 int i = ZERO;
89 while (i < len) {
90 if (!isdigit(name_of_bus[i]) && !islower(name_of_bus[i])) {
91 fprintf (stdout,\
92 "ERROR: bus name must contain only digits or small chars\n");
93 arg_checked = 1;
94 break;
95 }
96 i++;
97 }
98 arg_checked = ZERO;
99 }
100 return EXIT_SUCCESS;
101 }
102
103 void create_busses_list (long busses_number, char *name_of_bus, BusLine
104 *bus_list)
105 {
106 int i = ZERO;
107 int distance = ZERO;
108 int duration = ZERO;
109 while (i < busses_number)
110 {
111 check_arguments (name_of_bus, &distance, &duration);
112 struct BusLine bus;
113 strcpy (bus.name, name_of_bus); // copies one string to another
114 bus.distance = distance;
115 bus.duration = duration;
116 bus_list[i] = bus;
117 i++;
118 }
119 }
120
121 void print_busses_from_list (BusLine *bus_list, int busses_number)
122 {
123 int i = ZERO;
124 while (i < busses_number)
125 {
126 fprintf (stdout, "%s,%d,%d\n", (bus_list[i]).name,
127 bus_list[i].distance, bus_list[i].duration);

6
128 i++;
129 }
130 }
131 void print_test_condition (int result, int number, int type)
132 {
133 if ((result == ZERO && type == ZERO) || (result == ONE && type == ONE))
134 {
135 fprintf (stdout, "TEST %d PASSED:\n", number);
136 }
137 else if ((result == ONE && type == ZERO) || (result == ZERO && type == ONE))
138 {
139 fprintf (stdout, "TEST %d FAILED:\n", number);
140 }
141 }
142
143 void tester_helper (BusLine *beginning, BusLine *de_end,
144 BusLine *new_beginning, BusLine *de_new_end)
145 {
146 quick_sort (new_beginning, de_new_end, DISTANCE);
147
148 int test_1 = is_sorted_by_distance (new_beginning, de_new_end);
149 print_test_condition (test_1, ONE, ZERO);
150
151 int test_2 = is_equal (new_beginning, \
152 de_new_end, beginning, de_end);
153 print_test_condition (test_2, TWO, ONE);
154 quick_sort (new_beginning, de_new_end, DURATION);
155
156 int test_3 = is_sorted_by_duration (new_beginning, de_new_end);
157 print_test_condition (test_3, THREE, ZERO);
158
159 int test_4 = is_equal (new_beginning, \
160 de_new_end, beginning, de_end);
161 print_test_condition (test_4, FOUR, ONE);
162
163 bubble_sort (new_beginning, de_new_end);
164 int test_5 = is_sorted_by_name (new_beginning, de_new_end);
165 print_test_condition (test_5, FIVE, ZERO);
166
167 int test_6 = is_equal (new_beginning, \
168 de_new_end, beginning, de_end);
169 print_test_condition (test_6, SIX, ONE);
170 }
171
172 int initiate_tests (int bus_number, BusLine *bus_list, char *bus_name)
173 {
174 BusLine *new_bus_list = malloc (bus_number * sizeof (BusLine));
175 memcpy (new_bus_list, bus_list, bus_number * sizeof (BusLine));
176 tester_helper (new_bus_list, new_bus_list + bus_number,
177 bus_list, bus_list + bus_number);
178
179 free (new_bus_list);
180 free (bus_list);
181 free (bus_name);
182 new_bus_list = NULL;
183 new_bus_list = NULL;
184 bus_name = NULL;
185 return EXIT_SUCCESS;
186 }
187
188 int main (int argc, char *argv[])
189 {
190 if ((check_file_args (argc, argv)) == EXIT_FAILURE)
191 {
192 return EXIT_FAILURE;
193 }
194 int buss_number = get_num_of_buses ();
195 char *bus_name = malloc ((TWENTY_ONE) * sizeof (char));

7
196 BusLine *busses_list = malloc (buss_number * sizeof (BusLine));
197 if (bus_name == NULL || busses_list == NULL)
198 {
199 fprintf (stdout, "ERROR: Failed to allocate memory\n");
200
201 // Free the allocated memory if one of the mallocs failed
202 free (bus_name);
203 free (busses_list);
204 busses_list = NULL;
205 bus_name = NULL;
206 return EXIT_FAILURE;
207 }
208 create_busses_list \
209 (buss_number, bus_name, busses_list);
210 if (strcmp (argv[ONE], "by_duration") == ZERO)
211 {
212 quick_sort \
213 (busses_list, busses_list + buss_number, DURATION);
214 print_busses_from_list (busses_list, buss_number);
215 }
216 if (strcmp (argv[ONE], "by_distance") == ZERO)
217 {
218 quick_sort \
219 (busses_list, busses_list + buss_number, DISTANCE);
220 print_busses_from_list (busses_list, buss_number);
221 }
222 if (strcmp (argv[ONE], "by_name") == ZERO)
223 {
224 bubble_sort (busses_list, busses_list + buss_number);
225 print_busses_from_list (busses_list, buss_number);
226 }
227 if (strcmp (argv[ONE], "test") == ZERO)
228 {
229 return initiate_tests(buss_number, busses_list, bus_name);
230 }
231 free (busses_list);
232 free (bus_name);
233 busses_list = NULL;
234 bus_name = NULL;
235 return EXIT_SUCCESS;
236 }

8
4 ex2/sort bus lines.h
1 #ifndef EX2_REPO_SORTBUSLINES_H
2 #define EX2_REPO_SORTBUSLINES_H
3 // write only between #define EX2_REPO_SORTBUSLINES_H and #endif
4 // EX2_REPO_SORTBUSLINES_H
5 #include <string.h>
6 #define NAME_LEN 21
7 /**
8 * Structure representing a bus line.
9 */
10 typedef struct BusLine
11 {
12 char name[NAME_LEN];
13 int distance, duration;
14 } BusLine;
15
16 /**
17 * Enum representing the sorting criteria.
18 */
19 typedef enum SortType
20 {
21 DISTANCE, // Sort by distance
22 DURATION // Sort by duration
23 } SortType;
24
25 /**
26 * Perform bubble sort on the given array of BusLine structures.
27 *
28 * @param start Pointer to the beginning of the array.
29 * @param end Pointer to the end of the array.
30 */
31 void bubble_sort(BusLine *start, BusLine *end);
32
33 /**
34 * Perform quick sort on the given array of BusLine structures.
35 *
36 * @param start Pointer to the beginning of the array.
37 * @param end Pointer to the end of the array.
38 * @param sort_type Sorting criteria: DISTANCE or DURATION.
39 */
40 void quick_sort(BusLine *start, BusLine *end, SortType sort_type);
41
42 /**
43 * Partition function for quick sort.
44 *
45 * @param start Pointer to the beginning of the array.
46 * @param end Pointer to the end of the array.
47 * @param sort_type Sorting criteria: DISTANCE or DURATION.
48 * @return Pointer to the partition point.
49 */
50 BusLine *partition(BusLine *start, BusLine *end, SortType sort_type);
51
52 #endif //EX2_REPO_SORTBUSLINES_H

9
5 ex2/sort bus lines.c
1 #include "sort_bus_lines.h"
2
3 // Function to swap two BusLine structures
4 void swap(BusLine *a, BusLine *b) {
5 BusLine temp = *a;
6 *a = *b;
7 *b = temp;
8 }
9
10 // Bubble Sort implementation
11 void bubble_sort(BusLine *start, BusLine *end) {
12 int n = end - start;
13 for (int i = 0; i < n - 1; i++) {
14 for (int j = 0; j < n - i - 1; j++) {
15 if (strcmp(start[j].name, start[j + 1].name) > 0) {
16 swap(&start[j], &start[j + 1]);
17 }
18 }
19 }
20 }
21
22
23 BusLine *partition(BusLine *start, BusLine *end, SortType sort_type) {
24 BusLine *pivot = end - 1;
25 BusLine *i = start - 1;
26
27 for (BusLine *j = start; j < end - 1; j++) {
28 if ((sort_type == DISTANCE && j->distance <= pivot->distance) ||
29 (sort_type == DURATION && j->duration <= pivot->duration)) {
30 i++;
31 swap(i, j);
32 }
33 }
34
35 swap(i + 1, pivot);
36 return i + 1;
37 }
38
39 void quick_sort(BusLine *start, BusLine *end, SortType sort_type) {
40 if (start < end) {
41 BusLine *pivot = partition(start, end, sort_type);
42 quick_sort(start, pivot, sort_type);
43 quick_sort(pivot + 1, end, sort_type);
44 }
45 }
46
47

10
6 ex2/test bus lines.h
1 #ifndef EX2_REPO_TESTBUSLINES_H
2 #define EX2_REPO_TESTBUSLINES_H
3 // write only between #define EX2_REPO_TESTBUSLINES_H and #endif
4 // EX2_REPO_TESTBUSLINES_H
5 #include "sort_bus_lines.h"
6
7 /**
8 * @brief Check if a range of BusLines is sorted by distance.
9 *
10 * This function checks whether a range of BusLines, defined by the pointers
11 * `start` and `end`, is sorted in ascending order based on their distances.
12 *
13 * @param start Pointer to the first BusLine in the range.
14 * @param end Pointer to the last BusLine in the range.
15 * @return 1 if the range is sorted by distance, 0 otherwise.
16 */
17 int is_sorted_by_distance (BusLine *start, BusLine *end);
18
19 /**
20 * @brief Check if a range of BusLines is sorted by duration.
21 *
22 * This function checks whether a range of BusLines, defined by the pointers
23 * `start` and `end`, is sorted in ascending order based on their durations.
24 *
25 * @param start Pointer to the first BusLine in the range.
26 * @param end Pointer to the last BusLine in the range.
27 * @return 1 if the range is sorted by duration, 0 otherwise.
28 */
29 int is_sorted_by_duration (BusLine *start, BusLine *end);
30
31 /**
32 * @brief Check if a range of BusLines is sorted by name.
33 *
34 * This function checks whether a range of BusLines, defined by the pointers
35 * `start` and `end`, is sorted in lexicographical order based on their names.
36 *
37 * @param start Pointer to the first BusLine in the range.
38 * @param end Pointer to the last BusLine in the range.
39 * @return 1 if the range is sorted by name, 0 otherwise.
40 */
41 int is_sorted_by_name (BusLine *start, BusLine *end);
42
43 /**
44 * @brief Check if two ranges of BusLines are equal.
45 *
46 * This function checks whether two ranges of BusLines are equal. The ranges
47 * are defined by the pointers `start_sorted` and `end_sorted` for the sorted
48 * range, and `start_original` and `end_original` for the original range.
49 *
50 * @param start_sorted Pointer to the first BusLine in the sorted range.
51 * @param end_sorted Pointer to the last BusLine in the sorted range.
52 * @param start_original Pointer to the first BusLine in the original range.
53 * @param end_original Pointer to the last BusLine in the original range.
54 * @return 1 if the ranges are equal, 0 otherwise.
55 */
56 int is_equal (BusLine *start_sorted, BusLine *end_sorted,
57 BusLine *start_original, BusLine *end_original);
58
59 #endif // EX2_REPO_TESTBUSLINES_H

11
7 ex2/test bus lines.c
1 #include "test_bus_lines.h"
2 #include <stdlib.h>
3 int is_sorted_by_distance (BusLine *start, BusLine *end)
4 {
5 int len = end - start;
6 for (int i = 0; i < len - 1; i++)
7 {
8 if ((start + i)->distance > (start + i + 1)->distance)
9 {
10 return EXIT_FAILURE;
11 }
12 }
13 return EXIT_SUCCESS;
14 }
15
16 int is_sorted_by_duration (BusLine *start, BusLine *end)
17 {
18 int len = end - start;
19 for (int i = 0; i <= len; i++)
20 {
21 if ((start + i)->duration > (start + i + 1)->duration)
22 {
23 return EXIT_FAILURE;
24 }
25 }
26 return EXIT_SUCCESS;
27 }
28
29 int is_sorted_by_name (BusLine *start, BusLine *end)
30 {
31 int len = end - start;
32 for (int i = 0; i <= len; i++)
33 {
34 if (strcmp ((start + i)->name, (start + i + 1)->name) > 0)
35 {
36 return EXIT_FAILURE;
37 }
38 }
39 return EXIT_SUCCESS;
40 }
41
42 int is_equal (BusLine *start_sorted, BusLine *end_sorted,
43 BusLine *start_original, BusLine *end_original)
44 {
45 int length_original = end_original - start_original;
46 int length_sorted = end_sorted - start_sorted;
47
48 if (length_original != length_sorted)
49 {
50 return 0;
51 }
52 for (int i = 0; i < length_original; i++)
53 {
54 int found_match = 0;
55
56 for (int j = 0; j < length_sorted; j++)
57 {
58 if (strcmp ((start_original + i)->name, (start_sorted + j)->name) == 0)
59 {

12
60 found_match = 1;
61 break;
62 }
63 }
64 if (!found_match)
65 {
66 return 0;
67 }
68 }
69 return 1;
70 }
71

13

You might also like