帮你把月份,日期输入过范围的条件都给加入了 看看适合吗
#include <iostream.h>
#include <fstream.h>
#include <stdlib.h>
#include <string.h>
void main()
{
int n,y,days;
do
{
fstream f;
f.open("date.txt",ios::out|ios::app);
cout<<"请输入年份:";cin>>n;
cout<<"请输入月份:";cin>>y;
cout<<"请输入日期:";cin>>days;
f<<n<<" "<<y<<" "<<days<<endl;
f.close();
int nextdays;
nextdays=days;
switch(y)
{
case 1:
if(days>31)
{
cout<<y<<"月不可能超过31天"<<endl;
}else
if(nextdays=days)
cout<<n<<"年1月"<<days<<"日"<<"是"<<n<<"年的第"<<days<<"天."<<endl;
break;
case 2:
if((n%4==0)&&(n%100!=0)||(n%400==0))
{
if(days>29)
{
cout<<"闰年的"<<y<<"月不可能超过29天"<<endl;
}else
if(nextdays=days)
{
cout<<n<<"年2月"<<days<<"日";
days=nextdays+31;
cout<<"是"<<n<<"年的第"<<days<<"天."<<endl;
}
}
else
{
if(days>28)
{
cout<<y<<"月不可能超过28天"<<endl;
}else
if(nextdays=days)
{
cout<<n<<"年2月"<<days<<"日";
days=nextdays+31;
cout<<"是"<<n<<"年的第"<<days<<"天."<<endl;
}
}
break;
case 3:
if((n%4==0)&&(n%100!=0)||(n%400==0))
{
if(days>31)
{
cout<<y<<"月不可能超过31天"<<endl;
}else
if(nextdays=days)
{
cout<<n<<"年3月"<<days;
days=31+29+nextdays;
cout<<"日"<<"是"<<n<<"年的第"<<days<<"天."<<endl;
}
}
else
{
if(days>31)
{
cout<<y<<"月不可能超过31天"<<endl;
}else
if(nextdays=days)
{
cout<<n<<"年3月"<<days<<"日";
days=31+28+nextdays;
cout<<"是"<<n<<"年的第"<<days<<"天."<<endl;
}
}
break;
case 4:
if((n%4==0)&&(n%100!=0)||(n%400==0))
{
if(days>30)
{
cout<<y<<"月不可能超过30天"<<endl;
}else
if(nextdays=days)
{
cout<<n<<"年"<<y<<"月"<<days<<"日";
days=31+29+31+nextdays;
cout<<"是"<<n<<"年的第"<<days<<"天."<<endl;
}
}
else
{
if(days>30)
{
cout<<y<<"月不可能超过30天"<<endl;
}else
if(nextdays=days)
{
cout<<n<<"年"<<y<<"月"<<days<<"日";
days=31+28+31+nextdays;
cout<<"是"<<n<<"年的第"<<days<<"天."<<endl;
}
}
break;
case 5:
if((n%4==0)&&(n%100!=0)||(n%400==0))
{
if(days>31)
{
cout<<y<<"月不可能超过31天"<<endl;
}else
if(nextdays=days)
{
cout<<n<<"年"<<y<<"月"<<days<<"日";
days=31+29+31+30+nextdays;
cout<<"是"<<n<<"年的第"<<days<<"天."<<endl;
}
}
else
{
if(days>31)
{
cout<<y<<"月不可能超过31天"<<endl;
}else
if(nextdays=days)
{
cout<<n<<"年"<<y<<"月"<<days<<"日";
days=31+28+31+30+nextdays;
cout<<"是"<<n<<"年的第"<<days<<"天."<<endl;
}
}
break;
case 6:
if((n%4==0)&&(n%100!=0)||(n%400==0))
{
if(days>30)
{
cout<<y<<"月不可能超过30天"<<endl;
}else
if(nextdays=days)
{
cout<<n<<"年"<<y<<"月"<<days<<"日";
days=31+29+31+30+31+nextdays;
cout<<"是"<<n<<"年的第"<<days<<"天."<<endl;
}
}
else
if(days>30)
{
cout<<y<<"月不可能超过30天"<<endl;
}else
if(nextdays=days)
{
cout<<n<<"年"<<y<<"月"<<days<<"日";
days=31+28+31+30+31+nextdays;
cout<<"是"<<n<<"年的第"<<days<<"天."<<endl;
}
break;
case 7:
if((n%4==0)&&(n%100!=0)||(n%400==0))
{
if(days>31)
{
cout<<y<<"月不可能超过31天"<<endl;
}else
if(nextdays=days)
{
cout<<n<<"年"<<y<<"月"<<days<<"日";
days=31+29+31+30+31+30+nextdays;
cout<<"是"<<n<<"年的第"<<days<<"天."<<endl;
}
}
else
if(days>31)
{
cout<<y<<"月不可能超过31天"<<endl;
}else
if(nextdays=days)
{
cout<<n<<"年"<<y<<"月"<<days<<"日";
days=31+28+31+30+31+30+nextdays;
cout<<"是"<<n<<"年的第"<<days<<"天."<<endl;
}
break;
case 8:
if((n%4==0)&&(n%100!=0)||(n%400==0))
{
if(days>31)
{
cout<<y<<"月不可能超过31天"<<endl;
}else
if(nextdays=days)
{
cout<<n<<"年"<<y<<"月"<<days<<"日";
days=31+29+31+30+31+30+31+nextdays;
cout<<"是"<<n<<"年的第"<<days<<"天."<<endl;
}
}
else
if(days>31)
{
cout<<y<<"月不可能超过31天"<<endl;
}else
if(nextdays=days)
{
cout<<n<<"年"<<y<<"月"<<days<<"日";
days=31+28+31+30+31+30+31+nextdays;
cout<<"是"<<n<<"年的第"<<days<<"天."<<endl;
}
break;
case 9:
if((n%4==0)&&(n%100!=0)||(n%400==0))
{
if(days>30)
{
cout<<y<<"月不可能超过30天"<<endl;
}else
if(nextdays=days)
{
cout<<n<<"年"<<y<<"月"<<days<<"日";
days=31+29+31+30+31+30+31+31+nextdays;
cout<<"是"<<n<<"年的第"<<days<<"天."<<endl;
}
}
else
if(days>30)
{
cout<<y<<"月不可能超过30天"<<endl;
}else
if(nextdays=days)
{
cout<<n<<"年"<<y<<"月"<<days<<"日";
days=31+28+31+30+31+30+31+31+nextdays;
cout<<"是"<<n<<"年的第"<<days<<"天."<<endl;
}
break;
case 10:
if((n%4==0)&&(n%100!=0)||(n%400==0))
{
if(days>31)
{
cout<<y<<"月不可能超过31天"<<endl;
}else
if(nextdays=days)
{
cout<<n<<"年"<<y<<"月"<<days<<"日";
days=31+29+31+30+31+30+31+31+30+nextdays;
cout<<"是"<<n<<"年的第"<<days<<"天."<<endl;
}
}
else
if(days>31)
{
cout<<y<<"月不可能超过31天"<<endl;
}else
if(nextdays=days)
{
cout<<n<<"年"<<y<<"月"<<days<<"日";
days=31+28+31+30+31+30+31+31+30+nextdays;
cout<<"是"<<n<<"年的第"<<days<<"天."<<endl;
}
break;
case 11:
if((n%4==0)&&(n%100!=0)||(n%400==0))
{
if(days>30)
{
cout<<y<<"月不可能超过30天"<<endl;
}else
if(nextdays=days)
{
cout<<n<<"年"<<y<<"月"<<days<<"日";
days=31+29+31+30+31+30+31+31+30+31+nextdays;
cout<<"是"<<n<<"年的第"<<days<<"天."<<endl;
}
}
else
if(days>30)
{
cout<<y<<"月不可能超过30天"<<endl;
}else
if(nextdays=days)
{
cout<<n<<"年"<<y<<"月"<<days<<"日";
days=31+28+31+30+31+30+31+31+30+31+nextdays;
cout<<"是"<<n<<"年的第"<<days<<"天."<<endl;
}
break;
case 12:
if((n%4==0)&&(n%100!=0)||(n%400==0))
{
if(days>31)
{
cout<<y<<"月不可能超过31天"<<endl;
}else
if(nextdays=days)
{
cout<<n<<"年"<<y<<"月"<<days<<"日";
days=31+29+31+30+31+30+31+31+30+31+30+nextdays;
cout<<"是"<<n<<"年的第"<<days<<"天."<<endl;
}
}
else
if(days>31)
{
cout<<y<<"月不可能超过31天"<<endl;
}else
if(nextdays=days)
{
cout<<n<<"年"<<y<<"月"<<days<<"日";
days=31+28+31+30+31+30+31+31+30+31+30+nextdays;
cout<<"是"<<n<<"年的第"<<days<<"天."<<endl;
}
break;
default:
days=0;
break;
}
if(days==0)
cout<<"月份输入错误!"<<endl;
system("pause");
}while(1);
fstream f;
f.open("date.txt",ios::in);
while(f>>n)
{
f>>y;
f>>days;
}
cout<<n<<" "<<y<<" "<<days<<endl;
f.close();
}