summaryrefslogtreecommitdiff
path: root/projects/12/String.jack
diff options
context:
space:
mode:
Diffstat (limited to 'projects/12/String.jack')
-rw-r--r--projects/12/String.jack43
1 files changed, 42 insertions, 1 deletions
diff --git a/projects/12/String.jack b/projects/12/String.jack
index 2d5c440..398fdfe 100644
--- a/projects/12/String.jack
+++ b/projects/12/String.jack
@@ -47,6 +47,10 @@ class String {
/** Sets the character at the j-th location of this string to c. */
method void setCharAt(int j, char c) {
+ if ((j < 0) | (j + 1 > len)){
+ Output.printString("String.setCharAt: index out of range!");
+ Sys.error(5);
+ }
let s[j] = c;
return;
}
@@ -110,7 +114,44 @@ class String {
}
/** Sets this string to hold a representation of the given value. */
- method void setInt(int val) {
+ method void setInt(int val) { //change Output.printInt after this
+ var int l;
+ var boolean neg;
+ if (val < 0) {
+ let neg = true;
+ len = 2;
+ } else {
+ let neg = false;
+ len = 1;
+ }
+ let x = Math.abs(val);
+ if (x > 9999) {
+ let len = len + 4;
+ } else { if (x > 999) {
+ let len = len + 3;
+ } else { if (x > 99) {
+ let len = len + 2;
+ } else { if (x > 9) {
+ let len = len + 1;
+ }}}}
+ if (len > maxLen) {
+ Output.printString("String.setInt: val is too big for the string!");
+ Sys.error(5);
+ }
+ if (x = 0) {
+ setCharAt(0, 48);
+ return;
+ }
+ if (neg) {
+ setCharAt(0, 45);
+ }
+ let i = len - 1;
+ while (x > 0) {
+ let y = x / 10;
+ setCharAt(i, x - (y * 10) + 48);
+ let x = y;
+ }
+ return;
}
/** Returns the new line character. */