c语言第七次作业

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

第七次作业:结构体

1.计算日期的差值

(1)编写一函数,计算两个日期之间的时间差,并将其值返回。

日期以年、月、日表示。

“时间差”以天数表示。

注意考虑日期之间的闰年。

函数的输入参数为日期1和日期2,

函数的返回值为时间差,单位为天数。

(2)编写一程序,在主函数中输入两个日期,调用上述函数计算两个日期之间的时间差,并将结果输出。

为了计算简便,假设用户输入的日期1总是早于日期2。

#include

#include

struct date

{int day;int month;int year;}date1,date2;

int totaldays(struct date *p,struct date *q)

{

int years,total=0,i;

int monthday[12]={31,28,31,30,31,30,31,31,30,31,30,31};

years=q->year-p->year;

if(years==0)

{

if(p->month==q->month)

total=q->day-p->day;

else

{for(i=p->month;imonth-1;i++)

total+=monthday[i];

total+=monthday[p->month-1]-p->day+q->day;}

if(((q->year)%4==0&&(q->year)%100!=0)||(q->year)%400==0)

if(p->month<=2||q->month>2)

total++;

}

else if(years!=0)

{

for(i=p->month;i<12;i++)total+=monthday[i];

total+=monthday[p->month-1]-p->day;

for(i=0;imonth-1;i++)total+=monthday[i];

total+=q->day;

for(i=p->year+1;i<(q->year);i++)

{

total+=365;

if((i%4==0&&i%100!=0)||i%400==0)

total++;

}

if(((p->year%4==0&&p->year%100!=0)||p->year%400==0)&&p->month<=

2)total++;

if(((q->year%4==0&&q->year%100!=0)||q->year%400==0)&&q->month>2 )total++;

}

return total;

}

void main()

{

int days;

printf("please input date1(year,month,day):");

scanf("%d,%d,%d",&date1.year,&date1.month,&date1.day);

printf("please input date2(year,month,day):");

scanf("%d,%d,%d",&date2.year,&date2.month,&date2.day);

days=totaldays(&date1,&date2);

printf("days=%d\n",days);

}

2.结构体数组应用

请定义一个描述学生基本信息的结构,包括姓名,学号,籍贯,身份证号,年龄,家庭住址,性别,联系方式等。并定义一个结构体数组。编程:

a)编写函数input() , 输入基本信息(3~5条记录);

b)编写函数print(),输出全体记录信息;

c)编写函数search(), 检索一个指定的学生信息并返回, 由主函数打印到屏幕上;

d)说明,访问结构的时候,什么时候应该用运算符“.”,什么时候应该用运算符“->”。#include

#include

struct student

{char name[20];int num;char home[20];char IDnum[20];int age;char addr[50];char sex;char phone[20];}stu[10];

void input(struct student *p)

{

scanf("%s %d %s %s %d %s %c %s",&p->name,&p->num,&p->home,&p->IDn um,&p->age,&p->addr,&p->sex,&p->phone);

}

void print(struct student *p)

{

printf("%s,%d,%s,%s,%d,%s,%c,%s\n",p->name,p->num,p->home,p->IDnum ,p->age,p->addr,p->sex,p->phone);

}

int search(struct student stu[],int n,char stu_name[20])

{

int i;

for(i=0;i

{

if(strcmp(stu_name,stu[i].name)==0){return i;break;}

else continue;

}

return n+1;

相关文档
最新文档