Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

C++日历错误 求大牛解决

Posted by LT1201 at 2010-03-10 16:45:35
#include<iostream>
#include<iomanip>

using namespace std;

class Date
{
private:
	int year;
	int month;
	int week;
public:
	void input();
	int nyear(int);
	int nmonth(int,int);
	void nweek();
	void output();
};

void Date::input()
{
	cout<<"年:";
	cin>>year;
	cout<<"月:";
	cin>>month;
}

int Date::nyear(int y)
{
	int result;
	if(year%4==0&&(year%100!=0||year%100==0&&year%400!=0))
		result=366;
	else
		result=365;

	return result;
}

int Date::nmonth(int m,int y)
{
	int result;
	if(m==1||m==3||m==5||m==7||m==8||m==10||m==12)
		result=31;
	else if(nyear(y)==366&&month==2)
		result=29;
	else if(nyear(y)==365&&month==2)
		result=28;
	else
		result=30;

	return result;
}

void Date::nweek()
{
	int i,j;
	int sum=0;
	if(year>1980)
		for(i=1980;i<year;i++)
			sum+=nyear(i);
	if(month>1)
		for(j=1;j<month;i++)
			sum+=nmonth(j,year);

	week=(2+sum)%7;
}

void Date::output()
{
	nweek();

	int d,w;
	int i;
	cout<<setw(6)<<setiosflags(ios::right)<<"Mon"
		<<setw(6)<<setiosflags(ios::right)<<"Tue"
		<<setw(6)<<setiosflags(ios::right)<<"Wed"
		<<setw(6)<<setiosflags(ios::right)<<"Thu"
		<<setw(6)<<setiosflags(ios::right)<<"Fri"
		<<setw(6)<<setiosflags(ios::right)<<"Sat"
		<<setw(6)<<setiosflags(ios::right)<<"Sun"<<endl;
	for(i=1;i<week;i++)
		cout<<setw(6)<<setiosflags(ios::right)<<"";
	for(d=1,w=week;d<=nmonth(month,year);d++,w++)
	{
		if(w<=7)
		{
			cout<<setw(6)<<setiosflags(ios::right)<<d;
			if(w==7)
			{
				cout<<endl;
				w=1;
			}
		}
	}
}

int main()
{
	Date d;
	d.input();
	d.output();
	cout<<endl;
    
	return 0;
}
	
	


Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator