从键盘上输入一个日期,请输出该日期的前一天、后一天的日期,该日期是该年的第几天,该日期是星期几等信息。
#include "stdio.h"
#include "math.h"
int main()
{
double a,b,c,sum,area;
printf("请输入三个数:");
scanf("%lf%lf%lf",&a,&b,&c);
if(a>0 && b>0 && c>0 && a+b>c && b+c>a && a+c>b)
{
printf("输入的三边能组成三角形,面积:");
sum=(a+b+c)/2;
area=sqrt(sum*(sum-a)*(sum-b)*(sum-c));
printf("%lf\n",area);
}
else
{
printf("Data error!\n");
}
}
#include <stdio.h>
#include <stdlib.h>
int year,month,date,sumDate;
int checkYear(int a){
if((a%4==0&&a%100!=0)||(a%400==0)){
return 1;
}else{
return 0;
}
}
void sum(int a,int b,int c){
int arr[]={31,28,31,30,31,30,31,31,30,31,30,31};
int arr1[]={31,29,31,30,31,30,31,31,30,31,30,31};
int count=0; //计算闰年的数量
int curSumDate=0;
int i;
int week;
for(i=1970;i<a;i++){
if( checkYear(i)){
count++; //计算这期间所有的闰年数
}
}
sumDate=365*(a-1970)+count; //将本年以前所有的天数计算出来
if(checkYear(a)){//当年是闰年
for(i=0;i<b-1;i++){
sumDate+=arr1[i]; //将本年当月之前的所有月份加起来
curSumDate+=arr1[i];//计算当年月份,用来输出位于当年的多少天
}
}else{
for(i=0;i<b-1;i++){
sumDate+=arr[i];
curSumDate+=arr[i];
}
}
sumDate+=c; //将本年本月的天数加起来
curSumDate+=c;
switch((sumDate+3)%7){ //由于1970年1月1日是星期四所以整除要+3天
case 0:week=7;break;
case 1:week=1;break;
case 2:week=2;break;
case 3:week=3;break;
case 4:week=4;break;
case 5:week=5;break;
case 6:week=6;break;
}
printf("%d年%d月%d日是%d年的第%d天,星期%d\n",a,b,c,a,curSumDate,week);
}
int main(void) {
aa: printf("请输入年月日,之间用空格隔开\n");
scanf("%d%d%d",&year,&month,&date);
if(year<1970){
printf("年份不合法,请重新输入\n");
system("pause");
goto aa;
}else{
if(month<1||month>12){
printf("月份不合法,请重新输入\n");
system("pause");
goto aa;
}else{
if(date<1||date>31){
printf("日期不合法,请重新输入\n");
system("pause");
goto aa;
}
}
}
// printf("\n%d此年是闰年\n",year);
switch(month){
case 4:;
case 6:;
case 9:;
case 11:
if(date>30){
printf("日期不合法,请重新输入\n");
system("pause");
goto aa;
}else{
}
;
case 2:
if((year%4==0&&year%100!=0)||(year%400==0)){
if(date>29){
printf("日期不合法,请重新输入\n");
system("pause");
goto aa;
}
}else{
if(date>28){
printf("日期不合法,请重新输入\n");
system("pause");
goto aa;
}
}
}
sum(year,month,date);
sum(year,month,date-1);
sum(year,month,date+1);
return 0;
}
我告诉你,你觉得麻烦还是因为你懒。这个程序里面校验也加了,不过说实话,这个程序我前前后后,修改也花了快1个小时了
思路:第几天要判断这一年是平年还是闰年,然后计算出来,星期几比较麻烦,你要计算从1970-输入日期的天数,然后这个天数对7求余数得到星期几,根据这个思路尝试着敲代码,百度上有现成的判断 平年和闰年的例子