用C语言定义一个包含数组的Date类型的函数,输出年,月,日

作者&投稿:梅追 (若有异议请与网页底部的电邮联系)
用C语言写一个函数,给出年月日,计算该日是该年的第几天~

#include

int main()
{
int year,month,day;//年月日
int judge,i;
int sum=0;//标记天数
int date[2][12]={
{31,28,31,30,31,30,31,31,30,31,30,31},
{31,29,31,30,31,30,31,31,30,31,30,31}};//储存闰年和非闰年每一月的天数
printf("输入年月日:");
scanf("%d %d %d",&year,&month,&day);

judge=(year%4==0)||(year%400==0 && year%100!=0);
//判断年份是不是闰年 是闰年judge就是1;否则就是0

for(i=0;i<month-1;i++)//加month之前的所有天数
sum+=date[judge][i];
sum+=day;//加当天月份的天数day

printf("它是%d年中的第%d天
",year,sum);
return 0;
}

#include const int days[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};struct Date{int year, month, day;};int DateCmp(Date a, Date b){if (a.year != b.year)return a.year > b.year;if (b.month != a.month)return a.month > b.month;return a.day > b.day;}int isLunar(int year){return year % 400 == 0 || (year % 100 && year % 4 == 0);}int DateDistace(Date a, Date b){if (DateCmp(a, b))//保证a小于b{Date t = b;b = a;a = t;}//计算结果存储在res中//首先计算相差的年int i = a.year + 1, j = b.year - 1, res = a.year == b.year ? 0 : (j - i + 1) * 365;for (int ii = (i + 3) / 4 * 4, jj = j / 4 * 4;ii <= jj;ii += 4, res++);for (int ii = (i + 99) / 100 * 100, jj = j / 100 * 100;ii <= jj;ii += 100, res--);for (int ii = (i + 399) / 400 * 400, jj = j / 400 * 400;ii <= jj;ii += 400, res++);//然后计算相差的月if (a.year != b.year){for (int ii = a.month + 1;ii <= 12;ii++)res += ii == 2 ? 28 + isLunar(a.year) : days[ii];for (int ii = 1;ii <= b.month - 1;ii++)res += ii == 2 ? 28 + isLunar(b.year) : days[ii];}else{for (int ii = a.month + 1;ii < b.month;ii++)res += ii == 2 ? 28 + isLunar(a.year) : days[ii];}//最后计算相差的日if (a.year != b.year || a.month != b.month){res += (a.month == 2 ? 28 + isLunar(a.year) : days[a.month]) - a.day + b.day;}else{res += b.day - a.day;}return res;}int main(){Date a, b;a.year = 2000; a.month = 12; a.day = 31;b.year = 1000; b.month = 1; b.day = 1;printf("%d
", DateDistace(b, a));return 0;}

#includestructdate//定义结构体类型{inty;intm;intd;};voidmain(){datedt;//定义结构体变量inti,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;i2&&(dt.y%4==0&&dt.y%100!=0||dt.y%400==0))count+=1;//如果是闰年,则要把3月份和3月份以后的总天数加1printf("这一日是这一年的第%d天\n",count);}


一个完整的单片机c语言程序包含哪几个部分?
个人觉得,一个完整C程序至少应该包含头文件、初始化、主程序四个部分,头文件是程序编译预处理的重要组成部分,缺了它就无法生成目标代码;初始化部分包含变量初始化和端口初始化;主程序是一个C程序的核心代码,由此执行或调用一些具有特定功能的函数模块以完成程序的预设功能。

一个C语言程序中至少应包含一个什么?
做菜 ”这个过程就是“主函数”。在主函数中,根据情况,可能还需要调用“买菜,切菜,炒菜”等子函数。扩展:在源程序中,很多符号都是成对匹配出现的,为避免遗漏必须配对使用的符号,在输入时,可连续输入这些起止标识符,然后再在其中进行插入来完成内容的编辑。虽然C语言程序的书写格式非常自由,但从...

C语言编写一个函数,在主函数中输入包含N个元素一维数组,求此数组中最...
include<stdio.h> int N;int max3(int a[],int m[]){int i,j,k,t,b[N];for(i=0;i<N;i++)b[i]=a[i];for(i=0;i<3;i++){k=i;for(j=i+1;j<N;j++)if(b[j]>b[k])k=j;t=b[i];b[i]=b[k];b[k]=t;m[i]=b[i];} } int main(){ scanf("%d",&...

(1)编写一个C语言程序,包含字符串值"Hello World"。程序将字符串中每个...
第一题:include "stdio.h"void main(){ char String[13]="Hello World";char *pStr=String;printf("与0异或:");while(*pStr != '\\0'){ putchar((*pStr)^0);++pStr;} printf("\\n与1异或:");pStr=String;while(*pStr != '\\0'){ putchar((*pStr)^1);++pStr;} } 第二题...

C语言:先定义一个结构体Student,它包含四个成员:学号(number),姓名(nam...
){\/\/struct student stu_[3];void input(student stu[], int n);void print(student stu[], int n);input(stu,3);printf("学生信息如下:\\n");print(stu,3);return 0;}void input(student stu_[], int n){int i = 0;for(;i<n;i++){printf("请输入第%d个学生的信息:...

c语言 定义一个函数,判断某字符串中是否包含一个子串
int find(char* source, char* target)\/\/source为源字符串,target为子字符串,如找到则返回在源串中的位置,如未找到则返回-1,如果要改为找到返回1,把return i改为return 1;{ int i,j; int s_len=strlen(source); int t_len=strlen(target); if(t_len>s_len) { retu...

c语言宏定义中,可以文件包含另一个c源程序吗?
可以倒是可以,一般工作中,常见的写法是 一个hehe1.C源程序,定义了fun1();fun2();等等,在对应的hehe1.h中声明用到的两个函数即可。另一个hehe2如果要使用到hehe1.c的函数时,只要包含hehe.h就好。如:在hehe1.c include<stdio.h> void fun1(void){} void fun2(void){} 在hehe1...

求C语言答案.定义一个学生结构体就一个学生,包含学生姓名,学号,性别,年...
char sno[max]; \/\/ 学号 char sname[max]; \/\/姓名 char sex[max]; \/\/性别 char age[max]; \/\/年龄 struct student* next;} student;void regist(){ char ch;student *s,*ptr; \/\/s用来建新结点,ptr用来暂存头结点 do { s=(student*)malloc(sizeof(student)); \/\/ 新建一个...

C语言在一个函数里面定义了一个结构体,怎么在另外一个函数里面使用?
在另外一个函数里面使用的源代码如下:include"stdio.h"include "conio.h" \/\/---添加这个头文件,因为getch()函数来自它,否则编译会有警告 struct student \/*定义结构体*\/ { void main(){ void data_in(struct student putin[]);void data_out(struct student *p);void data_pout(struct ...

C语言:编写一个程序,使其能读入并计算以个只包含加减运算的表达式_百度...
include <stdio.h> int main(){ double a;char fh='+';double sum=0.0;while(scanf("%lf",&a)!=EOF){ if(fh=='+')sum+=a;else sum-=a;scanf("%c",&fh);if(fh==';')break;} printf("value=%lf\\n",sum);return 0;} 我假设的是表达式最前面有个'+',这不影响表达式的...

井陉县15366952207: 用C语言定义一个包含数组的Date类型的函数,输出年,月,日 -
车寒复合: #includestructdate//定义结构体类型{inty;intm;intd;};voidmain(){datedt;//定义结构体变量inti,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...

井陉县15366952207: 用数组实现输入年year、月month、日date,计算该日期是这年的第几天. 用C语言编写 求助 -
车寒复合: main() { int n,year,month,day,month_day_sum=0,sum, month_day[13]={0,31,28,31,30,31,30,31,31,30,31,30,31}; printf("please input the \"year month day\"\n"); scanf("%d%d%d",&year,&month,&day); if(year%100!=0&&year%4==0||year%...

井陉县15366952207: 定义一个日期的结构体数据类型(年、月、日),从键盘输入10个日期数据(用数组存储),然后将输入输出 -
车寒复合: #include struct student { int num; char name; int score[3]; }; void main() { void print(struct student); struct student stu[5]; int i; for(i=0;i { scanf("%d %c%d%d%d",&stu[i].num,&stu[i].name,&stu[i].score[0],&stu[i].score[1],&stu[i].score[2]); } printf("...

井陉县15366952207: 定义一个描述日期的类Date,包含的内容如下? -
车寒复合: 设计一个日期类MyDate,包含一个构造方法,3个私有成员变量year、month、day以及它们相应的setter和getter方法,再加上一个转换成“XXXX年XX月XX日”格式字符串的方法toString.... 设计一个日期类MyDate,包含一个构造方法,3个私有成员变量year、month、day以及它们相应的setter和getter方法,再加上一个转换成“XXXX年XX月XX日”格式字符串的方法toString.

井陉县15366952207: 用C语言编写一份日历系统
车寒复合: 这个就是计算每年的第一天和每个月的第一天是周几的问题.显示很简单计算每年第一天是周几int yy(int ye){ int y=ye%400; //400年一轮回,你可以计算一下400年的所有天数刚好是7的倍数 int day=y-1; //一年365天,364是7的倍数,那么一年多...

井陉县15366952207: '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); ///先判断是否是闰年,符合条件就是闰...

井陉县15366952207: c语言中,定义一个名为student - record的结构(用struct),包含学生姓名name,出生日期()... -
车寒复合: struct date {int day ;int month;int year };struct student_record {char name[10];struct date birthday;int score; }

井陉县15366952207: c语言编程 定义一个结构体数组,存放12个月的信息,每个数组元素有三个成员组成:月份的数字表%C
车寒复合: struct Info { char month[4]; //...另外两个成员 }; Info Ainfo[12]; ////初始化/// char temp[4]; for(int i=0;i&lt;12;i++) { sprintf(temp,"%c",i); strcpy(Ainfo[i].month,temp); }

井陉县15366952207: 用C语言编程,使用结构数组存储订餐的情况,包括订餐编号、顾客姓名、用餐人数、用餐时间等. -
车寒复合: #include struct sqlist{ int serial; char name[16]; int number; char date[16]; struct sqlist *next; }*head; int sernum = 2012000; reserve(struct sqlist *T) { struct sqlist *q, *p = T; while(p->next != NULL){ p = p->next; } printf("Input the format: name number ...

井陉县15366952207: C语言:用户输入年份月份日期,输入该日期是该年的第几天?(要求:用数组存放一年12个月的天数) -
车寒复合: #include int main() { int year,month,day; int days[]={31,28,31,30,31,30,31,31,30,31,30,31}; int i=0 , dayth=0; printf("input yyyy-mm-dd :"); scanf("%d-%d-%d" , &year , &month, &day ); if ( month > 12 || month { printf("input month error!\n");...

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