您好,登錄后才能下訂單哦!
本篇內容主要講解“C語言如何輸出教學日歷表”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“C語言如何輸出教學日歷表”吧!
按照格式分別輸入學期,開學時間,總周數即可得到課程表,以回車結束。
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語言如何輸出教學日歷表”有了更深的了解,不妨來實際操作一番吧!這里是億速云網站,更多相關內容可以進入相關頻道進行查詢,關注我們,繼續學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。