跪求C语言编写的万年历原代码,大哥们帮帮忙啊~
//-------------系统库定义-------------------
#include "stdio.h"
#include "BIOS.h"
#include "dos.h"
#define X " Sun Mon Tue Wed Thu Fri Sat"
#define LEFT 0x4b00
#define RIGHT 0x4d00
#define DOWN 0x5000
#define UP 0x4800
#define PGUP 0x4900
#define PGDW 0x5100
#define ESC 0x011b
#define QUERY 0x1071
#define QUERYD 0x1051
//--------------------------------------------
struct date dm ;//时间结构定义
int Cyear,Cmonth,Cday,Cmonnum,Lmonnum ;//全局变量
int isleap(int year)//判断闰年
{
if(year%4==0&&year%100||year%400==0)
return 1 ;
else
return 0 ;
}
int getday(int year,int month,int day)//计算X年X月X日是星期几
{
int flag,s,i ;
int a[13]={0,31,28,31,30,31,30,31,31,30,31,30,31} ;
int cont=0 ;
flag=isleap(year);
if(flag==1)
a[2]++;
for(i=1;i<month;i++)
{
cont=cont+a[i];
}
cont=cont+day ;
s=year+1+(year-1)/4+(year-1)/100+(year-1)/400+cont ;
return s%7 ;
}
void printspace(int n)//输出空格
{
int i ;
for(i=0;i<n;i++)printf(" ");
}
void spacer()//输出分隔符(分隔线)
{
int i ;
for(i=0;i<30;i++)
{
printf("=");
}
printf("
");
}
void keyinfo()//输出键盘操作信息
{
printf("
-------Operation manual-------
");
printf("Year:Up key and Down key
");
printf("Month:Left key and Right key
");
printf("Day:PageUp key and PageDown key
");
printf("Query date of the calendar:Q key
");
printf("Exit calendar:Esc key
");
}
void week()//输出当前日期是星期几
{
int day ;
day=getday(Cyear,Cmonth,Cday);
if(day==0)
printf("
%d-%d-%d is Sunday!
",Cyear,Cmonth,Cday);
if(day==1)
printf("
%d-%d-%d is Monday!
",Cyear,Cmonth,Cday);
if(day==2)
printf("
%d-%d-%d is Tuesday!
",Cyear,Cmonth,Cday);
if(day==3)
printf("
%d-%d-%d is Wednesday!
",Cyear,Cmonth,Cday);
if(day==4)
printf("
%d-%d-%d is Thursday!
",Cyear,Cmonth,Cday);
if(day==5)
printf("
%d-%d-%d is Friday!
",Cyear,Cmonth,Cday);
if(day==6)
printf("
%d-%d-%d is Saturday!
",Cyear,Cmonth,Cday);
}
void calendar(int year,int month,int day)//显示日历
{
int i,j,d,m,flag,week;
int a[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
m=0,d=0,week=0;
Cmonnum=a[month];//当前月的天数
Lmonnum=a[month-1];//上一月的天数
week=d=getday(year,month,1);//取得X年X月的1日是星期几
flag=isleap(year);
if(flag==1)
{
a[2]++;
}
clrscr();
printf("
The calendar of year %d
",year);
spacer();
switch(month)
{
case 1 :
printf(" January 1 ");
break ;
case 2 :
printf(" February 2 ");
break ;
case 3 :
printf(" March 3 ");
break ;
case 4 :
printf(" April 4 ");
break ;
case 5 :
printf(" May 5 ");
break ;
case 6 :
printf(" June 6 ");
break ;
case 7 :
printf(" July 7 ");
break ;
case 8 :
printf(" August 8 ");
break ;
case 9 :
printf(" September 9 ");
break ;
case 10 :
printf(" October 10 ");
break ;
case 11 :
printf(" Nevember 11 ");
break ;
case 12 :
printf(" December 12");
break ;
}
printf("
");
printf(X);
printf("
");
for(i=0;i<6;i++)
{
if(i==0)//如果是第一个星期时,执行!
{
printspace(d*4);
for(j=0;j<7-d;j++)
{
if(m==day-1)//m到达day的前一天时,输出[%d]
{
printf(" [%d]",++m);
}
else if(m==day && week!=0)//m到达当天时的,输出%3d
{
printf("%3d",++m);
}
else
{
printf("%4d",++m);
}
week=(week<6)?week+1:0;//计算当天是星期几
}
printf("
");
}
else//不是第一个星期时,执行!
{
for(j=0;j<7;j++)
{
if(m<a[month])
{
if(m==day-1)//m到达day的前一天时,输出[%d]
{
if(m<9)//代表只有一位数的数字如9,8,7.....等。
printf(" [%d]",++m);
else//代表只有两位数的数字如10,11,12.....等。
printf(" [%d]",++m);
}
else if(m==day && week!=0)
{
printf("%3d",++m);
}
else
{
printf("%4d",++m);
}
}
week=(week<6)?week+1:0;//计算当天是星期几
}
printf("
");
if(m==a[month])break ;
}
}
spacer();
if(flag==1)
printf("
The year %d is leap year!
",year);
if(flag==0)
printf("
The year %d is not leap year!
",year);
week();
printf("
The current date is: %d-%d-%d
",dm.da_year,dm.da_mon,dm.da_day);
keyinfo();
}
//keyboard Operation
void key()
{
int key ;
Cyear=dm.da_year,Cmonth=dm.da_mon,Cday=dm.da_day ;
calendar(Cyear,Cmonth,Cday);
while(1)
{
key=bioskey(0);
if(key==RIGHT)
{
if(Cmonth<12&&Cmonth>=1)
{
Cmonth++;
}
else
{
Cyear++;
Cmonth=1 ;
}
}
if(key==LEFT)
{
if(Cmonth<=12&&Cmonth>1)
{
Cmonth--;
}
else
{
Cyear--;
Cmonth=12 ;
}
}
if(key==UP)
{
Cyear++;
}
if(key==DOWN)
{
Cyear--;
}
if(key==PGUP)
{
if(Cday!=1)
{
Cday--;
}
else if(Cday==1&&Cmonth==1)
{
Cyear--;
Cmonth=12 ;
Cday=31 ;
}
else
{
Cmonth--;
Cday=Lmonnum ;
}
}
if(key==PGDW)
{
if(Cmonnum!=Cday)
{
Cday++;
}
else if(Cmonnum==Cday&&Cmonth==12)
{
Cyear++;
Cmonth=1 ;
Cday=1 ;
}
else
{
Cmonth++;
Cday=1 ;
}
}
if(key==QUERY||key==QUERYD)
{
printf("Input date format(YYYY,MM,DD):");
scanf("%d,%d,%d",&Cyear,&Cmonth,&Cday);
}
if(key==ESC)break ;
calendar(Cyear,Cmonth,Cday);
}
}
void main()
{
clrscr();
getdate(&dm);
key();
}
解释一个万年历其中的一个代码
if 结构在里面 是 起 第一个 是 判断 年 是不是 闰年
小于7是因为 一年 只有 7个大月 其余都是小月
最后一个循环 是 因为 一个星期 7 天 它是按 星期日 为一个星期的第一天 循环的
java程序的万年历编程代码?
public static void main(String[] args) {
e5a48de588b662616964757a686964616f31333330333631Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.YEAR, 2012);
calendar.set(Calendar.MONTH,1);
calendar.set(Calendar.DAY_OF_MONTH, 1);
int first = calendar.get(Calendar.DAY_OF_WEEK) - 1;
System.out.println(calendar.get(Calendar.YEAR) + "年"
+ (calendar.get(Calendar.MONTH) + 1) + "月万年历");
System.out.println("日 一 二 三 四 五 六");
for (int i = 0; i < first; i++) {
System.out.print(" ");
}
int maxDay = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
for (int i = 0; i < maxDay; i++) {
if (first / 7 == 1) {
first = 0;
System.out.println();
}
System.out.print(i + 1
+ (first / 6 == 1 || i == maxDay - 1 ? "" : " "));
first++;
}
}
用c语言设计一个简单的万年历怎么写代码
#include"stdio.h"#include"stdlib.h"main(){intYear,Month;//年、月intFirstDay_Year,FirstDay_Month;//某年的第一天是星期几,某年某月的第一天是星期几(范围是0~6,其中0代表星期日)intIsLeapYear;//是否为闰年,0表示不是闰年,1表示是闰年inti,d,y;//临时变量charYN;//YesNo,程序是否要继续intDays[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};//Days[1~12]存储每个月有多少天,其中二月的天数是可变的(闰年29天,平年28天),这里初始化为28天printf("C语言简单万年历
");//打印标题XunHuan://循环标号(可以通过goto跳转到这里)printf("请输入年份:");//提示输入年份(0~9999)scanf("%d",&Year);//把输入的年份赋值给变量Yearprintf("请输入月份:");//提示输入月份(1~12)scanf("%d",&Month);//把输入的月份赋值给变量Monthy=Year;FirstDay_Year=5*(y/4)+(y%4)-(y/100)+(y/400);//蔡勒公式(计算某年的第一天是星期几)IsLeapYear=(y%4==4&&y%100!=100||y%400==0)?1:0;//判断是否为闰年Days[2]=(IsLeapYear==1)?29:28;//闰年二月29天,非闰年二月28天for(i=1,d=0;i
JAVA万年历代码
package clock;
/** *//**
* MainFrame.java
* Summary 万年历主类
* Created on
* @author
* remark
*/
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Date;
import java.util.Calendar;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
class MainFrame extends JFrame {
/** *//**
*
*/
private static final long serialVersionUID = 1L;
JPanel panel = new JPanel(new BorderLayout());
JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel(new GridLayout(7, 7));
JPanel panel3 = new JPanel();
JLabel[] label = new JLabel[49];
JLabel y_label = new JLabel("年份");
JLabel m_label = new JLabel("月份");
1 = new JComboBox();
2 = new JComboBox();
int re_year, re_month;
int x_size, y_size;
String year_num;
Calendar now = Calendar.getInstance(); // 实例化Calendar
MainFrame() {
super("万年历");
setSize(300, 350);
x_size = (int) (Toolkit.getDefaultToolkit().getScreenSize().getWidth());
y_size = (int) (Toolkit.getDefaultToolkit().getScreenSize().getHeight());
setLocation((x_size - 300) / 2, (y_size - 350) / 2);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel1.add(y_label);
1);
panel1.add(m_label);
2);
for (int i = 0; i < 49; i++) {
label[i] = new JLabel("", JLabel.CENTER);// 将显示的字符设置为居中
panel2.add(label[i]);
}
panel3.add(new Clock(this));
panel.add(panel1, BorderLayout.NORTH);
panel.add(panel2, BorderLayout.CENTER);
panel.add(panel3, BorderLayout.SOUTH);
panel.setBackground(Color.white);
panel1.setBackground(Color.white);
panel2.setBackground(Color.white);
panel3.setBackground(Color.white);
Init();
1.addActionListener(new ClockAction());
2.addActionListener(new ClockAction());
setContentPane(panel);
setVisible(true);
setResizable(false);
}
class ClockAction implements ActionListener {
public void actionPerformed(ActionEvent arg0) {
int c_year, c_month, c_week;
c_year = 1.getSelectedItem().toString()); // 得到当前所选年份
c_month = 2.getSelectedItem().toString()) - 1; // 得到当前月份,并减1,计算机中的月为0-11
c_week = use(c_year, c_month); // 调用函数use,得到星期几
Resetday(c_week, c_year, c_month); // 调用函数Resetday
}
}
public void Init() {
int year, month_num, first_day_num;
String log[] = { "日", "一", "二", "三", "四", "五", "六" };
for (int i = 0; i < 7; i++) {
label[i].setText(log[i]);
}
for (int i = 0; i < 49; i = i + 7) {
label[i].setForeground(Color.red); // 将星期日的日期设置为红色
}
for (int i = 6; i < 49; i = i + 7) {
label[i].setForeground(Color.green);// 将星期六的日期设置为绿色
}
for (int i = 1; i < 10000; i++) {
1.addItem("" + i);
}
for (int i = 1; i < 13; i++) {
2.addItem("" + i);
}
month_num = (int) (now.get(Calendar.MONTH)); // 得到当前时间的月份
year = (int) (now.get(Calendar.YEAR)); // 得到当前时间的年份
1.setSelectedIndex(year - 1); // 设置下拉列表显示为当前年
2.setSelectedIndex(month_num); // 设置下拉列表显示为当前月
first_day_num = use(year, month_num);
Resetday(first_day_num, year, month_num);
}
public int use(int reyear, int remonth) {
int week_num;
now.set(reyear, remonth, 1); // 设置时间为所要查询的年月的第一天
week_num = (int) (now.get(Calendar.DAY_OF_WEEK));// 得到第一天的星期
return week_num;
}
@SuppressWarnings("deprecation")
public void Resetday(int week_log, int year_log, int month_log) {
int month_day_score; // 存储月份的天数
int count;
month_day_score = 0;
count = 1;
Date date = new Date(year_log, month_log + 1, 1); // now
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.MONTH, -1); // 前个月
month_day_score = cal.getActualMaximum(Calendar.DAY_OF_MONTH);// 最后一天
for (int i = 7; i < 49; i++) { // 初始化标签
label[i].setText("");
}
week_log = week_log + 6; // 将星期数加6,使显示正确
month_day_score = month_day_score + week_log;
for (int i = week_log; i < month_day_score; i++, count++) {
label[i].setText(count + "");
}
}
public static void main(String[] args) {
JFrame.setDefaultLookAndFeelDecorated(true);
new MainFrame();
}
}