|
| 1 | +begin |
| 2 | + procedure usage; |
| 3 | + begin |
| 4 | + outstring(1, "Usage: please input a non-negative integer\n"); |
| 5 | + stop |
| 6 | + end usage; |
| 7 | + |
| 8 | + comment Input a digit character from stdin and return the following: |
| 9 | + - "0" to "9" maps to 0 to 9 |
| 10 | + - "+" maps to 10 |
| 11 | + - "-" maps to 11 |
| 12 | + - whitespace maps to 12 |
| 13 | + - comma maps to 13 |
| 14 | + - null byte maps to -1 |
| 15 | + - invalid bytes map to -2; |
| 16 | + integer procedure indigit; |
| 17 | + begin |
| 18 | + comment Mapping: |
| 19 | + - "0" to "9" maps to 1 to 10 |
| 20 | + - "+" maps to 11 |
| 21 | + - "-" maps to 12 |
| 22 | + - "," mapps to 13 |
| 23 | + - "\t" maps to 14 |
| 24 | + - "\r" maps to 15 |
| 25 | + - "\n" maps to 16 |
| 26 | + - " " maps to 17 |
| 27 | + - null byte maps to 18 |
| 28 | + - invalid byte maps 0; |
| 29 | + integer ch; |
| 30 | + inchar(0, "0123456789+-,\t\r\n ", ch); |
| 31 | + if ch < 1 then ch := -2 |
| 32 | + else if ch < 14 then ch := ch - 1 |
| 33 | + else if ch < 18 then ch := 12 |
| 34 | + else ch := -1; |
| 35 | + indigit := ch |
| 36 | + end indigit; |
| 37 | + |
| 38 | + comment Input an integer from stdin into 'result' and parse it. |
| 39 | + The last character is read into 'ch'. |
| 40 | + return true if integer is valid, false otherwise; |
| 41 | + boolean procedure inValidInteger(result, ch); |
| 42 | + integer result, ch; |
| 43 | + begin |
| 44 | + boolean valid; |
| 45 | + integer s; |
| 46 | + |
| 47 | + result := 0; |
| 48 | + valid := false; |
| 49 | + s := 1; |
| 50 | + |
| 51 | + comment Ignore whitespace; |
| 52 | + for ch := indigit while ch = 12 do; |
| 53 | + |
| 54 | + comment Process signs: ignore "+" and invert sign if "-"; |
| 55 | + signloop: |
| 56 | + if ch = 10 | ch = 11 then |
| 57 | + begin |
| 58 | + if ch = 11 then s := -s; |
| 59 | + ch := indigit; |
| 60 | + goto signloop |
| 61 | + end; |
| 62 | + |
| 63 | + comment Process digits: update value; |
| 64 | + valueloop: |
| 65 | + if ch >= 0 & ch <= 9 then |
| 66 | + begin |
| 67 | + comment Invalid if overflow or underflow; |
| 68 | + valid := false; |
| 69 | + if (s > 0 & (maxint - ch) % 10 < result) | |
| 70 | + (s < 0 & (-1 - maxint + ch) % 10 > result) then goto done; |
| 71 | + |
| 72 | + result := result * 10 + s * ch; |
| 73 | + ch := indigit; |
| 74 | + valid := true; |
| 75 | + goto valueloop |
| 76 | + end; |
| 77 | + |
| 78 | + comment Ignore whitespace; |
| 79 | + for ch := ch while ch = 12 do ch := indigit; |
| 80 | + |
| 81 | + done: |
| 82 | + inValidInteger := valid |
| 83 | + end inValidInteger; |
| 84 | + |
| 85 | + integer procedure mod(x, n); |
| 86 | + value x, n; |
| 87 | + integer x, n; |
| 88 | + begin |
| 89 | + mod := x - n * (x % n) |
| 90 | + end mod; |
| 91 | + |
| 92 | + boolean procedure isPalindromicNumber(x); |
| 93 | + value x; |
| 94 | + integer x; |
| 95 | + begin |
| 96 | + integer xrev, temp; |
| 97 | + |
| 98 | + comment Reverse the passed in value; |
| 99 | + xrev := 0; |
| 100 | + for temp := x, temp % 10 while temp > 0 do |
| 101 | + xrev := xrev * 10 + mod(temp, 10); |
| 102 | + |
| 103 | + isPalindromicNumber := x = xrev |
| 104 | + end isPalindromicNumber; |
| 105 | + |
| 106 | + integer argc, result, ch; |
| 107 | + |
| 108 | + comment Get number of parameters. Exit if too few; |
| 109 | + ininteger(0, argc); |
| 110 | + if argc < 1 then usage; |
| 111 | + |
| 112 | + comment Get integer value from 1st argument. Exit if invalid, not |
| 113 | + end of argument, or negative; |
| 114 | + if !inValidInteger(result, ch) | ch != -1 | result < 0 then usage; |
| 115 | + |
| 116 | + comment Output "true" if number is palindromic number, "false" otherwise; |
| 117 | + if isPalindromicNumber(result) then outstring(1, "true") |
| 118 | + else outstring(1, "false") |
| 119 | +end |
0 commit comments