annotate lab4/pas0.c @ 0:bfdcc3820b32

Basis
author Mike Spivey <mike@cs.ox.ac.uk>
date Thu, 05 Oct 2017 08:04:15 +0100
parents
children
rev   line source
0
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
1 /* pas0.c */
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
2
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
3 #include <stdio.h>
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
4 #include <stdlib.h>
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
5 #include <string.h>
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
6
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
7 extern void pmain();
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
8
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
9 static int save_argc;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
10 static char **save_argv;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
11 static FILE *infile;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
12
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
13 int main(int argc, char **argv) {
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
14 save_argc = argc;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
15 save_argv = argv;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
16 infile = stdin;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
17 pmain();
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
18 return 0;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
19 }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
20
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
21 int argc(void) {
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
22 return save_argc;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
23 }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
24
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
25 void argv(int n, char *s) {
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
26 strcpy(s, save_argv[n]);
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
27 }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
28
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
29 void print_string(char *s, int n) {
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
30 printf("%.*s", n, s);
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
31 }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
32
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
33 void print_num(int n) {
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
34 printf("%d", n);
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
35 }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
36
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
37 void print_char(int c) {
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
38 printf("%c", c);
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
39 }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
40
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
41 void newline(void) {
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
42 printf("\n");
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
43 }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
44
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
45 void read_char(char *c) {
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
46 int c0 = fgetc(infile);
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
47 *c = (c0 == EOF ? 127 : c0);
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
48 }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
49
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
50 int open_in(char *s) {
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
51 FILE *f = fopen(s, "r");
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
52 if (f == NULL) return 0;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
53 if (infile != stdin) fclose(infile);
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
54 infile = f;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
55 return 1;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
56 }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
57
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
58 void close_in(void) {
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
59 if (infile == stdin) return;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
60 fclose(infile);
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
61 infile = stdin;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
62 }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
63
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
64 void check(int n) {
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
65 fprintf(stderr, "Array bound error on line %d\n", n);
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
66 exit(2);
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
67 }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
68
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
69 void nullcheck(int n) {
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
70 fprintf(stderr, "Null pointer check on line %d\n", n);
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
71 exit(2);
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
72 }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
73
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
74 void *new(int n) {
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
75 char *q = malloc(n);
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
76 if (q == NULL) {
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
77 fprintf(stderr, "Out of memory space\n");
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
78 exit(2);
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
79 }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
80 return q;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
81 }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
82
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
83 int int_div(int a, int b) {
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
84 int quo = a / b, rem = a % b;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
85 if (rem != 0 && (rem ^ b) < 0) quo--;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
86 return quo;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
87 }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
88
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
89 int int_mod(int a, int b) {
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
90 int rem = a % b;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
91 if (rem != 0 && (rem ^ b) < 0) rem += b;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
92 return rem;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
93 }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
94