当前位置:首页 > 通信资讯 > 正文

本文实例为大家分享了C++实现万年历的具体代码,供大家参考,具体内容如下

用C++写了个简易的万年历。

具体功能如下:

1.打印指定年(用户输入)所有月份的年历

2.打印指定年指定月(用户输入)的月份

3.打印指定日期(用户输入)的星期数

4.可重复输入

贴上源码:

  1. #include<iostream>
  2. #include<windows.h>
  3. #include<iomanip>
  4. using namespace std;
  5. int number; //菜单键
  6. int year, month, day; //年、月、日
  7. int i, j, t; //for循环用的量
  8. int s; //星期X
  9. char c; //存放随机输入的数字,以实现“按任意键返回主菜单”的功能
  10. char months[] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 }; //平年每个月的天数
  11. void Pos(int x, int y); //光标位置
  12. void menu(); //主菜单函数
  13. void runnian(); //如是闰年则变第二个月天数28为29
  14. void oneyear(); //输出一整年的年历
  15. void onemonth(); //输出一个月的月历
  16. void xianshiweek(); //显示星期数
  17. void Pos(int x, int y)//光标位置
  18. {
  19. COORD pos;
  20. HANDLE hOutput;
  21. pos.X = x;
  22. pos.Y = y;
  23. hOutput = GetStdHandle(STD_OUTPUT_HANDLE);
  24. SetConsoleCursorPosition(hOutput, pos);
  25. }
  26. void menu()//主菜单函数
  27. {
  28. Pos(40, 3);
  29. cout << "***********************************" << endl;
  30. Pos(40, 4);
  31. cout << "* 欢迎使用万年历 *" << endl;
  32. Pos(40, 5);
  33. cout << "* ---made by pjr *" << endl;
  34. Pos(40, 6);
  35. cout << "***********************************" << endl;
  36. Pos(20, 8);
  37. cout << "操作键:" << endl;
  38. Pos(20, 9);
  39. cout << "1.显示一年的年历" << endl;
  40. Pos(20, 10);
  41. cout << "2.显示一月的月历" << endl;
  42. Pos(20, 11);
  43. cout << "3.显示某一天是星期几" << endl;
  44. Pos(20, 12);
  45. cout << "0.退出" << endl;
  46. Pos(20, 14);
  47. cout << "请输入操作键(0~3):";
  48. cin >> number;
  49. if (number < 0 || number>3)
  50. {
  51. system("cls");
  52. Pos(20, 15);
  53. cout << "输入数字无效,请重新输入!" << endl;
  54. menu();
  55. }
  56. }
  57. void runnian() //如是闰年则变第二个月天数28为29
  58. {
  59. cin >> year;
  60. if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) //闰年判断公式
  61. {
  62. months[2] = 29;
  63. }
  64. }
  65. void oneyear() //输出一整年的年历
  66. {
  67. cout << "请输入年份:";
  68. runnian();
  69. system("cls"); //清屏
  70. cout << "请输入年份:" << year << endl << endl;
  71. s = (year - 1 + (year - 1) / 4 - (year - 1) / 100 + (year - 1) / 400 + 1) % 7; //该年1月1日的星期数
  72. for (i = 1; i <= 12; i++)
  73. {
  74. cout << i << "月份的月历如下:" << endl;
  75. cout << setw(6) << "日" << setw(6) << "一" << setw(6) << "二" << setw(6) << "三" << setw(6) << "四" << setw(6) << "五" << setw(6) << "六" << endl;
  76. for (j = 0; j < s; j++)
  77. {
  78. cout << setw(6) << " ";
  79. }
  80. for (t = 1; t <= months[i]; t++)
  81. {
  82. cout << setw(6) << t;
  83. s = (s + 1) % 7;
  84. if (s % 7 == 0) //当打印到星期六时,换行
  85. {
  86. cout << endl;
  87. }
  88. }
  89. cout << endl;
  90. }
  91. fflush(stdin);
  92. cout << "请按任意键返回主菜单:";
  93. cin >> c;
  94. system("cls");
  95. menu();
  96. }
  97. void onemonth()//输出一个月的月历
  98. {
  99. int s = 0;
  100. cout << "请输入年份:";
  101. runnian();
  102. cout << "请输入月份:";
  103. cin >> month;
  104. system("cls");
  105. cout << "请输入年份:" << year << endl << endl;
  106. cout << "请输入月份:" << month << endl << endl;
  107. for (i = 1; i <= month - 1; i++)
  108. {
  109. s = s + months[i]; //该年1月1日到所求日期前一天的天数
  110. }
  111. s = (year - 1 + (year - 1) / 4 - (year - 1) / 100 + (year - 1) / 400 + 1 + s) % 7; //所求日期的星期数
  112. cout << month << "月份的月历如下:" << endl;
  113. cout << setw(6) << "日" << setw(6) << "一" << setw(6) << "二" << setw(6) << "三" << setw(6) << "四" << setw(6) << "五" << setw(6) << "六" << endl;
  114. for (j = 0; j < s; j++)
  115. {
  116. cout << setw(6) << " ";
  117. }
  118. for (t = 1; t <= months[month]; t++)
  119. {
  120. cout << setw(6) << t;
  121. s = (s + 1) % 7;
  122. if (s % 7 == 0)
  123. {
  124. cout << endl;
  125. }
  126. }
  127. cout << endl;
  128. cout << "请按任意键返回主菜单:";
  129. cin >> c;
  130. system("cls");
  131. menu();
  132. }
  133. void xianshiweek() //显示星期数
  134. {
  135. int s = 0;
  136. cout << "请输入年份:";
  137. runnian();
  138. cout << "请输入月份:";
  139. cin >> month;
  140. cout << "请输入日期:";
  141. cin >> day;
  142. system("cls");
  143. cout << "请输入年份:" << year << endl << endl;
  144. cout << "请输入月份:" << month << endl << endl;
  145. cout << "请输入日期:" << day << endl << endl;
  146. for (i = 1; i <= month - 1; i++)
  147. {
  148. s = s + months[i];
  149. }
  150. s = (year - 1 + (year - 1) / 4 - (year - 1) / 100 + (year - 1) / 400 + day + s) % 7;
  151. cout << "显示的星期数如下:" << s << endl;
  152. cout << endl;
  153. cout << "请按任意键返回主界面:";
  154. cin >> c;
  155. system("cls");
  156. menu();
  157. }
  158. int main()//主函数
  159. {
  160. setlocale(LC_ALL, "chs");//转中文
  161. menu();
  162. while (number != 0)
  163. {
  164. switch (number)
  165. {
  166. case 1:
  167. {
  168. oneyear();
  169. break;
  170. }
  171. case 2:
  172. {
  173. onemonth();
  174. break;
  175. }
  176. case 3:
  177. {
  178. xianshiweek();
  179. break;
  180. }
  181. }
  182. months[2] = 28; //把months[2]变为初值
  183. }
  184. if (number == 0)
  185. {
  186. system("pause");
  187. }
  188. return 0;
  189. }

运行效果如下:

c语言实现万年历(C语言万年历可添加什么小chuangyi)

c语言实现万年历(C语言万年历可添加什么小chuangyi)

c语言实现万年历(C语言万年历可添加什么小chuangyi)

c语言实现万年历(C语言万年历可添加什么小chuangyi)

c语言实现万年历(C语言万年历可添加什么小chuangyi)

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

原文链接:https://blog.csdn.net/qq_36224413/article/details/73302156

如果您对该产品感兴趣,请填写办理(客服微信:xiaoxiongyidong)

为您推荐:

发表评论

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。