| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 670 人关注过本帖
标题:求助 有关变成过程中的一些问题
取消只看楼主 加入收藏
ZHY871029
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2006-10-2
收藏
 问题点数:0 回复次数:3 
求助 有关变成过程中的一些问题

题目要求:Program 2
Write the program date_check which should check if a date is valid.

Requirements:

The program should get a date from the user in the format day/month/year. Tip: the function scanf can be used to input a formatted date string using a format like "%d/%d/%d".

The program should check the three parts of the date (day, month, year), and print a warning message for any part of the date that is wrong. If the whole date is correct, it should print a message saying that the date is valid.

The normal rules for dates apply: valid days are from 1 to 28, 29, 30, or 31 (depending on the month and on whether the year is a leap year); valid months are from 1 to 12; valid years start from 1582 (the year in which our current calendar system started).

The rule for deciding if a year is a leap year is as follows: a year evenly divisble by 4 is a leap year; except if the year is evenly divisible by 100, in which case it is not a leap year; except if the year is evenly divisible by 400, in which case it is a leap year.

Examples: (user input is shown in bold)

One invalid part of the date:
Enter a date (d/m/y): 29/2/2002
The day 29 is not valid!

Several invalid parts of the date:
Enter a date (d/m/y): 32/15/1002
The year 1002 is not valid!
The month 15 is not valid!
The day 32 is not valid!

Valid date:
Enter a date (d/m/y): 20/9/2002
The given date is valid.


程序:
/* Judge whether a date is valid */
#include <stdio.h>

int main()
{
/* Define the data type for each variable */
int DAY;
int MONTH;
int YEAR;


/* Display the requirement and get the data from the user */
printf(" Enter a date(d/m/y):");
scanf("%d/%d/%d", &DAY, &MONTH, &YEAR);

/* Begin the selection */
/* First Judge wheter it's a leap year Then beging the first selection*/
if(((YEAR>=1582)&&(YEAR % 400!=0)||((YEAR%100 !=0)&&(YEAR % 4 !=0)))&&
(((MONTH==1)&&(DAY>=1&&DAY<=31))||((MONTH==2)&&(DAY>=1&&DAY<=28))||
((MONTH==3)&&(DAY>=1&&DAY<=31))||((MONTH==4)&&(DAY>=1&&DAY<=30))||
((MONTH==5)&&(DAY>=1&&DAY<=31))||((MONTH==6)&&(DAY>=1&&DAY<=30))||
((MONTH==7)&&(DAY>=1&&DAY<=31))||((MONTH==8)&&(DAY>=1&&DAY<=31))||
((MONTH==9)&&(DAY>=1&&DAY<=30))||((MONTH==10)&&(DAY>=1&&DAY<=31))||
((MONTH==11)&&(DAY>=1&&DAY<=30))||((MONTH==12)&&(DAY>=1&&DAY<=31))))
{
printf(" The given date is valid \n");
}
else if(YEAR<1582)
{
printf(" The year %d is not valid.\n" ,YEAR);
}
if(MONTH<1||MONTH>12)
{
printf(" The month %d is not valid.\n", MONTH);
}
if(((YEAR>=1582)&&(YEAR % 400!=0)||((YEAR%100 !=0)&&(YEAR % 4 !=0))||(YEAR<1582))&&
(((MONTH==1)&&(DAY<1&&DAY>31))||((MONTH==2)&&(DAY<1&&DAY>28))||
((MONTH==3)&&(DAY<1&&DAY>31))||((MONTH==4)&&(DAY<1&&DAY>30))||
((MONTH==5)&&(DAY<1&&DAY>31))||((MONTH==6)&&(DAY<1&&DAY>30))||
((MONTH==7)&&(DAY<1&&DAY>31))||((MONTH==8)&&(DAY<1&&DAY>31))||
((MONTH==9)&&(DAY<1&&DAY>30))||((MONTH==10)&&(DAY<1&&DAY>31))||
((MONTH==11)&&(DAY<1&&DAY>30))||((MONTH==12)&&(DAY<1&&DAY>31))))
{
printf(" The day %d is not valid.\n" , DAY);
}
/* Then if it is not a leap year Then beging the second selection*/
else if
(((YEAR>=1582)&&(YEAR % 400==0)||((YEAR%100 !=0)&&(YEAR % 4 ==0)))&&
(((MONTH==1)&&(DAY>=1&&DAY<=31))||((MONTH==2)&&(DAY>=1&&DAY<=28))||
((MONTH==3)&&(DAY>=1&&DAY<=31))||((MONTH==4)&&(DAY>=1&&DAY<=30))||
((MONTH==5)&&(DAY>=1&&DAY<=31))||((MONTH==6)&&(DAY>=1&&DAY<=30))||
((MONTH==7)&&(DAY>=1&&DAY<=31))||((MONTH==8)&&(DAY>=1&&DAY<=31))||
((MONTH==9)&&(DAY>=1&&DAY<=30))||((MONTH==10)&&(DAY>=1&&DAY<=31))||
((MONTH==11)&&(DAY>=1&&DAY<=30))||((MONTH==12)&&(DAY>=1&&DAY<=31))))
{
printf(" The given date is valid\n");
}
else if(YEAR<1582)
{
printf(" The year %d is not valid.\n" ,YEAR);
}
if(MONTH<1||MONTH>12)
{
printf(" The month %d is not valid.\n", MONTH);
}

if(((YEAR>=1582)&&(YEAR % 400==0)||((YEAR%100 !=0)&&(YEAR % 4 ==0))||(YEAR<1582))&&
(((MONTH==1)&&(DAY<1&&DAY>31))||((MONTH==2)&&(DAY<1&&DAY>29))||
((MONTH==3)&&(DAY<1&&DAY>31))||((MONTH==4)&&(DAY<1&&DAY>30))||
((MONTH==5)&&(DAY<1&&DAY>31))||((MONTH==6)&&(DAY<1&&DAY>30))||
((MONTH==7)&&(DAY<1&&DAY>31))||((MONTH==8)&&(DAY<1&&DAY>31))||
((MONTH==9)&&(DAY<1&&DAY>30))||((MONTH==10)&&(DAY<1&&DAY>31))||
((MONTH==11)&&(DAY<1&&DAY>30))||((MONTH==12)&&(DAY<1&&DAY>31))))
{
printf(" The day %d is not valid.\n", DAY);
}


return 0;

}


