关于C语言的问题。设计一个程序,根据用户的选择,将1、日期转换为天数2、将天数转换为日期。
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了关于C语言的问题。设计一个程序,根据用户的选择,将1、日期转换为天数2、将天数转换为日期。相关的知识,希望对你有一定的参考价值。
#include<stdio.h>
int main()
#define MAXNUM1 14
#define MAXNUM2 14
int a,b,c,Y,M,D,Days;
int Days_[MAXNUM1]=0,31,28,31,30,31,30,31,31,30,31,30,31;
int Date[MAXNUM2]=0,1,2,3,4,5,6,7,8,9,10,11,12;
printf("Plese choose what you want to do.\n1.Date change Days\n2.Days change Date\n");
scanf("%d",&a);
if(a==1)
printf("Plese enter a date(Y\M\D).\n");
scanf("%d %d %d",&Y,&M,&D);
if(Y%4==0||Y%100!=0||Y%400==0)
for(b=1;b<2;b++)
if(b<2)
Days=Days_[b]+D;
printf("The date which you enter is the %d days.\n",Days);
else if(b>=2)
Days=Days_[b]+D+1;
printf("The date which you enter is the %d days.\n",Days);
else
for(b=1;b<=M;b++)
if(b<2)
Days=Days_[b]+D;
printf("The date which you enter is the %d days.\n",Days);
else if(b>=2)
Days=Days_[b]+D;
printf("The date which you enter is the %d days.\n",Days);
else
printf("Plese enter a days.");
scanf("%d",&Days);
代码存在一些问题:
#include<stdio.h#define MAXNUM1 14
#define MAXNUM2 14
int main()
int a,b,c,Y,M,D,Days;
int Days_[MAXNUM1]=0,31,28,31,30,31,30,31,31,30,31,30,31;
int Date[MAXNUM2]=0,1,2,3,4,5,6,7,8,9,10,11,12;
printf("Plese choose what you want to do.\\n1.Date change Days\\n2.Days change Date\\n");
scanf("%d",&a);
if(a==1)
Days = 0;
printf("Plese enter a date(Y\\M\\D).\\n");
scanf("%d %d %d",&Y,&M,&D);
if(Y%4==0&&Y%100!=0||Y%400==0)//这里改下
for(b=1;b < M;b++)
if(b==2)//闰年二月加一天
Days+=Days_[b]+1;
else
Days+=Days_[b];
else
for(b=1;b < M;b++)
Days+=Days_[b];
Days += D;
printf("The date which you enter is the %d days.\\n",Days);
else //a==2
printf("Plese enter a days.");
scanf("%d",&Days);
for(b=1;b < 13;b++)//未考虑年份的问题
if(Days <= Days_[b]) break;
Days -= Days_[b];
printf("The day which you enter is the %d\\%d days.\\n",b,Days);
上面是简单的实现,希望有帮助
追问多谢啦。我之前已经采纳了一个,不过你的也是很好的。如果能采纳两个的话就好了。我就连你的也采纳。
参考技术A 是当年的吧,要是跨年,计算就不是这水平的了~
~
~ 参考技术B // xx.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include<stdio.h>
int main()
#define MAXNUM1 14
#define MAXNUM2 14
int a,b,Y,M,D,Days;
int Days_[MAXNUM1]=0,31,28,31,30,31,30,31,31,30,31,30,31;
int Date[MAXNUM2]=0,1,2,3,4,5,6,7,8,9,10,11,12;
printf("Plese choose what you want to do.\\n1.Date change Days\\n2.Days change Date\\n");
scanf("%d",&a);
if(a==1)
printf("Plese enter a date(Y\\\\M\\\\D).\\n");
scanf("%d %d %d",&Y,&M,&D);
for(Days=0,b=1;b<M;++b)
Days += Days_[b];
Days += D;
if(Y%4==0 && (Y%100!=0||Y%400==0))
++Days;
printf("The date which you enter is the %d days.\\n",Days);
else
printf("Plese enter a days.");
scanf("%d",&Days);
return 0;
本回答被提问者采纳 参考技术C 没明白你说的天数是什么意思?
以上是关于关于C语言的问题。设计一个程序,根据用户的选择,将1、日期转换为天数2、将天数转换为日期。的主要内容,如果未能解决你的问题,请参考以下文章