From 24e262a1e60158edd0610dce3e4280d674559a26 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafa=C5=82=20-rsm-=20Marek?= Date: Thu, 18 Jul 2024 17:15:33 +0200 Subject: [PATCH] Exerc 7_2. --- .../7_2.print_input/Makefile | 3 + chapter_7.input_output/7_2.print_input/main.c | 27 ++ .../7_2.print_input/tests/0_main_c.tin | 24 ++ .../7_2.print_input/tests/0_main_c.tout | 14 + .../7_2.print_input/tests/1_longer_c.tin | 295 ++++++++++++++++++ .../7_2.print_input/tests/1_longer_c.tout | 277 ++++++++++++++++ .../7_2.print_input/tests/2_bin_rand.tin | 1 + .../7_2.print_input/tests/2_bin_rand.tout | 6 + .../tests/3_bin_rand_longer.tin | Bin 0 -> 1024 bytes .../tests/3_bin_rand_longer.tout | 43 +++ 10 files changed, 690 insertions(+) create mode 100644 chapter_7.input_output/7_2.print_input/Makefile create mode 100644 chapter_7.input_output/7_2.print_input/main.c create mode 100644 chapter_7.input_output/7_2.print_input/tests/0_main_c.tin create mode 100644 chapter_7.input_output/7_2.print_input/tests/0_main_c.tout create mode 100644 chapter_7.input_output/7_2.print_input/tests/1_longer_c.tin create mode 100644 chapter_7.input_output/7_2.print_input/tests/1_longer_c.tout create mode 100644 chapter_7.input_output/7_2.print_input/tests/2_bin_rand.tin create mode 100644 chapter_7.input_output/7_2.print_input/tests/2_bin_rand.tout create mode 100644 chapter_7.input_output/7_2.print_input/tests/3_bin_rand_longer.tin create mode 100644 chapter_7.input_output/7_2.print_input/tests/3_bin_rand_longer.tout diff --git a/chapter_7.input_output/7_2.print_input/Makefile b/chapter_7.input_output/7_2.print_input/Makefile new file mode 100644 index 0000000..14ffc83 --- /dev/null +++ b/chapter_7.input_output/7_2.print_input/Makefile @@ -0,0 +1,3 @@ +BINARY=print_input + +include ../../Exercise_incl.mk diff --git a/chapter_7.input_output/7_2.print_input/main.c b/chapter_7.input_output/7_2.print_input/main.c new file mode 100644 index 0000000..90bbbd6 --- /dev/null +++ b/chapter_7.input_output/7_2.print_input/main.c @@ -0,0 +1,27 @@ +# include +# include + +# define LINESIZE 24 +# define LINEBLOCK 4 + +int main () +{ + int c, col = 0; + char line[LINESIZE+1]; + + while ((c = getchar()) != EOF) { + if (col == LINESIZE) { + line[col] = '\0'; + printf("| %s\n", line); + col = 0; + } + line[col++] = isgraph(c) ? c : '.'; + printf(col % LINEBLOCK == 0 ? "%02x " : "%02x ", c); + } + line[col] = '\0'; + while (col < LINESIZE) + printf(++col % LINEBLOCK == 0 ? " " : " "); + printf("| %s\n", line); + + return 0; +} diff --git a/chapter_7.input_output/7_2.print_input/tests/0_main_c.tin b/chapter_7.input_output/7_2.print_input/tests/0_main_c.tin new file mode 100644 index 0000000..39b8f95 --- /dev/null +++ b/chapter_7.input_output/7_2.print_input/tests/0_main_c.tin @@ -0,0 +1,24 @@ +# include +# include + +# define COLBREAK 80 + +int main () +{ + int c, col = 0; + + while ((c = getchar()) != EOF) { + if (isgraph(c)) { + putchar(c); + col++; + } else { + printf("{0x%02x}", c); + col += 6; + } + if (col >= COLBREAK) { + putchar('\n'); + col = 0; + } + } + return 0; +} diff --git a/chapter_7.input_output/7_2.print_input/tests/0_main_c.tout b/chapter_7.input_output/7_2.print_input/tests/0_main_c.tout new file mode 100644 index 0000000..fafe3b7 --- /dev/null +++ b/chapter_7.input_output/7_2.print_input/tests/0_main_c.tout @@ -0,0 +1,14 @@ +23 20 69 6e 63 6c 75 64 65 20 3c 73 74 64 69 6f 2e 68 3e 0a 23 20 69 6e | #.include..#.in +63 6c 75 64 65 20 3c 63 74 79 70 65 2e 68 3e 0a 0a 23 20 64 65 66 69 6e | clude...#.defin +65 20 43 4f 4c 42 52 45 41 4b 20 38 30 0a 0a 69 6e 74 20 6d 61 69 6e 20 | e.COLBREAK.80..int.main. +28 29 0a 7b 0a 20 20 69 6e 74 20 63 2c 20 63 6f 6c 20 3d 20 30 3b 0a 0a | ().{...int.c,.col.=.0;.. +20 20 77 68 69 6c 65 20 28 28 63 20 3d 20 67 65 74 63 68 61 72 28 29 29 | ..while.((c.=.getchar()) +20 21 3d 20 45 4f 46 29 20 7b 0a 20 20 20 20 69 66 20 28 69 73 67 72 61 | .!=.EOF).{.....if.(isgra +70 68 28 63 29 29 20 7b 0a 20 20 20 20 20 20 70 75 74 63 68 61 72 28 63 | ph(c)).{.......putchar(c +29 3b 0a 20 20 20 20 20 20 63 6f 6c 2b 2b 3b 0a 20 20 20 20 7d 20 65 6c | );.......col++;.....}.el +73 65 20 7b 0a 20 20 20 20 20 20 70 72 69 6e 74 66 28 22 7b 30 78 25 30 | se.{.......printf("{0x%0 +32 78 7d 22 2c 20 63 29 3b 0a 20 20 20 20 20 20 63 6f 6c 20 2b 3d 20 36 | 2x}",.c);.......col.+=.6 +3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 20 28 63 6f 6c 20 3e 3d 20 43 | ;.....}.....if.(col.>=.C +4f 4c 42 52 45 41 4b 29 20 7b 0a 20 20 20 20 20 20 70 75 74 63 68 61 72 | OLBREAK).{.......putchar +28 27 5c 6e 27 29 3b 0a 20 20 20 20 20 20 63 6f 6c 20 3d 20 30 3b 0a 20 | ('\n');.......col.=.0;.. +20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a | ...}...}...return.0;.}. diff --git a/chapter_7.input_output/7_2.print_input/tests/1_longer_c.tin b/chapter_7.input_output/7_2.print_input/tests/1_longer_c.tin new file mode 100644 index 0000000..c075f63 --- /dev/null +++ b/chapter_7.input_output/7_2.print_input/tests/1_longer_c.tin @@ -0,0 +1,295 @@ +# include +# include /* for atof */ +# include /* strcmp */ +# include +# include + +int getop (char s[]); +int getch (void); +void ungetch (int c); +void push (double f); +double pop (void); +double peek (void); +void clear (void); +void set_var (char var_name, double value); +double get_var (char var_name); +double set_last (double value); +double get_last (void); + +# define GETOP_NUMBER '0' /* signal that a number was found */ +# define GETOP_SYMBOL 'a' /* signal that a symbol was found */ +# define MAXOP 128 /* max size of operand or operator */ +# define BUFSIZE 128 +# define MAXVAL 128 /* max depth of val stack */ + +int sp = 0; /* next free stack position */ +double val[MAXVAL]; /* value stack */ + +int buf[BUFSIZE]; /* buffer for ungetch */ +int bufp = 0; /* next free position in buf */ + +double vars['Z'-'A']; +double last_val; + + +/* reverse Polish calculator */ +int main () +{ + int type; + double op2, op_r; + char s[MAXOP]; + + while ((type = getop(s)) != EOF) { + switch (type) { + case GETOP_SYMBOL: + if (strcmp("sin", s) == 0) { /* math sin */ + push(sin(pop())); + } else if (strcmp("cos", s) == 0) { /* math cos */ + push(cos(pop())); + } else if (strcmp("tan", s) == 0) { /* math tan */ + push(tan(pop())); + } else if (strcmp("asin", s) == 0) { /* math asin */ + push(asin(pop())); + } else if (strcmp("acos", s) == 0) { /* math acos */ + push(acos(pop())); + } else if (strcmp("atan", s) == 0) { /* math atan */ + push(atan(pop())); + } else if (strcmp("atan2", s) == 0) { /* math atan2 */ + push(atan2(pop(), pop())); + } else if (strcmp("sinh", s) == 0) { /* math sinh */ + push(sinh(pop())); + } else if (strcmp("cosh", s) == 0) { /* math cosh */ + push(cosh(pop())); + } else if (strcmp("tanh", s) == 0) { /* math tanh */ + push(tanh(pop())); + } else if (strcmp("exp", s) == 0) { /* math exp */ + push(exp(pop())); + } else if (strcmp("log", s) == 0) { /* math log */ + push(log(pop())); + } else if (strcmp("log10", s) == 0) { /* math log10 */ + push(log10(pop())); + } else if (strcmp("pow", s) == 0) { /* math pow */ + op2 = pop(); + push(pow(pop(), op2)); + } else if (strcmp("sqrt", s) == 0) { /* math sqrt */ + push(sqrt(pop())); + } else if (strcmp("ceil", s) == 0) { /* math ceil */ + push(ceil(pop())); + } else if (strcmp("floor", s) == 0) { /* math floor */ + push(floor(pop())); + } else if (strcmp("abs", s) == 0) { /* math fabs */ + push(fabs(pop())); + } else if (strcmp("clear", s) == 0) { /* clear the stack */ + clear(); + } else if (strcmp("peek", s) == 0) { /* peek top stack element */ + printf("\t%.8g\n", peek()); + } else if (strcmp("swap", s) == 0) { /* swap top two stack elements */ + op_r = pop(); + op2 = pop(); + push(op_r); + push(op2); + } else if (strlen(s)==2 && + ((s[0]==':' && ((s[1]>='A' && s[1]<='Z') || (s[1]=='?'))) || + (s[1]=='=' && s[0]>='A' && s[0]<='Z'))) { /* variables */ + if (s[1]=='=') { /* variable set */ + set_var(s[0], pop()); + } else if (s[1]=='?') { /* last return */ + push(get_last()); + } else { /* variable get */ + push(get_var(s[1])); + } + } else { + printf("error: unknown symbol %s\n", s); + } + break; + case GETOP_NUMBER: + push(atof(s)); + break; + case '+': + push(pop() + pop()); + break; + case '*': + push(pop() * pop()); + break; + case '-': + op2 = pop(); + push(pop() - op2); + break; + case '/': + op2 = pop(); + if (op2 != 0.0) { + push(pop() / op2); + } else { + printf("error: zero divisor\n"); + } + break; + case '%': + op2 = pop(); + if (op2 != 0.0) { + push((long int) pop() % (long int) op2); + } else { + printf("error: zero modulus\n"); + } + break; + case '\n': + printf("\t%.8g\n", set_last(pop())); + break; + default: + printf("error: unknown command %s\n", s); + break; + } + } + return 0; +} + + +/* getop: get next character, numeric operand or function/command */ +int getop (char s[]) +{ + int i, c; + + while ((s[0] = c = getch()) == ' ' || c == '\t') { + } + + if (c == EOF || c == '\n') { + return c; + } + /* operator, single digit number or single letter symbol */ + if ((s[1] = c = getch()) == ' ' || c == '\t' || c == '\n' || c == EOF) { + ungetch(c); + s[1] = '\0'; + c = s[0]; + /* number */ + if (isdigit(c)) { + return GETOP_NUMBER; + } + /* single letter symbol [a-zA-Z] */ + else if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z')) { + return GETOP_SYMBOL; + } + /* operator */ + else { + return c; + } + } + /* number or symbol */ + else { + ungetch(c); + c = s[0]; + /* number */ + if (isdigit(c) || c == '-' || c == '.') { + i = 0; + if (c == '-') { /* move by a minus (-) sign */ + s[++i] = c = getch(); + } + if (isdigit(c)) { /* collect integer part */ + while (isdigit(s[++i] = c = getch())) { + } + } + if (c == '.') { /* collect fraction part */ + while (isdigit(s[++i] = c = getch())) { + } + } + s[i] = '\0'; + ungetch(c); + return GETOP_NUMBER; + } + /* symbol */ + else { + i = 0; + while ((s[++i] = c = getch()) != ' ' && c != '\t' && c != '\n' && c != EOF) { + } + s[i] = '\0'; + ungetch(c); + return GETOP_SYMBOL; + } + } +} + + +/* getch: get a (possibly pushed-back) character */ +int getch (void) +{ + return (bufp > 0) ? buf[--bufp] : getchar(); +} + + +/* ungetch: push character back on input */ +void ungetch (int c) +{ + if (bufp >= BUFSIZE) { + printf("ungetch: too many characters\n"); + } else { + buf[bufp++] = c; + } +} + + +/* push: push f onto stack */ +void push (double f) +{ + if (sp < MAXVAL) { + val[sp++] = f; + } else { + printf("error: stack full, can't push %g\n", f); + } +} + + +/* pop: pop and return top value from stack */ +double pop (void) +{ + if (sp > 0) { + return val[--sp]; + } else { + printf("error: stack empty\n"); + return 0.0; + } +} + + +/* peek: return top value from stack without popping */ +double peek (void) +{ + if (sp > 0) { + return val[sp - 1]; + } else { + printf("error: stack empty\n"); + return 0.0; + } +} + + +/* clear: clear the stack */ +void clear (void) +{ + sp = 0; +} + + +/* set_var: set variable */ +void set_var (char var_name, double value) +{ + vars[var_name-'A'] = value; +} + + +/* get_var: get variable value */ +double get_var (char var_name) +{ + return vars[var_name-'A']; +} + + +/* set_last: set last returned value */ +double set_last (double value) +{ + return last_val = value; +} + + +/* get_last: get last returned value */ +double get_last (void) +{ + return last_val; +} diff --git a/chapter_7.input_output/7_2.print_input/tests/1_longer_c.tout b/chapter_7.input_output/7_2.print_input/tests/1_longer_c.tout new file mode 100644 index 0000000..da1d53a --- /dev/null +++ b/chapter_7.input_output/7_2.print_input/tests/1_longer_c.tout @@ -0,0 +1,277 @@ +23 20 69 6e 63 6c 75 64 65 20 3c 73 74 64 69 6f 2e 68 3e 0a 23 20 69 6e | #.include..#.in +63 6c 75 64 65 20 3c 73 74 64 6c 69 62 2e 68 3e 20 2f 2a 20 66 6f 72 20 | clude../*.for. +61 74 6f 66 20 2a 2f 0a 23 20 69 6e 63 6c 75 64 65 20 3c 73 74 72 69 6e | atof.*/.#.include../*.strcmp.*/.#.incl +75 64 65 20 3c 6d 61 74 68 2e 68 3e 0a 23 20 69 6e 63 6c 75 64 65 20 3c | ude..#.include.< +63 74 79 70 65 2e 68 3e 0a 0a 69 6e 74 20 67 65 74 6f 70 20 28 63 68 61 | ctype.h>..int.getop.(cha +72 20 73 5b 5d 29 3b 0a 69 6e 74 20 67 65 74 63 68 20 28 76 6f 69 64 29 | r.s[]);.int.getch.(void) +3b 0a 76 6f 69 64 20 75 6e 67 65 74 63 68 20 28 69 6e 74 20 63 29 3b 0a | ;.void.ungetch.(int.c);. +76 6f 69 64 20 70 75 73 68 20 28 64 6f 75 62 6c 65 20 66 29 3b 0a 64 6f | void.push.(double.f);.do +75 62 6c 65 20 70 6f 70 20 28 76 6f 69 64 29 3b 0a 64 6f 75 62 6c 65 20 | uble.pop.(void);.double. +70 65 65 6b 20 28 76 6f 69 64 29 3b 0a 76 6f 69 64 20 63 6c 65 61 72 20 | peek.(void);.void.clear. +28 76 6f 69 64 29 3b 0a 76 6f 69 64 20 73 65 74 5f 76 61 72 20 28 63 68 | (void);.void.set_var.(ch +61 72 20 76 61 72 5f 6e 61 6d 65 2c 20 64 6f 75 62 6c 65 20 76 61 6c 75 | ar.var_name,.double.valu +65 29 3b 0a 64 6f 75 62 6c 65 20 67 65 74 5f 76 61 72 20 28 63 68 61 72 | e);.double.get_var.(char +20 76 61 72 5f 6e 61 6d 65 29 3b 0a 64 6f 75 62 6c 65 20 73 65 74 5f 6c | .var_name);.double.set_l +61 73 74 20 28 64 6f 75 62 6c 65 20 76 61 6c 75 65 29 3b 0a 64 6f 75 62 | ast.(double.value);.doub +6c 65 20 67 65 74 5f 6c 61 73 74 20 28 76 6f 69 64 29 3b 0a 0a 23 20 64 | le.get_last.(void);..#.d +65 66 69 6e 65 20 47 45 54 4f 50 5f 4e 55 4d 42 45 52 20 27 30 27 20 20 | efine.GETOP_NUMBER.'0'.. +20 2f 2a 20 73 69 67 6e 61 6c 20 74 68 61 74 20 61 20 6e 75 6d 62 65 72 | ./*.signal.that.a.number +20 77 61 73 20 66 6f 75 6e 64 20 2a 2f 0a 23 20 64 65 66 69 6e 65 20 47 | .was.found.*/.#.define.G +45 54 4f 50 5f 53 59 4d 42 4f 4c 20 27 61 27 20 20 20 2f 2a 20 73 69 67 | ETOP_SYMBOL.'a'.../*.sig +6e 61 6c 20 74 68 61 74 20 61 20 73 79 6d 62 6f 6c 20 77 61 73 20 66 6f | nal.that.a.symbol.was.fo +75 6e 64 20 2a 2f 0a 23 20 64 65 66 69 6e 65 20 4d 41 58 4f 50 20 31 32 | und.*/.#.define.MAXOP.12 +38 20 20 20 20 2f 2a 20 6d 61 78 20 73 69 7a 65 20 6f 66 20 6f 70 65 72 | 8..../*.max.size.of.oper +61 6e 64 20 6f 72 20 6f 70 65 72 61 74 6f 72 20 2a 2f 0a 23 20 64 65 66 | and.or.operator.*/.#.def +69 6e 65 20 42 55 46 53 49 5a 45 20 31 32 38 0a 23 20 64 65 66 69 6e 65 | ine.BUFSIZE.128.#.define +20 4d 41 58 56 41 4c 20 31 32 38 20 2f 2a 20 6d 61 78 20 64 65 70 74 68 | .MAXVAL.128./*.max.depth +20 6f 66 20 76 61 6c 20 73 74 61 63 6b 20 2a 2f 0a 0a 69 6e 74 20 73 70 | .of.val.stack.*/..int.sp +20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 6e 65 78 74 20 66 72 | .=.0;........./*.next.fr +65 65 20 73 74 61 63 6b 20 70 6f 73 69 74 69 6f 6e 20 2a 2f 0a 64 6f 75 | ee.stack.position.*/.dou +62 6c 65 20 76 61 6c 5b 4d 41 58 56 41 4c 5d 3b 20 2f 2a 20 76 61 6c 75 | ble.val[MAXVAL];./*.valu +65 20 73 74 61 63 6b 20 2a 2f 0a 0a 69 6e 74 20 62 75 66 5b 42 55 46 53 | e.stack.*/..int.buf[BUFS +49 5a 45 5d 3b 20 20 20 20 2f 2a 20 62 75 66 66 65 72 20 66 6f 72 20 75 | IZE];..../*.buffer.for.u +6e 67 65 74 63 68 20 2a 2f 0a 69 6e 74 20 62 75 66 70 20 3d 20 30 3b 20 | ngetch.*/.int.bufp.=.0;. +20 20 20 20 20 20 20 2f 2a 20 6e 65 78 74 20 66 72 65 65 20 70 6f 73 69 | ......./*.next.free.posi +74 69 6f 6e 20 69 6e 20 62 75 66 20 2a 2f 0a 0a 64 6f 75 62 6c 65 20 76 | tion.in.buf.*/..double.v +61 72 73 5b 27 5a 27 2d 27 41 27 5d 3b 0a 64 6f 75 62 6c 65 20 6c 61 73 | ars['Z'-'A'];.double.las +74 5f 76 61 6c 3b 0a 0a 0a 2f 2a 20 72 65 76 65 72 73 65 20 50 6f 6c 69 | t_val;.../*.reverse.Poli +73 68 20 63 61 6c 63 75 6c 61 74 6f 72 20 2a 2f 0a 69 6e 74 20 6d 61 69 | sh.calculator.*/.int.mai +6e 20 28 29 0a 7b 0a 20 20 69 6e 74 20 74 79 70 65 3b 0a 20 20 64 6f 75 | n.().{...int.type;...dou +62 6c 65 20 6f 70 32 2c 20 6f 70 5f 72 3b 0a 20 20 63 68 61 72 20 73 5b | ble.op2,.op_r;...char.s[ +4d 41 58 4f 50 5d 3b 0a 0a 20 20 77 68 69 6c 65 20 28 28 74 79 70 65 20 | MAXOP];....while.((type. +3d 20 67 65 74 6f 70 28 73 29 29 20 21 3d 20 45 4f 46 29 20 7b 0a 20 20 | =.getop(s)).!=.EOF).{... +20 20 73 77 69 74 63 68 20 28 74 79 70 65 29 20 7b 0a 20 20 20 20 63 61 | ..switch.(type).{.....ca +73 65 20 47 45 54 4f 50 5f 53 59 4d 42 4f 4c 3a 0a 20 20 20 20 20 20 69 | se.GETOP_SYMBOL:.......i +66 20 28 73 74 72 63 6d 70 28 22 73 69 6e 22 2c 20 73 29 20 3d 3d 20 30 | f.(strcmp("sin",.s).==.0 +29 20 7b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6d 61 74 68 20 73 69 6e | ).{........../*.math.sin +20 2a 2f 0a 09 70 75 73 68 28 73 69 6e 28 70 6f 70 28 29 29 29 3b 0a 20 | .*/..push(sin(pop()));.. +20 20 20 20 20 7d 20 65 6c 73 65 20 69 66 20 28 73 74 72 63 6d 70 28 22 | .....}.else.if.(strcmp(" +63 6f 73 22 2c 20 73 29 20 3d 3d 20 30 29 20 7b 20 20 20 2f 2a 20 6d 61 | cos",.s).==.0).{.../*.ma +74 68 20 63 6f 73 20 2a 2f 0a 09 70 75 73 68 28 63 6f 73 28 70 6f 70 28 | th.cos.*/..push(cos(pop( +29 29 29 3b 0a 20 20 20 20 20 20 7d 20 65 6c 73 65 20 69 66 20 28 73 74 | )));.......}.else.if.(st +72 63 6d 70 28 22 74 61 6e 22 2c 20 73 29 20 3d 3d 20 30 29 20 7b 20 20 | rcmp("tan",.s).==.0).{.. +20 2f 2a 20 6d 61 74 68 20 74 61 6e 20 2a 2f 0a 09 70 75 73 68 28 74 61 | ./*.math.tan.*/..push(ta +6e 28 70 6f 70 28 29 29 29 3b 0a 20 20 20 20 20 20 7d 20 65 6c 73 65 20 | n(pop()));.......}.else. +69 66 20 28 73 74 72 63 6d 70 28 22 61 73 69 6e 22 2c 20 73 29 20 3d 3d | if.(strcmp("asin",.s).== +20 30 29 20 7b 20 20 2f 2a 20 6d 61 74 68 20 61 73 69 6e 20 2a 2f 0a 09 | .0).{../*.math.asin.*/.. +70 75 73 68 28 61 73 69 6e 28 70 6f 70 28 29 29 29 3b 0a 20 20 20 20 20 | push(asin(pop()));...... +20 7d 20 65 6c 73 65 20 69 66 20 28 73 74 72 63 6d 70 28 22 61 63 6f 73 | .}.else.if.(strcmp("acos +22 2c 20 73 29 20 3d 3d 20 30 29 20 7b 20 20 2f 2a 20 6d 61 74 68 20 61 | ",.s).==.0).{../*.math.a +63 6f 73 20 2a 2f 0a 09 70 75 73 68 28 61 63 6f 73 28 70 6f 70 28 29 29 | cos.*/..push(acos(pop()) +29 3b 0a 20 20 20 20 20 20 7d 20 65 6c 73 65 20 69 66 20 28 73 74 72 63 | );.......}.else.if.(strc +6d 70 28 22 61 74 61 6e 22 2c 20 73 29 20 3d 3d 20 30 29 20 7b 20 20 2f | mp("atan",.s).==.0).{../ +2a 20 6d 61 74 68 20 61 74 61 6e 20 2a 2f 0a 09 70 75 73 68 28 61 74 61 | *.math.atan.*/..push(ata +6e 28 70 6f 70 28 29 29 29 3b 0a 20 20 20 20 20 20 7d 20 65 6c 73 65 20 | n(pop()));.......}.else. +69 66 20 28 73 74 72 63 6d 70 28 22 61 74 61 6e 32 22 2c 20 73 29 20 3d | if.(strcmp("atan2",.s).= +3d 20 30 29 20 7b 20 2f 2a 20 6d 61 74 68 20 61 74 61 6e 32 20 2a 2f 0a | =.0).{./*.math.atan2.*/. +09 70 75 73 68 28 61 74 61 6e 32 28 70 6f 70 28 29 2c 20 70 6f 70 28 29 | .push(atan2(pop(),.pop() +29 29 3b 0a 20 20 20 20 20 20 7d 20 65 6c 73 65 20 69 66 20 28 73 74 72 | ));.......}.else.if.(str +63 6d 70 28 22 73 69 6e 68 22 2c 20 73 29 20 3d 3d 20 30 29 20 7b 20 20 | cmp("sinh",.s).==.0).{.. +2f 2a 20 6d 61 74 68 20 73 69 6e 68 20 2a 2f 0a 09 70 75 73 68 28 73 69 | /*.math.sinh.*/..push(si +6e 68 28 70 6f 70 28 29 29 29 3b 0a 20 20 20 20 20 20 7d 20 65 6c 73 65 | nh(pop()));.......}.else +20 69 66 20 28 73 74 72 63 6d 70 28 22 63 6f 73 68 22 2c 20 73 29 20 3d | .if.(strcmp("cosh",.s).= +3d 20 30 29 20 7b 20 20 2f 2a 20 6d 61 74 68 20 63 6f 73 68 20 2a 2f 0a | =.0).{../*.math.cosh.*/. +09 70 75 73 68 28 63 6f 73 68 28 70 6f 70 28 29 29 29 3b 0a 20 20 20 20 | .push(cosh(pop()));..... +20 20 7d 20 65 6c 73 65 20 69 66 20 28 73 74 72 63 6d 70 28 22 74 61 6e | ..}.else.if.(strcmp("tan +68 22 2c 20 73 29 20 3d 3d 20 30 29 20 7b 20 20 2f 2a 20 6d 61 74 68 20 | h",.s).==.0).{../*.math. +74 61 6e 68 20 2a 2f 0a 09 70 75 73 68 28 74 61 6e 68 28 70 6f 70 28 29 | tanh.*/..push(tanh(pop() +29 29 3b 0a 20 20 20 20 20 20 7d 20 65 6c 73 65 20 69 66 20 28 73 74 72 | ));.......}.else.if.(str +63 6d 70 28 22 65 78 70 22 2c 20 73 29 20 3d 3d 20 30 29 20 7b 20 20 20 | cmp("exp",.s).==.0).{... +2f 2a 20 6d 61 74 68 20 65 78 70 20 2a 2f 0a 09 70 75 73 68 28 65 78 70 | /*.math.exp.*/..push(exp +28 70 6f 70 28 29 29 29 3b 0a 20 20 20 20 20 20 7d 20 65 6c 73 65 20 69 | (pop()));.......}.else.i +66 20 28 73 74 72 63 6d 70 28 22 6c 6f 67 22 2c 20 73 29 20 3d 3d 20 30 | f.(strcmp("log",.s).==.0 +29 20 7b 20 20 20 2f 2a 20 6d 61 74 68 20 6c 6f 67 20 2a 2f 0a 09 70 75 | ).{.../*.math.log.*/..pu +73 68 28 6c 6f 67 28 70 6f 70 28 29 29 29 3b 0a 20 20 20 20 20 20 7d 20 | sh(log(pop()));.......}. +65 6c 73 65 20 69 66 20 28 73 74 72 63 6d 70 28 22 6c 6f 67 31 30 22 2c | else.if.(strcmp("log10", +20 73 29 20 3d 3d 20 30 29 20 7b 20 2f 2a 20 6d 61 74 68 20 6c 6f 67 31 | .s).==.0).{./*.math.log1 +30 20 2a 2f 0a 09 70 75 73 68 28 6c 6f 67 31 30 28 70 6f 70 28 29 29 29 | 0.*/..push(log10(pop())) +3b 0a 20 20 20 20 20 20 7d 20 65 6c 73 65 20 69 66 20 28 73 74 72 63 6d | ;.......}.else.if.(strcm +70 28 22 70 6f 77 22 2c 20 73 29 20 3d 3d 20 30 29 20 7b 20 20 20 2f 2a | p("pow",.s).==.0).{.../* +20 6d 61 74 68 20 70 6f 77 20 2a 2f 0a 09 6f 70 32 20 3d 20 70 6f 70 28 | .math.pow.*/..op2.=.pop( +29 3b 0a 09 70 75 73 68 28 70 6f 77 28 70 6f 70 28 29 2c 20 6f 70 32 29 | );..push(pow(pop(),.op2) +29 3b 0a 20 20 20 20 20 20 7d 20 65 6c 73 65 20 69 66 20 28 73 74 72 63 | );.......}.else.if.(strc +6d 70 28 22 73 71 72 74 22 2c 20 73 29 20 3d 3d 20 30 29 20 7b 20 20 2f | mp("sqrt",.s).==.0).{../ +2a 20 6d 61 74 68 20 73 71 72 74 20 2a 2f 0a 09 70 75 73 68 28 73 71 72 | *.math.sqrt.*/..push(sqr +74 28 70 6f 70 28 29 29 29 3b 0a 20 20 20 20 20 20 7d 20 65 6c 73 65 20 | t(pop()));.......}.else. +69 66 20 28 73 74 72 63 6d 70 28 22 63 65 69 6c 22 2c 20 73 29 20 3d 3d | if.(strcmp("ceil",.s).== +20 30 29 20 7b 20 20 2f 2a 20 6d 61 74 68 20 63 65 69 6c 20 2a 2f 0a 09 | .0).{../*.math.ceil.*/.. +70 75 73 68 28 63 65 69 6c 28 70 6f 70 28 29 29 29 3b 0a 20 20 20 20 20 | push(ceil(pop()));...... +20 7d 20 65 6c 73 65 20 69 66 20 28 73 74 72 63 6d 70 28 22 66 6c 6f 6f | .}.else.if.(strcmp("floo +72 22 2c 20 73 29 20 3d 3d 20 30 29 20 7b 20 2f 2a 20 6d 61 74 68 20 66 | r",.s).==.0).{./*.math.f +6c 6f 6f 72 20 2a 2f 0a 09 70 75 73 68 28 66 6c 6f 6f 72 28 70 6f 70 28 | loor.*/..push(floor(pop( +29 29 29 3b 0a 20 20 20 20 20 20 7d 20 65 6c 73 65 20 69 66 20 28 73 74 | )));.......}.else.if.(st +72 63 6d 70 28 22 61 62 73 22 2c 20 73 29 20 3d 3d 20 30 29 20 7b 20 20 | rcmp("abs",.s).==.0).{.. +20 2f 2a 20 6d 61 74 68 20 66 61 62 73 20 2a 2f 0a 09 70 75 73 68 28 66 | ./*.math.fabs.*/..push(f +61 62 73 28 70 6f 70 28 29 29 29 3b 0a 20 20 20 20 20 20 7d 20 65 6c 73 | abs(pop()));.......}.els +65 20 69 66 20 28 73 74 72 63 6d 70 28 22 63 6c 65 61 72 22 2c 20 73 29 | e.if.(strcmp("clear",.s) +20 3d 3d 20 30 29 20 7b 20 2f 2a 20 63 6c 65 61 72 20 74 68 65 20 73 74 | .==.0).{./*.clear.the.st +61 63 6b 20 2a 2f 0a 09 63 6c 65 61 72 28 29 3b 0a 20 20 20 20 20 20 7d | ack.*/..clear();.......} +20 65 6c 73 65 20 69 66 20 28 73 74 72 63 6d 70 28 22 70 65 65 6b 22 2c | .else.if.(strcmp("peek", +20 73 29 20 3d 3d 20 30 29 20 7b 20 20 20 2f 2a 20 70 65 65 6b 20 74 6f | .s).==.0).{.../*.peek.to +70 20 73 74 61 63 6b 20 65 6c 65 6d 65 6e 74 20 2a 2f 0a 09 70 72 69 6e | p.stack.element.*/..prin +74 66 28 22 5c 74 25 2e 38 67 5c 6e 22 2c 20 70 65 65 6b 28 29 29 3b 0a | tf("\t%.8g\n",.peek());. +20 20 20 20 20 20 7d 20 65 6c 73 65 20 69 66 20 28 73 74 72 63 6d 70 28 | ......}.else.if.(strcmp( +22 73 77 61 70 22 2c 20 73 29 20 3d 3d 20 30 29 20 7b 20 2f 2a 20 73 77 | "swap",.s).==.0).{./*.sw +61 70 20 74 6f 70 20 74 77 6f 20 73 74 61 63 6b 20 65 6c 65 6d 65 6e 74 | ap.top.two.stack.element +73 20 2a 2f 0a 09 6f 70 5f 72 20 3d 20 70 6f 70 28 29 3b 0a 09 6f 70 32 | s.*/..op_r.=.pop();..op2 +20 3d 20 70 6f 70 28 29 3b 0a 09 70 75 73 68 28 6f 70 5f 72 29 3b 0a 09 | .=.pop();..push(op_r);.. +70 75 73 68 28 6f 70 32 29 3b 0a 20 20 20 20 20 20 7d 20 65 6c 73 65 20 | push(op2);.......}.else. +69 66 20 28 73 74 72 6c 65 6e 28 73 29 3d 3d 32 20 26 26 0a 09 09 20 28 | if.(strlen(s)==2.&&....( +28 73 5b 30 5d 3d 3d 27 3a 27 20 26 26 20 28 28 73 5b 31 5d 3e 3d 27 41 | (s[0]==':'.&&.((s[1]>='A +27 20 26 26 20 73 5b 31 5d 3c 3d 27 5a 27 29 20 7c 7c 20 28 73 5b 31 5d | '.&&.s[1]<='Z').||.(s[1] +3d 3d 27 3f 27 29 29 29 20 7c 7c 0a 09 09 20 20 28 73 5b 31 5d 3d 3d 27 | =='?'))).||.....(s[1]==' +3d 27 20 26 26 20 73 5b 30 5d 3e 3d 27 41 27 20 26 26 20 73 5b 30 5d 3c | ='.&&.s[0]>='A'.&&.s[0]< +3d 27 5a 27 29 29 29 20 7b 20 2f 2a 20 76 61 72 69 61 62 6c 65 73 20 2a | ='Z'))).{./*.variables.* +2f 0a 09 69 66 20 28 73 5b 31 5d 3d 3d 27 3d 27 29 20 7b 20 2f 2a 20 76 | /..if.(s[1]=='=').{./*.v +61 72 69 61 62 6c 65 20 73 65 74 20 2a 2f 0a 09 20 20 73 65 74 5f 76 61 | ariable.set.*/....set_va +72 28 73 5b 30 5d 2c 20 70 6f 70 28 29 29 3b 0a 09 7d 20 65 6c 73 65 20 | r(s[0],.pop());..}.else. +69 66 20 28 73 5b 31 5d 3d 3d 27 3f 27 29 20 7b 20 2f 2a 20 6c 61 73 74 | if.(s[1]=='?').{./*.last +20 72 65 74 75 72 6e 20 2a 2f 0a 09 20 20 70 75 73 68 28 67 65 74 5f 6c | .return.*/....push(get_l +61 73 74 28 29 29 3b 0a 09 7d 20 65 6c 73 65 20 7b 20 2f 2a 20 76 61 72 | ast());..}.else.{./*.var +69 61 62 6c 65 20 67 65 74 20 2a 2f 0a 09 20 20 70 75 73 68 28 67 65 74 | iable.get.*/....push(get +5f 76 61 72 28 73 5b 31 5d 29 29 3b 0a 09 7d 0a 20 20 20 20 20 20 7d 20 | _var(s[1]));..}.......}. +65 6c 73 65 20 7b 0a 09 70 72 69 6e 74 66 28 22 65 72 72 6f 72 3a 20 75 | else.{..printf("error:.u +6e 6b 6e 6f 77 6e 20 73 79 6d 62 6f 6c 20 25 73 5c 6e 22 2c 20 73 29 3b | nknown.symbol.%s\n",.s); +0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 | .......}.......break;... +20 20 63 61 73 65 20 47 45 54 4f 50 5f 4e 55 4d 42 45 52 3a 0a 20 20 20 | ..case.GETOP_NUMBER:.... +20 20 20 70 75 73 68 28 61 74 6f 66 28 73 29 29 3b 0a 20 20 20 20 20 20 | ...push(atof(s));....... +62 72 65 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 27 2b 27 3a 0a 20 20 20 | break;.....case.'+':.... +20 20 20 70 75 73 68 28 70 6f 70 28 29 20 2b 20 70 6f 70 28 29 29 3b 0a | ...push(pop().+.pop());. +20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 27 2a | ......break;.....case.'* +27 3a 0a 20 20 20 20 20 20 70 75 73 68 28 70 6f 70 28 29 20 2a 20 70 6f | ':.......push(pop().*.po +70 28 29 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 63 | p());.......break;.....c +61 73 65 20 27 2d 27 3a 0a 20 20 20 20 20 20 6f 70 32 20 3d 20 70 6f 70 | ase.'-':.......op2.=.pop +28 29 3b 0a 20 20 20 20 20 20 70 75 73 68 28 70 6f 70 28 29 20 2d 20 6f | ();.......push(pop().-.o +70 32 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 63 61 | p2);.......break;.....ca +73 65 20 27 2f 27 3a 0a 20 20 20 20 20 20 6f 70 32 20 3d 20 70 6f 70 28 | se.'/':.......op2.=.pop( +29 3b 0a 20 20 20 20 20 20 69 66 20 28 6f 70 32 20 21 3d 20 30 2e 30 29 | );.......if.(op2.!=.0.0) +20 7b 0a 09 70 75 73 68 28 70 6f 70 28 29 20 2f 20 6f 70 32 29 3b 0a 20 | .{..push(pop()./.op2);.. +20 20 20 20 20 7d 20 65 6c 73 65 20 7b 0a 09 70 72 69 6e 74 66 28 22 65 | .....}.else.{..printf("e +72 72 6f 72 3a 20 7a 65 72 6f 20 64 69 76 69 73 6f 72 5c 6e 22 29 3b 0a | rror:.zero.divisor\n");. +20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 | ......}.......break;.... +20 63 61 73 65 20 27 25 27 3a 0a 20 20 20 20 20 20 6f 70 32 20 3d 20 70 | .case.'%':.......op2.=.p +6f 70 28 29 3b 0a 20 20 20 20 20 20 69 66 20 28 6f 70 32 20 21 3d 20 30 | op();.......if.(op2.!=.0 +2e 30 29 20 7b 0a 09 70 75 73 68 28 28 6c 6f 6e 67 20 69 6e 74 29 20 70 | .0).{..push((long.int).p +6f 70 28 29 20 25 20 28 6c 6f 6e 67 20 69 6e 74 29 20 6f 70 32 29 3b 0a | op().%.(long.int).op2);. +20 20 20 20 20 20 7d 20 65 6c 73 65 20 7b 0a 09 70 72 69 6e 74 66 28 22 | ......}.else.{..printf(" +65 72 72 6f 72 3a 20 7a 65 72 6f 20 6d 6f 64 75 6c 75 73 5c 6e 22 29 3b | error:.zero.modulus\n"); +0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 | .......}.......break;... +20 20 63 61 73 65 20 27 5c 6e 27 3a 0a 20 20 20 20 20 20 70 72 69 6e 74 | ..case.'\n':.......print +66 28 22 5c 74 25 2e 38 67 5c 6e 22 2c 20 73 65 74 5f 6c 61 73 74 28 70 | f("\t%.8g\n",.set_last(p +6f 70 28 29 29 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 | op()));.......break;.... +20 64 65 66 61 75 6c 74 3a 0a 20 20 20 20 20 20 70 72 69 6e 74 66 28 22 | .default:.......printf(" +65 72 72 6f 72 3a 20 75 6e 6b 6e 6f 77 6e 20 63 6f 6d 6d 61 6e 64 20 25 | error:.unknown.command.% +73 5c 6e 22 2c 20 73 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 | s\n",.s);.......break;.. +20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a | ...}...}...return.0;.}.. +0a 2f 2a 20 67 65 74 6f 70 3a 20 67 65 74 20 6e 65 78 74 20 63 68 61 72 | ./*.getop:.get.next.char +61 63 74 65 72 2c 20 6e 75 6d 65 72 69 63 20 6f 70 65 72 61 6e 64 20 6f | acter,.numeric.operand.o +72 20 66 75 6e 63 74 69 6f 6e 2f 63 6f 6d 6d 61 6e 64 20 2a 2f 0a 69 6e | r.function/command.*/.in +74 20 67 65 74 6f 70 20 28 63 68 61 72 20 73 5b 5d 29 0a 7b 0a 20 20 69 | t.getop.(char.s[]).{...i +6e 74 20 69 2c 20 63 3b 0a 20 20 0a 20 20 77 68 69 6c 65 20 28 28 73 5b | nt.i,.c;......while.((s[ +30 5d 20 3d 20 63 20 3d 20 67 65 74 63 68 28 29 29 20 3d 3d 20 27 20 27 | 0].=.c.=.getch()).==.'.' +20 7c 7c 20 63 20 3d 3d 20 27 5c 74 27 29 20 7b 0a 20 20 7d 0a 0a 20 20 | .||.c.==.'\t').{...}.... +69 66 20 28 63 20 3d 3d 20 45 4f 46 20 7c 7c 20 63 20 3d 3d 20 27 5c 6e | if.(c.==.EOF.||.c.==.'\n +27 29 20 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 63 3b 0a 20 20 7d 0a 20 | ').{.....return.c;...}.. +20 2f 2a 20 6f 70 65 72 61 74 6f 72 2c 20 73 69 6e 67 6c 65 20 64 69 67 | ./*.operator,.single.dig +69 74 20 6e 75 6d 62 65 72 20 6f 72 20 73 69 6e 67 6c 65 20 6c 65 74 74 | it.number.or.single.lett +65 72 20 73 79 6d 62 6f 6c 20 2a 2f 0a 20 20 69 66 20 28 28 73 5b 31 5d | er.symbol.*/...if.((s[1] +20 3d 20 63 20 3d 20 67 65 74 63 68 28 29 29 20 3d 3d 20 27 20 27 20 7c | .=.c.=.getch()).==.'.'.| +7c 20 63 20 3d 3d 20 27 5c 74 27 20 7c 7c 20 63 20 3d 3d 20 27 5c 6e 27 | |.c.==.'\t'.||.c.==.'\n' +20 7c 7c 20 63 20 3d 3d 20 45 4f 46 29 20 7b 0a 20 20 20 20 75 6e 67 65 | .||.c.==.EOF).{.....unge +74 63 68 28 63 29 3b 0a 20 20 20 20 73 5b 31 5d 20 3d 20 27 5c 30 27 3b | tch(c);.....s[1].=.'\0'; +0a 20 20 20 20 63 20 3d 20 73 5b 30 5d 3b 0a 20 20 20 20 2f 2a 20 6e 75 | .....c.=.s[0];...../*.nu +6d 62 65 72 20 2a 2f 0a 20 20 20 20 69 66 20 28 69 73 64 69 67 69 74 28 | mber.*/.....if.(isdigit( +63 29 29 20 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 47 45 54 4f 50 | c)).{.......return.GETOP +5f 4e 55 4d 42 45 52 3b 0a 20 20 20 20 7d 0a 20 20 20 20 2f 2a 20 73 69 | _NUMBER;.....}...../*.si +6e 67 6c 65 20 6c 65 74 74 65 72 20 73 79 6d 62 6f 6c 20 5b 61 2d 7a 41 | ngle.letter.symbol.[a-zA +2d 5a 5d 20 2a 2f 0a 20 20 20 20 65 6c 73 65 20 69 66 20 28 28 63 20 3e | -Z].*/.....else.if.((c.> +3d 20 27 41 27 20 26 26 20 63 20 3c 3d 20 27 5a 27 29 20 7c 7c 20 28 63 | =.'A'.&&.c.<=.'Z').||.(c +20 3e 3d 20 27 61 27 20 26 26 20 63 20 3c 3d 20 27 7a 27 29 29 20 7b 0a | .>=.'a'.&&.c.<=.'z')).{. +20 20 20 20 20 20 72 65 74 75 72 6e 20 47 45 54 4f 50 5f 53 59 4d 42 4f | ......return.GETOP_SYMBO +4c 3b 0a 20 20 20 20 7d 0a 20 20 20 20 2f 2a 20 6f 70 65 72 61 74 6f 72 | L;.....}...../*.operator +20 2a 2f 0a 20 20 20 20 65 6c 73 65 20 7b 0a 20 20 20 20 20 20 72 65 74 | .*/.....else.{.......ret +75 72 6e 20 63 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 2f 2a 20 6e 75 | urn.c;.....}...}.../*.nu +6d 62 65 72 20 6f 72 20 73 79 6d 62 6f 6c 20 2a 2f 0a 20 20 65 6c 73 65 | mber.or.symbol.*/...else +20 7b 0a 20 20 20 20 75 6e 67 65 74 63 68 28 63 29 3b 0a 20 20 20 20 63 | .{.....ungetch(c);.....c +20 3d 20 73 5b 30 5d 3b 0a 20 20 20 20 2f 2a 20 6e 75 6d 62 65 72 20 2a | .=.s[0];...../*.number.* +2f 0a 20 20 20 20 69 66 20 28 69 73 64 69 67 69 74 28 63 29 20 7c 7c 20 | /.....if.(isdigit(c).||. +63 20 3d 3d 20 27 2d 27 20 7c 7c 20 63 20 3d 3d 20 27 2e 27 29 20 7b 0a | c.==.'-'.||.c.==.'.').{. +20 20 20 20 20 20 69 20 3d 20 30 3b 0a 20 20 20 20 20 20 69 66 20 28 63 | ......i.=.0;.......if.(c +20 3d 3d 20 27 2d 27 29 20 7b 20 2f 2a 20 6d 6f 76 65 20 62 79 20 61 20 | .==.'-').{./*.move.by.a. +6d 69 6e 75 73 20 28 2d 29 20 73 69 67 6e 20 2a 2f 0a 09 73 5b 2b 2b 69 | minus.(-).sign.*/..s[++i +5d 20 3d 20 63 20 3d 20 67 65 74 63 68 28 29 3b 0a 20 20 20 20 20 20 7d | ].=.c.=.getch();.......} +0a 20 20 20 20 20 20 69 66 20 28 69 73 64 69 67 69 74 28 63 29 29 20 7b | .......if.(isdigit(c)).{ +20 2f 2a 20 63 6f 6c 6c 65 63 74 20 69 6e 74 65 67 65 72 20 70 61 72 74 | ./*.collect.integer.part +20 2a 2f 0a 09 77 68 69 6c 65 20 28 69 73 64 69 67 69 74 28 73 5b 2b 2b | .*/..while.(isdigit(s[++ +69 5d 20 3d 20 63 20 3d 20 67 65 74 63 68 28 29 29 29 20 7b 0a 09 7d 0a | i].=.c.=.getch())).{..}. +20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 20 28 63 20 3d 3d 20 27 | ......}.......if.(c.==.' +2e 27 29 20 7b 20 2f 2a 20 63 6f 6c 6c 65 63 74 20 66 72 61 63 74 69 6f | .').{./*.collect.fractio +6e 20 70 61 72 74 20 2a 2f 0a 09 77 68 69 6c 65 20 28 69 73 64 69 67 69 | n.part.*/..while.(isdigi +74 28 73 5b 2b 2b 69 5d 20 3d 20 63 20 3d 20 67 65 74 63 68 28 29 29 29 | t(s[++i].=.c.=.getch())) +20 7b 0a 09 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 73 5b 69 5d | .{..}.......}.......s[i] +20 3d 20 27 5c 30 27 3b 0a 20 20 20 20 20 20 75 6e 67 65 74 63 68 28 63 | .=.'\0';.......ungetch(c +29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 47 45 54 4f 50 5f 4e 55 | );.......return.GETOP_NU +4d 42 45 52 3b 0a 20 20 20 20 7d 0a 20 20 20 20 2f 2a 20 73 79 6d 62 6f | MBER;.....}...../*.symbo +6c 20 2a 2f 0a 20 20 20 20 65 6c 73 65 20 7b 0a 20 20 20 20 20 20 69 20 | l.*/.....else.{.......i. +3d 20 30 3b 0a 20 20 20 20 20 20 77 68 69 6c 65 20 28 28 73 5b 2b 2b 69 | =.0;.......while.((s[++i +5d 20 3d 20 63 20 3d 20 67 65 74 63 68 28 29 29 20 21 3d 20 27 20 27 20 | ].=.c.=.getch()).!=.'.'. +26 26 20 63 20 21 3d 20 27 5c 74 27 20 26 26 20 63 20 21 3d 20 27 5c 6e | &&.c.!=.'\t'.&&.c.!=.'\n +27 20 26 26 20 63 20 21 3d 20 45 4f 46 29 20 7b 0a 20 20 20 20 20 20 7d | '.&&.c.!=.EOF).{.......} +0a 20 20 20 20 20 20 73 5b 69 5d 20 3d 20 27 5c 30 27 3b 0a 20 20 20 20 | .......s[i].=.'\0';..... +20 20 75 6e 67 65 74 63 68 28 63 29 3b 0a 20 20 20 20 20 20 72 65 74 75 | ..ungetch(c);.......retu +72 6e 20 47 45 54 4f 50 5f 53 59 4d 42 4f 4c 3b 0a 20 20 20 20 7d 0a 20 | rn.GETOP_SYMBOL;.....}.. +20 7d 0a 7d 0a 0a 0a 2f 2a 20 67 65 74 63 68 3a 20 67 65 74 20 61 20 28 | .}.}.../*.getch:.get.a.( +70 6f 73 73 69 62 6c 79 20 70 75 73 68 65 64 2d 62 61 63 6b 29 20 63 68 | possibly.pushed-back).ch +61 72 61 63 74 65 72 20 2a 2f 0a 69 6e 74 20 67 65 74 63 68 20 28 76 6f | aracter.*/.int.getch.(vo +69 64 29 0a 7b 0a 20 20 72 65 74 75 72 6e 20 28 62 75 66 70 20 3e 20 30 | id).{...return.(bufp.>.0 +29 20 3f 20 62 75 66 5b 2d 2d 62 75 66 70 5d 20 3a 20 67 65 74 63 68 61 | ).?.buf[--bufp].:.getcha +72 28 29 3b 0a 7d 0a 0a 0a 2f 2a 20 75 6e 67 65 74 63 68 3a 20 70 75 73 | r();.}.../*.ungetch:.pus +68 20 63 68 61 72 61 63 74 65 72 20 62 61 63 6b 20 6f 6e 20 69 6e 70 75 | h.character.back.on.inpu +74 20 2a 2f 0a 76 6f 69 64 20 75 6e 67 65 74 63 68 20 28 69 6e 74 20 63 | t.*/.void.ungetch.(int.c +29 0a 7b 0a 20 20 69 66 20 28 62 75 66 70 20 3e 3d 20 42 55 46 53 49 5a | ).{...if.(bufp.>=.BUFSIZ +45 29 20 7b 0a 20 20 20 20 70 72 69 6e 74 66 28 22 75 6e 67 65 74 63 68 | E).{.....printf("ungetch +3a 20 74 6f 6f 20 6d 61 6e 79 20 63 68 61 72 61 63 74 65 72 73 5c 6e 22 | :.too.many.characters\n" +29 3b 0a 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20 62 75 66 5b 62 75 | );...}.else.{.....buf[bu +66 70 2b 2b 5d 20 3d 20 63 3b 0a 20 20 7d 0a 7d 0a 0a 0a 2f 2a 20 70 75 | fp++].=.c;...}.}.../*.pu +73 68 3a 20 70 75 73 68 20 66 20 6f 6e 74 6f 20 73 74 61 63 6b 20 2a 2f | sh:.push.f.onto.stack.*/ +0a 76 6f 69 64 20 70 75 73 68 20 28 64 6f 75 62 6c 65 20 66 29 0a 7b 0a | .void.push.(double.f).{. +20 20 69 66 20 28 73 70 20 3c 20 4d 41 58 56 41 4c 29 20 7b 0a 20 20 20 | ..if.(sp.<.MAXVAL).{.... +20 76 61 6c 5b 73 70 2b 2b 5d 20 3d 20 66 3b 0a 20 20 7d 20 65 6c 73 65 | .val[sp++].=.f;...}.else +20 7b 0a 20 20 20 20 70 72 69 6e 74 66 28 22 65 72 72 6f 72 3a 20 73 74 | .{.....printf("error:.st +61 63 6b 20 66 75 6c 6c 2c 20 63 61 6e 27 74 20 70 75 73 68 20 25 67 5c | ack.full,.can't.push.%g\ +6e 22 2c 20 66 29 3b 0a 20 20 7d 0a 7d 0a 0a 0a 2f 2a 20 70 6f 70 3a 20 | n",.f);...}.}.../*.pop:. +70 6f 70 20 61 6e 64 20 72 65 74 75 72 6e 20 74 6f 70 20 76 61 6c 75 65 | pop.and.return.top.value +20 66 72 6f 6d 20 73 74 61 63 6b 20 2a 2f 0a 64 6f 75 62 6c 65 20 70 6f | .from.stack.*/.double.po +70 20 28 76 6f 69 64 29 0a 7b 0a 20 20 69 66 20 28 73 70 20 3e 20 30 29 | p.(void).{...if.(sp.>.0) +20 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 76 61 6c 5b 2d 2d 73 70 5d 3b | .{.....return.val[--sp]; +0a 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20 70 72 69 6e 74 66 28 22 | ...}.else.{.....printf(" +65 72 72 6f 72 3a 20 73 74 61 63 6b 20 65 6d 70 74 79 5c 6e 22 29 3b 0a | error:.stack.empty\n");. +20 20 20 20 72 65 74 75 72 6e 20 30 2e 30 3b 0a 20 20 7d 0a 7d 0a 0a 0a | ....return.0.0;...}.}... +2f 2a 20 70 65 65 6b 3a 20 72 65 74 75 72 6e 20 74 6f 70 20 76 61 6c 75 | /*.peek:.return.top.valu +65 20 66 72 6f 6d 20 73 74 61 63 6b 20 77 69 74 68 6f 75 74 20 70 6f 70 | e.from.stack.without.pop +70 69 6e 67 20 2a 2f 0a 64 6f 75 62 6c 65 20 70 65 65 6b 20 28 76 6f 69 | ping.*/.double.peek.(voi +64 29 0a 7b 0a 20 20 69 66 20 28 73 70 20 3e 20 30 29 20 7b 0a 20 20 20 | d).{...if.(sp.>.0).{.... +20 72 65 74 75 72 6e 20 76 61 6c 5b 73 70 20 2d 20 31 5d 3b 0a 20 20 7d | .return.val[sp.-.1];...} +20 65 6c 73 65 20 7b 0a 20 20 20 20 70 72 69 6e 74 66 28 22 65 72 72 6f | .else.{.....printf("erro +72 3a 20 73 74 61 63 6b 20 65 6d 70 74 79 5c 6e 22 29 3b 0a 20 20 20 20 | r:.stack.empty\n");..... +72 65 74 75 72 6e 20 30 2e 30 3b 0a 20 20 7d 0a 7d 0a 0a 0a 2f 2a 20 63 | return.0.0;...}.}.../*.c +6c 65 61 72 3a 20 63 6c 65 61 72 20 74 68 65 20 73 74 61 63 6b 20 2a 2f | lear:.clear.the.stack.*/ +0a 76 6f 69 64 20 63 6c 65 61 72 20 28 76 6f 69 64 29 0a 7b 0a 20 20 73 | .void.clear.(void).{...s +70 20 3d 20 30 3b 0a 7d 0a 0a 0a 2f 2a 20 73 65 74 5f 76 61 72 3a 20 73 | p.=.0;.}.../*.set_var:.s +65 74 20 76 61 72 69 61 62 6c 65 20 2a 2f 0a 76 6f 69 64 20 73 65 74 5f | et.variable.*/.void.set_ +76 61 72 20 28 63 68 61 72 20 76 61 72 5f 6e 61 6d 65 2c 20 64 6f 75 62 | var.(char.var_name,.doub +6c 65 20 76 61 6c 75 65 29 0a 7b 0a 20 20 76 61 72 73 5b 76 61 72 5f 6e | le.value).{...vars[var_n +61 6d 65 2d 27 41 27 5d 20 3d 20 76 61 6c 75 65 3b 0a 7d 0a 0a 0a 2f 2a | ame-'A'].=.value;.}.../* +20 67 65 74 5f 76 61 72 3a 20 67 65 74 20 76 61 72 69 61 62 6c 65 20 76 | .get_var:.get.variable.v +61 6c 75 65 20 2a 2f 0a 64 6f 75 62 6c 65 20 67 65 74 5f 76 61 72 20 28 | alue.*/.double.get_var.( +63 68 61 72 20 76 61 72 5f 6e 61 6d 65 29 0a 7b 0a 20 20 72 65 74 75 72 | char.var_name).{...retur +6e 20 76 61 72 73 5b 76 61 72 5f 6e 61 6d 65 2d 27 41 27 5d 3b 0a 7d 0a | n.vars[var_name-'A'];.}. +0a 0a 2f 2a 20 73 65 74 5f 6c 61 73 74 3a 20 73 65 74 20 6c 61 73 74 20 | ../*.set_last:.set.last. +72 65 74 75 72 6e 65 64 20 76 61 6c 75 65 20 2a 2f 0a 64 6f 75 62 6c 65 | returned.value.*/.double +20 73 65 74 5f 6c 61 73 74 20 28 64 6f 75 62 6c 65 20 76 61 6c 75 65 29 | .set_last.(double.value) +0a 7b 0a 20 20 72 65 74 75 72 6e 20 6c 61 73 74 5f 76 61 6c 20 3d 20 76 | .{...return.last_val.=.v +61 6c 75 65 3b 0a 7d 0a 0a 0a 2f 2a 20 67 65 74 5f 6c 61 73 74 3a 20 67 | alue;.}.../*.get_last:.g +65 74 20 6c 61 73 74 20 72 65 74 75 72 6e 65 64 20 76 61 6c 75 65 20 2a | et.last.returned.value.* +2f 0a 64 6f 75 62 6c 65 20 67 65 74 5f 6c 61 73 74 20 28 76 6f 69 64 29 | /.double.get_last.(void) +0a 7b 0a 20 20 72 65 74 75 72 6e 20 6c 61 73 74 5f 76 61 6c 3b 0a 7d 0a | .{...return.last_val;.}. diff --git a/chapter_7.input_output/7_2.print_input/tests/2_bin_rand.tin b/chapter_7.input_output/7_2.print_input/tests/2_bin_rand.tin new file mode 100644 index 0000000..e1a1400 --- /dev/null +++ b/chapter_7.input_output/7_2.print_input/tests/2_bin_rand.tin @@ -0,0 +1 @@ +.\6Οp?QAzF KWjQ߀ mnqk7S˟ FM\3 Xs+}"E03CE!Ry0.?hdʨUn3h:U(-b&b51m< \ No newline at end of file diff --git a/chapter_7.input_output/7_2.print_input/tests/2_bin_rand.tout b/chapter_7.input_output/7_2.print_input/tests/2_bin_rand.tout new file mode 100644 index 0000000..afe335c --- /dev/null +++ b/chapter_7.input_output/7_2.print_input/tests/2_bin_rand.tout @@ -0,0 +1,6 @@ +ec 07 eb 0e 19 bb f1 2e 5c 7f 1d ee 36 ce 9f 17 70 3f f3 51 e8 41 83 7a | ........\...6...p?.Q.A.z +e0 46 ae d9 0c 4b 9d 7f ff 57 6a 51 9b c4 f4 a8 df 80 09 90 6d cf 6e ad | .F...K...WjQ........m.n. +b2 71 6b e3 17 37 53 b3 1f cb 9f f9 1b ea c3 09 46 89 4d ab 5c 33 0d 58 | .qk..7S.........F.M.\3.X +8c e1 d2 1a 73 1c 2b 9a 99 7d 22 45 30 33 02 43 45 21 f9 52 79 30 ac 2e | ....s.+..}"E03.CE!.Ry0.. +ea 3f f6 68 64 ca a8 ea 55 6e 33 68 b5 3a 55 d2 28 b2 2d 01 62 ac 26 9c | .?.hd...Un3h.:U.(.-.b.&. +62 a6 35 91 dc 31 6d 3c | b.5..1m< diff --git a/chapter_7.input_output/7_2.print_input/tests/3_bin_rand_longer.tin b/chapter_7.input_output/7_2.print_input/tests/3_bin_rand_longer.tin new file mode 100644 index 0000000000000000000000000000000000000000..912dcadee3a39f011b73270267adf6f6b03847d3 GIT binary patch literal 1024 zcmV+b1poV+|68__CRWLe2kI!c#kQ2%G<$LFIb${ zuy5vIhgR2(1>#P!IT8r}D%Xv_^3AHBsYto?Q8%3O?EWXzo~{@^%Wk|no+^-bE}#@g z3?$@0pdFBl5V+?12phDU8}fVyf(_%c-QoY17Xx}bKRp02fKo`I7WbQ9{Yk}YAC0tq z#93Ffnc_@Kv{c6nOA;hma-ud)Gc+V%F1_``mJb`5fC86xVz=M#Bz&$vDGS)V&#`l= z;^W=I8%%Oh`$1Bzw^vM!%JbTMY_a55P#UIyL2+#_P4yWjL9TOy1>1I+0KR>_jiPcg zM4jPEn4b{q?qWn42d6*5*j&Ua((4^JPcU|%si6{$ia z?2b0J-XP(4M7(Sz4=CgCZqAjxY73p<_%e24fawKozuvqaBET;^Q#3;Qv!A!J#h_X? zICw9@=D@A0{wTa$5;8X2aY~?XTT*2dz)nW&&7KS%_YzDrZ;m6q4iz`@c6?FB=zeq= zL*_4RXh(#Hz{=CqMA6I?Eg3-&M6;j=^{$Z(PUYBGyenPVUTBbf3+hzf(NeV5-)BT+ zRG(F5kUj+P3$2+TilJ%1uUYR z4ah-0Q*u?4v}^=;)NF(csyOPWk!Pvgh<*8rV^CJ1((HHi349)kK&vgU!F5S!(RLRn zLY>p1RLYE1pG|B(BrPmB3>f3XO~&f}V$+04Fme~l|5((m+3*b-%eFqo4r}Ei1`<^} zU0EsG_+xn9tm;3Kk8Hg!4R)QBj=3;Ko5b2NngjDe5bDdd$d=?dBs-w-FTo%<=uYX4^?QITRQrFISEgekBFyYRq#R zIwCJu=oR(lMn5b4a5;O+D>=hsM@NeL;W6KSj=0)X45C0r2ek&6zCgW7A(vp;;@h`l zNYoMn8`1AmaHD8${)B@FbK(*(n&nD5rIhza%(1sR#s0)kgjMAVJ%nJxN)v.n.:.*.v... +47 0c 24 e4 40 a0 1d 90 8b 10 b8 e6 fb 08 1b b4 9b 1b f2 7c 07 82 0d e3 | G.$.@..............|.... +b2 dd e1 ff 96 17 03 7a 3b 3f 3d 00 30 80 52 48 a1 16 f7 9b 5f fd 49 c5 | .......z;?=.0.RH...._.I. +6a 1f 8d b4 7d c4 59 57 b3 99 e2 4c 4b b4 54 c7 0b 4b 12 24 59 72 a2 36 | j...}.YW...LK.T..K.$Yr.6 +4e 33 34 24 60 2e bd f5 c3 96 0f 1b 99 80 02 97 76 62 b7 df ef 24 7c ae | N34$`...........vb...$|. +3f 29 0b d8 bc cf b1 73 aa e2 e3 dd c2 1b 4c 72 51 fb 41 52 ad b7 57 4c | ?).....s......LrQ.AR..WL +8d ca f3 da 7c 6c b1 e4 57 50 1a a6 80 41 71 6d 2f 4d f5 19 26 41 ae 73 | ....|l..WP...Aqm/M..&A.s +83 05 db 76 99 00 be 7d bd 8d a2 72 32 44 a0 f3 42 b2 14 5d 5f 65 5b 64 | ...v...}...r2D..B..]_e[d +5b e6 bb e1 e7 21 35 2e 1c 8c ef 33 a9 76 1c 0b 96 65 5c 33 32 15 32 56 | [....!5....3.v...e\32.2V +3a ce 77 63 ee 0f 4f 1f a8 60 5f 31 3b 15 a9 42 23 ec 8e 36 b6 de 20 e1 | :.wc..O..`_1;..B#..6.... +78 44 bc 6c 25 0f 28 e3 f0 6e ce 95 bd 6a 0b 9d e0 f8 32 76 62 80 e9 05 | xD.l%.(..n...j....2vb... +6d bf de bc 1e 22 c0 2f 3c 53 34 42 fa b3 9f b7 b2 c5 a0 5a 36 38 78 2f | m...."./........l.... +ea 2b cc 7f 19 7a be ed d8 9d 4e 39 0f 68 97 2f c0 e2 95 63 a0 b1 29 12 | .+...z....N9.h./...c..). +4c 22 f2 0b e8 96 3a 9a d0 d4 ba 25 48 76 bb 4b 97 23 be 56 05 17 8d e6 | L"....:....%Hv.K.#.V.... +ba 83 df da 90 42 95 d2 37 26 e1 a8 5e a8 b2 4b 66 be 48 e9 c5 de 84 93 | .....B..7&..^..Kf.H..... +70 95 e3 fa d5 3a e2 15 64 05 2c a2 9c 0d c8 41 3e 53 72 55 93 b4 6c 04 | p....:..d.,....A>SrU..l. +78 d4 6c 84 0b aa 38 ea a7 91 67 a9 dc 88 7d f9 8a 63 50 56 a1 d2 ec 77 | x.l...8...g...}..cPV...w +f4 09 7c 1e 8a 40 ab 2d af c1 75 49 68 d1 76 17 27 42 9d d3 a2 54 ca 8c | ..|..@.-..uIh.v.'B...T.. +55 9f 4d 6c 3f 24 2d 2c 38 0c 18 e3 c3 4d c6 ea fe 62 d3 84 49 30 72 17 | U.Ml?$-,8....M...b..I0r. +ca ff 58 d4 ad d9 f0 0d 1a cb b6 3e c6 0e 6b e5 22 06 12 55 3b 5d 59 29 | ..X........>..k."..U;]Y) +da f8 63 78 de ac ea 3f 92 8f 6c bd 2f 0d 76 9d 94 8e b9 30 46 9b c4 da | ..cx...?..l./.v....0F... +31 9a 03 f3 41 10 ea cb b5 c8 96 e4 5a 56 35 b0 a7 cd 2e d2 a9 1d 7f 3b | 1...A.......ZV5........; +8a c5 fe f1 d1 b9 71 0d 42 95 34 b5 8d 43 88 90 81 c9 d3 ac b8 29 9a 8d | ......q.B.4..C.......).. +ec 16 c1 2c e3 6a 52 77 88 52 78 14 48 da 47 1c 52 09 b5 56 76 4a 2b 61 | ...,.jRw.Rx.H.G.R..VvJ+a +a1 81 fd 0c 0e f4 fd ff c5 e3 93 84 bf e2 3a f5 1d a2 37 a9 ab 67 58 ab | ..............:...7..gX. +54 ea c9 9e 9e b7 11 14 ff cc f1 fd ba 66 db 49 39 39 14 18 25 2f 57 8e | T............f.I99..%/W. +15 7e 25 05 e8 6a cc 73 1b 3a 22 2f 57 e8 15 f5 e5 46 3f 2b fd 70 39 7b | .~%..j.s.:"/W....F?+.p9{ +cb 2b 39 c3 63 47 47 8a fb e1 31 df 7e 8e b8 da 54 0c a2 40 46 07 b5 06 | .+9.cGG...1.~...T..@F... +98 be 40 bd 4a 21 97 60 d9 e2 db b7 63 48 d4 12 02 1b d1 ef 53 70 a3 68 | ..@.J!.`....cH......Sp.h +6e fe 84 83 08 73 e2 12 30 9a e5 4a 3a a5 94 f7 47 cc b1 b7 3a c5 fe c4 | n....s..0..J:...G...:... +4f 84 55 e5 0a 3d 84 60 c3 4a 13 83 e1 6c 32 5d fd 39 46 bd 69 4e de c2 | O.U..=.`.J...l2].9F.iN.. +7c a2 86 58 1e 7a 97 7d 87 45 5e 2f c8 c2 e3 a9 b6 3b 63 81 12 7a 02 6f | |..X.z.}.E^/.....;c..z.o +1c b5 9d fd 3a 0d 97 a7 8b 94 32 c9 d7 55 51 e5 2b 85 a1 75 26 bd 32 d7 | ....:.....2..UQ.+..u&.2. +5f a8 a4 3b 00 28 41 a9 b6 70 51 61 65 09 a7 0b 26 fe 8a ac 0f e8 93 28 | _..;.(A..pQae...&......( +da f6 6b 9a 16 fc 5b b9 aa bb 34 a7 b0 fb b3 0b | ..k...[...4.....