无论如何,我可以比较使用指针制作的结构的结构吗?

时间:2019-04-13 12:18:49

标签: c sorting struct

我正在处理一个问题,其中要对日期进行排序,并且必须按其顺序显示输入(排序一个)。有两个日期,一个是母猪日期,另一个是收获日期。根据输入内容对它们进行排序和显示。

最初获取的农作物数量是用该名称输入的,即降雨,温度,播种日期,收获日期。作物通常是两种或两种以上,我只能写出两种作物的比较,但我需要两种以上。另外,情况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

1 个答案:

答案 0 :(得分:2)

这是家庭作业,因此我不会给出完整的解决方案,但是我会给出“要考虑的要点”。

        case 1 :
          for(i=0;i<num;i++)
          {
              if(list[i].rainfall>list[i+1].rainfall)  // (1)
                  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; // (2)
          }  // (3)

(1):您确定如果这是真的,那么您找到了最高的吗?

(2):您确定要打破for循环了吗?

(3):case 1:的结尾。接下来要执行的语句是什么?

注意:i+1的最大值是多少?该元素存在吗?


case 2:的相同评论
case 3: 显示以sowDate的升序排序的农作物

这意味着您必须对list进行排序。另外,该名称最好是cropArray之类的,因为它不是列表。

查找“ man quicksort”。它为您提供了许多C库中可用的排序功能。您必须编写比较两个母猪日期的比较函数。


case 4:好吧,这现在很容易。只需为quicksort编写另一个比较功能。