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

题目要求: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
sunyuantz
Rank: 1
等 级:新手上路
威 望:1
帖 子:407
专家分:0
注 册:2006-3-20
收藏
得分:0 

偶E文太差,看不懂,请楼主翻译一下。


我不是名人,所以不要签名。等哪天我成名人了......你都认识我了还要签名干嘛!
2006-10-02 12:49
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
nuciewth
Rank: 14Rank: 14Rank: 14Rank: 14
来 自:我爱龙龙
等 级:贵宾
威 望:104
帖 子:9786
专家分:208
注 册:2006-5-23
收藏
得分:0 

就是判断输入的年份是否合理.

int is_ok(int year,int month,int day)
{
if(year<1582)
{
return(0);
}
if(month<1||month>12)
{
return(0);
}
switch(month)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:if(day<1||day>31)
{
return(0);
}
case 4:
case 6:
case 9:
case 11:if(day<1||day>30)
{
return(0);
}
case 2:if(is_leap(year))//判断是否是闰年
{
if(day<1||day>29)
{
return(0);
}
}
else
{
if(day<1||day>28)
{
return(0);
}
}
}
return(1);
}





倚天照海花无数,流水高山心自知。
2006-10-02 14:07
unicorn
Rank: 4
等 级:贵宾
威 望:14
帖 子:1066
专家分:0
注 册:2005-10-25
收藏
得分:0 

楼主怎么写的那么多...

#include<stdio.h>
void main()
{
int y,m,d;
printf(\"please input date(d/m/y):\");
scanf(\"%d/%d/%d\",&d,&m,&y);
int a[]={1,3,5,7,8,10,12};
int b[]={4,6,9,11};
if(y>=1582)
printf(\"%d is valid!\n\",y);
else printf(\"%d is not valid!\n\",y);
if(m>=1&&m<=12) printf(\"%d is valid!\n\",m);
else printf(\"%d is not valid\n\",m);

for(int i=0,j=0;((i<7||j<4)&&m!=2);i++,j++)
{
if(m==a[i])
{if(d>=1&&d<=31) printf(\"%d is valid!\n\",d);
else printf(\"%d is not valid!\n\",d);}
if(m==b[j])
{if(d>=1&&d<=30) printf(\"%d is valid!\n\",d);
else printf(\"%d is not valid!\n\",d);}
}
if(m==2)
{

if((y%4==0&&y%100!=0)||y%400==0)
if(d>=1&&d<=29)
printf(\"%d is valid!\n\",d);
else printf(\"%d is not valid!\n\",d);

if(!((y%4==0&&y%100!=0)||y%400==0))
if(d>=1&&d<=28)
printf(\"%d is valid!\n\",d);
else printf(\"%d is not valid!\n\",d);
}

}


unicorn-h.spaces. ◇◆ sava-scratch.spaces.  noh enol ! pue pu!w hw u! shemle aq ll!m noh 
2006-10-02 14:36
ZHY871029
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2006-10-2
收藏
得分:0 

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

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



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

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