k&r : excercise solutions to chapter 3
parent
abccef71d3
commit
2d2e6880a2
|
@ -0,0 +1,69 @@
|
|||
#include <stdio.h>
|
||||
|
||||
#define SIZE 1000000
|
||||
#define X 2*((SIZE-1) / 2) + 1
|
||||
#define T 10000000
|
||||
|
||||
int binsearch(int x, int v[], int n);
|
||||
int binsearch_old(int x, int v[], int n);
|
||||
|
||||
/* run binsearch on array of size SIZE
|
||||
* if arg is provided use old binsearch */
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
long i;
|
||||
int arr[SIZE];
|
||||
|
||||
for (i = 0; i < SIZE; ++i)
|
||||
arr[i] = 2 * i;
|
||||
|
||||
if (argc == 1) {
|
||||
for (i = 0; i < T; ++i)
|
||||
binsearch(X, arr, SIZE);
|
||||
printf("binsearch : %d\n", binsearch(X, arr, SIZE));
|
||||
}
|
||||
else {
|
||||
for (i = 0; i < T; ++i)
|
||||
binsearch_old(X, arr, SIZE);
|
||||
printf("old binsearch : %d\n", binsearch_old(X, arr, SIZE));
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* binsearch: find x in sorted v of length n, using one comparison in loop */
|
||||
int binsearch(int x, int v[], int n)
|
||||
{
|
||||
int low, high, mid;
|
||||
|
||||
low = 0;
|
||||
high = n - 1;
|
||||
while (low < high) {
|
||||
mid = (low + high) / 2;
|
||||
if (x <= v[mid])
|
||||
high = mid;
|
||||
else
|
||||
low = mid + 1;
|
||||
}
|
||||
if (v[low] == x)
|
||||
return low;
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* binsearch: find x in sorted v of length n, using two comparisons in loop */
|
||||
int binsearch_old(int x, int v[], int n)
|
||||
{
|
||||
int low, high, mid;
|
||||
|
||||
low = 0;
|
||||
high = n - 1;
|
||||
while (low <= high) {
|
||||
mid = (low + high) / 2;
|
||||
if (x < v[mid])
|
||||
high = mid - 1;
|
||||
else if (x > v[mid])
|
||||
low = mid + 1;
|
||||
else
|
||||
return mid;
|
||||
}
|
||||
return -1;
|
||||
}
|
|
@ -0,0 +1,64 @@
|
|||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#define ESCAPE_SEQS \
|
||||
X('\a', 'a') X('\b', 'b') X('\f', 'f') X('\n', 'n') \
|
||||
X('\r', 'r') X('\t', 't') X('\v', 'v')
|
||||
|
||||
#define SIZE 1000
|
||||
|
||||
void escape(char *s, char *t);
|
||||
void unescape(char *s, char *t);
|
||||
|
||||
/* runs escape and unescape on stdin, prints results*/
|
||||
int main()
|
||||
{
|
||||
int i, c;
|
||||
char s[SIZE+1], t[2*SIZE+1];
|
||||
|
||||
for (i = 0; (c = getchar()) != EOF; i++) {
|
||||
if (i >= SIZE) {
|
||||
printf("warning : buffer overflow\n");
|
||||
break;
|
||||
}
|
||||
s[i] = c;
|
||||
}
|
||||
s[i] = '\0';
|
||||
escape(s, t);
|
||||
printf("--- escaped ---\n%s\n", t);
|
||||
|
||||
unescape(t, s);
|
||||
printf("\n-- unescaped --\n%s", s);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* escape: makes escape sequences visible as it copies s to t
|
||||
* size of t must be 2*size of s */
|
||||
void escape(char *s, char *t)
|
||||
{
|
||||
for (; *s != '\0'; s++, t++)
|
||||
switch (*s) {
|
||||
#define X(e, c) case e : *(t++) = '\\'; *t = c; break;
|
||||
ESCAPE_SEQS
|
||||
#undef X
|
||||
default : *t = *s; break;
|
||||
}
|
||||
*t = '\0';
|
||||
}
|
||||
|
||||
/* unescape: inverse of escape */
|
||||
void unescape(char *s, char *t)
|
||||
{
|
||||
for (; *s != '\0'; s++, t++) {
|
||||
if (*s == '\\')
|
||||
switch (*(s+1)) {
|
||||
#define X(e, c) case c : *t = e; s++; continue;
|
||||
ESCAPE_SEQS
|
||||
#undef X
|
||||
}
|
||||
*t = *s;
|
||||
}
|
||||
*t = '\0';
|
||||
}
|
||||
|
||||
|
|
@ -0,0 +1,75 @@
|
|||
#include <stdio.h>
|
||||
#include <ctype.h>
|
||||
|
||||
#define SIZE 1000
|
||||
|
||||
/* check if c1-c2 is valid range */
|
||||
#define IS_RANGE(c1, c2) ( \
|
||||
(isdigit(c1) && isdigit(c2)) || \
|
||||
(islower(c1) && islower(c2)) || \
|
||||
(isupper(c1) && isupper(c2)) \
|
||||
)
|
||||
|
||||
int getstr(char *s, int n);
|
||||
void expand(char *s, char *t, int n);
|
||||
|
||||
/* copy stdin to string, run expand, print output */
|
||||
int main()
|
||||
{
|
||||
char s[SIZE], t[SIZE];
|
||||
if (getstr(s, SIZE) < 0)
|
||||
printf("warning : overlfow on input\n");
|
||||
expand(s, t, SIZE);
|
||||
printf("%s\n", t);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* macro for checking overlfow of t in expand(s, t, n) */
|
||||
#define CHECK_OVERFLOW(i, n) \
|
||||
do { \
|
||||
if (i >= n-1) { \
|
||||
printf("warning : overflow in expand\n"); \
|
||||
goto terminate; \
|
||||
} \
|
||||
} while (0)
|
||||
|
||||
/* expand: expand a-z from s to to abc...xyz in t (of size n), same for
|
||||
* digits and uppercase letters */
|
||||
void expand(char *s, char *t, int n)
|
||||
{
|
||||
int i = 0;
|
||||
if (*s == '\0')
|
||||
goto terminate;
|
||||
|
||||
int c;
|
||||
for (t[i++] = *(s++); *s != '\0'; s++, i++) {
|
||||
if (*s == '-' && IS_RANGE(*(s-1), *(s+1))) {
|
||||
for (c = *(s-1) + 1; c < *(s+1); c++, i++) {
|
||||
CHECK_OVERFLOW(i, n);
|
||||
t[i] = c;
|
||||
}
|
||||
s++;
|
||||
}
|
||||
CHECK_OVERFLOW(i, n);
|
||||
t[i] = *s;
|
||||
}
|
||||
terminate:
|
||||
t[i] = '\0';
|
||||
}
|
||||
|
||||
/* getstr: copies stdin to array s of length n, returns size of string or
|
||||
* -1 if overflow */
|
||||
int getstr(char *s, int n)
|
||||
{
|
||||
int i, c;
|
||||
|
||||
for (i = 0; (c = getchar()) != EOF; i++) {
|
||||
if (i >= n-1) {
|
||||
s[i] = '\0';
|
||||
return -1;
|
||||
}
|
||||
s[i] = c;
|
||||
}
|
||||
s[i] = '\0';
|
||||
return i;
|
||||
}
|
|
@ -0,0 +1,51 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <math.h>
|
||||
#include <string.h>
|
||||
#include <limits.h>
|
||||
|
||||
|
||||
void itoa(int n, char s[]);
|
||||
|
||||
/* passes arg to itoa or INT_MIN if no arg is given */
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
int n;
|
||||
if (argc < 2) {
|
||||
printf("expected : %d\n", INT_MIN);
|
||||
n = INT_MIN;
|
||||
} else
|
||||
n = atoi(argv[1]);
|
||||
|
||||
char s[(int)log10(INT_MAX) + 3];
|
||||
itoa(n, s);
|
||||
printf("%s\n", s);
|
||||
return 0;
|
||||
}
|
||||
|
||||
void reverse(char *s);
|
||||
|
||||
/* itoa: convert n to characters in s */
|
||||
void itoa(int n, char s[])
|
||||
{
|
||||
int i;
|
||||
unsigned absn = (n < 0) ? -n : n; /* handles INT_MIN */
|
||||
|
||||
i = 0;
|
||||
do {
|
||||
s[i++] = absn % 10 + '0';
|
||||
} while ((absn /= 10) > 0);
|
||||
|
||||
if (n < 0)
|
||||
s[i++] = '-';
|
||||
s[i] = '\0';
|
||||
reverse(s);
|
||||
}
|
||||
|
||||
/* reverse: reverses string s in place */
|
||||
void reverse(char *s)
|
||||
{
|
||||
char c, *s2;
|
||||
for (s2 = s + strlen(s) - 1; s < s2; s++, s2--)
|
||||
c = *s, *s = *s2, *s2 = c;
|
||||
}
|
|
@ -0,0 +1,53 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <limits.h>
|
||||
#include <math.h>
|
||||
#include <string.h>
|
||||
|
||||
void itob(int n, char *s, unsigned b);
|
||||
|
||||
/* passes args to itob, n defaults to INT_MIN, base defaults to 10 */
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
int n, b;
|
||||
n = (argc > 1) ? atoi(argv[1]) : INT_MIN;
|
||||
b = (argc > 2) ? atoi(argv[2]) : 10;
|
||||
if (b <= 0) {
|
||||
printf("error : invalid base %d\n", b);
|
||||
return 1;
|
||||
}
|
||||
|
||||
char s[(int)log10(INT_MAX) + 3]; /* +3 for sign and \0 */
|
||||
itob(n, s, b);
|
||||
printf("%s\n", s);
|
||||
return 0;
|
||||
}
|
||||
|
||||
void reverse(char *s);
|
||||
|
||||
/* itob: converts int n to string in base b and writes it to s */
|
||||
void itob(int n, char *s, unsigned b)
|
||||
{
|
||||
unsigned un;
|
||||
if (n < 0) {
|
||||
un = -n;
|
||||
*(s++) = '-';
|
||||
} else
|
||||
un = n;
|
||||
|
||||
char d, *sc = s;
|
||||
do {
|
||||
d = un % b;
|
||||
*(sc++) = d + ((d > 9) ? 'a' - 10 : '0');
|
||||
} while ((un /= b) > 0);
|
||||
*sc = '\0';
|
||||
reverse(s);
|
||||
}
|
||||
|
||||
/* reverse: reverses string s in place */
|
||||
void reverse(char *s)
|
||||
{
|
||||
char c, *s2;
|
||||
for (s2 = s + strlen(s) - 1; s < s2; s++, s2--)
|
||||
c = *s, *s = *s2, *s2 = c;
|
||||
}
|
|
@ -0,0 +1,62 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <math.h>
|
||||
#include <string.h>
|
||||
#include <limits.h>
|
||||
|
||||
|
||||
void itoa(int n, char s[], unsigned w);
|
||||
|
||||
/* passes args to itoa or INT_MIN if no arg is given */
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
int n, w;
|
||||
n = (argc > 1) ? atoi(argv[1]) : INT_MIN;
|
||||
w = (argc > 2) ? atoi(argv[2]) : (int)log10(abs(n)) + 1 + (n < 0);
|
||||
|
||||
if (w < 0) {
|
||||
printf("error : invalid width %d\n", w);
|
||||
return 1;
|
||||
}
|
||||
|
||||
char s[w+1];
|
||||
itoa(n, s, w);
|
||||
printf("%s\n", s);
|
||||
return 0;
|
||||
}
|
||||
|
||||
void reverse(char *s);
|
||||
|
||||
/* itoa: convert n to characters (base 10) in s, pads with leading spaces
|
||||
* to width w, truncates (left part) string of n if its is wider than w */
|
||||
void itoa(int n, char s[], unsigned w)
|
||||
{
|
||||
int i = 0;
|
||||
unsigned un = (n < 0) ? -n : n;
|
||||
|
||||
/* handle zero */
|
||||
if (!n && w)
|
||||
s[i++] = '0';
|
||||
|
||||
/* handle number */
|
||||
for (; i < w && un > 0; un /= 10, i++) {
|
||||
s[i] = un % 10 + '0';
|
||||
}
|
||||
/* handle sign */
|
||||
if (n < 0 && i < w)
|
||||
s[i++] = '-';
|
||||
/* pad with spaces */
|
||||
while (i < w)
|
||||
s[i++] = ' ';
|
||||
|
||||
s[i] = '\0';
|
||||
reverse(s);
|
||||
}
|
||||
|
||||
/* reverse: reverses string s in place */
|
||||
void reverse(char *s)
|
||||
{
|
||||
char c, *s2;
|
||||
for (s2 = s + strlen(s) - 1; s < s2; s++, s2--)
|
||||
c = *s, *s = *s2, *s2 = c;
|
||||
}
|
Loading…
Reference in New Issue