c - 如何按 C 中的子午线(AM 和 PM)排序?

标签 c

我在尝试让我的程序按上午和下午排序时遇到问题。这是我的作业要求的一部分,否则我会以 24 小时格式完成。 到目前为止,这是我的代码

#include <stdio.h>


struct date
{
    int month;
    int day;
    int year;
    int hour;
    int min;
    int meridian;
};


typedef enum {undergraduate, graduate, post_doc } StudentLevel;


struct student
{
    char name[20];
    long int banner_id;
    struct date date_of_enrollment;
    struct date time_of_enrollment;
    StudentLevel level;
}data[5];


int lessThan(struct student S1 , struct student S2) {
    struct date A = S1.date_of_enrollment;
    struct date B = S2.date_of_enrollment;
    struct date C = S1.time_of_enrollment;
    struct date D = S2.time_of_enrollment;
    if (A.year < B.year) return 1;
    else if (A.year == B.year && A.month < B.month) return 1;
    else if (A.year == B.year && A.month == B.month && A.day < B.day) return 1;
    else if (A.year == B.year && A.month == B.month && A.day == B.day && C.hour < D.hour) return 1;
    else if (A.year == B.year && A.month == B.month && A.day == B.day && C.hour == D.hour && C.min < D.min) return 1;
     else if (A.year == B.year && A.month == B.month && A.day == B.day && C.hour == D.hour && C.min == D.min & C.meridian < D.meridian) return 1;
    else return 0;
}


int sort(struct student *data)
{
    int i, j;
    struct student x;
    for (i = 0; i < 5; i++){
        for (j = 0; j < 5; j++){
            if (lessThan(data[i], data[j])){
                x = data[j];
                data[j] = data[i];
                data[i] = x;
            }
        }
    }
    return 0;
}


int main ()
{
    
    int i;
    printf ("Enter Name, Banner ID, the date of Enrollment (Month Day Year), the time of enrollemtn (Hour Min Meridian(am or pm) and Student Level(0=Undergraduate, 1=graduate, or 2=Post Doc): \n");
    
    for (i=0;i<2;i++)
        
        scanf ("%s %ld %d %d %d %d %d %d %d",data[i].name,&data[i].banner_id,
               &data[i].date_of_enrollment.month,&data[i].date_of_enrollment.day, &data[i].date_of_enrollment.year, &data[i].time_of_enrollment.hour, &data[i].time_of_enrollment.min, &data[i].time_of_enrollment.meridian, &data[i].level);
    
    
    printf ("You entered : \n");
    sort(data);
    for (i=0;i<2;i++)
        printf("%s %ld %d %d %d %d %d %d %d \n",data[i].name, data[i].banner_id,data[i].date_of_enrollment.month,data[i].date_of_enrollment.day,data[i].date_of_enrollment.year,data[i].time_of_enrollment.hour, data[i].time_of_enrollment.min, data[i].time_of_enrollment.meridian,data[i].level);
    
    
    return 0;
}

程序似乎不想按经络排序。我也不知道如何告诉它该怎么做,所以这完全是我的错。任何提示将不胜感激。

最佳答案

虽然很简单。您在上次比较中缺少一个 &:

C.min == D.min & C.meridian < D.meridian

应该是

C.min == D.min && C.meridian < D.meridian

关于c - 如何按 C 中的子午线(AM 和 PM)排序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20458918/

相关文章:

c++ - 抄送 : language CC not recognized

一个预处理器函数可以用来定义多个预处理器宏吗?

c - 试图理解排列生成

c - 如何从片段着色器中读回数据

c - 如何为 C 中的套接字提供自定义 IP?

c - 逐行替换txt中的字符串

c - 如何从 C 中的命令行参数读取 "string"?

c - 如何从 C 宏返回值?

c - C 中的 While 循环

c - ARM Linux 可执行文件神秘地运行在 x86_64 上