您好,登錄后才能下訂單哦!
本篇內(nèi)容主要講解“C語(yǔ)言如何輸出教學(xué)日歷表”,感興趣的朋友不妨來(lái)看看。本文介紹的方法操作簡(jiǎn)單快捷,實(shí)用性強(qiáng)。下面就讓小編來(lái)帶大家學(xué)習(xí)“C語(yǔ)言如何輸出教學(xué)日歷表”吧!
按照格式分別輸入學(xué)期,開(kāi)學(xué)時(shí)間,總周數(shù)即可得到課程表,以回車(chē)結(jié)束。
eg.
學(xué)期:2021-2022_2
開(kāi)學(xué)時(shí)間:2021/11/25
總周數(shù):20
輸出結(jié)果
#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大學(xué)教學(xué)日歷\t\t"; char title2[] = "0000-0000學(xué)年第0學(xué)期"; 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("學(xué)期:"); scanf("%d-%d_%c", &term1, &term2, &term3); printf("開(kāi)學(xué)時(shí)間:"); scanf("%d/%d/%d", &year, &mouth, &day);; printf("總周數(shù):"); 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) { //返回輸入時(shí)間的星期數(shù) 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) { //返回一個(gè)月最多有多少天 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大學(xué)教學(xué)日歷\t\t"; char title2[] = "0000-0000學(xué)年第0學(xué)期"; char tab[60] = "周 一 二 三 四 五 六 日"; int term1, term2; char term3; int week; char calender[100][8]; int cnt = 1; int tempday, tempmouth; int flag = 1; printf("學(xué)期:"); scanf_s("%d-%d_%c", &term1, &term2, &term3); //輸入學(xué)期 printf("開(kāi)學(xué)時(shí)間:"); scanf_s("%d/%d/%d", &year, &mouth, &day);; printf("總周數(shù):"); scanf_s("%d", &week); tempday = day; tempmouth = mouth; for (int i = 3; i >= 0; i--) //把輸入的int型的學(xué)期數(shù)轉(zhuǎn)化為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) { //日歷第一行同部星期數(shù) 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); //前景色|背景色|前景加強(qiáng)|背景加強(qiáng) printf("%d月\t", -calender[i][j]); SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7 | 0 | 0 | 0); } else { printf("%2d\t", calender[i][j]); } } printf("\n"); } }
到此,相信大家對(duì)“C語(yǔ)言如何輸出教學(xué)日歷表”有了更深的了解,不妨來(lái)實(shí)際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢(xún),關(guān)注我們,繼續(xù)學(xué)習(xí)!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。