C语言输出教学日历表的方法实例

2022-11-13 09:11:29 实例 输出 日历表

按照格式分别输入学期,开学时间,总周数即可得到课程表,以回车结束。

eg.

学期:2021-2022_2

开学时间:2021/11/25

总周数:20

输出结果

#include<stdio.h>
#include<windows.h>
 
int Weekdayofyear(int year);
int Isprime(int year);
int Days(int year, int mouth);
int Weekday(int year, int mouth, int day);
int Max(int year,int mouth);
 
 
 
int main() {
	char title1[] = "\t\txxxxxx大学教学日历\t\t";
	char title2[] = "0000-0000学年第0学期";
	char tab[60] = "周      一      二      三      四      五      六      日";
	char term3;
	char calender[100][8];
	int tempday, tempmouth,flag = 1,b,c,d,e,f,g,h,i,j, cnt = 1, week,term1, term2,year, mouth, day;
 
	printf("学期:");
	scanf("%d-%d_%c", &term1, &term2, &term3);   
	printf("开学时间:");
	scanf("%d/%d/%d", &year, &mouth, &day);;
	printf("总周数:");
	scanf("%d", &week);
	tempday = day;
	tempmouth = mouth;
	for (b = 3; b >= 0; b--)          
	{                                      
		title2[b] = term1 % 10 + 48;
		term1 /= 10;
	}
	for (c = 8; c >= 5; c--)           
	{
		title2[c] = term2 % 10 + 48;
		term2 /= 10;
	}
	title2[15] = term3;                     
	for (d = 0; d < week; d++) {
		calender[d][0] = d + 1;
	}
	for (e = 0; e < week; e++) {
		for (f = 1; f < 8; f++) { 
			if (e == 0) {                   
				for (g = 1; g < Weekday(year, mouth, day); g++) {
					calender[e][g] = 0;
				}
				for (h = Weekday(year, mouth, day);h < 8; h++) {
					calender[e][h] = day++;
				}
				break;
		    }
			if (day > Max(year, mouth)) {
				mouth += 1;
				if (mouth > 12) {
					mouth = 1;
				}
				calender[e][f] = -mouth;
				day = 2;
 
			}
			else {
				calender[e][f] = day++;
			}
			
		}
	}
	puts(title1);
	printf("\t\t  ");
	puts(title2);
	puts(tab);   
	for (i = 0; i < week; i++) {                            
		for (j = 0; j < 8; j++) {
			if (calender[i][j] == 0) {  
				printf("  \t");
				continue;
			}
			if (calender[i][j] < 0) {
			//	SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 15 | 0 | 128 | 0);  
				printf("%d月\t", -calender[i][j]);
			//	SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7 | 0 | 0 | 0);
			}
			else {
				printf("%2d\t", calender[i][j]);
			}
		}
		printf("\n");
	}
}
 
int Weekdayofyear(int year) {            
	int days = 0, week;
	int a;
	for (a = 0; a < year - 1900; a++) {
		if (a % 4 == 0) days += 366;
		else days += 365;
	}
	week = days % 7;
	if (week == 0) week = 7;
	return week;
}
int Isprime(int year) {    
	char flag;
	if (year % 4 == 0) {
		flag = 1;
	}
	else {
		flag = 0;
	}
	return flag;
}
int Days(int year, int mouth) {     
	int days = 0;
	int isprime;
	isprime = Isprime(year);
	switch (mouth) {
	case 12:days += 31;
	case 11:days += 30;
	case 10:days += 31;
	case 9:days += 30;
	case 8:days += 31;
	case 7:days += 31;
	case 6:days += 30;
	case 5:days += 31;
	case 4:days += 30;
	case 3:days += 31;
	case 2:if (isprime == 1) days += 29;
		  else days += 28;
	case 1:days += 31;
	}
	return days;
}
int Weekday(int year, int mouth, int day) {     
	int days;
	int weekday;
	days = Days(year, mouth - 1);
	days += day;
	weekday = days % 7;
	weekday = weekday + Weekdayofyear(year) - 1;
	if (weekday > 7) weekday -= 7;
	return weekday;
}
int Max(int year,int mouth) {     
	int days;
	int isprime;
	isprime = Isprime(year);
	switch (mouth) {
	case 12:days = 31; break;
	case 11:days = 30; break;
	case 10:days = 31; break;
	case 9:days = 30; break;
	case 8:days = 31; break;
	case 7:days = 31; break;
	case 6:days = 30; break;
	case 5:days = 31; break;	
	case 4:days = 30; break;
	case 3:days = 31; break;
	case 2:if (isprime == 1) days = 29; 
		  else days = 28; break;
	case 1:days = 31; break;
	}
	return days;
}

VS版本

