要变得更强。
今天上课刚讲了结构体,就来玩玩啦。
问题描述
操作系统中在查找文件时会按照某个规则对文件排序,例如下图为按照文件修改日期逆序排
序(最后修改的排在最前面)。
但目前操作系统不支持同时按照多个字段进行排序。现在请你写一个程序能够同时按照修改
日期和文件大小对文件进行排序,排序规则为:
输出说明
将输入数据按题目描述的规则排序后输出,每行输出一个文件的修改日期和文件大小。
输入样例
8 2018/1/8 1024
2012/10/31 256
2014/10/29 300
2012/10/31 457
2014/10/27 512
2011/10/27 95
2014/11/3 1102
2017/11/24 1535
输出样例
2018/1/8 1024
2017/11/24 1535
2014/11/3 1102
2014/10/29 300
2014/10/27512
2012/10/31 457
2012/10/31 256
2011/10/27 95
我的代码
#include<stdio.h>
typedef struct{
int memory;
struct{
int year,month,day;
}date;
}PROFILE;
int main(){
int n,i,j,temp1,temp2,temp3,temp4;
PROFILE files[100];
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d/%d/%d %d",&files[i].date.year,&files[i].date.month,&files[i].date.day,&files[i].memory);
for(i=0;i<n;i++){
for(j=0;j<n-i-1;j++){
if(files[j].date.year<files[j+1].date.year){
temp1=files[j].date.year;
files[j].date.year=files[j+1].date.year;
files[j+1].date.year=temp1;
temp2=files[j].date.month;
files[j].date.month=files[j+1].date.month;
files[j+1].date.month=temp2;
temp3=files[j].date.day;
files[j].date.day=files[j+1].date.day;
files[j+1].date.day=temp3;
temp4=files[j].memory;
files[j].memory=files[j+1].memory;
files[j+1].memory=temp4;
}
if(files[j].date.year==files[j+1].date.year){
if(files[j].date.month<files[j+1].date.month){
temp2=files[j].date.month;
files[j].date.month=files[j+1].date.month;
files[j+1].date.month=temp2;
temp3=files[j].date.day;
files[j].date.day=files[j+1].date.day;
files[j+1].date.day=temp3;
temp4=files[j].memory;
files[j].memory=files[j+1].memory;
files[j+1].memory=temp4;
}
if(files[j].date.month==files[j+1].date.month){
if(files[j].date.day<files[j+1].date.day){
temp3=files[j].date.day;
files[j].date.day=files[j+1].date.day;
files[j+1].date.day=temp3;
temp4=files[j].memory;
files[j].memory=files[j+1].memory;
files[j+1].memory=temp4;
}
if(files[j].date.day==files[j+1].date.day){
if(files[j].memory<files[j+1].memory){
temp4=files[j].memory;
files[j].memory=files[j+1].memory;
files[j+1].memory=temp4;
}
}
}
}
}
}
for(i=0;i<n;i++){
printf("%d/%d/%d %d\n",files[i].date.year,files[i].date.month,files[i].date.day,files[i].memory);
}
}
不过老实说,结构体数组的调用,字母真的好多好长啊!!
复制粘贴真麻烦(滑稽
因篇幅问题不能全部显示,请点此查看更多更全内容