Я работаю над проблемой, когда существует массив дат, которые должны быть отсортированы, и в их порядке должен отображаться вход (отсортированный).Есть две даты: одна - дата посева, другая - дата сбора урожая.На основе входных данных они должны быть отсортированы и отображены.
Первоначально берется количество культур, и с этим именем берутся количество осадков, температура, дата посева, дата сбора урожая.Урожай, как правило, два или более, я смог написать сравнение только для двух культур, но мне нужно больше двух.Кроме случаев 3 и 4 я не могу понять.Структуры Date
и структуры Crop
должны быть обязательными.
#include <stdio.h>
#include <stdlib.h>
struct Date {
int dd;
int mm;
int yyyy;
};
struct Crop {
char name[30];
float rainfall;
int temperature;
struct Date *sowDate;
struct Date *harvestDate;
} c;
int main(void) {
printf("Enter the number of Crops\n");
int num;
scanf("%d", &num);
struct Crop *list = (struct Crop *)malloc(num * sizeof(struct Crop));
int i, j;
for (i = 0; i < num; i++) {
printf("Enter the details of Crops %d\n", i + 1);
printf("Enter name\n");
scanf(" %s", list[i].name);
printf("Enter rainfall\n");
scanf("%f", &list[i].rainfall);
printf("Enter temperature\n");
scanf("%d", &list[i].temperature);
list[i].sowDate = (struct Date *)malloc(sizeof(struct Date));
list[i].harvestDate = (struct Date *)malloc(sizeof(struct Date));
printf("Enter sowDate\n");
scanf("%d %d %d", &list[i].sowDate->dd, &list[i].sowDate->mm, &list[i].sowDate->yyyy);
printf("Enter harvestDate\n");
scanf("%d %d %d", &list[i].harvestDate->dd, &list[i].harvestDate->mm, &list[i].harvestDate->yyyy);
}
printf("Menu\n");
printf("1)Crop that needs the highest rainfall\n");
printf("2)Crop that needs the highest temperature\n");
printf("3)Display the crop sorted in ascending order of the sowDate\n");
printf("4)Display the crop sorted in ascending order of the harvestDate\n");
printf("Enter your Choice\n");
scanf("%d", &j);
switch (j) {
case 1:
for (i = 0; i < num; i++) {
if (list[i].rainfall > list[i + 1].rainfall)
printf("Crop that needs the highest rainfall is %s\n", list[i].name);
else
printf("Crop that needs the highest rainfall is %s\n", list[i + 1].name);
break;
}
case 2:
for (i = 0; i < num; i++) {
if (list[i].temperature > list[i + 1].temperature)
printf("Crop that needs highest temperature is %s\n", list[i].name);
else
printf("Crop that needs highest temperature is %s\n", list[i + 1].name);
break;
}
case 3:
for (i = 0; i < num; i++)
break;
case 4:
for (i = 0; i < num; i++)
break;
default:
exit(0);
}
return 0;
}
Вот взаимодействие программы:
Enter the number of Crops
2
Enter the details of Crops 1
Enter name
Rice
Enter rainfall
15
Enter temperature
23
Enter sowDate
25 7 2016
Enter harvestDate
30 11 2016
Enter the details of Crops 2
Enter name
Wheat
Enter rainfall
7
Enter temperature
29
Enter sowDate
24 7 2016
Enter harvestDate
31 11 2016
Menu
1)Crop that needs the highest rainfall
2)Crop that needs the highest temperature
3)Display the crop sorted in ascending order of the sowDate
4)Display the crop sorted in ascending order of the harvestDate
Enter your Choice
1
Crop that needs the highest rain fall is Rice
Menu
1)Crop that needs the highest rainfall
2)Crop that needs the highest temperature
3)Display the crop sorted in ascending order of the sowDate
4)Display the crop sorted in ascending order of the harvestDate
Enter your Choice
2
Crop that needs the highest temperature is Wheat
Menu
1)Crop that needs the highest rainfall
2)Crop that needs the highest temperature
3)Display the crop sorted in ascending order of the sowDate
4)Display the crop sorted in ascending order of the harvestDate
Enter your Choice
3
Wheat
7.00
29
24 7 2016
31 11 2016
Rice
15.00
23
25 7 2016
30 11 2016
Menu
1)Crop that needs the highest rainfall
2)Crop that needs the highest temperature
3)Display the crop sorted in ascending order of the sowDate
4)Display the crop sorted in ascending order of the harvestDate
Enter your Choice
4
Rice
15.00
23
25 7 2016
30 11 2016
Wheat
7.00
29
24 7 2016
31 11 2016
Menu
1)Crop that needs the highest rainfall
2)Crop that needs the highest temperature
3)Display the crop sorted in ascending order of the sowDate
4)Display the crop sorted in ascending order of the harvestDate
Enter your Choice
5
EXIT