| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 664 人关注过本帖
标题:已知今天的日期(含年、月、日),编程求得明天的日期(含年、月、日)。
只看楼主 加入收藏
zhangwef8012
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2007-10-17
收藏
 问题点数:0 回复次数:9 
已知今天的日期(含年、月、日),编程求得明天的日期(含年、月、日)。

#include<stdio.h>
void month_nm(struct date day);
void tomorrow(struct date day);
int run(struct date day);
struct date
{
int day;
int month;
int year;
};
void main()
{
struct date day;
int a;
printf("\n以数字形式输入日期(年月日)\n");
printf("\n输入年(1900-2005):");
scanf("%d",&day.year);
fflush(stdin);
printf("\n输入月:");
scanf("%d",&day.month);
printf("\n输入日: ");
scanf("%d",&day.day);
printf("\n今天日期是:");
if((day.year>=1900)&&(day.year<=2005))
{ printf("%d年",day.year);
a=run(day);
}
else
printf("\n请输入的无效哦?");
if((day.month>=1)&&(day.month<=12))
{month_nm(day);
}
else
printf("\n请输入的无效哦?");
if((day.month==1||day.month==3||day.month==5||day.month==7||day.month==8||day.month==10||day.month==12)&&(day.day>=1&&day.day<=31))
printf("%d日",day.day);
else if ((day.month==4||day.month==6||day.month==9||day.month==11)&&(day.day>=1&&day.day<=30))
printf("%d日",day.day);
else if((day.month==2)&&(day.day>=1&&day.day<=28+a))
printf("%d日",day.day);
printf("\n明天日期是:");
tomorrow(day);
printf("\n");

}

void month_nm(struct date day)
{
switch(day.month)
{
case 1:printf("一月");break;
case 2:printf("二月");break;
case 3:printf("三月");break;
case 4:printf("四月");break;
case 5:printf("五月");break;
case 6:printf("六月");break;
case 7:printf("七月");break;
case 8:printf("八月");break;
case 9:printf("九月");break;
case 10:printf("十月");break;
case 11:printf("十一月");break;
case 12:printf("十二月");break;
}
}

int run(struct date day)
{int b;
if((day.year%4==0)&&(day.year%100!=0)||(day.year%400==0))
b=1;
else
b=0;
return b;
}

void tomorrow(struct date day)
{
struct date tomorrow;
if((day.month==1||day.month==3||day.month==5||day.month==7||day.month==8||day.month==10||day.month==12)&&(day.day<31))
{
tomorrow.day=day.day+1;
tomorrow.month=day.month;
tomorrow.year=day.year;

}
else if ((day.month==4||day.month==6||day.month==9||day.month==11)&&(day.day<30))
{
tomorrow.day=day.day+1;
tomorrow.month=day.month;
tomorrow.year=day.year;

}
else if((day.month==2)&&(day.day<(28+run(day))))
{
tomorrow.day=day.day+1;
tomorrow.month=day.month;
tomorrow.year=day.year;
}

else
{
tomorrow.month=day.month+1;
tomorrow.day=1;
tomorrow.year=day.year;
}
if(tomorrow.month>=12)
{
tomorrow.year=day.year+1;
tomorrow.month=1;
tomorrow.day=1;
}
printf("%d年",tomorrow.year);
printf("%d月",tomorrow.month);
printf("%d日",tomorrow.day);

}

搜索更多相关主题的帖子: day int date struct 
2007-10-17 23:35
zhangwef8012
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2007-10-17
收藏
得分:0 

求助好的C语言的算法

2007-10-17 23:36
zhangwef8012
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2007-10-17
收藏
得分:0 
2007-10-17 23:36
zhangwef8012
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2007-10-17
收藏
得分:0 
2007-10-17 23:37
zhangwef8012
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2007-10-17
收藏
得分:0 

#include <stdio.h>
#include <string.h>

struct date
{
int month;
int year;
int day;
};

struct date update(struct date today)
{
struct date tomorrow;
const int dayspermonth[12]={31,28,31,30,31,30,31,31,30,31,30,31};
int days;
if( (today.year%4 == 0 && today.year%100 != 0) || (today.year %400 == 0 && today.month == 2))
{
days=29;
}
else
{
days=dayspermonth[today.month-1];
}
if(today.day!=days)
{
tomorrow=(struct date){today.month,today.day+1,today.year};
}
else
{
if (today.month == 12)
{
tomorrow=(struct date){1,1,today.year+1};
}
else
{
tomorrow=(struct date){today.month+1,1,today.year};
}
}
return tomorrow;
}


int main()
{
struct date thisdate,nextdate;
struct date update(struct date today);
printf("Input date's date (mm dd yyyy)");
scanf("%i:%i:%i",&thisdate.month,&thisdate.day,&thisdate.year);
nextdate = update(thisdate);
printf("%i:%i:%i",nextdate.month,nextdate.day,nextdate.year);
return 0;
}

2007-10-17 23:38
zhangwef8012
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2007-10-17
收藏
得分:0 
kan bu  dong a  
2007-10-17 23:38
longfeng867
Rank: 1
来 自:重庆
等 级:新手上路
威 望:1
帖 子:182
专家分:0
注 册:2007-5-20
收藏
得分:0 

