C语言报表日期合法性检查:验证输入长度和日期格式
#include <stdio.h>
int isValidDate(int date) { int year = date / 10000; // 获取年份 int month = date % 100; // 获取月份
if (year >= 2015 && year <= 2019 && month >= 1 && month <= 12) {
return 1; // 日期合法
} else {
return 0; // 日期不合法
}
}
int main() { int reportDate;
printf('请输入报表日期(格式:年份月份):');
scanf('%d', &reportDate);
// 检查输入是否为六位数字
if (reportDate < 100000 || reportDate > 999999) {
printf('输入的日期格式不正确\n');
return 0;
}
if (isValidDate(reportDate)) {
printf('报表日期合法\n');
} else {
printf('报表日期不合法\n');
}
return 0;
}
在修改后的程序中,先对输入的报表日期进行了长度检查,确保输入为六位数字。如果输入不满足六位数字的要求,则输出错误提示信息并结束程序。然后再继续进行报表日期的合法性检查,输出相应的结果。
原文地址: https://www.cveoy.top/t/topic/zTZ 著作权归作者所有。请勿转载和采集!