C++ 定义一个结构体变量的定义,计算该日是一年中的第几天

第九章作业_百度文库
两大类热门资源免费畅读
续费一年阅读会员,立省24元!
第九章作业
上传于||暂无简介
阅读已结束,如果下载本文需要使用0下载券
想免费下载更多文档?
定制HR最喜欢的简历
你可能喜欢定义一个结构体变量(包括年、月、日)。计算该日在本年中是第几天?注意闰年问题。_百度知道博客访问: 1408781
博文数量: 301
博客积分: 5547
博客等级: 大校
技术积分: 3614
注册时间:
hello world.
IT168企业级官微
微信号:IT168qiye
系统架构师大会
微信号:SACC2013
分类: C/C++
&&& 定义一个结构体变量(包括年,月,日)。计算该日在本年中式第几天?注意闰年问题。
&&& 我们需要顶一个一个关于日期的结构体,因为涉及到闰年问题,因此我们通过书写一个宏来判断是否为闰年的问题。我们可以把每个月的天数保存在一个数组中,然后根据输入的月份去判断去累加每一个月的天数。代码如下:
#include <stdio.h>#define ISLOOP(year) (((year) % 4 == 0 && (year) % 100 != 0) || ((year) %400 == 0)) ? 1 : 0struct date{&&&&&&&int year;&&&&&&&int month;&&&&&&&int day;};const int months[12] = {31,28,31,30,31,30,31,31,30,31,30,31};int days(struct date *);int main(int argc, char * argv[]){&&&&struct date dt1,*p;&&&&p = &dt1;&&&&printf("please input year,month,day:");&&&&scanf("%d,%d,%d",&dt1.year,&dt1.month,&dt1.day);&&&&while (dt1.year < 1900 || dt1.month > 12 || dt1.month < 1 || dt1.day > 31 || dt1.day < 1)&&&&{&&&&&&&&&&printf("your put data is error,please reinput:\n");&&&&&&&&&&scanf("%d,%d,%d",&dt1.year,&dt1.month,&dt1.day);&&&&}&&&&printf("%d year %d month %d day is : %d day's.",p->year,p->month,p->day,days(p));&&&&system("pause");&&&&return 0;}int days(struct date *p){&&&&int *month = months;&&&&int i,result = 0;&&&&for (i = 0; i < p->month - 1;i++)&&&&{&&&&&&&&result +=*month++;&&&&}&&&&result += p->day;&&&&if(p->month > 2 && ISLOOP(p->year))&&&&{&&&&&&&&result += 1;&&&&}&&&&return result;}
阅读(1083) | 评论(0) | 转发(0) |
相关热门文章
给主人留下些什么吧!~~
请登录后评论。给出年、月、日,计算该日是该年的第几天。请教高手!_c++吧_百度贴吧
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&签到排名:今日本吧第个签到,本吧因你更精彩,明天继续来努力!
本吧签到人数:0成为超级会员,使用一键签到本月漏签0次!成为超级会员,赠送8张补签卡连续签到:天&&累计签到:天超级会员单次开通12个月以上,赠送连续签到卡3张
关注:248,733贴子:
给出年、月、日,计算该日是该年的第几天。请教高手!收藏
以下是我写的程序,用的是最笨的办法,有没有更高级的写法,求高手指点。#include &iostream&int main(void){
bool leap(int year);int nian,yue,ri,
cout && "请输入年月日:";
cin && nian && yue &&
if (leap(nian))
{switch (yue)
{case 1 : day =
case 2 : day = 31 +
case 3 : day = 31 + 29 +
case 4 : day = 31 + 29 + 31 +
case 5 : day = 31 + 29 + 31 + 30 +
case 6 : day = 31 + 29 + 31 + 30 + 31 +
case 7 : day = 31 + 29 + 31 + 30 + 31 + 30 +
case 8 : day = 31 + 29 + 31 + 30 + 31 + 30 + 31 +
case 9 : day = 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 +
case 10 : day = 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 +
case 11 : day = 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 +
case 12 : day = 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 +
default : cout && "error! \n";
switch (yue)
case 1 : day =
case 2 : day = 31 +
case 3 : day = 31 + 28 +
case 4 : day = 31 + 28 + 31 +
case 5 : day = 31 + 28 + 31 + 30 +
case 6 : day = 31 + 28 + 31 + 30 + 31 +
case 7 : day = 31 + 28 + 31 + 30 + 31 + 30 +
case 8 : day = 31 + 28 + 31 + 30 + 31 + 30 + 31 +
case 9 : day = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 +
case 10 : day = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 +
case 11 : day = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 +
case 12 : day = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 +
default : cout && "error! \n";
cout && "该日是第" && day && "天" &&
return 0;}bool leap(int year){
if ( (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0) )}
c++一般需要4-8周,就可以掌握.4个月=两年的工作经验,月薪过万.c++的费用根据培训课时定,免费试听编程课程,名师授课,手把手传教.
我给你推荐一个思想,建立一个二维数组分别存储平年和闰年每个月的天数,
即使按 楼主的思维, 难道就不能 在闰年的时候多加 1天吗? , 而要重新写一遍.....其中和上面只有 28 和 29的差别...
这是我写的,共同学习啦#include &iostream&int main(){
unsigned int year(0), month(0), day(0), sum(0), mp1;
cout&&"请输入‘年’‘月’‘日’"&&
cin&&year&&month&&
if((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
if(month & 1)
sum = (month-2)*30 + (int)(month/2) + mp1 +
cout&&"天数为:"&&sum&&
return 0;}
你的方法不佳,建议换种想法
方法不是很好,没有考虑闰年和平年,还有,定义的时候能不能别用拼音。。。。
int isleapyear(year){if((year%4==0&&year%100!=0)||(year%400==0))return 1;else return 0;}int main(){int year,month,day,i,j,count=0;int days[2][12]={{31,29,31,30,31,30,31,31,30,31,30,31},{31,28,31,30,31,30,31,31,30,31,30,31}};printf(&请输入年、月、日,用空格隔开,输入回车结束!\n&);scanf(&%d%d%d&,&year,&month,&day);i=isleapyear(year);for(j=0;j&month-1;j++){count=count+days[i][j];}count=count+printf(&%d年%d月%d日&,year,month,day);printf(&该日是该年的第%d天&,count);}
楼主已经很高级了
设一个数组 分别表示12个月的天数。二月设为28天。再用一个For循环累加整月天数 再加上天数。再用if判断是否是闰年 是的加1。最后C0Ut
闰年2月29天,平年2月28天.1月2月不用算,3月开始算
登录百度帐号推荐应用
为兴趣而生,贴吧更懂你。或

我要回帖

更多关于 定义全局结构体变量 的文章

 

随机推荐