#include <stdio.h>
int main()
{
/* 定义需要计算的日期 */
int year;
int month;
int day;
printf("请输入年月日\n");
scanf_s("%d,%d,%d",&year,&month,&day);
int sum;
switch (month)
{
case 1:sum = 0; break;
case 2:sum = 31; break;
case 3:sum = 59; break;
case 4:sum = 89; break;
case 5:sum = 120; break;
case 6:sum = 150; break;
case 7:sum = 181; break;
case 8:sum = 212; break;
case 9:sum = 242; break;
case 10:sum = 273; break;
case 11:sum = 303; break;
case 12:sum = 334; break;
default:printf("一年中只有十二个月哦");
}
if (year % 400 ==0 || year % 4 ==0 && year%100 != 0)
{
if (month > 2)
{
sum = sum + day + 1;
}
else
{
sum = sum + day;
}
printf("%d年%d月中的第%d天\n",year,month ,sum);
}
return 0;
}
在VS中这是因为scanf()在读取数据时不检查边界,所以可能会造成内存访问越界;
---对于某些程序而言,scanf 与 scanf_s 都是一样的。主要看是否造成 内存访问越界 问题
如果不让VS进行这项检查,可以加上:
#define _CRT_SECURE_NO_WARNINGS
#define _CRT_SECURE_NO_DEPRECATE
#pragma warning(disable:4996);