前言
本篇随笔将一份万年历系统稍加注释,并添加了一个新功能,供大家参考
对原代码的理解
主要功能:
- 查询某日期是周几
- 查询某年是否为闰年
- 打印某年的日历
- 查询某月的最大天数
1 #include<stdio.h> 2 #include<stdlib.h> 3 #include<windows.h> 4 #include<algorithm> 5 6 int year, month, day; 7 //闰年 8 int run[12] = { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; 9 //平年 10 int ping[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; 11 //星期 12 char weekday[7][10] = { "一", "二", "三", "四", "五", "六", "日" }; 13 //打印 14 15 const int MAXN = 1e4 + 7; 16 struct node { 17 int nian; 18 int yue; 19 int ri; 20 char str[100]; 21 friend bool operator < (const node& a, const node& b) { 22 if (a.nian == b.nian) { 23 if (a.yue == b.yue) { 24 return a.ri < b.ri; 25 } else return a.yue < b.yue; 26 } else return a.nian < b.nian; 27 } 28 } a[MAXN]; 29 30 //清屏+修改颜色 31 void cls(){ 32 system("cls"); 33 system("color 0B"); 34 } 35 36 void print_week() { 37 //打印日期 38 for (int i = 0; i < 7; i++) { 39 printf("%s\t", weekday[i]); 40 } 41 printf("\n"); 42 } 43 void print_title() { 44 printf("\n\t\t 万年历查询系统\n\n"); 45 printf("**************************************************\n"); 46 printf(" 1 查询某年某月某日是星期几\n 2 查询某年是否是闰年\n 3 打印某年的日历\n 4 查询某月的最大天数\n 5 退出\n"); 47 printf("**************************************************\n"); 48 printf("\n请选择:"); 49 } 50 51 52 void refresh() { 53 cls(); 54 print_title(); 55 } 56 void loading() { 57 printf("\n\n\t\tLoading......\n\n\n"); 58 printf(" "); 59 for (int i = 4; i <= 20; ++i) { 60 printf("%c ", 1); 61 Sleep(50); 62 } 63 printf(" "); 64 } 65 66 int leap(int year) { 67 if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) { 68 return 1; //返回1代表闰年 69 } else if (year % 4 == 0 && year % 100 != 0 && year % 400 == 0) { 70 return 1; //返回1代表闰年 71 } else { 72 return 0; //返回0代表平年 73 } 74 } 75 //日历从2018年开始,2018.01.01是星期一 76 //星期几是怎么来的?第一次算星期的时候开始做比较 77 //计算year距离2018相差多少天 78 int daySum(int year, int month, int run[], int ping[]) { 79 //参照物 1990.01.01是星期一 80 int i, sum = 0; 81 //计算年份的天数:闰年加366,平年加365 82 while(year<2018) year+=400; //400年为一个周期 83 for (i = 2018; i < year; i++) { 84 if (leap(i) == 1) { 85 sum += 366; 86 } else { 87 sum += 365; 88 } 89 } 90 //计算月份的天数 91 for (i = 0; i < month - 1; i++) { 92 if (leap(year) == 1) { 93 sum += run[i]; 94 } else { 95 sum += ping[i]; 96 } 97 } 98 return sum; 99 100 } 101 102 //打印日历 103 void printData(int sum, int year, int month) { 104 int result, temp, i; //result代表每个月之前的空格 105 result = sum % 7; //每个月之前的空格,就是余数 123456%7---》0--6 106 //当前月份空余的天数用空格表示 107 for (i = 0; i < result; i++) { 108 printf("\t"); // 与打印的日期排头对齐 109 } 110 temp = 7 - result; //开始打印一号 111 if (leap(year) == 1) { 112 //月份数组下标,从0开始,但是月份从一开始 113 for (i = 1; i <= run[month - 1]; i++) { 114 printf("%d\t", i); 115 //换行处理 116 if (i == temp || (i - temp) % 7 == 0) { 117 printf("\n"); 118 } 119 120 } 121 printf("\n"); 122 } else { 123 //月份数组下标,从0开始,但是月份从一开始 124 for (i = 1; i <= ping[month - 1]; i++) { 125 printf("%d\t", i); 126 //换行处理 127 if (i == temp || (i - temp) % 7 == 0) { 128 printf("\n"); 129 } 130 } 131 printf("\n"); 132 } 133 } 134 135 int judge_month(int month) { //判断月份是否合法 136 if (month > 12 || month < 0) { 137 return 1; 138 } 139 return 0; 140 } 141 142 int judge_day(int year, int month, int day) { 143 if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) {//大月的天数判断 144 if (day < 0 || day>31) { 145 return 1; 146 } 147 else if (month == 4 || month == 6 || month == 9 || month == 11) {//小月的天数判断 148 if (day < 0 || day>30) { 149 return 1; 150 } 151 else if (leap(year) == 1 || month == 2) {//判断闰年二月 152 if (day < 0 || day>29) { 153 return 1; 154 } 155 else if (leap(year) == 0 || month == 2) {//判断平年二月 156 if (day < 0 || day>28) { 157 158 return 1; 159 } 160 } 161 } 162 } 163 } 164 return 0; 165 } 166 167 void opt1() { 168 cls(); 169 printf("\n请输入年 月 日:\n\n"); 170 (void)scanf("%d %d %d", &year, &month, &day); 171 while (judge_month(month) || judge_day(year, month, day)) { 172 (void)scanf("%d %d %d", &year, &month, &day); 173 } 174 175 int sum = daySum(year, month, run, ping) + day; //距离基准相差的天数 176 int yushu = sum % 7; //计算 177 switch (yushu + 1) { 178 case 1: 179 printf("\n这一天是星期日\n\n"); 180 break; 181 case 2: 182 printf("\n这一天是星期一\n\n"); 183 break; 184 case 3: 185 printf("\n这一天是星期二\n\n"); 186 break; 187 case 4: 188 printf("\n这一天是星期三\n\n"); 189 break; 190 case 5: 191 printf("\n这一天是星期四\n\n"); 192 break; 193 case 6: 194 printf("\n这一天是星期五\n\n"); 195 break; 196 case 7: 197 printf("\n这一天是星期六\n\n"); 198 break; 199 } 200 } 201 202 void opt2() { 203 cls(); 204 printf("\n请输入年份:\n\n"); 205 int year; 206 (void)scanf("%d", &year); 207 if (leap(year) == 1) printf("\n这一年是闰年\n\n"); 208 if (leap(year) == 0) printf("\n这一年是平年\n\n"); 209 } 210 211 void opt3() { 212 cls(); 213 printf("\n请输入年份:\n\n"); 214 (void)scanf("%d", &year); 215 for (month = 1; month <= 12; month++) { 216 if(month==1) printf("\n**************************************************"); 217 int sum = daySum(year, month, run, ping); //计算与基准相差的天数 218 printf("\n\n"); 219 printf("\t\t %d月\n\n", month); 220 print_week(); 221 printData(sum, year, month); 222 printf("\n**************************************************\n");//按格式打印 223 } 224 } 225 226 void opt4() { 227 cls(); 228 printf("\n请输入 年 月:\n\n"); 229 (void)scanf("%d %d", &year, &month); 230 while (judge_month(month)) { 231 (void)scanf("%d %d", &year, &month); 232 } 233 if (judge_month(month) == 0) { 234 if (leap(year) == 1) printf("\n本月的天数为%d\n\n", run[month - 1]); //从数组中读取 235 if (leap(year) == 0) printf("\n本月的天数为%d\n\n", ping[month - 1]); 236 } 237 } 238 239 240 void opt5() { 241 cls(); 242 printf("\n\n\t感谢使用"); 243 } 244 245 246 int main() { 247 system("color 0B"); //修改配色 248 loading(); //加载界面 249 cls(); 250 int opt; 251 print_title(); 252 while (1) { // 死循环,只通过opt7退出程序 253 (void)scanf("%d", &opt); 254 switch (opt) { 255 case 1: 256 opt1(); 257 system("pause"); 258 refresh(); 259 break; 260 case 2: 261 opt2(); 262 system("pause"); 263 refresh(); 264 break; 265 case 3: 266 opt3(); 267 system("pause"); 268 refresh(); 269 break; 270 case 4: 271 opt4(); 272 system("pause"); 273 refresh(); 274 break; 275 case 5: 276 opt5(); 277 Sleep(1000); //延迟关闭,优化体验 278 return 0; 279 default: 280 cls(); 281 system("pause"); 282 refresh(); 283 break; 284 } 285 } 286 return 0; 287 }点击查看代码
代码修改
该万年历系统已较为完善,因此笔者仅稍加修改,在程序中额外加入添加日程和查询日程的功能
#include<stdio.h> #include<stdlib.h> #include<windows.h> #include<algorithm> int year, month, day; //闰年 int run[12] = { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; //平年 int ping[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; //星期 char weekday[7][10] = { "一", "二", "三", "四", "五", "六", "日" }; //打印 const int MAXN = 1e4 + 7; struct node { int nian; int yue; int ri; char str[100]; friend bool operator < (const node& a, const node& b) { if (a.nian == b.nian) { if (a.yue == b.yue) { return a.ri < b.ri; } else return a.yue < b.yue; } else return a.nian < b.nian; } } a[MAXN]; //清屏+修改颜色 void cls(){ system("cls"); system("color 0B"); } void print_week() { //打印日期 for (int i = 0; i < 7; i++) { printf("%s\t", weekday[i]); } printf("\n"); } void print_title() { printf("\n\t\t 万年历查询系统\n\n"); printf("**************************************************\n"); printf(" 1 查询某年某月某日是星期几\n 2 查询某年是否是闰年\n 3 打印某年的日历\n 4 查询某月的最大天数\n 5 添加日程\n 6 查询日程\n 7 退出\n"); printf("**************************************************\n"); printf("\n请选择:"); } void refresh() { cls(); print_title(); } void loading() { printf("\n\n\t\tLoading......\n\n\n"); printf(" "); for (int i = 4; i <= 20; ++i) { printf("%c ", 1); Sleep(50); } printf(" "); } int leap(int year) { if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) { return 1; //返回1代表闰年 } else if (year % 4 == 0 && year % 100 != 0 && year % 400 == 0) { return 1; //返回1代表闰年 } else { return 0; //返回0代表平年 } } //日历从2018年开始,2018.01.01是星期一 //星期几是怎么来的?第一次算星期的时候开始做比较 //计算year距离2018相差多少天 int daySum(int year, int month, int run[], int ping[]) { //参照物 1990.01.01是星期一 int i, sum = 0; //计算年份的天数:闰年加366,平年加365 while(year<2018) year+=400; //400年为一个周期 for (i = 2018; i < year; i++) { if (leap(i) == 1) { sum += 366; } else { sum += 365; } } //计算月份的天数 for (i = 0; i < month - 1; i++) { if (leap(year) == 1) { sum += run[i]; } else { sum += ping[i]; } } return sum; } //打印日历 void printData(int sum, int year, int month) { int result, temp, i; //result代表每个月之前的空格 result = sum % 7; //每个月之前的空格,就是余数 123456%7---》0--6 //当前月份空余的天数用空格表示 for (i = 0; i < result; i++) { printf("\t"); // 与打印的日期排头对齐 } temp = 7 - result; //开始打印一号 if (leap(year) == 1) { //月份数组下标,从0开始,但是月份从一开始 for (i = 1; i <= run[month - 1]; i++) { printf("%d\t", i); //换行处理 if (i == temp || (i - temp) % 7 == 0) { printf("\n"); } } printf("\n"); } else { //月份数组下标,从0开始,但是月份从一开始 for (i = 1; i <= ping[month - 1]; i++) { printf("%d\t", i); //换行处理 if (i == temp || (i - temp) % 7 == 0) { printf("\n"); } } printf("\n"); } } int judge_month(int month) { //判断月份是否合法 if (month > 12 || month < 0) { return 1; } return 0; } int judge_day(int year, int month, int day) { if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) {//大月的天数判断 if (day < 0 || day>31) { return 1; } else if (month == 4 || month == 6 || month == 9 || month == 11) {//小月的天数判断 if (day < 0 || day>30) { return 1; } else if (leap(year) == 1 || month == 2) {//判断闰年二月 if (day < 0 || day>29) { return 1; } else if (leap(year) == 0 || month == 2) {//判断平年二月 if (day < 0 || day>28) { return 1; } } } } } return 0; } void opt1() { cls(); printf("\n请输入年 月 日:\n\n"); (void)scanf("%d %d %d", &year, &month, &day); while (judge_month(month) || judge_day(year, month, day)) { (void)scanf("%d %d %d", &year, &month, &day); } int sum = daySum(year, month, run, ping) + day; //距离基准相差的天数 int yushu = sum % 7; //计算 switch (yushu + 1) { case 1: printf("\n这一天是星期日\n\n"); break; case 2: printf("\n这一天是星期一\n\n"); break; case 3: printf("\n这一天是星期二\n\n"); break; case 4: printf("\n这一天是星期三\n\n"); break; case 5: printf("\n这一天是星期四\n\n"); break; case 6: printf("\n这一天是星期五\n\n"); break; case 7: printf("\n这一天是星期六\n\n"); break; } } void opt2() { cls(); printf("\n请输入年份:\n\n"); int year; (void)scanf("%d", &year); if (leap(year) == 1) printf("\n这一年是闰年\n\n"); if (leap(year) == 0) printf("\n这一年是平年\n\n"); } void opt3() { cls(); printf("\n请输入年份:\n\n"); (void)scanf("%d", &year); for (month = 1; month <= 12; month++) { if(month==1) printf("\n**************************************************"); int sum = daySum(year, month, run, ping); //计算与基准相差的天数 printf("\n\n"); printf("\t\t %d月\n\n", month); print_week(); printData(sum, year, month); printf("\n**************************************************\n");//按格式打印 } } void opt4() { cls(); printf("\n请输入 年 月:\n\n"); (void)scanf("%d %d", &year, &month); while (judge_month(month)) { (void)scanf("%d %d", &year, &month); } if (judge_month(month) == 0) { if (leap(year) == 1) printf("\n本月的天数为%d\n\n", run[month - 1]); //从数组中读取 if (leap(year) == 0) printf("\n本月的天数为%d\n\n", ping[month - 1]); } } void opt5() { cls(); printf("\n输入 年 月 日 日程 ,每一项用空格或回车隔开:\n\n"); node tmp; (void)scanf("%d%d%d%s", &tmp.nian, &tmp.yue, &tmp.ri, tmp.str); while (judge_month(tmp.yue) || judge_day(tmp.nian, tmp.yue, tmp.ri)) { (void)scanf("%d%d%d%s", &tmp.nian, &tmp.yue, &tmp.ri, tmp.str); } FILE* fp; if ((fp = fopen("data.dat", "ab+")) == NULL) { //存入文件,若不存在则先生成 fp = fopen("data.dat", "wb+"); } fwrite(&tmp, sizeof(struct node), 1, fp); //写入 printf("\n已添加以下日程:\n"); printf("\n%d %d %d %s\n\n", tmp.nian, tmp.yue, tmp.ri, tmp.str); fclose(fp); } void opt6() { cls(); FILE* fp; if ((fp = fopen("data.dat", "rb+")) == NULL) { printf("\n\t无日程!\n\n"); return; } int cnt = 0; while (fread(&a[++cnt], sizeof(struct node), 1, fp)); //将文件中的日程信息读出来,接着排序 cnt--; std::sort(a + 1, a + 1 + cnt); printf("\n按时间排序后,日程如下:\n"); //输出 for (int i = 1; i <= cnt; ++i) { printf("\n%d %d %d %s\n", a[i].nian, a[i].yue, a[i].ri, a[i].str); } printf("\n"); fclose(fp); } void opt7() { cls(); printf("\n\n\t感谢使用"); } int main() { system("color 0B"); //修改配色 loading(); //加载界面 cls(); int opt; print_title(); while (1) { // 死循环,只通过opt7退出程序 (void)scanf("%d", &opt); switch (opt) { case 1: opt1(); system("pause"); refresh(); break; case 2: opt2(); system("pause"); refresh(); break; case 3: opt3(); system("pause"); refresh(); break; case 4: opt4(); system("pause"); refresh(); break; case 5: opt5(); system("pause"); refresh(); break; case 6: opt6(); system("pause"); refresh(); break; case 7: opt7(); Sleep(1000); //延迟关闭,优化体验 return 0; default: cls(); system("pause"); refresh(); break; } } return 0; }点击查看代码
修改后的程序截图
完结撒花
祝天下所有女神节日快乐!
标签:万年历,int,31,year,month,printf,优化,day From: https://www.cnblogs.com/3113538cc/p/17187896.html