#日历程序 Lunar=(1,3,5,7,8,10,12) def IsleapYear(year): flag =False if(year%4==0 and year%100!=0) or year%400==0: flag = True return flag def calculation(year,month): sum = 0 s_year=1990 while s_year < year-1: s_year +=1 if IsleapYear(s_year): sum+=366 else: sum+=365 s_month=1 while s_month<month: if s_month in Lunar: sum += 31 elif s_month==2: if IsleapYear(year): sum+=29 else: sum+=28 else: sum+=30 s_month+=1 return sum def display(sum,year,month): week=(sum+1)%7 if month in Lunar: day=31 elif month==2: if IsleapYear(year): day=29 else: day=28 else: day=30 print("日 一 二 三 四 五 六") count = 0 space = 0 while space<=week: space+=1 count+=1 print(" ",end="") if count%7==0: print(" ",end="") days=1 while days<=day: print(days," ",end="") days+=1 count+=1 if count %7 ==0: print("") def main(): year =int( input("输入年:")) if year<1990 or year>9999: print("year输入错误") main() month = int(input("输入月:")) if month<1 or month > 12: print("month输入错误") main() sums=calculation(year,month) display(sums,year,month) if __name__=="__main__": while True: main() print() choose=input("是否继续:(y/n)") if choose in("n","N"): break怎么用C语言表示
时间: 2024-02-14 09:20:51 浏览: 100
以下是用 C 语言实现该日历程序的示例代码:
#include <stdio.h>
int IsLeapYear(int year) {
int flag = 0;
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
flag = 1;
}
return flag;
}
int Calculation(int year, int month) {
int sum = 0;
int s_year = 1990;
while (s_year < year - 1) {
s_year++;
if (IsLeapYear(s_year)) {
sum += 366;
} else {
sum += 365;
}
}
int s_month = 1;
while (s_month < month) {
if (s_month == 1 || s_month == 3 || s_month == 5 || s_month == 7 || s_month == 8 || s_month == 10 || s_month == 12) {
sum += 31;
} else if (s_month == 2) {
if (IsLeapYear(year)) {
sum += 29;
} else {
sum += 28;
}
} else {
sum += 30;
}
s_month++;
}
return sum;
}
void Display(int sum, int year, int month) {
int week = (sum + 1) % 7;
int day;
if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) {
day = 31;
} else if (month == 2) {
if (IsLeapYear(year)) {
day = 29;
} else {
day = 28;
}
} else {
day = 30;
}
printf("日 一 二 三 四 五 六\n");
int count = 0;
int space = 0;
while (space <= week) {
space++;
count++;
printf(" ");
if (count % 7 == 0) {
printf(" ");
}
}
int days = 1;
while (days <= day) {
printf("%-3d ", days);
days++;
count++;
if (count % 7 == 0) {
printf("\n");
}
}
}
int main() {
int year, month;
char choose;
do {
printf("输入年:");
scanf("%d", &year);
if (year < 1990 || year > 9999) {
printf("year输入错误\n");
continue;
}
printf("输入月:");
scanf("%d", &month);
if (month < 1 || month > 12) {
printf("month输入错误\n");
continue;
}
int sums = Calculation(year, month);
Display(sums, year, month);
printf("\n是否继续:(y/n)");
scanf(" %c", &choose);
} while (choose == 'y' || choose == 'Y');
return 0;
}
阅读全文