搜索更多相关主题的帖子: function message warning should 
2006-10-02 09:32
ZHY871029
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2006-10-2
收藏
得分:0 

写一个程序判断输入的日期是否有效
要求:
程序应该从用户那得到数据,数据的格式是 日/月/年。 Tips: 你可以利用scanf函数来输入:%d/%d/%d 来获得日/月/年的格式。
程序应该检查所输入的3个数据(日,月,年)是否有效,如果任何一个存在问题,即提示有误,如果没有问题,则显示数据正确。
数据应该满足:从1582年开始(即现有日历系统确定的时候),天数随是否闰年及月份改变(如闰年2月有29天),有效月份是从1到12月。
决定一个年份是不是闰年可以这么判断:如果直接能够被400整除就是闰年,如果不能直接整除,那么如果被100整除有余数但能被4整除就是闰年。


Examples: (user input is shown in bold)

One invalid part of the date:
Enter a date (d/m/y): 29/2/2002
The day 29 is not valid!

Several invalid parts of the date:
Enter a date (d/m/y): 32/15/1002
The year 1002 is not valid!
The month 15 is not valid!
The day 32 is not valid!

Valid date:
Enter a date (d/m/y): 20/9/2002
The given date is valid.

2006-10-02 13:38
ZHY871029
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2006-10-2
收藏
得分:0 
谢谢 如果能够解答 我将万分感谢
顺便问一个问题
就是一个程序中 能不能存在if语句是并行的阿
比如一块分闰年的 一块是非闰年的 非闰年里头分日期分有效的和无效的
另一块闰年的 也分个有效的和无效的

能不能举个范例程序阿
2006-10-02 13:42
ZHY871029
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2006-10-2
收藏
得分:0 

我菜菜嘛 谢谢咯 以后我多来问问

2006-10-02 15:56
快速回复:求助 有关变成过程中的一些问题
数据加载中...
 
   



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

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