用c语言编写程序

2024年11月16日 00:33
有3个网友回答
网友(1):

我编一个程序,可以算出xx年xx月xx日是星期几

#include
#include
 
void main(){
         system("title 算星期");/*程序预定义颜色、标题*/
         system("color F1");
         
         int run[13] = { 0, 5, 1, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4 };/*run是闰(闰年)的拼音*/
    int pin[13] = { 0, 6, 2, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4 };/*pin是平(平年)的拼音*/
         int year, month, day, century;/*年、月、日、世纪的变量*/
         int answer = 0;/*求和变量*/
         int defaultture = 1;/*在确定世纪的时候用作flag*/
 
         printf_s("请输入日期,格式为年-月-日,请使用英文半角输入法\n");
         system("say 请输入日期,格式为年-月-日,请使用英文半角输入法");
         scanf_s("%d-%d-%d", &year, &month, &day);
 
         if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0 || year == 1){/*判断闰年还是平年,从表格中取得相应的值*/
                  answer += run[month];
         }
         else{
                  answer += pin[month];
         }
 
         answer += day;/*添加day的值,算出世纪*/
         century = year / 100 + 1;
 
         while (defaultture == 1){/*算出年份对应的代码*/
                  switch (century){
                  case 21:
                          year -= 2000;
                          answer = answer + (year / 4 + year) % 7;
                          defaultture = 0;
                          break;
                  case 22:
                          year -= 2100;
                          answer = answer + (year / 4 + year) % 7 - 2;
                          defaultture = 0;
                          break;
                  case 20:
                          year -= 1900;
                          answer = answer + (year / 4 + year) % 7 + 1;
                          defaultture = 0;
                          break;
                  case 19:
                          year -= 1800;
                          answer = answer + (year / 4 + year) % 7 + 3;
                          defaultture = 0;
                          break;
                  default:/*不断改变世纪,使世纪编号是19~22*/
                          if (century > 22){
                                   century -= 4;
                                   year -= 400;
                          }
                          else{
                                   century += 4;
                                   year += 400;
                          }
                          defaultture = 1;
                  }
         }
 
         answer %= 7;/*将取值范围缩小到7(一周的天数)以内*/
 
         printf_s("这一天是星期");/*打印星期,程序结束*/
         switch (answer){
         case 1:
                  printf_s("一\n");
                  system("say 这一天是星期一");
                  break;
         case 2:
                  printf_s("二\n");
                  system("say 这一天是星期二");
                  break;
         case 3:
                  printf_s("三\n");
                  system("say 这一天是星期三");
                  break;
         case 4:
                  printf_s("四\n");
                  system("say 这一天是星期四");
                  break;
         case 5:
                  printf_s("五\n");
                  system("say 这一天是星期五");
                  break;
         case 6:
                  printf_s("六\n");
                  system("say 这一天是星期六");
                  break;
         case 0:
                  printf_s("日\n");
                  system("say 这一天是星期日");
                  break;
         }
         printf_s("\n");
 
         system("pause");
}

网友(2):

具体实现什么功能呢

网友(3):

你到底要问什么?