mirror of
https://github.com/peter-tanner/CITS-3007-labs.git
synced 2024-11-30 09:00:19 +08:00
initial commit
This commit is contained in:
commit
5889ce58f7
0
.gitignore
vendored
Normal file
0
.gitignore
vendored
Normal file
5
README.md
Normal file
5
README.md
Normal file
|
@ -0,0 +1,5 @@
|
|||
# CITS3007 labs
|
||||
|
||||
Lab solutions. Code modified from sources at the [CITS3007 Github](https://github.com/cits3007).
|
||||
|
||||
NOTICE - These labs are not assessed and are publically available material. There should be NO issues of plagarism keeping this repository public.
|
3
lab01-leap-year/.gitignore
vendored
Normal file
3
lab01-leap-year/.gitignore
vendored
Normal file
|
@ -0,0 +1,3 @@
|
|||
.*.swp
|
||||
*.o
|
||||
test_leap
|
9
lab01-leap-year/Makefile
Normal file
9
lab01-leap-year/Makefile
Normal file
|
@ -0,0 +1,9 @@
|
|||
|
||||
CFLAGS = -pedantic -Wall -Wextra
|
||||
|
||||
all: test_leap
|
||||
|
||||
test_leap: test_leap.o
|
||||
|
||||
test_leap.o:
|
||||
|
20
lab01-leap-year/README.md
Normal file
20
lab01-leap-year/README.md
Normal file
|
@ -0,0 +1,20 @@
|
|||
# CITS3007 lab 1 demo repository – leap year
|
||||
|
||||
This repository contains a Makefile and C code for a program (`test_leap`)
|
||||
intended to show whether a year (supplied as a command-line argument) is a leap
|
||||
year or not.
|
||||
|
||||
## Building
|
||||
|
||||
Build with `make`.
|
||||
|
||||
## Use
|
||||
|
||||
The program can be run by supplying a year as a command-line argument:
|
||||
|
||||
```
|
||||
$ ./test_leap 1901
|
||||
1901 is not a leap year
|
||||
```
|
||||
|
||||
|
53
lab01-leap-year/test_leap.c
Normal file
53
lab01-leap-year/test_leap.c
Normal file
|
@ -0,0 +1,53 @@
|
|||
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <errno.h>
|
||||
|
||||
/* return 0 (false) or 1 (true), depending on whether
|
||||
* `year` is a leap year or not.
|
||||
*/
|
||||
int is_leap(long year) {
|
||||
|
||||
if (year % 4 != 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (year % 100 == 0) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
argc--;
|
||||
argv++;
|
||||
|
||||
if (argc != 1) {
|
||||
fprintf(stderr, "Error: expected 1 command-line argument (a YEAR), but got %d\n", argc);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
char *end;
|
||||
|
||||
// clear errno so we can check whether strtol fails
|
||||
errno = 0;
|
||||
long year = strtol(argv[0], &end, 10);
|
||||
int res_errno = errno;
|
||||
|
||||
if (end == argv[0]) {
|
||||
fprintf(stderr, "Error: couldn't interpret '%s' as a number\n", argv[0]);
|
||||
exit(1);
|
||||
} else if (res_errno == ERANGE) {
|
||||
fprintf(stderr, "Error: '%s' is outside the range of numbers we can handle\n", argv[0]);
|
||||
exit(1);
|
||||
} else {
|
||||
if (is_leap(year)) {
|
||||
printf("%ld is a leap year\n", year);
|
||||
} else {
|
||||
printf("%ld is not a leap year\n", year);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user