1、首先下载安装Notepad++,这是一款免费的且能够编辑C语言的软件。
2、然后下载安装tdm-gcc,这是为了给电脑配置环境变量,以便能够编译C语言的。
3、在安装完以上两款软件后,还要配置一下环境变量。
4、然后开始编辑C语言万年历,首先要判断一个年份是闰年还是平年,用一个子程序来做:
5、然后就开始写主程序:首先用scanf得到一个年份,在判断这个年份是平年还是闰年后用printf在CMD中打出来。
6、在编写完成后,在Notepad++界面下按下F5,在输入框中输入:
cmd /k gcc -o "$(CURRENT_DIRECTORY)\$(NAME_PART).exe" "$(FULL_CURRENT_PATH)" &&CLS &&"$(CURRENT_DIRECTORY)\$(NAME_PART).exe" &PAUSE &EXIT
7、最后点击运行,会d出CMD,在里面输入年份后回车:例如输入2017,然后回车,就会生成2017年的万年历了!
#include <conio.h>#include <stdio.h>
#include <time.h>
#include <stdlib.h>
const monthDay[] = {0,31,28,31,30,31,30,31,31,30,31,30,31}
int isLeap(int year)
{
if(year % 4) return 0
if(year % 400) return 1
if(year % 100) return 0
return 1
}
int getWeek(int year, int month, int day)
{
int c, y, week
if(month == 1 || month == 2) //判断month是否为1或2
{
year--
month+=12
}
c= year / 100
y = year - c * 100
week = (c / 4) - 2 * c + (y + y / 4) + (13 * (month + 1) / 5) + day - 1
while(week < 0) {week += 7}
week %= 7
return week
}
void display(int year, int month)
{
int monthDays, weekFirst, i
monthDays = monthDay[month] + (month==2 ? isLeap(year) : 0)
weekFirst = getWeek(year, month, 1)
system("cls")
printf(" -------%4d年----%2d月-------\n", year, month)
printf(" 星期日 星期一 星期二 星期三 星期四 星期五 星期六\n")
for(i=0 i<weekFirst i++) printf(" ")
for(i=1 i<=monthDays i++)
{
printf("%8d", i)
weekFirst++
if(weekFirst>=7) {printf("\n") weekFirst=0}
}
}
void main()
{
int year, month, chr
time_t timer
struct tm *tblock
timer = time(NULL)
tblock = localtime(&timer)
year = tblock->tm_year + 1900
month = tblock->tm_mon +1
while(1)
{
display(year, month)
chr = getch()
if(chr == 0xe0)
{
chr = getch()
if(chr == 0x4b) /* 方向键(←) */
{
month --
if(month<1) {month = 12 year--}
}
else if(chr == 0x4d) /* 方向键(→) */
{
month ++
if(month>12) {month = 1 year++}
}
}
else if(chr == 'q' || chr == 'Q') break
}
}
#include <stdio.h>void main()
{
int i,j,k,year,week
int day[]={31,28,31,30,31,30,31,31,30,31,30,31}
char month[12][10]={"January","February","March","April","May","June","July","August","September","October","November","December"}
printf("Input year you want to print:")
scanf("%d",&year)
if((year%4==0&&year%100!=0)||(year%400==0)) day[1]++//判断是否闰年
week=(35+year+year/4-year/100+year/400)%7//计算当年的1月1日是周几的公式
for(i=0i<12i++)
{
printf("\n%s\n",month[i])//输出月份的英文
printf("Sun Mon Tue Wed Thu Fri Sat\n")//输出表头
for(k=0k<weekk++) printf(" ")//每月一号对齐它的星期数
for(j=1j<=day[i]j++)
{
printf("%3d ",j)
if(++week>=7)
{
printf("\n")
week=week%7
}
}
printf("\n")
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)