c语言编程
我想用C语言编这样一个程序:当输入任意的年份和月份时,屏幕能显示该月的月历。各位变成大虾可否帮帮我。 不知道楼主做了有啥用处?
作业,兴趣爱好?还是纯粹无聊? 以前写过一个,难度在算法,但是只要知道那个年月的关系也就不难了. 你想在屏幕上怎么显示?
最难是这个。。 楼上说的是这个吗?
假设这个月的1号是星期二,并且这个月有30天.
#include<iostream>
using namespace std;
int main()
{
char *weeks[]={"日","一","二","三","四","五","六"};
for(int i=0;i<7;i++)cout<<weeks<<"\t";
cout<<endl;
int firstDay=2;//如果是星期天则为0
for(int i=0;i<firstDay;i++)cout<<"\t";
for(int i=1;i<31;i++)
{
cout<<i<<"\t";
if((i+firstDay)%7==0)cout<<endl;
}
}
效果图:
日 一 二 三 四 五 六
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 不是那么简单的,我需要的是任意年月的日期表.例如:我输入2006年4月,就会把这一年这一月的日期表输出.
只是想试一下,自己无聊的时候试了一下,但老是无法得到自己满意的答案~~~~~
所以各为大虾帮一下咯~~~~
给个vbs参考程序,没多检验,请大家指正
'make by ljy'2006-4-6
yea = Inputbox ("请输入年份","日历测试",2006)
If yea > 9999 Or yea < 0 Then
Msgbox "年份输入错误"
Wsh.Quit
End If
mon = Inputbox ("请输入月份","日历测试",1)
If mon > 12 Or mon < 0 Then
Msgbox "月份输入错误"
Wsh.Quit
End If
dat = mon & "/" & "1/" & yea '以一号计算是星期几
olddate = "31/1/0000" '以0000年1月31日求每个月的天数
diff = DateDiff("m",olddate,dat)
endat = DateAdd("m",diff,olddate)
lastday = Right(endat,2) '月份的最后一天是几号
intDay = DatePart("w", dat) '月份的第一天是星期几
calendar = "Sun Mon Tue Wen Thu Fri Sat" & vbCrLf
For i = 1 to intDay-1
calendar = calendar & " "
Next
For i = 1 to lastday
If i < 10 Then
calendar = calendar & i & " "
Else
calendar = calendar & i & ""
End If
If (intDay + i - 1) Mod 7 = 0 Then
calendar = calendar & vbCrLf
End If
Next
msgbox calendar,,"日历测试"
C的,没检查,可能有bug
#include<stdio.h>static int Dtab =
{
{0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
{0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
};
int main()
{
int year,mon,date=1,day,last,i,temp;
scanf("%d,%d",&year,&mon);
if(year>9999||year<0||mon>12||mon<0)
{
printf("输入错误\n");
return 1;
}
temp = date + 2*mon + 3*(mon+1)/5 + year + year/4 - year/100 + year/400;
day=temp%7+1;
if((year)%4 == 0 && (year)%100 != 0 || (year)%400 == 0)
last = Dtab;
else last = Dtab;
printf("Sun Mon Tue Wen Thu Fri Sat\n");
if(day != 7)
{
for(i=1;i<day+1;i++)
printf(" ");
}
for(i=1;i<=last;i++)
{
if(i<10) printf("%d ",i);
else printf("%d",i);
if((i+day)%7==0)
printf("\n");
}
printf("\n");
return 0;
} 强人 都很强的~
页:
[1]