万年历的框架(ing)还没完成,只是一个类
本帖最后由 popo2008 于 2010-12-4 15:14 编辑原来没写说类还一个函数没写完,先保存一下~~在VS2010编译
/********************************************************************************************************
* PROGRAM : Calendar 0.11(DEC.3 0.1)
* DATE - TIME: Methew DEC. 4,2010
* AUTHOR : Methew
* FILENAME : Calendar.h
* LICENSE :
* COMMENTARY :
********************************************************************************************************/
#pragma once
class CCalendar
{
public:
CCalendar(void);
virtual ~CCalendar(void);
// 判断闰年
int isBissextile(int iYear);
// 当年天数
int fallday(int iYear, int iMonth, int iDay);
// 当年的1月1日星期几
int week(int iYear);
// TC的模拟
void gotoxy(short x, short y);
// 日历的格式化
void FormatDate(int FstWeek);
};
Calendar.cpp
#include "StdAfx.h"
#include "Calendar.h"
CCalendar::CCalendar(void)
{
}
CCalendar::~CCalendar(void)
{
}
// 判断闰年
int CCalendar::isBissextile(int iYear)
{
return iYear%(iYear%100?4:400)?0:1;
}
// 当年天数
int CCalendar::fallday(int iYear, int iMonth, int iDay)
{
int a,allday;
allday =0;
for(a = 1;a <= iMonth-1;a++)
{
if(a == 1 || a == 3 ||a == 5 ||a == 7 ||a == 8 ||a == 10 ||a == 12)
{
allday += 31;
}
else if(a == 2)
{
if (isBissextile(iYear))
{
allday +=29;
}
else
{
allday +=28;
}
}
else
{
allday += 30;
}
}
allday += iDay;
return allday;
}
// 当年的1月1日星期几
int CCalendar::week(int iYear)
{
char aday,week;
int n,i;
week = 1;
n = iYear - 1900;
for(i = 1;i <= n ;i++)
{
if(isBissextile(iYear+i))
{
aday = 2;
}
else
{
aday = 1;
}
week += aday;
if(week >7){week = week%7;}
}
return week;
}
// TC的模拟
void CCalendar::gotoxy(short x, short y)
{
HANDLE hOut;
hOut = GetStdHandle(STD_OUTPUT_HANDLE);
COORD pos = {0,0};
pos.X = x;
pos.Y = y;
//SetConsoleTextAttribute(hOut, 0x01|0x05|FOREGROUND_INTENSITY );
SetConsoleCursorPosition(hOut, pos);
//CloseHandle(hOut);
}
// 日历的格式化
void CCalendar::FormatDate(int FstWeek)
{
int a;
int k = 1;
for (int i = 1;i < 37;i++)
{
if ( i < FstWeek)
{
a = 1
}
else
{
a = k;
k++;
}
}
}
DateLi.cpp
#include "stdafx.h"
#include "Calendar.h"
int _tmain(int argc, _TCHAR* argv[])
{
int j,k;
CCalendar calendar;
j = calendar.fallday(2010,12,3);
calendar.gotoxy(20,3);
k = calendar.week(2010);
k = j + k-1;
k = k % 7;
printf("%d",k);
return 0;
}
// stdafx.h : 标准系统包含文件的包含文件,
// 或是经常使用但不常更改的
// 特定于项目的包含文件
//
#pragma once
#include "targetver.h"
#include <stdio.h>
#include <tchar.h>
#include <Windows.h>
// TODO: 在此处引用程序需要的其他头文件
你这析构函数没做事情可以不写的,让类调用默认的析构函数就可以了 回复 2# komany
谢谢ls,我把它删除了,我只是刚开始写的,忘记删除了~~
这个其实是原来写的是c的版本,我把原来的函数弄成了类·~~还有个函数没写完·~写出来代码太多~~本身代码就很多~~·看看能简化不~~哈哈~~水平很菜,谢谢了 本帖最后由 komany 于 2010-12-4 21:38 编辑
//sousce
#include "Head.h"
int main()
{
int month=0;
Calendar a;
long int year=0;
system("cls");
do
{
cout<<"\n\nPlease input one integer number as 'year'(0~3000):\n";
cin>>year;
if (year<0||year>3000) printf ("ERROR,please input again!");
}
while (year<0||year>3000);
cout<<"\n\n";
do
{
cout<<"please input the month(1~12)\n\n"<<endl;
cin>>month;
if (month<1||month>12) printf ("please input again!");
}
while (month<1|month>=12);
//printf ("\n\n\n");
cout<<endl;
cout<<endl;
cout<<endl;
//printf("\t\t\t\t%ld\t%d\n\n",year,month);
cout<<"\t"<<"\t"<<"\t"<<year<<"\t"<<"\t"<<month<<endl;
cout<<"\n********************************************************************************\n"<<endl;
cout<<"\t\t Sun\t Mon\t Tue\t Wen\t Thu\t Fri\t Sat"<<endl;
a.Calendar::monthprint(year,month);
printf("\n\n");
return 0;
}#include <stdio.h>
#include <stdlib.h>
#include <iostream>
using namespace std;
#include "Headech.h"
class Calendar
{
public:
int leap_year(int y);
int count_leap(long year);
int monthdays(int month,long year);
int monthprint(long year,int month);
};
int Calendar:: leap_year(int y)
{
return (y%4==0&&y%100!=0||y%400==0);
}
int Calendar:: count_leap(long year)
{
int i=0,j,min,max;
if(year>2006) {min=2006;max=year;}
else {min=year+1;max=2006;}
for(j=min;j<max;j++)
if(leap_year(j)) i++;
return i;
}
int Calendar::monthdays(int month,long year)
{
int sum=0,i,j;
if(year>=2006)
{ int t={31,0,31,30,31,30,31,31,30,31,30,31};
j=month-1;
if(leap_year(year)) t=29;
else t=28;
for(i=0;i<j;i++)
sum=sum+t;
}
else
{ int t1={31,30,31,30,31,31,30,31,30,31,0,31};
j=12-month;
if(leap_year(year)) t1=29;
else t1=28;
for(i=0;i<=j;i++)
sum=sum+t1;
}
return sum;
}
int Calendar::monthprint(long year,int month)
{
int t={31,0,31,30,31,30,31,31,30,31,30,31};
int i,y,weekday=0;
long days=0;
if(leap_year(year)) t=29;
else t=28;
y=t;
if(year>2006)
{
days=(year-2006)*365+count_leap(year)+monthdays(month,year);
weekday=days%7;
}
else if(year<2006)
{
days=(2005-year)*365+count_leap(year)+monthdays(month,year);
weekday=7-days%7;
}
else
{
days=monthdays(month,year);weekday=days%7;
}
for (i=1;i<=weekday+2;i++)
cout<<"\t";
for (i=1;i<=y;i++)
{
if ((i+weekday-1)%7==0) printf ("\n\n\t\t%3d\t",i);
else printf ("%3d\t",i);
};
cout<<"\n********************************************************************************\n\n"<<endl;
return 0;
} // TC的模拟
void CCalendar::gotoxy(short x, short y)
{
HANDLE hOut;
hOut = GetStdHandle(STD_OUTPUT_HANDLE);
COORD pos = {0,0};
pos.X = x;
pos.Y = y;
//SetConsoleTextAttribute(hOut, 0x01|0x05|FOREGROUND_INTENSITY );
SetConsoleCursorPosition(hOut, pos);
//CloseHandle(hOut);
}
这个的设计逻辑上 有待改进
handle 从程序开始到结束 是不会变的 学习中..很好的转变
叹一个...字数字数字数字数
http://www.discuz.net/static/image/common/sigline.gif
燃文
页:
[1]