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