鱼C论坛

 找回密码
 立即注册
查看: 2597|回复: 7

[已解决]C语言作业设计万年历求帮助非常感谢

[复制链接]
发表于 2019-6-25 13:14:19 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x

一、设计项目
万年历。编写程序实现当用户输入年份(>2000)后,屏幕上输出该年1月的日历,按回车输出下月的日历。直到输出12月的日历结束。
二、设计提交方式
1、 在计算机上给出运行的结果;
2、 程序设计综合训练设计报告。
三、内容要求
1、用C语言实现程序设计
2、显示万年历。
最佳答案
2019-6-25 22:44:33
算了,我帮你写了
你写不出来,说明你这部分没学好,如果你喜欢编程,那有的是时间学,如果不喜欢那也就算了
这个程序真的不难

  1. #include <stdio.h>
  2. #include <stdbool.h>

  3. // 判断是否为闰年
  4. bool is_leap_year(size_t year)
  5. {
  6.         if((year % 4 == 0 && year % 100 != 0 ) || ( year % 400 == 0))
  7.                 return true;
  8.         return false;
  9. }

  10. // 给定年月日,返回星期几
  11. size_t get_week(size_t year, size_t month, size_t day)
  12. {
  13.         day += 1;
  14.         month += 1;
  15.         return (day + 2 * month + 3 * (month + 1) / 5 + year + year / 4 - year / 100 + year / 400) % 7;
  16. }

  17. void print_month(size_t year, size_t month)
  18. {
  19.         // 平年每个月的天数
  20.         size_t days[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  21.         const char *year_name[12] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
  22.         if(is_leap_year(year))
  23.                 days[1] = 29;

  24.         printf("            %lu        %s             \n", year, year_name[month]);
  25.         printf("--------------------------------------\n");
  26.         printf("   Sun  Mon  Tue  Wed  Thu  Fri  Sat  \n");
  27.         printf("--------------------------------------\n");
  28.         size_t start = get_week(year, month, 0);
  29.         for(size_t i = 0; i < start; ++i)
  30.                 printf("     ");
  31.         for(size_t i = 0; i < days[month]; ++i)
  32.         {
  33.                 if((i + start) != 0 && (i + start) % 7 == 0)
  34.                         printf("\n");
  35.                 printf("%5lu", i + 1);
  36.         }
  37.         printf("\n");
  38. }

  39. int main(void)
  40. {
  41.         size_t year;
  42.         printf("请输入年份: ");
  43.         scanf("%lu", &year); getchar();
  44.         for(size_t i = 0; i < 12; ++i)
  45.         {
  46.                 print_month(year, i);
  47.                 getchar();
  48.         }
  49.         return 0;
  50. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2019-6-25 14:12:33 | 显示全部楼层
在这里索要作业的完整源代码的人,只有不到10%的人成功了,也就是说你有90%以上的概率不会得到答案
那么你还在等什么?自己动手写呀
把你在写代码的过程中遇到的问题贴出来,有90%以上的概率你能得到答案
对于你这个问题,我说有98%以上的概率你相信吗?
所以,自己动手写

评分

参与人数 1荣誉 +5 鱼币 +5 贡献 +3 收起 理由
RIXO + 5 + 5 + 3 本来想答的,哈哈哈

查看全部评分

小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

 楼主| 发表于 2019-6-25 16:22:40 | 显示全部楼层
人造人 发表于 2019-6-25 14:12
在这里索要作业的完整源代码的人,只有不到10%的人成功了,也就是说你有90%以上的概率不会得到答案
那么你 ...

就是完全没有头绪嘛,无从下手,我怎么写
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-6-25 17:44:47 | 显示全部楼层
求你了,网上搜的看 不懂
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-6-25 19:15:29 | 显示全部楼层
先做这样的界面
1.png
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-6-25 20:34:39 | 显示全部楼层

#include<stdio.h>
int main()
{
int years,mounth,start=0,days,i,shu,j,count=0;
printf("请输入年份\n");
scanf("%d",&years);
days=(years-1)*365+(years-1)/4-(years-1)/100+(years-1)/400+1;
start=days%7;

for(mounth=1;mounth<13;mounth++)
{
printf("%d月份\n",mounth);
if(mounth%2!=0||mounth==8) shu=31;  //判断月份天数
else
  {
  if(mounth==2)
   {
   if(years%4==0&&years%100!=0) shu=29;
   else shu=28;
   }
  else shu=30;
  }
printf("星期日\t星期一\t星期二\t星期三\t星期四\t星期五\t星期六\n");

  for(i=0;i<start;i++)  //输出月份前几天的空格
   {
   printf("\t");
   count++;
   }
for(i=1;i<=shu;i++)  //输出每个月的天数
  {
  printf("%d\t",i);
  count++;
  if(count%7==0) printf("\n");
  }
start=count%7;  //判断除了一月份的其他月份的月前空格数
count=0;
printf("\n");
}
return 0;
}

不达到题目要求,一次回车就打应下一个月的
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-6-25 20:39:17 | 显示全部楼层
能不能发一下你的,你这么厉害
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-6-25 22:44:33 | 显示全部楼层    本楼为最佳答案   
算了,我帮你写了
你写不出来,说明你这部分没学好,如果你喜欢编程,那有的是时间学,如果不喜欢那也就算了
这个程序真的不难

  1. #include <stdio.h>
  2. #include <stdbool.h>

  3. // 判断是否为闰年
  4. bool is_leap_year(size_t year)
  5. {
  6.         if((year % 4 == 0 && year % 100 != 0 ) || ( year % 400 == 0))
  7.                 return true;
  8.         return false;
  9. }

  10. // 给定年月日,返回星期几
  11. size_t get_week(size_t year, size_t month, size_t day)
  12. {
  13.         day += 1;
  14.         month += 1;
  15.         return (day + 2 * month + 3 * (month + 1) / 5 + year + year / 4 - year / 100 + year / 400) % 7;
  16. }

  17. void print_month(size_t year, size_t month)
  18. {
  19.         // 平年每个月的天数
  20.         size_t days[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  21.         const char *year_name[12] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
  22.         if(is_leap_year(year))
  23.                 days[1] = 29;

  24.         printf("            %lu        %s             \n", year, year_name[month]);
  25.         printf("--------------------------------------\n");
  26.         printf("   Sun  Mon  Tue  Wed  Thu  Fri  Sat  \n");
  27.         printf("--------------------------------------\n");
  28.         size_t start = get_week(year, month, 0);
  29.         for(size_t i = 0; i < start; ++i)
  30.                 printf("     ");
  31.         for(size_t i = 0; i < days[month]; ++i)
  32.         {
  33.                 if((i + start) != 0 && (i + start) % 7 == 0)
  34.                         printf("\n");
  35.                 printf("%5lu", i + 1);
  36.         }
  37.         printf("\n");
  38. }

  39. int main(void)
  40. {
  41.         size_t year;
  42.         printf("请输入年份: ");
  43.         scanf("%lu", &year); getchar();
  44.         for(size_t i = 0; i < 12; ++i)
  45.         {
  46.                 print_month(year, i);
  47.                 getchar();
  48.         }
  49.         return 0;
  50. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-7-1 14:29

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表