VC里面有系统自带的时间函数啊~!~~~


在这个连处女膜都可以伪造的世界里,还有什么值得我相信!
2007-10-18 09:29
gaoamining
Rank: 1
等 级:新手上路
帖 子:38
专家分:0
注 册:2007-10-17
收藏
得分:0 

我写了个,是用来求你要算出几天以后的日期。这个看看你能用吗?
#include <iostream.h>
#ifndef _DATE_TIME_H
#define _DATE_TIME_H

class CDateTime
{
public:
// int NumberValidate();
int AddYear(int &num);
int AddMonth(int &num,int counts);
int AddDay();
CDateTime(int year,int month,int day);
~CDateTime();
private:
int m_Year;
int m_Month;
int m_Day;
};

#endif

CDateTime::CDateTime(int year,int month,int day)
{
m_Year=year;
m_Month=month;
m_Day=day;
}

CDateTime::~CDateTime()
{
}

int CDateTime::AddYear(int &num)
{
do
{
do
{
switch(m_Month)
{
case 1:
AddMonth(num,31);
break;
case 2:
if(m_Year%4==0 && m_Year%100!=0 || m_Year%400==0)
{
AddMonth(num,29);
break;
}
else
{
AddMonth(num,28);
break;
}
break;
case 3:
AddMonth(num,31);
break;
case 4:
AddMonth(num,30);
break;
case 5:
AddMonth(num,31);
break;
case 6:
AddMonth(num,30);
break;
case 7:
AddMonth(num,31);
break;
case 8:
AddMonth(num,31);
break;
case 9:
AddMonth(num,30);
break;
case 10:
AddMonth(num,31);
break;
case 11:
AddMonth(num,30);
break;
case 12:
AddMonth(num,31);
break;
}
}
while(m_Month<=12 && num>0);
if(m_Month>12)
{
m_Month=1;
m_Year++;
}
}
while(num>0);
cout<<"m_Year = "<<m_Year<<" m_Month = "<<m_Month<<" m_Day = "<<m_Day<<endl;
return m_Year;
}

int CDateTime::AddMonth(int &num,int counts)
{
int i;
for(i=0;i<num;i++)
{
if(m_Day==counts)
{
m_Month++;
num=num-i-1;
m_Day=1;
break;
}
AddDay();
}
if(i==num)
num=0;
// cout<<"m_Month = "<<m_Month<<endl;
return num;
}

int CDateTime::AddDay()
{
m_Day++;
// cout<<"m_Day = "<<m_Day<<endl;
return m_Day;
}
void main()
{
int m_year,m_month,m_day,num;
cout<<"Pleace enter a number:";
cin>>m_year>>m_month>>m_day>>num;

CDateTime dt(m_year,m_month,m_day);
dt.AddYear(num);
}


2007-10-18 09:45
hnegz
Rank: 1
等 级:新手上路
帖 子:35
专家分:0
注 册:2007-10-18
收藏
得分:0 
wu
2007-10-18 10:47
dingpin
Rank: 1
等 级:新手上路
帖 子:281
专家分:0
注 册:2007-9-29
收藏
得分:0 

你试试我这个吧,在VC++中试......
#include <stdio.h>
struct datatimes
{
int year;//年
int month;//月
int day;//日
};
void main()
{
struct datatimes date;
int days;
printf("请输入日期(年 月 日):\n");
printf("输入年:");
fflush(stdin);
scanf("%d",&date.year);
printf("你输入的年份是:%d年\n",date.year);
do
{
printf("输入月(1-12):");
fflush(stdin);
scanf("%d",&date.month);
if(date.month>=1&&date.month<=12)
printf("你输入的月份是:%d月\n",date.month);
else
printf("输入的月无效\n请重新输入\n");
}while(date.month<1||date.month>12);
do
{
printf("输入日:");
fflush(stdin);
scanf("%d",&date.day);
if(date.month==1||date.month==3||date.month==5||date.month==7||date.month==8||date.month==10||date.month==12)
days=31;
else if(date.month==4||date.month==6||date.month==9||date.month==11)
days=30;
else if((date.year%4==0 && date.year%100!=0||date.year%400==0) &&date.month==2)
days=29;
else
days=28;
if(date.day>=1&&date.day<=days)
printf("你输入的日是:%d日\n",date.day);
else
printf("输入的日无效\n请重新输入\n");
}while(date.day<1||date.day>days);
if(date.month==12&&date.day==31)
{
printf("今天的日期是:%d年%d月%d日\n",date.year,date.month,date.day);
printf("明天的日期是:%d年1月1日\n",(date.year+1));
}
else if(date.month!=12&&date.day==days)
{
printf("今天的日期是:%d年%d月%d日\n",date.year,date.month,date.day);
printf("明天的日期是:%d年%d月1日\n",date.year,(date.month+1));
}
else
{
printf("今天的日期是:%d年%d月%d日\n",date.year,date.month,date.day);
printf("明天的日期是:%d年%d月%d日\n",date.year,date.month,(date.day+1));
}
}


我的地盘我做主!
2007-10-18 11:06
快速回复:已知今天的日期(含年、月、日),编程求得明天的日期(含年、月、日) ...
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.026359 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved