当前位置: 代码迷 >> C语言 >> 求助 有关变成过程中的一些问题
  详细解决方案

求助 有关变成过程中的一些问题

热度:385   发布时间:2006-10-02 09:32:02.0
求助 有关变成过程中的一些问题

题目要求: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;

}



----------------解决方案--------------------------------------------------------

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


----------------解决方案--------------------------------------------------------

写一个程序判断输入的日期是否有效
要求:
程序应该从用户那得到数据,数据的格式是 日/月/年。 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.


----------------解决方案--------------------------------------------------------
谢谢 如果能够解答 我将万分感谢
顺便问一个问题
就是一个程序中 能不能存在if语句是并行的阿
比如一块分闰年的 一块是非闰年的 非闰年里头分日期分有效的和无效的
另一块闰年的 也分个有效的和无效的

能不能举个范例程序阿
----------------解决方案--------------------------------------------------------

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

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);
}





----------------解决方案--------------------------------------------------------

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

程序代码:
#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);
}

}


----------------解决方案--------------------------------------------------------

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


----------------解决方案--------------------------------------------------------