首页 > 试题广场 >

编写一个函数。当给出月份号,程序返回一年中到该月为止(包括该

[问答题]
编写一个函数。当给出月份号,程序返回一年中到该月为止(包括该月)总共的天数。假定在外部声明了以下的结构模板和一个该结构的数组。
#include <stdio.h>
struct house {
float sqft;
int rooms;
int stories;
char address[40];
};
int main (void)
{
struct house fruzt = {1560.0, 6, 1, "22 Spiffo Road"};
struct house *sign;
sign = &fruzt;
printf ("%d %d\n", fruzt.rooms, sign->stories);
printf ("%s \n", fruzt.address);
printf ("%c %c\n", sign->address[3], fruzt.address[4]);
return 0;
}

推荐
extern struct month months[ ];
int days (int month)
{
int index, total;
if (month < 1 l l month > 12)
return (-1);   /* 错误标志  */
else
{
for (index = 0, total = 0; index < month; index ++)
total += months[index] .days;
return (total);
}
}
注意index比月号小1,因为数组的下标是从0开始的。因此使用index<month来代替index<=month。
发表于 2018-03-23 21:50:12 回复(0)