/*
*程序的版权和版本声明部分:
*Copyright(c)2014,烟台大学计算机学院学生
*All rights reserved.
*文件名称:
*作者:田成琳
*完成日期:2014 年 2月 24 日
*版本号:v1.0
*对任务及求解方法的描述部分:
*输入描述: 两个人的生日
*问题描述:编一个程序,求出两个人出生相差的天数
*程序输出:相差的天数
*问题分析:
*算法设计:
*/
#include <iostream>
using namespace std;
int abs(int);//绝对值函数
struct Date
{
int year;
int month;
int day;
};
struct People
{
char name[18];
struct Date birthday;
};
int main()
{
People a,b;
int yeara,days1,days2,c[13]= {0,31,28,31,30,31,30,31,31,30,31,30,31},yu,tian;
cout<<"请输入A姓名:"<<endl;
cin>>a.name;
cout<<"请输入A生日:"<<endl;
cin>>a.birthday.year>>a.birthday.month>>a.birthday.day;
cout<<"请输入B姓名:"<<endl;
cin>>b.name;
cout<<"请输入B生日:"<<endl;
cin>>b.birthday.year>>b.birthday.month>>b.birthday.day;
if(a.birthday.year<=b.birthday.year)
{
yeara=b.birthday.year-a.birthday.year;
days1=a.birthday.day;
for(int i=1; i<a.birthday.month; i++)
{
days1+=c[i];
}
if(a.birthday.month>2&&(a.birthday.year%4==0&&a.birthday.year%100!=0||a.birthday.year%400==0))
{
days1++;
}
days2=b.birthday.day;
for(int j=1; j<b.birthday.month; j++)
{
days2+=c[j];
}
if(b.birthday.month>2&&(b.birthday.year%4==0&&b.birthday.year%100!=0||b.birthday.year%400==0))
{
days2++;
}
yu=yeara/4;
tian=yeara*365+yu+days2-days1;
}
else
{
yeara=a.birthday.year-b.birthday.year;
days1=a.birthday.day;
for(int i=1; i<a.birthday.month; i++)
{
days1+=c[i];
}
if(a.birthday.month>2&&(a.birthday.year%4==0&&a.birthday.year%100!=0||a.birthday.year%400==0))
{
days1++;
}
days2=b.birthday.day;
for(int j=1; j<b.birthday.month; j++)
{
days2+=c[j];
}
if(b.birthday.month>2&&(b.birthday.year%4==0&&b.birthday.year%100!=0||b.birthday.year%400==0))
{
days2++;
}
yu=yeara/4;
tian=yeara*365+yu+days2-days1;
}
if(tian<0)
{
tian=-tian;
}
cout<<a.name<<"与"<<b.name<<"相差"<<tian<<"天."<<endl;
return 0;
}
运行结果:
心得体会:感觉老多写不少代码,但精华就那些,精而少的程序写不出来