看看这个行不?
#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),f1(0),f2(0),f3(0)
{
data=0;
month=0;
year=1900;
}
Data(int d,int m,int y):sum(0),f1(0),f2(0),f3(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(f1<=3)
{
cout<<"请输入年"<<endl;
int yy;
cin>>yy;
if(year<1900)
{
cout<<"底于最小值"<<endl;
f1=f1+1;
continue;
}
else if(year>3000)
{
cout<<"大于最大值"<<endl;
f1=f1+1;
continue;
}
else
{
year=yy;
break;
}
}
while(f2<=3)
{
cout<<"请输入月值"<<endl;
int mm;
cin>>mm;
if(mm<=12&&mm>=1)
{
month=mm;
break;
}
else
{
cout<<"输入月值有误"<<endl;
f2=f2+1;
continue;
}
}
while(f3<=3)
{
cout<<"输入日值"<<endl;
int dd;
cin>>dd;
int m1=1,m2=3,m3=5,m4=7,m5=8,m6=10,m7=12;//枚举应该怎么定义 我C没学好 基础有点差 希望大家指点指点吧
int n1=4,n2=6,n3=9,n4=11;
if((dd>=1&&dd<=30&&((month==n1)||(month==n2)||(month==n3)||(month==n4)))||(dd>=1&&dd<=31&&(month==m1||month==m2||month==m3||month==m4||month==m5||month==m6||month==m7)))
{
data=dd;
break;
}
else if(defineyear()&&month==2)
{
data=29;
break;
}
else if(!defineyear()&&month==2)
{
data=28;
break;
}
else
{
cout<<"输入的日值有误"<<endl;
f3=f3+1;
continue;
}
}
if(f1+f2+f3>=10)
{
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 temp;
temp.sum=data1.sum-data2.sum;
return temp;
}//为什么去掉friend就不对了 如果不用friend 那该怎么做呢?
friend ostream & operator<<(ostream &out,const Data &other)
{
out<<other.sum;
return out;
}//同上个注释
double getsum()
{
return sum;
}
private:
int year;
int month;
int data;
int f1,f2,f3;
double sum;
};
void main(void)
{
Data x1;
Data x2;
x1.inputdata();
x1.sumdata();
cout<<x1.getsum();
x2.inputdata();
x2.sumdata();
cout<<x2.getsum();
cout<<endl;
cout<<x1-x2<<endl;
}