实验(四) 调试
验证型实验/必做
(保存电子版程序)
本部分的程序计算某年某月有几天,程序运行存在错误。请改正其中的语法错误使得程序编译成功。程序编译后,请比较输出结果与正确的实例输出,对可能存在的逻辑错误进行修改。
1. 正确的输入和输出
运行1:
enter year and month , example 1996, 5:
2006 , 5
2006 – 5 has 31 days
运行2:
enter year and month , example 1996, 5:
1996 , 2
2006 – 5 has 28 days
2. 错误的程序代码
/*debug.c*/
#include <stdio.h>
void main()
{ int year , month , days ;
printf(“\n enter year and month , example 1996, 5:\n”);
scanf(“%d %d”, &year , &month);
switch(month)
{ case 1 :
case 3 :
case 5 :
case 7 :
case 8:
case 10 :
case 12: days=31 ;
case 4: case 6: case 9 : case 11: days = 30 ;
case 2: days = 29;
default : printf(“input error!\n”)
}
printf(“%d-%d has %d days \n”, year , month , days);
}
3. 问题解决技巧
程序应对润年做出处理