#include<stdio.h>
struct time{
int year;
int month;
int day;
};
struct book{
char name[61];
int n;
struct time y;
int p;
int s;
};
int sort1(struct time*y1,struct time*y2);
void swap(struct book *s1,struct book *s2);
void comb1(struct book *s1,struct book *s2);
int main()
{
int n,i,loc,j;
scanf("%d",&n);
getchar();
struct book shu[n];
for(i=0;i<n;i++){
scanf("%s",shu[i].name);
scanf("%d",&shu[i].n);
scanf("%d",&shu[i].y.year);
scanf("%d",&shu[i].y.month);
scanf("%d",&shu[i].y.day);
scanf("%d",&shu[i].p);
scanf("%d",&shu[i].s);
}
for(loc=n-1;loc>0;loc--){
for(j=0;j<loc;j++){
comb1(&shu[j],&shu[j+1]);
}
}
for(i=0;i<n;i++){
printf("%s %d %d %d %d %d %d\n",shu[i].name,shu[i].n,shu[i].y.year,shu[i].y.month,shu[i].y.day,shu[i].p,shu[i].s);
}
return 0;
}
int sort1(struct time*y1,struct time*y2)
{
if(y1->year<y2->year){
return 1;
}
else if(y1->year==y2->year){
if(y1->month<y2->month){
return 1;
}
else if(y1->month==y2->month){
if(y1->day<y2->day){
return 1;
}
else if(y1->day==y2->day){
return 0;
}
}
}
}
void swap(struct book *s1,struct book *s2)
{
struct book temp;
temp=*s1;
*s1=*s2;
*s2=temp;
}
void comb1(struct book *s1,struct book *s2)
{
if((s1->p)>(s2->p)){
swap(s1,s2);
}
else if((s1->p)==(s2->p)){
if(sort1(&(s1->y),&(s2->y))==1){
swap(s1,s2);
}
else if(sort1(&(s1->y),&(s2->y))==0){
if(s1->n<s2->n){
swap(s1,s2);
}
else if(s1->n==s2->n){
if(s1->s<s2->s){
swap(s1,s2);
}
}
}
}
}
你的sort1函数中少一种返回值
int sort1(struct time* y1, struct time* y2)
{
if (y1->year < y2->year) {
return 1;
}
else if (y1->year == y2->year) {
if (y1->month < y2->month) {
return 1;
}
else if (y1->month == y2->month) {
if (y1->day < y2->day) {
return 1;
}
else if (y1->day == y2->day) {
return 0;
}
}
}
return 2;//少一种类型
}
你的比较逻辑有问题,帮你修改好了
#include <stdio.h>
struct time
{
int year;
int month;
int day;
};
struct book
{
char name[61];
int n;
struct time y;
int p;
int s;
};
int compare_time(struct time *y1, struct time *y2);
int compare_book(struct book *s1, struct book *s2);
void swap(struct book *s1, struct book *s2);
int main()
{
int n, i, loc, j;
scanf("%d", &n);
getchar();
struct book shu[n];
for (i = 0; i < n; i++)
{
scanf("%s", shu[i].name);
scanf("%d", &shu[i].n);
scanf("%d", &shu[i].y.year);
scanf("%d", &shu[i].y.month);
scanf("%d", &shu[i].y.day);
scanf("%d", &shu[i].p);
scanf("%d", &shu[i].s);
}
for (loc = n - 1; loc > 0; loc--)
{
for (j = 0; j < loc; j++)
{
if (!compare_book(&shu[j], &shu[j + 1]))
swap(&shu[j], &shu[j + 1]);
}
}
for (i = 0; i < n; i++)
{
printf("%s %d %d %d %d %d %d\n", shu[i].name, shu[i].n, shu[i].y.year, shu[i].y.month, shu[i].y.day, shu[i].p, shu[i].s);
}
return 0;
}
// -1: y1 < y2
// 0: y1 == y2
// 1: y1 > y2
int compare_time(struct time *y1, struct time *y2)
{
if (y1->year < y2->year)
return -1;
if (y1->year > y2->year)
return 1;
if (y1->month < y2->month)
return -1;
if (y1->month > y2->month)
return 1;
if (y1->day < y2->day)
return -1;
if (y1->day > y2->day)
return 1;
return 0;
}
void swap(struct book *s1, struct book *s2)
{
struct book temp;
temp = *s1;
*s1 = *s2;
*s2 = temp;
}
// 返回1表示s1应该排在s2前,否则返回0
int compare_book(struct book *s1, struct book *s2)
{
// 比较价格:价格低的靠前
if (s1->p < s2->p)
return 1;
if (s1->p > s2->p)
return 0;
// 价格相同
// 比较出版时间:出版晚的靠前
int r = compare_time(&s1->y, &s2->y);
if (r > 0)
return 1;
if (r < 0)
return 0;
// 出版时间相同
// 比较销量:销量大的靠前
if (s1->n > s2->n)
return 1;
if (s1->n < s2->n)
return 0;
// 销量相同
// 比较用户评分:评分高的靠前
return s1->s > s2->s;
}
长度小于60,不满足
只有大小写字母,不满足