第十七周项目四—生日差几天

问题及代码:
/*
 * Copyright (c) 2014, 烟台大学计算机学院
 * All rights reserved.
 * 文件名称:test.cpp
 * 作    者:刘佳琦
 * 完成日期:2014年 12 月 21 日
 * 版 本 号:v1.0
 *
 * 问题描述:输入两个人的生日,求出他们相差多少天。
 * 输入描述:两个人的生日
 * 程序输出:生日相差多少天
 */
#include <iostream>
using namespace std;
struct Date
{
    int year;
    int month;
    int day;
};
struct Student
{
    char name[2];
    Date birthday;
}student1,student2;
int main()
{
    int compare(struct Student);
    int days1,days2,days3;
    cout<<"第一位学生信息:";
    cin>>student1.name>>student1.birthday.year>>student1.birthday.month>>student1.birthday.day;
    cout<<"第二位学生信息:";
    cin>>student2.name>>student2.birthday.year>>student2.birthday.month>>student2.birthday.day;
    days1=compare(student1);
    days2=compare(student2);
    days3=days1-days2;
    if(days3<0)
        days3=-days3;
    cout<<days1<<" "<<days2<<" "<<days3;
}
int compare(struct Student x)
{
    int days=0,i,a;
    //计算days
    for(i=1; i<x.birthday.month; i++)
    {
        if(i==1||i==3||i==5||i==7||i==8||i==10||i==12)
            a=31;
        else if(i==2)
        {
            if((x.birthday.year)%4==0&&(x.birthday.year)%100!=0||(x.birthday.year)%400==0)
                a=29;
            else
                a=28;
        }
        else
            a=30;
        days+=a;
    }
    days+=x.birthday.day;
    return days;
}


运行结果:

学习心得:这道题小错误不断,但是通过不断改正和自己找错误中也学会好多。弄清了结构体定义,变量名,调用的运用。在调用里用结构体,必须将前面的定义写完整。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值