用蔡勒公式即可:
#include <stdio.h>
int main ()
{
int y, m, d, c, w;
printf ("输入年 月 日(空格间隔):");
scanf ("%d %d %d", &y, &m, &d);
if (m ==1 || m == 2)
{ //判断月份是否为1或2y--;m += 12;}
c = y / 100;
y = y - c * 100;
w = (c / 4) - 2 * c + (y + y / 4) + (13 * (m + 1) / 5) + d - 1;
while (w < 0) w += 7;
w %= 7;
if (w == 0) printf ("星期日");
else printf ("星期%d", w);
return 0;
}
方法二:
#include
void main ()
{
int y,m,d,c,s,w;
printf("请输入8位年月日,以空格隔开:");
scanf("%d%d%d",&y,&m,&d);
if (m==1) c=d;
else if (m==2) c=31 d;
else if (m==3) c=31 28 d;
else if (m==4) c=31 28 31 d;
else if (m==5) c=31 28 31 30 d;
else if (m==6) c=31 28 31 30 31 d;
else if (m==7) c=31 28 31 30 31 30 d;
else if (m==8) c=31 28 31 30 31 30 31 d;
else if (m==9) c=31 28 31 30 31 30 31 31 d;
else if (m==10) c=31 28 31 30 31 30 31 31 30 d;
else if (m==11) c=31 28 31 30 31 30 31 31 30 31 d;
else if (m==12) c=31 28 31 30 31 30 31 31 30 31 30 d;
if (m>2)
{if (y0==0) {if (y@0==0) c=c 1;}
else if (y%4==0) c=c 1;}
s=(y-1)*365 (y-1)/4-(y-1)/100 (y-1)/400 c;
w=s%7;
switch (w)
{
case 0: printf("星期日");break;
case 1: printf("星期一");break;
case 2: printf("星期二");break;
case 3: printf("星期三");break;
case 4: printf("星期四");break;
case 5: printf("星期五");break;
case 6: printf("星期六");break;
printf("
");
}
}
扩展资料:
C语言日期函数,日期处理函数:
定义一个表示日期的结构体类型,再分别定义函数完成下列功能:计算某一天是对应年的第几天,这一年一共多少天;计算两个日期之间相隔的天数。两个日期由键盘输入。
算法思想:
设定结构体类型表示日期类型名为 Date,利用 typedef 将其定义为日期型类型名,有三个整型类型的成员分别表示年、月、日。
设定函数计算输入的日期是这一年的第几天。函数的形参为日期型变量,函数体中设定整型数组存放每个月的天数,二月份的天数为 28 天。
设定函数判断年份是否为闰年以决定二月份的天数。根据输入的日期月份,在数组中将相应的月份天数求和,假日曰期即为天数。
设定函数完成两个日期的比较,比较形参 d 和 s 两个日期的大小。首先比较年,同年的比较月,同月的比较日。变量 start 保存输入的小的日期年份,end 保存输入日期大的年份,然后计算两个日期之间的天数。
程序由 6 个函数构成,yearday() 函数计算某年的天数,monthday() 函数计算某年二月份的天数,dayofyeaK() 函数计算某日期是某年的第几天。
cmpdate() 函数比较两个日期的大小,interday() 函数计算两个日期之间的天数。
dayofyear() 函数调用 monthday() 函数,interday() 函数调用 cmpdate() 函数。
yearday() 函数、dayofyear() 函数;主函数调用 yearday() 函数、dayofyear() 函数、interday() 函数。
简单,用蔡勒公式即可:
#include <stdio.h>
int main ()
{
int y, m, d, c, w;
printf ("输入年 月 日(空格间隔):");
scanf ("%d %d %d", &y, &m, &d);
if (m ==1 || m == 2) { //判断月份是否为1或2
y--;
m += 12;
}
c = y / 100;
y = y - c * 100;
w = (c / 4) - 2 * c + (y + y / 4) + (13 * (m + 1) / 5) + d - 1;
while (w < 0) w += 7;
w %= 7;
if (w == 0) printf ("星期日");
else printf ("星期%d", w);
return 0;
}
#include <stdio.h>
void main()
{
int y,m,d,c,s,w;
printf("请输入8位年月日,以空格隔开:");
scanf("%d %d %d",&y,&m,&d);
if (m==1) c=d;
else if (m==2) c=31+d;
else if (m==3) c=31+28+d;
else if (m==4) c=31+28+31+d;
else if (m==5) c=31+28+31+30+d;
else if (m==6) c=31+28+31+30+31+d;
else if (m==7) c=31+28+31+30+31+30+d;
else if (m==8) c=31+28+31+30+31+30+31+d;
else if (m==9) c=31+28+31+30+31+30+31+31+d;
else if (m==10) c=31+28+31+30+31+30+31+31+30+d;
else if (m==11) c=31+28+31+30+31+30+31+31+30+31+d;
else if (m==12) c=31+28+31+30+31+30+31+31+30+31+30+d;
if (m>2)
{if (y%100==0) {if (y%400==0) c=c+1;}
else if (y%4==0) c=c+1;}
s=(y-1)*365+(y-1)/4-(y-1)/100+(y-1)/400+c;
w=s%7;
switch (w)
{
case 0: printf("星期日");break;
case 1: printf("星期一");break;
case 2: printf("星期二");break;
case 3: printf("星期三");break;
case 4: printf("星期四");break;
case 5: printf("星期五");break;
case 6: printf("星期六");break;
printf("
");
}
}
实现以下功能:
第一任意一年有多少天。
第二,任意的日期是当年的第几天
第三,0年1月1日是星期几,或者其他的一个固定日期你必须确定是星期几。这里比如0年1月1日是星期一。
第四,根据1,2可以知道给定的日期对0年1月一日来说是第几天。这样直接除以7,可以知道该天比“星期一”大几天。直接求出来该日期是星期几即可。
C语言程序,输入年月日算出当天星期几~
先做一个函数,计算1900年1月1日到该天的天数,然后用膜除求出星期几并输出。
具体代码如下:
#include #include int Day(int y,int m,int d);int main(int argc, char *argv[]) {int i; //循环变量int year,month,day; //读取用户输入的年月日int days; //储存从1900-1-1到该天的天数int week=0; //储存求余后的星期//字符串组合输出星期char b[]={"星期"};char c[7][4]={"日","一","二","三","四","五","六"};loop:printf("请输入年 月 日:(空格分隔)
");scanf(" %d%d%d",&year,&month,&day);days=Day(year,month,day);//增强程序的健壮性if(days==-1){printf("输入格式有误,");fflush(stdin); //防止用户输入字母等其他非数字字符goto loop;}else{week=days%7; //用求余计算星期几printf("%d年%d月%d日是%s%s
",year,month,day,b,c[week]);}system("pause");return 0;}//函数功能:输入年月日,输出1900-1-1到该天的天数,-1位输入不合法int Day(int y,int m,int d){int i;int r=0; //储存1900到该天的闰年个数int a[]={31,28,31,30,31,30,31,31,30,31,30}; //m给每出月的天数if(y>=1900 && m>0 && m0 && d<32){for(i=1900;i<=y;i++) //计算闰年的个数{if(i%400==0||i%100!=0&&i%4==0)r++;}for(i=0;i<m-1;i++) //统计当年1月1日到当月的天数{d+=a[i];}if((y%400==0||y%100!=0&&y%4==0)&&m<3) //如果当年是闰年但不到二月就不能算进去r--;return 365*(y-1900)+r+d;}elsereturn -1;}
#include
#define SIZE 31
void inputCheck(int,int,int);
int leaYear(int);
main()
{
int year,month,day,d1,result,sum=0;
scanf("%d%d%d",&year,&month,&day);
inputCheck(year,month,day);
for(;year>1; year--)
{
if((year%4==0 && year%100!=0)||(year%400==0))
sum+=366;
else
sum+=365;
}
d1=leaYear(month);
if((year%4==0 && year%100!=0) || (year%400==0))
{
result=(sum+d1+day)%7;}
else
{ if(month>2)
result=(sum+d1+day-1)%7;
else result=(sum+d1+day)%7;
}
switch(result)
{
case 1:printf("Monday
");break;
case 2:printf("Tuesday
");break;
case 3:printf("Wedensday
");break;
case 4:printf("Thurfay
");break;
case 5:printf("Friday
");break;
case 6:printf("Saturday
");break;
case 7:printf("Sunday
");break;
}
return 0;
}
int leaYear(int m)
{
int t=0;
for(; m>1;m--)
{
switch(m)
{
case 2:
t+=29;
break;
case 4:
case 6:
case 9:
case 11:
t+=30;
break;
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
t+=31;break;
}
}
return t;
}
void inputCheck(int year,int month,int day)
{
if(year<0)
printf("input is wrong!
");
else
{
switch(month)
{
case 2:
if(day>28 && (!((year%4==0 && year%100!=0) || (year%400==0))))
printf("input is wrong!
");
if(day>29 && ((year%4==0 && year%100!=0) || (year%400==0)))
printf("input is wrong!
");break;
case 4:
case 6:
case 9:
case 11:
if(day>30)
printf("input is wrong!
");break;
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
if(day>31)
printf("input is wrong!
");
}
}
}
感觉应该加上判断输入数据是否合格的函数,即便没这个要求,我用测试数据1900 1 1试了试,我的结果是星期一,你的错误你来找吧
#18411522731#
用c语言编写一个程序实现以下功能 - ******
#哈邢# 12345678910 #include "stdio.h" intmain(intargc,char*argv[]){ 5261int a,b; doublex,y; charch1,ch2; printf("Please enter the data...\n"); scanf("%d%d%lf%lf %c%c",&a,&b,&x,&y,&ch1,&ch2); printf("\na=%d,b=%d\nx=%f,y=%f\nch1=%c,ch2=%c\n",a,b,x,y,ch1,ch2); return0; } 运行样例:
#18411522731#
用C程序编程序实现功能,求出任一输入字符的ASCII码 - ******
#哈邢# #include "stdio.h" void main(void){ char ch; printf("Type a character...\nch="); ch=getchar(); printf("The code of \"%c\" is %d.\n",ch,ch):}
#18411522731#
请编写程序,实现以下功能?请编写程序,实现以下功能:任意输入一个 ******
#哈邢# #include int x,y; char *output[20]; int p; void main(){ printf("请输入一个坐标如:3,3\n"); while(scanf("%d,%d",&x,&y)!=EOF) { if(x > 0 && y > 0) p=1; else if(x > 0 && y 0) p=2; else if(x
#18411522731#
用C语言编写程序,完成如下功能 - ******
#哈邢# #include<stdio.h>#include<Windows.h> struct STU{ long sno; char sname[8]; int sage; int sclass; struct STU *next; }; int *pi; void Print_slist_no(struct STU *h) { struct STU *p; p=h->next; if(p=='\0') { printf("Linklist is null!\n"); } else { // printf("heead...
#18411522731#
用C语言编程 编写一个程序实现下面的功能? ******
#哈邢# #include <stdlib.h> #include <stdio.h> #include <math.h> bool smaller(char* word1, char* word2) { int i; bool ret = false; int size1 = strlen(word1); int size2 = strlen(word2); int step = size1 < size2 ? size1 : size2; if(size1 > size2) { return false; } else if(...
#18411522731#
C语言编程1、编写一个程序实现如下功能:输入10个学生5门课程的成绩,分别用函数求:①每个学生的平均分;②每门课程的平均分;③找出最高的分数所... - ******
#哈邢#[答案] 第一题:#include #include #define STUDENT_NUM 2 //学生数量,修改成10即可#define COURSE_NUM 5 //课程数量,为5typedef struct { float course[COURSE_NUM]; float ave;} student;void main(){ student s...
#18411522731#
1.编程序实现如下功能:输入任意一个年份和月份,按以下格式输出该月份的公历日历. - ******
#哈邢# #include main(){ int year,monthmax,m,i,j,d; printf("请输入年份:\n"); scanf("%d",...
#18411522731#
编写一个程序,完成以下功能:(C语言) - ******
#哈邢# #include<stdio.h>#define N 10/*选择排序*/void sort(int a[]){int i,j,k,temp;for(i=0; i<N; i++){k = i;for(j=i+1; j<N; j++){if(a[k] > a[j]){k = j;}}if(k != i){temp = a[k];a[k] = a[i];a[i] = temp;}}}/*二分查找*/int find(int a[], int x){ int low,high,mid,index; low = 0; high = 19;...
#18411522731#
C语言问题 ******
#哈邢# 首先你要一次输入任意长度的字符,那么必须用程序运行时的参数来搞定,所以main函数的参数至关重要.然后,需要对字符串中的字符进行处理,因为只有10个数,所以用switch或者用if来判断都可以.以下是答案:#include <stdio.h>int main...
#18411522731#
c语言程序设计上机实验 ******
#哈邢# #include <iostream> using namespace std; int main(){ int day; int timeOfMon[] = {31,29,31,30,31,30,31,31,30,31,30,31}; printf("2012年日历 ==========\n"); int sumOfMon[13]; sumOfMon[0] = 0; for ( int i = 1; i<12; i++){ sumOfMon[i] = ...