自己用c写的一个日期计算程序[问题在尴尬中解决-_-!!!]

软件和网站开发以及相关技术探讨
回复
头像
linquid
帖子: 47
注册时间: 2007-11-22 14:15

自己用c写的一个日期计算程序[问题在尴尬中解决-_-!!!]

#1

帖子 linquid » 2007-12-03 23:44

说起来写这个程序的目的有点搞笑。当时上课的时候。忽然想计算我和我女朋友在一起有好多天了。于是就写了一个程序。如下:

代码: 全选

#include<stdio.h>
int CountDate(int month,int day);//日期的计算函数
int leap(int year);//闰年计算函数
int main()
{
	int Fyear,Fmonth,Fday,Byear,Bmonth,Bday;//定义的年月日。F开头 的是起始日期。B开头的是结束日期
	int flag=1;//定义循环的标志
	int Fdate,Bdate,result;//定义的结果变量Fdate,Bdate表示一年中过了多少天
	do
	{	flag++;
              //如果输入出错。就会打印这个。		
		if(flag>2)
		{
			printf("your date has error!");
		}
		printf("please input Start date(format is Year,Month,Day):\n");

		scanf("%d,%d,%d",&Fyear,&Fmonth,&Fday);
		
		printf("please intput Finally date(fomat is Year,Month,Day):\n");

		scanf("%d,%d,%d",&Byear,&Bmonth,&Bday);

		flag++;
	}while((Fyear>Byear)||(Fyear==Byear&&Fmonth>Bmonth)||(Fyear==Byear&&Fmonth==Bmonth&&Fday>Bday));//这些都是出错的条件。起始日期必须小于结束日期
	
	Fdate=CountDate(Fmonth,Fday);//计算一年中过了多少天
	
	if(leap(Fyear)&&Fmonth>=3)//如果是闰年。就+1
	Fdate=Fdate+1;


	Bdate=CountDate(Bmonth,Bday);//计算一年中过了多少天

	if(leap(Byear)&&Bmonth>=3)
	Bdate=Bdate+1;//如果是闰年。就+1
	
	if(Fyear!=Byear)
	{result=365*(Byear-Fyear)-Fdate+leap(Fyear)+Bdate;}//这个的意思比较难说。如果Fyear不等于Byear,365*结束年数与起始年数的差等于一个总天数。再减去起始日期过了多少天+是否为闰年+结束日期过了多少天。-_-!!!(各位到底看懂没有。。。。)
	else
	{
		result=Bdate-Fdate;//这个就是如果起始年数等于结束年数。那么就直接用结束的日期过的天数减去起始日期过的天数。
	}

	printf("OK!\n");
	printf("-----------------------------------------\n");
	printf("|					|\n");
	printf("|	the sum is	%d day	        |\n",result);
	printf("|                                       |\n");
	printf("-----------------------------------------\n");
	return 0;
}
//计算这一年中这个日期过了多少天
int CountDate(int month,int day)
{
	int day_tab[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
	int i;
	for(i=1;i<month;i++)
	   day+=day_tab[i];
	return(day);

}
//闰年的计算 函数。是闰年返值为1。
int leap(int year)
{
	int leap;
	leap=year%4==0&&year%100!=0||year%400==0;
	return leap;
}
[/code]

我现在想。如果我能再加上一个功能。就是给个日期然后写个数。然后推算出以后的日期就好了。向前和向后两种。各位高手们看这个怎么实现。或者能把我这个程序改进一下也行。。。。。。。
上次由 linquid 在 2007-12-04 23:11,总共编辑 1 次。
zhangsong023
帖子: 768
注册时间: 2006-09-20 19:56

#2

帖子 zhangsong023 » 2007-12-04 0:03

参见time.h
mktime及localtime函数。
fengshuitm
帖子: 2
注册时间: 2007-11-29 15:58

#3

帖子 fengshuitm » 2007-12-04 9:58

我汗
头像
madoldman
帖子: 599
注册时间: 2006-02-27 20:19
来自: works system
联系:

#4

帖子 madoldman » 2007-12-04 10:45

代码: 全选

#include <time.h>
#include <stdio.h>
int main(void)
{
        struct tm tm1, tm2;//tm1和tm2为你所需要的两个日期。
        int seconds;
        tm1.tm_mday = 6;
        tm1.tm_mon = 6;
        tm1.tm_year = 90;
        tm1.tm_hour=0;
        tm1.tm_min=0;
        tm1.tm_sec=0;
        tm2.tm_mday = 3;
        tm2.tm_mon = 4;
        tm2.tm_year = 92;
        tm2.tm_hour=0;
        tm2.tm_min=0;
        tm2.tm_sec=0;
        seconds = mktime(&tm2) - mktime(&tm1);//如果要算现在到tm1的时间可以直接用time(NULL) - mktime(&tm1)
        printf("There are %d days between 6/6/90 and 4/3/92.\n", seconds / 24 /60 / 60);
        return 0;
}
东西路,南北走
十字路口人咬狗
拿起狗来打砖头
砖头咬了狗一口
图片
头像
madoldman
帖子: 599
注册时间: 2006-02-27 20:19
来自: works system
联系:

#5

帖子 madoldman » 2007-12-04 10:47

直接把前几天回答别人的那个作业题复制过来了,呵呵
东西路,南北走
十字路口人咬狗
拿起狗来打砖头
砖头咬了狗一口
图片
头像
leros_H
帖子: 191
注册时间: 2005-06-01 21:32

#6

帖子 leros_H » 2007-12-04 13:40

还以前楼主是帮女友写了个经期计算器~~
头像
leros_H
帖子: 191
注册时间: 2005-06-01 21:32

#7

帖子 leros_H » 2007-12-04 13:40

还以为是楼主是帮女友写了个经期计算器~~
头像
BigSnake.NET
帖子: 12522
注册时间: 2006-07-02 11:16
来自: 廣州
联系:

#8

帖子 BigSnake.NET » 2007-12-04 13:44

leros_H 写了:还以为是楼主是帮女友写了个经期计算器~~
有这东西的

代码: 全选

$ aptitude show cycle
软件包: cycle
新: 是
状态: 未安装
版本号: 0.3.1-6
优先级: 可选
部分: universe/utils
维护者: Ubuntu MOTU Developers <ubuntu-motu@lists.ubuntu.com>
未压缩尺寸: 455k
依赖于: python (>= 2.3), python-support (>= 0.2), python-wxgtk2.6
描述: calendar program for women
 Cycle is a calendar for women. Given a cycle length or statistics for several
 periods, it can calculate the days until menstruation, the days of "safe" sex,
 the fertile period, and the days to ovulations, and define the d.o.b. of a
 child. It allows the user to write notes and helps to supervise the
 administration of hormonal contraceptive tablets. 
 
 Possibilities of the program: 
 * Calculate days of menstruation, based on the length of the cycle or on
   statistics of previous periods. 
 * Calculate days of "safe" sex, fertile period and day to ovulations. 
 * Definition of D.O.B. (Date Of Birth) of a child 
 * Allows to write notes. 
 * Helps to supervise reception of hormonal contraceptive tablets. 
 * Multiple users allowed. Data is protected by a password for every user. 
   
 NOTE: This program is not a reliable contraceptive method. It does neither help
 to prevent sexual transmision diseases like AIDS. It is just an electronic
 means of keeping track of some of your medical data and extract some
 statistical conclusions from them. You cannot consider this program as a
 substitute for your gynecologist in any way. 
 
 Homepage: http://cycle.sourceforge.net/
^_^ ~~~
要理解递归,首先要理解递归。

地球人都知道,理论上,理论跟实际是没有差别的,但实际上,理论跟实际的差别是相当大滴。
头像
linquid
帖子: 47
注册时间: 2007-11-22 14:15

#9

帖子 linquid » 2007-12-04 16:35

你们这群小子。。。。。。。我比你们还晕。。。。
头像
BigSnake.NET
帖子: 12522
注册时间: 2006-07-02 11:16
来自: 廣州
联系:

#10

帖子 BigSnake.NET » 2007-12-04 22:18

代码: 全选

#include <time.h>
#include <stdio.h>
int main(void)
{
    struct tm tm1;
    tm1.tm_mday = 23;
    tm1.tm_mon = 12;
    tm1.tm_year = 104;
    tm1.tm_hour = 17;
    tm1.tm_min = 30;
    tm1.tm_sec = 0;
    double seconds = difftime(time(NULL),mktime(&tm1));
    printf("There are %d days between 12/23/2004 and now.\n", (int)(seconds / 3600 / 24));
    return 0;
}

代码: 全选

$ ./a.out 
There are 1045 days between 12/23/2004 and now.
时间过得真快
^_^ ~~~
要理解递归,首先要理解递归。

地球人都知道,理论上,理论跟实际是没有差别的,但实际上,理论跟实际的差别是相当大滴。
imliujie
帖子: 3
注册时间: 2007-11-14 11:04

#11

帖子 imliujie » 2007-12-04 22:31

不是安全期啊
头像
aitilang
帖子: 1026
注册时间: 2007-04-28 21:38

#12

帖子 aitilang » 2007-12-04 22:32

ls可以你可以写一个的
不过我可不敢用。。。
thinkpad x61 2G DDR no cdrom
--------------------------------------------
ABS学习中
sed学习中
awk学习中
perl学习中
新手描述不清,老手猜测不到,胡乱指挥一通,后果难以预料
njayong071204
帖子: 9
注册时间: 2007-12-04 22:17
来自: 江苏南京

#13

帖子 njayong071204 » 2007-12-04 22:35

管它安不安全,日了再说
身在L营心在W
imliujie
帖子: 3
注册时间: 2007-11-14 11:04

#14

帖子 imliujie » 2007-12-04 22:55

12楼,那你就用for循环写一个自己用吧[/code]
头像
linquid
帖子: 47
注册时间: 2007-11-22 14:15

#15

帖子 linquid » 2007-12-04 23:10

我开始怀疑我是不是进错论坛了。。。。。。。。。。。。。。。登录这个论坛我没用代理阿。
回复