mirror of
https://github.com/peter-tanner/CITS-3007-labs.git
synced 2024-11-30 09:00:19 +08:00
lab 1 - soln
This commit is contained in:
parent
5889ce58f7
commit
60356de304
|
@ -6,24 +6,20 @@
|
||||||
/* return 0 (false) or 1 (true), depending on whether
|
/* return 0 (false) or 1 (true), depending on whether
|
||||||
* `year` is a leap year or not.
|
* `year` is a leap year or not.
|
||||||
*/
|
*/
|
||||||
int is_leap(long year) {
|
int is_leap(long year)
|
||||||
|
{
|
||||||
if (year % 4 != 0) {
|
return year % 4 == 0 // Every year that is exactly divisible by four is a leap year
|
||||||
return 0;
|
&& (year % 100 != 0 // except for years that are exactly divisible by 100
|
||||||
}
|
|| year % 400 == 0); // but these centurial years are leap years if they are exactly divisible by 400
|
||||||
|
|
||||||
if (year % 100 == 0) {
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, char **argv) {
|
int main(int argc, char **argv)
|
||||||
|
{
|
||||||
argc--;
|
argc--;
|
||||||
argv++;
|
argv++;
|
||||||
|
|
||||||
if (argc != 1) {
|
if (argc != 1)
|
||||||
|
{
|
||||||
fprintf(stderr, "Error: expected 1 command-line argument (a YEAR), but got %d\n", argc);
|
fprintf(stderr, "Error: expected 1 command-line argument (a YEAR), but got %d\n", argc);
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
@ -35,19 +31,25 @@ int main(int argc, char **argv) {
|
||||||
long year = strtol(argv[0], &end, 10);
|
long year = strtol(argv[0], &end, 10);
|
||||||
int res_errno = errno;
|
int res_errno = errno;
|
||||||
|
|
||||||
if (end == argv[0]) {
|
if (end == argv[0])
|
||||||
|
{
|
||||||
fprintf(stderr, "Error: couldn't interpret '%s' as a number\n", argv[0]);
|
fprintf(stderr, "Error: couldn't interpret '%s' as a number\n", argv[0]);
|
||||||
exit(1);
|
exit(1);
|
||||||
} else if (res_errno == ERANGE) {
|
}
|
||||||
|
else if (res_errno == ERANGE)
|
||||||
|
{
|
||||||
fprintf(stderr, "Error: '%s' is outside the range of numbers we can handle\n", argv[0]);
|
fprintf(stderr, "Error: '%s' is outside the range of numbers we can handle\n", argv[0]);
|
||||||
exit(1);
|
exit(1);
|
||||||
} else {
|
}
|
||||||
if (is_leap(year)) {
|
else
|
||||||
|
{
|
||||||
|
if (is_leap(year))
|
||||||
|
{
|
||||||
printf("%ld is a leap year\n", year);
|
printf("%ld is a leap year\n", year);
|
||||||
} else {
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
printf("%ld is not a leap year\n", year);
|
printf("%ld is not a leap year\n", year);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user