#include<stdio.h>
#include<windows.h>
int Isprimeofyear(int year) {    //判断是否为闰年
	char flag;
	if (year % 4 == 0) {
		flag = 1;
	}
	else {
		flag = 0;
	}
	return flag;
}
int Weekdayofyear(int year) {            //1900年1月1日是星期一,判断这一年的1月1日是星期几
	int days = 0, week;
	for (int i = 0; i < year - 1900; i++) {
		if (i % 4 == 0) days += 366;
		else days += 365;
	}
	week = days % 7;
	if (week == 0) week = 7;
	return week;
}
int Mouthofdays(int year, int mouth) {      //输出月份到年初有多少天
	int days = 0;
	int isprime;
	isprime = Isprimeofyear(year);
	switch (mouth) {
	case 12:days += 31;
	case 11:days += 30;
	case 10:days += 31;
	case 9:days += 30;
	case 8:days += 31;
	case 7:days += 31;
	case 6:days += 30;
	case 5:days += 31;
	case 4:days += 30;
	case 3:days += 31;
	case 2:if (isprime == 1) days += 29;
		  else days += 28;
	case 1:days += 31;
	}
	return days;
}
int Weekdayofday(int year, int mouth, int day) {     //返回输入时间的星期数
	int days;
	int weekday;
	days = Mouthofdays(year, mouth - 1);
	days += day;
	weekday = days % 7;
	weekday = weekday + Weekdayofyear(year) - 1;
	if (weekday > 7) weekday -= 7;
	return weekday;
}
int Maxofmouth(int year,int mouth) {     //返回一个月最多有多少天
	int days;
	int isprime;
	isprime = Isprimeofyear(year);
	switch (mouth) {
	case 12:days = 31; break;
	case 11:days = 30; break;
	case 10:days = 31; break;
	case 9:days = 30; break;
	case 8:days = 31; break;
	case 7:days = 31; break;
	case 6:days = 30; break;
	case 5:days = 31; break;	
	case 4:days = 30; break;
	case 3:days = 31; break;
	case 2:if (isprime == 1) days = 29; 
		  else days = 28; break;
	case 1:days = 31; break;
	}
	return days;
}
int main() {
	int year, mouth, day;
	char title1[] = "\t\txxxxxx大学教学日历\t\t";
	char title2[] = "0000-0000学年第0学期";
	char tab[60] = "周      一      二      三      四      五      六      日";
	int term1, term2;
	char term3;
	int week;
	char calender[100][8];
	int cnt = 1;
	int tempday, tempmouth;
	int flag = 1;
	printf("学期:");
	scanf_s("%d-%d_%c", &term1, &term2, &term3);   //输入学期
	printf("开学时间:");
	scanf_s("%d/%d/%d", &year, &mouth, &day);;
	printf("总周数:");
	scanf_s("%d", &week);
	tempday = day;
	tempmouth = mouth;
	for (int i = 3; i >= 0; i--)           //把输入的int型的学期数转化为char型存入(替代0)title2中以打印
	{                                      
		title2[i] = term1 % 10 + 48;
		term1 /= 10;
	}
	for (int j = 8; j >= 5; j--)           //同上
	{
		title2[j] = term2 % 10 + 48;
		term2 /= 10;
	}
	title2[15] = term3;                     //同上
	for (int i = 0; i < week; i++) {
		calender[i][0] = i + 1;
	}
	for (int i = 0; i < week; i++) {
		for (int j = 1; j < 8; j++) { 
			if (i == 0) {                   //日历第一行同部星期数
				for (int k = 1; k < Weekdayofday(year, mouth, day); k++) {
					calender[i][k] = 0;
				}
				for (int k = Weekdayofday(year, mouth, day); k < 8; k++) {
					calender[i][k] = day++;
				}
				break;
		    }
			if (day > Maxofmouth(year, mouth)) {
				mouth += 1;
				if (mouth > 12) {
					mouth = 1;
				}
				calender[i][j] = -mouth;
				day = 2;
 
			}
			else {
				calender[i][j] = day++;
			}
			
		}
	}
	puts(title1);
	printf("\t\t  ");
	puts(title2);
	puts(tab);   
	for (int i = 0; i < week; i++) {                            //输出表格
		for (int j = 0; j < 8; j++) {
			if (calender[i][j] == 0) {  
				printf("  \t");
				continue;
			}
			if (calender[i][j] == tempday && flag == 1) {
				printf("\b\b\b%d.%d.%d   ", year, tempmouth, tempday);	
				flag = 0;
				continue;
			}
			if (calender[i][j] < 0) {
				SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 15 | 0 | 128 | 0);  //前景色|背景色|前景加强|背景加强
				printf("%d月\t", -calender[i][j]);
				SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7 | 0 | 0 | 0);
			}
			else {
				printf("%2d\t", calender[i][j]);
			}
		}
		printf("\n");
	}
}

总结

到此这篇关于C语言输出教学日历表的文章就介绍到这了,更多相关C语言教学日历表内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

相关文章