程序代码如下:
#include<iostream.h>
#include<stdio.h>
#include<stdlib.h>
int d[2][12]={{31,28,31,30,31,30,31,31,30,31,30,31},{31,29,31,30,31,30,31,31,30,31,30,31}};
class Data
{
public:
Data():sum(0)
{
data=0;
month=0;
year=1900;
}
Data(int d,int m,int y):sum(0)
{
data=d;
month=m;
year=y;
}
bool define(int x)
{
if((x%4==0&&x%100!=0)||(x%400==0))
return true;
else
return false;
}
bool defineyear()
{
if((year%4==0&&year%100!=0)||(year%400==0))
return true;
else
return false;
}
void inputdata()
{
while(f<=3)
{
cout<<"请输入年"<<endl;
int yy;
cin>>yy;
if(year<1900)
{
cout<<"底于最小值"<<endl;
f=f+1;
}
else if(year>3000)
{
cout<<"大于最大值"<<endl;
f=f+1;
}
else
{
year=yy;
}
cout<<"请输入月值"<<endl;
int mm;
cin>>mm;
if(mm<=12&&mm>=1)
{
month=mm;
}
else
{
cout<<"输入月值有误"<<endl;
f=f+1;
}
cout<<"输入日值"<<endl;
int dd;
cin>>dd;
enum M1={1,3,5,7,8,10,12};//枚举应该怎么定义 我C没学好 基础有点差 希望大家指点指点吧
enum M2={4,6,9,11};
if((dd>=1&&dd<=30&&month==M2)||(dd>=1&&dd<=31&&month==M1))
{
data=dd;
}
else if(defineyear()&&month==2)
{
data=29;
}
else if(!defineyear()&&month==2)
{
data=28;
}
else
{
cout<<"输入的日值有误"<<endl;
f=f+1;
}
}
if(f>3)
{
cout<<"三次输入错误程序将终止"<<endl;
system("pause");
}
}
void sumdata()
{
int n=0;
for(int i=1900;i<=year;i++)
{
if(define(i))
{
n=n+1;
}
}
sum=sum+(year-1900-n)*365+n*366;
if(defineyear())
{
sum=sum+d[1][month-1];
}
else
{
sum=sum+d[0][month-1];
}
sum=sum+data;
}
friend Data operator-(const Data &data1,const Data &data2)
{
Data s;
s.sum=data1.sum-data2.sum;
return s;
}//为什么去掉friend就不对了 如果不用friend 那该怎么做呢?
friend ostream & operator<<(ostream &out,const Data &other)
{
out<<other.sum;
return out;
}//同上个注释
private:
int year;
int month;
int data;
int f;
int sum;
};
void main(void)
{
Data x1;
Data x2;
x1.inputdata();
x2.inputdata();
cout<<x1-x2<<endl;
}
让大家见笑了 请指点指点吧 出的问题用注释标出来了 谢谢大家了