|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
代码是
#include<stdio.h>
int main()
{
int n;
scanf("%d",&n);
int day,h,m;
while(n>0)
{
scanf("%d %d:%d",&day,&h,&m);
if(day==0) printf("%d %02d:%02d\n",day,h,m);
else{
if(day%2!=0)
{
h=h/2;
if(h%2!=0) m=(m+60)/2;
if(h%2==0) m=m/2;
printf("%d %02d:%02d\n",day,h,m);
}
if(day%2==0)
{ h=h/2;
if(h%2!=0) m=(m+60)/2;
if(h%2==0) m=m/2;
printf("%d %02d:%02d\n",day-1,h+12,m+30);
}
}
n--;
}
}
编译结果显示
a.c: In function ‘main’:
a.c:5:2: warning: ignoring return value of ‘scanf’, declared with attribute warn_unused_result [-Wunused-result]
scanf("%d",&n);
^~~~~~~~~~~~~~
a.c:9:3: warning: ignoring return value of ‘scanf’, declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d:%d",&day,&h,&m);
^~~~~~~~~~~~~~~~~~~~~~~~~~~~
为什么啊
这个代码问题出在两个scanf函数调用时没有检查返回值,导致编译器发出警告。scanf函数返回值为成功读入的参数个数,如果返回值不等于期望的参数个数,说明读入失败。在这个代码中,如果读入失败,可能会导致后面的计算和输出出现错误。因此,应该在scanf函数调用后检查返回值,确保读入成功。可以将代码修改为如下形式:
#include<stdio.h>
int main()
{
int n;
if(scanf("%d",&n) != 1) {
printf("Failed to read n\n");
return 1;
}
int day,h,m;
while(n>0)
{
if(scanf("%d %d:%d",&day,&h,&m) != 3) {
printf("Failed to read day, h, m\n");
return 1;
}
if(day==0) printf("%d %02d:%02d\n",day,h,m);
else{
if(day%2!=0)
{
h=h/2;
if(h%2!=0) m=(m+60)/2;
if(h%2==0) m=m/2;
printf("%d %02d:%02d\n",day,h,m);
}
if(day%2==0)
{ h=h/2;
if(h%2!=0) m=(m+60)/2;
if(h%2==0) m=m/2;
printf("%d %02d:%02d\n",day-1,h+12,m+30);
}
}
n--;
}
return 0;
}
|
|