Commit Diff


commit - a2ff55164aebbb5f63408939f740dc185eedb30e
commit + c13fd43ed6d961207b73d4e212e31989b365f730
blob - /dev/null
blob + f3411ed502dd63e0b63530048872059843bbdf2d (mode 644)
--- /dev/null
+++ 3-5-input
@@ -0,0 +1,21 @@
+20345 16 4F79
+26763 8 64213
+17903 2 100010111101111
+20032 8 47100
+28554 16 6F8A
+12840 10 12840
+1146 3 1120110
+25575 5 1304300
+24329 9 36332
+21646 6 244114
+12846 7 52311
+7522 23 E51
+26022 26 1CCM
+5034 36 3VU
+17756 15 53DB
+23943 14 8A23
+30962 28 1BDM
+19240 1 -1
+29529 37 -1
+13709 40 -1
+5872 4 1123300
blob - /dev/null
blob + 3440e63366853f3b1b4f060bad427cc3f2d5c6d3 (mode 644)
--- /dev/null
+++ 3-5-output
@@ -0,0 +1,21 @@
+Base10: 20345, Base16: 4f79, expect: 4F79
+Base10: 26763, Base8: 64213, expect: 64213
+Base10: 17903, Base2: 100010111101111, expect: 100010111101111
+Base10: 20032, Base8: 47100, expect: 47100
+Base10: 28554, Base16: 6f8a, expect: 6F8A
+Base10: 12840, Base10: 12840, expect: 12840
+Base10: 1146, Base3: 1120110, expect: 1120110
+Base10: 25575, Base5: 1304300, expect: 1304300
+Base10: 24329, Base9: 36332, expect: 36332
+Base10: 21646, Base6: 244114, expect: 244114
+Base10: 12846, Base7: 52311, expect: 52311
+Base10: 7522, Base23: e51, expect: E51
+Base10: 26022, Base26: 1ccm, expect: 1CCM
+Base10: 5034, Base36: 3vu, expect: 3VU
+Base10: 17756, Base15: 53db, expect: 53DB
+Base10: 23943, Base14: 8a23, expect: 8A23
+Base10: 30962, Base28: 1bdm, expect: 1BDM
+Base10: 19240, Base1: -1, expect: -1
+Base10: 29529, Base37: -1, expect: -1
+Base10: 13709, Base40: -1, expect: -1
+Base10: 5872, Base4: 1123300, expect: 1123300
blob - /dev/null
blob + 1b2d0250d3f77a68938f85715796550d3c8c7c64 (mode 644)
--- /dev/null
+++ 3-5.c
@@ -0,0 +1,70 @@
+/* 3-5 Write the function itob(n,s,b) that converts the integer n into a base b
+ * character representation in the string s. In particular, itob(n,s,16)
+ * formats n as a hexadecimal integer in s. */
+
+#include <stdio.h>
+#include <string.h>
+
+#define MAXCHARS 1000		/* maximum input line size */
+
+int itob(int n, char s[], int b);
+void reverse(char s[]);
+
+int main() {
+	int n, b;
+	char s[MAXCHARS];
+	char expect[MAXCHARS];
+
+	while (scanf("%d %d %s\n", &n, &b, expect) == 3) {
+		if (itob(n,s,b) < 0) {
+			printf("Base10: %d, Base%d: -1, expect: %s\n", n, b, expect);
+		} else {
+			printf("Base10: %d, Base%d: %s, expect: %s\n", n, b, s, expect);
+		}
+	}
+
+	return 0;
+}
+
+/* itob: return base b representation of the integer n in the string s.
+ * itob(n,s,16) formats n as a hexadecimal integer. Return length of s
+ * or -1 upon error */
+
+int itob(int n, char s[], int b) {
+	char digits[] = "0123456789abcdefghijklmnopqrstuvwxyz";
+	int i = 0;
+	if (b < 2 || b > 36)
+		return -1;
+	do {
+		int digit = n%b;
+		s[i++] = digits[digit];
+		n -= digit;
+	} while ((n /= b) > 0);
+	s[i] = '\0';
+	reverse(s);
+	return i;
+}
+
+/* reverse: reverse string s in place */
+void reverse(char s[]) {
+	int c, i, j;
+	for (i = 0, j = strlen(s)-1; i < j; i++, j--) {
+		c = s[i], s[i] = s[j], s[j] = c;
+	}
+}
+/*
+1000
+1000%16 = 8
+8
+1000-8 = 992;
+992/16 = 62
+62%16 = 14
+d8
+62 - 14 = 48
+
+16*6 = 96
+100 - 96
+4
+0x64
+100/16 = 6
+*/