mirror of
https://github.com/peter-tanner/CITS-3007-labs.git
synced 2024-11-30 06:10:18 +08:00
Lab 2
This commit is contained in:
parent
60356de304
commit
071853f99f
10
lab02/.gitignore
vendored
Normal file
10
lab02/.gitignore
vendored
Normal file
|
@ -0,0 +1,10 @@
|
|||
.*.swp
|
||||
*.o
|
||||
test_leap
|
||||
*.creator*
|
||||
*.cflags
|
||||
*.config
|
||||
*.cxxflags
|
||||
*.files
|
||||
*.includes
|
||||
.vscode/
|
9
lab02/README.md
Normal file
9
lab02/README.md
Normal file
|
@ -0,0 +1,9 @@
|
|||
# CITS3007 lab 2 demo repository
|
||||
|
||||
This repository contains a Makefile and C code for two programs,
|
||||
"factorial" and "segfault".
|
||||
|
||||
## Building
|
||||
|
||||
Build with `make`.
|
||||
|
59
lab02/factorial.c
Normal file
59
lab02/factorial.c
Normal file
|
@ -0,0 +1,59 @@
|
|||
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <errno.h>
|
||||
|
||||
// contains INT_MAX
|
||||
#include <limits.h>
|
||||
|
||||
/* return the factorial of n.
|
||||
* The result is undefined if n < 0.
|
||||
*
|
||||
* The largest factorial that can be calculated depends
|
||||
* on your platform. On platforms where a `long`
|
||||
* is 8 bytes, the results of the function for
|
||||
* any n greater than 20 are undefined.
|
||||
*/
|
||||
long factorial(int n) {
|
||||
long result = 1;
|
||||
for (int i = n; i > 0; i--) {
|
||||
result = result * i;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
argc--;
|
||||
argv++;
|
||||
|
||||
if (argc != 1) {
|
||||
fprintf(stderr, "Error: expected 1 command-line argument (an INT), but got %d\n", argc);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
char *end;
|
||||
|
||||
// clear errno so we can check whether strtol fails
|
||||
errno = 0;
|
||||
long n = 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 (n > INT_MAX) {
|
||||
fprintf(stderr, "Error: '%s' is too big to fit in an int\n", argv[0]);
|
||||
exit(1);
|
||||
} else if (n < 0) {
|
||||
fprintf(stderr, "Error: invalid value %ld:"
|
||||
"factorial can only be calculated for non-negative numbers\n", n);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
int nn = (short) n;
|
||||
long result = factorial(nn);
|
||||
printf("factorial(%d) is %ld\n", nn, result);
|
||||
}
|
27
lab02/segfault.c
Normal file
27
lab02/segfault.c
Normal file
|
@ -0,0 +1,27 @@
|
|||
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#define STR_BUF_SIZE 1024
|
||||
|
||||
#define STR_HELPER(x) #x
|
||||
#define STR(x) STR_HELPER(x)
|
||||
#define CHECK_ALLOC(block) \
|
||||
if (block == 0) \
|
||||
{ \
|
||||
printf("malloc() failed at " STR(__LINE__) ". Exited."); \
|
||||
exit(-1); \
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
char *buf;
|
||||
buf = malloc(STR_BUF_SIZE); // allocate a large buffer
|
||||
CHECK_ALLOC(buf)
|
||||
|
||||
printf("type some text and hit 'return':\n");
|
||||
fgets(buf, STR_BUF_SIZE, stdin); // read 1024 chars into buf
|
||||
printf("\n%s\n\n", buf); // print what was entered
|
||||
free(buf);
|
||||
return 0;
|
||||
}
|
Loading…
Reference in New Issue
Block a user