C语言结构体电脑文件筛选查询程序
【问题描述】从键盘输入 n 个文件的信息,包括:文件名、文件类型(用扩展名表示即可)、创建日期和最后修改日期(日期包括年、月、日,用结构体来定义),
本题中筛选的条件为最后修改时间和创建时间的差大于等于100天。试输出所有查到的文件(若没有满足条件的文件,则输出第一个文件的信息,并标注为 Not Found)。
【输入形式】输入文件数量 n 和 n 个文件数据。
【输出形式】输出查找的结果。
【样例输入】
3
rr txt 2016 4 3 2016 4 3
oj txt 2015 3 6 2015 11 2
y6 txt 2015 6 8 2015 6 11
【样例输出】
oj txt 2015 3 6 2015 11 2
【样例输入】
3
zz pdf 2015 8 8 2015 8 10
ru txt 2016 4 23 2016 4 23
ki doc 2016 3 6 2016 3 12
【样例输出】
Not Found
zz pdf 2015 8 8 2015 8 10
#include<stdio.h>
#include<stdlib.h>
struct Date
{
int year, month, day;
};
struct File_information
{
char name[12]; //文件名
char type[12]; //类型
struct Date establish, alter; //创建,修改日期
int interval_days = 0; //间隔天数
};
#define N 36
int main()
{
int Calculate(File_information x); //声明函数,计算时间差
struct File_information files[N];
int n, i, m;
m = i = 0;
scanf_s("%d", &n);
for (i = 0; i < n; i++)
{
//scanf_s("%s %s %d %d %d %d %d %d", files[i].name, 12, files[i].type, 3, &files[i].establish.year, &files[i].establish.month, &files[i].establish.day, &files[i].alter.year, &files[i].alter.month, &files[i].alter.day);
scanf_s("%s", files[i].name, 12);
scanf_s("%s", files[i].type, 12);
scanf_s("%d %d %d", &files[i].establish.year, &files[i].establish.month, &files[i].establish.day);
scanf_s("%d %d %d", &files[i].alter.year, &files[i].alter.month, &files[i].alter.day);
files[i].interval_days = Calculate(files[i]);
}
for (i = 0; i < n; i++)
{
if (files[i].interval_days >= 100)
{
printf("%s %s %d %d %d %d %d %d \n", files[i].name, files[i].type, files[i].establish.year, files[i].establish.month, files[i].establish.day, files[i].alter.year, files[i].alter.month, files[i].alter.day);
m += 1;
}
}
if (m == 0)
{
printf("Not Found \n");
printf("%s %s %d %d %d %d %d %d \n", files[0].name, files[0].type, files[0].establish.year, files[0].establish.month, files[0].establish.day, files[0].alter.year, files[0].alter.month, files[0].alter.day);
}
system("pause");
return 0;
}
int Calculate(File_information x) //函数定义,计算时间差
{
int Calculate_Days(int y, int m, int d); //函数声明,计算天数
int z, m, n;
//printf("创建日期:%d %d %d \n", x.establish.year, x.establish.month, x.establish.day); //验证变量的值是否正确传递
//printf("修改日期:%d %d %d \n", x.alter.year, x.alter.month, x.alter.day); //验证变量的值是否正确传递
m = Calculate_Days(x.establish.year, x.establish.month, x.establish.day);
n = Calculate_Days(x.alter.year, x.alter.month, x.alter.day);
z = n - m;
//printf("创建日期与修改日期的时间差:%d \n", z); //输出验证计算是否正确
return z;
}
int Calculate_Days(int y, int m, int d) //函数定义,计算天数
{
int z;
switch (m)
{
case 1:z = d; break;
case 2:z = 31 + d; break;
case 3:z = 59 + d; break;
case 4:z = 90 + d; break;
case 5:z = 120 + d; break;
case 6:z = 151 + d; break;
case 7:z = 181 + d; break;
case 8:z = 212 + d; break;
case 9:z = 243 + d; break;
case 10:z = 273 + d; break;
case 11:z = 304 + d; break;
case 12:z = 334 + d; break;
default:printf("Enter error! \n");
}
if (((y % 4 == 0 && y % 100 != 0) || y % 400 == 0) && m > 2)
z = z + 1;
//printf("%d年%d月%d日是该年的第%d天 \n", y, m, d, z); //检验计算是否正确
return z;
}
运行案例:
案例一
案例二
说明:
1.输入语句分开输入,不一次性输入,否则会报错,不知道为啥
2.计算时间间隔时,需是同一年,否则会出错,可以在源程序代码中加入相关语句进行修改,使其可以跨年计算天数
3.日期和文件信息可以定义为两个结构体,利用结构体嵌套来完成题目要求
4.使用system(“pause”);的原因:使运行结果暂停;使用该语句需包含头文件:#include<stdlib.h>