文章详情

短信预约-IT技能 免费直播动态提醒

请输入下面的图形验证码

提交验证

短信预约提醒成功

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

2024-04-02 19:55

关注

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

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语言教学日历表内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

阅读原文内容投诉

免责声明:

① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。

② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341

软考中级精品资料免费领

  • 历年真题答案解析
  • 备考技巧名师总结
  • 高频考点精准押题
  • 2024年上半年信息系统项目管理师第二批次真题及答案解析(完整版)

    难度     813人已做
    查看
  • 【考后总结】2024年5月26日信息系统项目管理师第2批次考情分析

    难度     354人已做
    查看
  • 【考后总结】2024年5月25日信息系统项目管理师第1批次考情分析

    难度     318人已做
    查看
  • 2024年上半年软考高项第一、二批次真题考点汇总(完整版)

    难度     435人已做
    查看
  • 2024年上半年系统架构设计师考试综合知识真题

    难度     224人已做
    查看

相关文章

发现更多好内容

猜你喜欢

AI推送时光机
位置:首页-资讯-后端开发
咦!没有更多了?去看看其它编程学习网 内容吧
首页课程
资料下载
问答资讯