From 5889ce58f74c213a981c490c57bd39a645e67366 Mon Sep 17 00:00:00 2001 From: Peter Date: Thu, 3 Aug 2023 18:45:02 +0000 Subject: [PATCH] initial commit --- .gitignore | 0 README.md | 5 ++++ lab01-leap-year/.gitignore | 3 +++ lab01-leap-year/Makefile | 9 +++++++ lab01-leap-year/README.md | 20 ++++++++++++++ lab01-leap-year/test_leap.c | 53 +++++++++++++++++++++++++++++++++++++ 6 files changed, 90 insertions(+) create mode 100644 .gitignore create mode 100644 README.md create mode 100644 lab01-leap-year/.gitignore create mode 100644 lab01-leap-year/Makefile create mode 100644 lab01-leap-year/README.md create mode 100644 lab01-leap-year/test_leap.c diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e69de29 diff --git a/README.md b/README.md new file mode 100644 index 0000000..ecbcf94 --- /dev/null +++ b/README.md @@ -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. \ No newline at end of file diff --git a/lab01-leap-year/.gitignore b/lab01-leap-year/.gitignore new file mode 100644 index 0000000..80795cd --- /dev/null +++ b/lab01-leap-year/.gitignore @@ -0,0 +1,3 @@ +.*.swp +*.o +test_leap diff --git a/lab01-leap-year/Makefile b/lab01-leap-year/Makefile new file mode 100644 index 0000000..587da1b --- /dev/null +++ b/lab01-leap-year/Makefile @@ -0,0 +1,9 @@ + +CFLAGS = -pedantic -Wall -Wextra + +all: test_leap + +test_leap: test_leap.o + +test_leap.o: + diff --git a/lab01-leap-year/README.md b/lab01-leap-year/README.md new file mode 100644 index 0000000..2a1305b --- /dev/null +++ b/lab01-leap-year/README.md @@ -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 +``` + + diff --git a/lab01-leap-year/test_leap.c b/lab01-leap-year/test_leap.c new file mode 100644 index 0000000..633579c --- /dev/null +++ b/lab01-leap-year/test_leap.c @@ -0,0 +1,53 @@ + +#include +#include +#include + +/* 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); + } + } + +} +