C语言问题,定义一个表示日期的结构体变量(包括年月日),写一个函数,返回值为某天是当年的第几天

作者&投稿:野使 (若有异议请与网页底部的电邮联系)
C语言定义一个结构体变量(包括年、月、日),输入一个日期,计算该日在本年中是第几天。~

//希望我的回答对你的学习有帮助#include struct ymd { int Y,M,D; }; const short MonthDay[]={0,31,28,31,30,31,30,31,31,30,31,30,31}; int YMD_2_JD(int Y, int M, int D){ const short MonthDay[]={0,31,28,31,30,31,30,31,31,30,31,30,31}; int JD,i; JD=D; for (i=0;i2)) JD++; return JD; } int main() { int d; struct ymd a; while(~scanf("%d%d%d",&a.Y,&a.M,&a.D)){d=YMD_2_JD(a.Y,a.M,a.D); printf("%d
",d); } return 0; }

#include
#include
#include

struct ymd {
int Y,M,D;
};
const short MonthDay[]={0,31,28,31,30,31,30,31,31,30,31,30,31};

int YMD_2_JD(int Y, int M, int D){
const short MonthDay[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
int JD,i;
JD=D;
for (i=0;i<M;i++) JD+=MonthDay[i];
if (((Y%4==0)&&(Y%100!=0)||(Y%400==0)) && (M>2)) JD++;
return JD;
}


void main()
{
int d,k;
struct ymd a;
printf("input Year Month day
");
k = scanf("%4d %2d %2d",&a.Y,&a.M,&a.D);
if (k != 3) {printf("input err
"); exit(0);};
if (a.M 12) {printf("err Month
"); exit(0); };
if ( (a.M==2) && (a.D==29) ) {
if ( (a.Y%4==0)&&(a.Y%100!=0)||(a.Y%400==0)) {goto Lab;} else {printf("err Day
"); exit(0); };
}
if (a.D MonthDay[a.M]) {printf("err Day
"); exit(0); };
Lab:;
if (a.Y < 0) {printf("negtive year
"); exit(0);};
d=YMD_2_JD(a.Y,a.M,a.D);
printf("%d",d);
exit(0);
}

#include <stdio.h>

struct date //定义结构体类型
{
int y;
int m;
int d;
};

void main()
{
date dt; //定义结构体变量
int i,count, mon[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};

printf("请输入日期(年 月 日):");
scanf("%d",&dt.y);
scanf("%d",&dt.m);
scanf("%d",&dt.d);

count=dt.d; //把 dt.d 计入总天数
for(i=0;i<dt.m;i++)
count+=mon[i]; //把月份 dt.m 前边的每月的天数累计入总数

if( dt.m>2 && (dt.y%4==0&&dt.y%100!=0||dt.y%400==0) )
count+=1; //如果是闰年,则要把3月份和3月份以后的总天数加1

printf("这一日是这一年的第 %d 天\n", count);
}


C语言中,如何定义一个变量,使其既可以在主函数中使用,也可以在子函数中...
把变量定义在主函数之外,即把变量设置为全局的。一般我们都把变量的定义放在函数中,因此变量的范围就是在本函数中。可是如果定义在所有函数的外面,这样所有的函数中,都可以使用本变量。你可以试一下这个程序。定义一个全局变量a,在主函数中进行赋值,然后在子函数中进行输出,结果是5.include<stdio....

c语言定义一个求和函数int sum(int start,int count)如sum(2,4)为
include <stdio.h>int sum(int start,int count){int i,s=0; for(i=start;i<start+count;i++)s+=i; return s;}int main(){int i,n,s=0; scanf("%d",&n); for(i=1;i<=n;i++) s+=sum(i,i+1); printf("%d\\n",s); return 0;} ...

c语言问题。我想声明一个超大的二维数组——a[1000][1000],如何...
直接定义 int a[1000][1000];变量存放在栈里,这个要看编译的时候栈的大小。如果栈空间不够大,可以通过new实现。int **a = new int *[1000];for(int i=0;i<1000;i++){ a[i] = new int[1000];}

c语言定义一个长度为10的数组并随机给数组赋值 赋值后输出当前数组 再...
include<stdio.h>#include<stdlib.h>#includeint main(){int a[10];int i,j,k,m,n;srand(time(NULL));for(i=0;i<10;i++)a[i]=rand();for(i=0;i<10;i++)printf("%d%c",a[i],i==9?10:32);for(i=0;i<9;i++){for(j=i+1;j<10;j++){if(a[i]...

C语言中定义一个指向字符的指针数组char *s[5], 则*s[1],s[1],*(s...
void sort(char *s[]) 这里接收到的是指针数组,即:数组中各元素 是指针地址,s是数组首地址 s[i]表示第i+1个元素 的值(地址,一个字符串的首地址,* s[i] 表示这个字符串的第一个元素,是个字符)s+i是第i+1个元素的地址,*(s+i)是地址中的数据,等同于s[i]

c语言如何定义一个函数可以返回多个值回去给主函数呀。
函数的返回值顶多是一个数,通过函数直接返回的这个数只有在是指针时才能给出多个数据结果。可以考虑以下几个方法:返回指针:int* display() { int *p; p=malloc(sizeof(int)*6); ... return p; } \/\/用p[0]~p[5]计算各位 void main() { int *p; p=display(); ... free(p); }...

C语言问题 复数包括实部和虚部,请先定义一个描述复数的结构体类型struct...
include <stdio.h>struct complex{ double re; double im;};struct complex add(struct complex c1, struct complex c2){ struct complex sum; sum.re = c1.re + c2.re; sum.im = c1.im + c2.im; return sum;}int main(void){ struct complex a, b, s; p...

C语言问题:为什么我在程序开头定义了一个全局数组并在主函数中赋值了...
\/\/ 你在main里面最后写错了void f1(void); \/\/是函数申明哦不是调用\/\/应该修改为f1();另外建议在main函数最开头的那行void f1(void);建议移动到 main 函数的上面去。\/\/格式大概是这样void f1(void);int main() { \/\/输入数据处理 f1(); return 0;}void f1(void) { \/\/f1的...

在c语言中自定义了一个函数,在main中调用时提示找不到标识符,怎么...
把定义的函数放在,main函数之前即可。自定义的函数需要声明在调用之前,比如说在main函数里调用自定义的fun(),则fun()的方法体需要写在main函数之前。在C语言中的执行总是从main函数开始的,无论main函数在哪个位置都肯定从它开始。如果你定义的函数在main函数之后。而且在main函数前没有进行该调用函数...

C语言中定义一个变量a,其地址还会不会变了?像a=b,意思是值变了,地址...
关键是要区分以下几点:a,表示a的值,即它装的东西,具体到这个例子,a装的是另一个int型变量的地址。如果a不是const类型的,则它装的东西可以改变。比如,这里先装的是b的地址(指针变量是用来装地址的),后改成了c的。&a,当然就表示a自己的地址了,你可以将a想象成一个箱子,它的地址就是...

平利县13548755193: C语言:日期结构体的设计 -
欧武金固: #include<stdio.h>#include<malloc.h> struct Date { int year; int month; int day; }; void SetDate(struct Date *date1, int y, int m, int d) { date1->year = y; date1->month = m; date1->day = d; } void Display(struct Date date1) { printf("%d-%d-%d\n",date1....

平利县13548755193: 用C语言的结构体写日期 -
欧武金固: struct 结构体名 { 结构体中的元素; };定义结构体变量 struct 结构体名 变量名;

平利县13548755193: 编写一个C语言程序,用结构存储日期信息(日、月、年), -
欧武金固: 当然后面switch语句可用更简单的办法替换...如果你需要 # include <stdio.h>int main(void) {struct date {int day;int month;int year;} theDate; int daysPerMonth[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; printf("Please ...

平利县13548755193: 用C语言编写一个程序显示日期 -
欧武金固: #include <stdio.h> #include <windows.h> main() { //定义一个时间结构存储时间数据; SYSTEMTIME *stm; //定义了十二个月份的数组,用的时候直接调用 char month[12][10] = {"January","February","March", "April","May","June...

平利县13548755193: 不知道哪里写错了求助c语言中定义一个含年、月、日的结构体,输入一个日期,计算该日期是该年的第多少天 -
欧武金固: #include struct date { int year; int month; int day; }; int main() { struct date days; //结构体days存放年月日3部分整数 int d=0,i; printf("输入年月日"); scanf("%d%d%d",&days.year,&days.month,&days.day); if(days.month==1) //如果月份为1...

平利县13548755193: 用C语言,试定义一结构,用来描述时间.该结构共有3个成员变量,分别描述时、分、秒信息. -
欧武金固: struct itime {int hour;int minute;int second; }

平利县13548755193: 用C程序编一个程序,设今天是20世纪的某一天,用三位整数表示今天的年(year)月(month)日(day)…… -
欧武金固: #include struct at_90s //定义日期结构 { int year; int month; char day; }; int isLeapYear(int year)//判断是否为闰年 { if((year%400==0)|((year%100!=0)&(year%4==0))) return 1; return 0; } int getNextMd(int year,int month)//计算本月日期长度 { int flag=...

平利县13548755193: 'C语言 定义一个包含年月日的结构体变量,任意输入一天,计算该日是本年的第几天 -
欧武金固: #include <stdio.h> #include<string.h> struct date { int year, mouth, day; }; int main() { struct date d; int i, k=0, nday=0; printf("请输入当前年月日: "); scanf("%d%d%d", &d.year, &d.mouth, &d.day); ///先判断是否是闰年,符合条件就是闰...

平利县13548755193: 编写一个程序,定义一个日期结构变量(由年、月、日3个整型数据组成),计算该日期是本年度的第几日 -
欧武金固: struct myDate { public: int year; int month; int day; int getiDay() {int iDay = 0; bool isLeapYear = false; if(year%4==0) { if(year%100!=0) isLeapYear =true; else if(year%400==0) isLeapYear =true; } for(int i=1;i<month;i++) { switch (i) { case 1: case 3: ...

平利县13548755193: C++编程,定义一个表示日期的结构体类型,确定它是本年度的第几天 -
欧武金固: #includevoidmain(){intyear,month,day,flag;scanf("%d-%d-%d",&year,&month,&day);if(year%4==0&&(year%100!=0||year%400==0))flag=1;elseflag=0;day++;if(month==1||month==3||month==5||month==7||month==8||month==10||month==12){if(day>...

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 星空见康网