Task:
From the keyboard, enter a sequence of records with information about the capitals of European states: , , , . A public holiday date is specified as a character string in the form Day of the Month, for example August 24. Then rearrange the data in calendar order according to the dates of the main public holiday.
Attempt:
The code below is a bit complex and long, I would like to either split it into functions or replace it altogether with something simpler
const char *months[MONTHS_NUM] = {
"jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct",
"nov", "dec"
};
typedef struct capital {
char name[20];
long int population;
char holiday[60];
char date[15];
} CAPITAL;
void
Sort_by_Date(CAPITAL arr[], int nc)
{
int i, j;
CAPITAL temp;
int day1, day2;
char month1[4], month2[4];
int month1_idx, month2_idx;
for (i = 0; i < nc - 1; i++) {
for (j = i + 1; j < nc; j++) {
sscanf(arr[i].date, "%d %3s", &day1, month1);
sscanf(arr[j].date, "%d %3s", &day2, month2);
for (month1_idx = 0; month1_idx < MONTHS_NUM; month1_idx++) {
if (strcmp(months[month1_idx], month1) == 0)
break;
}
for (month2_idx = 0; month2_idx < MONTHS_NUM; month2_idx++) {
if (strcmp(months[month2_idx], month2) == 0)
break;
}
if (month1_idx != month2_idx)
return month1_idx < month2_idx;
else if (day1 != day2)
return day1 < day2;
if (month1_idx > month2_idx) {
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
else if (month1_idx == month2_idx) {
if (day1 > day1) {
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
}
}