[C] 萬年歷, →→→→→进入此内容的聊天室

来自 , 2020-08-07, 写在 C, 查看 109 次.
URL http://www.code666.cn/view/1843e35d
  1. #include <iostream>
  2. #include<ctype.h> //字符处理函数: 本类别函数用于对单个字符进行处理,包括字符的类别测试和字符的大小写转换。
  3. #include<math.h> //数学函数: 本分类给出了各种数学计算函数。
  4. #include<signal.h>//信号处理: 该分类函数用于处理那些在程序执行过程中发生例外的情况。
  5. #include<stdarg.h> //可变参数处理: 本类函数用于实现诸如printf,scanf等参数数量可变底函数。
  6. #include<stdio.h> //输入输出函数:该分类用于处理包括文件、控制台等各种输入输出设备,各种函数以“流”的方式实现。
  7. #include<string.h> //字符串处理: 本分类的函数用于对字符串进行合并、比较等操作。
  8. #include<time.h> //日期和时间函数: 本类别给出时间和日期处理函数。
  9. #include<stdlib.h> //实用工具函数: 本分类给出了一些函数无法按以上分类,但又是编程所必须要的。
  10. #include<windows.h>//延迟函数。
  11. #define P printf//标准输出
  12. #define S scanf//标准输入
  13. #define C char//字符型
  14. #define F float//单精度型
  15. #define D double//双精度型
  16. #define L long//长整型
  17.  
  18. /* run this program using the console pauser or add your own getch, system("pause") or input loop */
  19.  
  20. int main(int argc, char** argv)
  21. {
  22. //各类自定义的函数 。
  23. int qiuxingqi(int nianfen,int yuefen,int rizi);//求星期几函数。
  24. int qiurunnian(int nianfen);//求是否为闰年函数。
  25. int yanchi(int yanchi);//延遲函數。
  26. void qiuyuerili(int nianfen,int yuefen);//打印某个月份的日历。
  27. void qiunianrili(int nianfen);//打印某年的日历。
  28. //各类自定义的变量。
  29. int xq,sj,nian,yue,ri,run;
  30. int zcxxh=1;//主程序循環。
  31. int zxh=0;//子程序循環。
  32. int cxgn;//功能選擇變量。
  33. int zpd;//子程序判斷。
  34. //系統日期顯示
  35. struct tm *ptr;
  36. time_t it;
  37. it=time(NULL);
  38. ptr=localtime(&it);
  39. //程序欢迎界面
  40. system("color 5");
  41. P("\n");
  42. P("\n");
  43. P("\n");
  44. P("\n");
  45. P("                      *                   *    *                   \n");
  46. P("                ***** *                    * **  ****           ** \n");
  47. P("                    * *****                  *   *  *           ** \n");
  48. P("                *   **    *                  *   *  *           ** \n");
  49. P("                 * **    *                ** *   *  *           ** \n");
  50. P("                 * *   *                   * *   *  *           ** \n");
  51. P("                  *    *                   * * * *  *              \n");
  52. P("                  *    *                   * **  *  *              \n");
  53. P("                 * *  * *                  * *   * **           ** \n");
  54. P("                 * *  * *                  *     *              ** \n");
  55. P("                *    *   *                 *     *                 \n");
  56. P("                    *     *               * *********              \n");
  57. yanchi(21111);
  58. system("cls");
  59. system("color 4");
  60. P("\n");
  61. P("\n");
  62. P("\n");
  63. P("\n");
  64. P("                                    *                        \n");
  65. P("                  ***********       *              **********\n");
  66. P("                     *              *********      *   *     \n");
  67. P("                     *             *    *          *   *     \n");
  68. P("                     *            *     *          * ******* \n");
  69. P("                     *******        ********       *   *   * \n");
  70. P("                     *     *        *   *          *   *   * \n");
  71. P("                     *     *        *   *          *   *   * \n");
  72. P("                     *     *      ***********      *   *   * \n");
  73. P("                    *      *            *          *  *    * \n");
  74. P("                   *       *            *          * *     * \n");
  75. P("                  *     ***             *         * *   ***  \n");
  76. yanchi(31111);
  77. system("cls");
  78. //主程序界面。
  79. while(zcxxh)
  80. {
  81.         system("color a2");
  82.         P("\n");
  83.         P("\n");
  84.         P("\n");
  85.         P("\n");
  86.         P("\n");
  87.         P("         --------------------------------------------------------------\n");
  88.         P("         |                                                            |\n");
  89.         P("         |                 現在是:%4d年%02d月%02d日                     |\n",ptr->tm_year+1900,ptr->tm_mon+1,ptr->tm_mday,ptr->tm_hour,ptr->tm_min,ptr->tm_sec);
  90.         P("         |                                                            |\n");
  91.         P("         |              1、輸入年份,程序判斷是否為閏年。             |\n");
  92.         P("         |                                                            |\n");
  93.         P("         |              2、輸入年月日,程序判斷這天為星期幾。         |\n");
  94.         P("         |                                                            |\n");
  95.         P("         |              3、輸入月份,打印出該月的日歷。               |\n");
  96.         P("         |                                                            |\n");
  97.         P("         |              4、輸入年份,打印出該年的日歷。               |\n");
  98.         P("         |                                                            |\n");
  99.         P("         |                                                            |\n");
  100.         P("         --------------------------------------------------------------\n");
  101.         P("\n");
  102.         P("\n");
  103.         P("請輸入您要選擇的功能!");
  104.         S("%d",&cxgn);
  105.         zxh=1;
  106.         if(cxgn==1)//功能1、判断闰年
  107.         {
  108.                 while(zxh)
  109.                 {
  110.                         system("cls");
  111.                         P("請輸入您想要判斷的年份:");
  112.                         S("%d",&nian);
  113.                         if(nian<=10000)
  114.                         {
  115.                                 run=qiurunnian(nian);
  116.                                 if(run==1)
  117.                                         P("您輸入的該年份為:閏年\n");
  118.                                 else if(run==0)
  119.                                         P("您輸入的該年份為:平年\n");
  120.                         }
  121.                         else
  122.                         {
  123.                                 system("cls");
  124.                                 P("您輸入的格式有誤,請重新輸入......");
  125.                                 yanchi(21111);
  126.                                 system("cls"); 
  127.                                 zxh=1;
  128.                                 continue;
  129.                         }
  130.                         yanchi(21111);
  131.                         P("\n");
  132.                         P("\n");
  133.                         P("您需要的是繼續判斷還是返回主窗口?\n");
  134.                         P("輸入0為主窗口,1為繼續判斷!\n");
  135.                         S("%d",&zpd);
  136.                         if(zpd==1)
  137.                                 zxh=1;
  138.                         else
  139.                         {      
  140.                                 system("cls");
  141.                                 break;
  142.                         }
  143.                 }
  144.         }
  145.         else if(cxgn==2)//功能2、判断星期
  146.         {
  147.                 while(zxh)
  148.                 {
  149.                         system("cls");
  150.                         P("請輸入您想要判斷日期,格式為:年-月-日\n");
  151.                         S("%d-%d-%d",&nian,&yue,&ri);
  152.                         if(yue>12||ri>31)
  153.                         {
  154.                                 P("您輸入的日期有誤!請您重新輸入\n");
  155.                                 system("cls");
  156.                                 zxh=1;
  157.                                 continue;  
  158.                         }
  159.                         else if(yue==2&&ri>29)
  160.                         {      
  161.                                 system("cls");
  162.                                 P("您輸入的日期有誤!請您重新輸入\n");
  163.                                 yanchi(21111);
  164.                                 system("cls");
  165.                                 zxh=1;
  166.                                 continue;
  167.                         }
  168.                         else
  169.                         {
  170.                                 xq=qiuxingqi(nian,yue,ri);
  171.                                 P("您查詢的這一天是:");
  172.                                 switch(xq)
  173.                                 {
  174.                                         case 0:P("星期日!\n");break;
  175.                                         case 1:P("星期一!\n");break;
  176.                                         case 2:P("星期二!\n");break;
  177.                                         case 3:P("星期三!\n");break;
  178.                                         case 4:P("星期四!\n");break;
  179.                                         case 5:P("星期五!\n");break;
  180.                                         case 6:P("星期六!\n");break;
  181.                                 }
  182.                         }
  183.                         yanchi(21111);
  184.                         P("\n");
  185.                         P("\n");
  186.                         P("您需要的是繼續查询還是返回主窗口?\n");
  187.                         P("輸入0為主窗口,1為繼續查询!\n");
  188.                         S("%d",&zpd);
  189.                         if(zpd==1)
  190.                                 zxh=1;
  191.                         else
  192.                         {      
  193.                                 system("cls");
  194.                                 break;
  195.                         }
  196.                 }      
  197.         }
  198.         else if(cxgn==3)//功能3、打印某月日历
  199.         {
  200.                 while(zxh)
  201.                 {
  202.                         system("cls");
  203.                         P("請輸入您想要打印的月份,格式為:年-月\n");
  204.                         S("%d-%d",&nian,&yue);
  205.                         if((nian<10000)||(yue<13||yue>0))
  206.                         {
  207.                                 P("     %d年%d月\n");
  208.                                 qiuyuerili(nian,yue);//打印某个月份的日历
  209.                         }
  210.                         else
  211.                         {
  212.                                 system("cls");
  213.                                 P("您輸入的日期有誤!請您重新輸入\n");
  214.                                 yanchi(21111);
  215.                                 system("cls");
  216.                                 zxh=1;
  217.                                 continue;
  218.                          }
  219.                         yanchi(21111);
  220.                         P("\n");
  221.                         P("\n");        
  222.                         P("您需要的是繼續打印還是返回主窗口?\n");
  223.                         P("輸入0為主窗口,1為繼續打印!\n");
  224.                         S("%d",&zpd);
  225.                         if(zpd==1)
  226.                                 zxh=1;
  227.                         else
  228.                         {      
  229.                                 system("cls");
  230.                                 break;
  231.                         }
  232.                 }
  233.         }
  234.                 else if(cxgn==4)//功能4、打印某年日历
  235.                 {
  236.                         while(zxh)
  237.                         {
  238.                                 system("cls");
  239.                                 P("請輸入您想要打印的年份,格式為:年\n");
  240.                                 S("%d",&nian);
  241.                                 if(nian<10000)
  242.                                         qiunianrili(nian);
  243.                                 else
  244.                                 {
  245.                                         system("cls");
  246.                                         P("您輸入的日期有誤!請您重新輸入\n");
  247.                                         yanchi(21111);
  248.                                         system("cls");
  249.                                         zxh=1;
  250.                                         continue;
  251.                                 }
  252.                                 yanchi(21111);
  253.                                 P("\n");
  254.                                 P("\n");        
  255.                                 P("您需要的是繼續打印還是返回主窗口?\n");
  256.                                 P("輸入0為主窗口,1為繼續打印!\n");
  257.                                 S("%d",&zpd);
  258.                                 if(zpd==1)
  259.                                         zxh=1;
  260.                                 else
  261.                                 {      
  262.                                         system("cls");
  263.                                         break;
  264.                                 }
  265.                         }
  266.                 }
  267. }
  268. return 0;
  269. }
  270. //求星期几函数
  271. int qiuxingqi(int nianfen,int yuefen,int rizi)//求某一天是星期几函数。
  272. {
  273.         if(yuefen==1)//蔡勒公式指出:如果是1,2月 要按照上一年的13,14月来计算。
  274.         {nianfen-=1;
  275.         yuefen=13;
  276.         }
  277.         if(yuefen==2)
  278.         {nianfen-=1;
  279.         yuefen=14;
  280.         }
  281. int shiji=nianfen/100;
  282. nianfen=nianfen-shiji*100;//年数的后两位数。
  283. int xingqi;
  284. xingqi=(nianfen+nianfen/4+shiji/4-2*shiji+26*(yuefen+1)/10+rizi-1)%7;//蔡勒公式。
  285. if(xingqi<0)//负数的时候要往前加星期而不是直接得出的星期.
  286.         xingqi+=7;
  287. return xingqi;//返回值为星期几。
  288. }
  289. //求闰年函数
  290. int qiurunnian(int nianfen)//求某一年是否为闰年。
  291. {
  292. int shifourunnian;//闰年该值为1否为0.
  293. if((nianfen%4==0&&nianfen%100!=0)||(nianfen%400==0))//计算是否为闰年。
  294.         shifourunnian=1;
  295. else
  296.         shifourunnian=0;
  297. return shifourunnian;
  298. }
  299. //延迟函数
  300. int yanchi(int yanchi)//延遲函數,延遲時間與自己cpu有關。
  301. {
  302.         int yccs1,yccs2;
  303.         for(yccs1=0;yccs1<=yanchi;yccs1++)
  304.                 for(yccs2=0;yccs2<=yanchi;yccs2++);
  305.  }
  306.  //打印某一个月份的日历
  307.  void qiuyuerili(int nianfen,int yuefen)//打印某个月份的日历
  308.  {
  309.         int qiurunnian(int nianfen);
  310.         int qiuxingqi(int nianfen,int yuefen,int rizi);
  311.         int runnian,xingqiji,tianshu,rn2yts;
  312.         int kgs=0,jcxh;
  313.         runnian=qiurunnian(nianfen);//判断该年的2月是多少天。
  314.         if(runnian==1)
  315.                 rn2yts=29;
  316.         else if(runnian==0)
  317.                 rn2yts=28;
  318.         xingqiji=qiuxingqi(nianfen,yuefen,1);
  319.         switch(yuefen)//判断该月总共多少天。
  320.         {
  321.                 case 1:tianshu=31;break;
  322.                 case 2:tianshu=rn2yts;break;
  323.                 case 3:tianshu=31;break;
  324.                 case 4:tianshu=30;break;
  325.                 case 5:tianshu=31;break;
  326.                 case 6:tianshu=30;break;
  327.                 case 7:tianshu=31;break;
  328.                 case 8:tianshu=31;break;
  329.                 case 9:tianshu=30;break;
  330.                 case 10:tianshu=31;break;
  331.                 case 11:tianshu=30;break;
  332.                 case 12:tianshu=31;break;      
  333.          }
  334.         kgs=xingqiji-1;
  335.         P("该月有%d天\n",tianshu);
  336.         P("日  一  二  三  四  五  六\n");
  337.         for(jcxh=0;jcxh<=kgs;jcxh++)//输出每月的第一天前面的空格
  338.                 P("    ");
  339.         for(jcxh=1;jcxh<=tianshu;jcxh++)//输出该月的每一天。
  340.                 {
  341.                         P("%-4d",jcxh);
  342.                         if((kgs+jcxh+1)%7==0)
  343.                                 P("\n");
  344.                 }
  345.         P("\n");
  346.  }
  347.  //打印某一年的日历
  348.  void qiunianrili(int nianfen)//打印某年的日历。
  349.  {
  350.         void qiuyuerili(int nianfen,int yuefen);//打印每月的日历计算。
  351.         int yuefen;
  352.         for(yuefen=1;yuefen<=12;yuefen++)//循环每月
  353.         {
  354.                 P("      %d年%d月\n",nianfen,yuefen);
  355.                 qiuyuerili(nianfen,yuefen);
  356.                 P("\n");
  357.                 P("\n");
  358.          }       
  359.   }

回复 "萬年歷,"

这儿你可以回复上面这条便签

captcha