嘿伙计们,我有问题试图让我的节目按上午和下午排序。这是我的任务要求的一部分,或者我将以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;
}
该程序似乎不想按经络排序。我也不知道如何告诉它如何做到这一点,这完全是我的错。任何提示将非常感谢。
答案 0 :(得分:1)
虽然很简单。您在上次比较中缺少一个&
:
C.min == D.min & C.meridian < D.meridian
应该是
C.min == D.min && C.meridian < D.meridian