solutions to excercises
parent
53a06aa213
commit
f11eef6a7a
|
@ -0,0 +1,48 @@
|
|||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#define MIN(A, B) ((A < B) ? (A) : (B))
|
||||
|
||||
/* -------- INPUT WITH PUSHBACK */
|
||||
int _getchar()
|
||||
{
|
||||
extern int buffi;
|
||||
extern char buffer[];
|
||||
return buffi ? buffer[--buffi] : getchar();
|
||||
}
|
||||
#undef getchar
|
||||
#define getchar() _getchar()
|
||||
|
||||
|
||||
/* --------- MAIN PROGRAM */
|
||||
#define SLEN 10
|
||||
void ungets(char *s);
|
||||
|
||||
/* this program copies its input to its output,
|
||||
* if ungets wokrks as expected */
|
||||
int main()
|
||||
{
|
||||
int i, c;
|
||||
char s[SLEN+1];
|
||||
|
||||
for (i = 0; i < SLEN && (c = getchar()) != EOF; i++)
|
||||
s[i] = c;
|
||||
s[i] = '\0';
|
||||
|
||||
ungets(s);
|
||||
while ((c = getchar()) != EOF)
|
||||
putchar(c);
|
||||
}
|
||||
|
||||
|
||||
#define BUFF_SIZE 100
|
||||
int buffi;
|
||||
char buffer[BUFF_SIZE];
|
||||
|
||||
/* ungets: pushes string s back to input */
|
||||
void ungets(char *s)
|
||||
{
|
||||
int i = strlen(s);
|
||||
for (i = MIN(i, BUFF_SIZE-buffi)-1; i >= 0; i--, buffi++)
|
||||
buffer[buffi] = s[i];
|
||||
}
|
|
@ -0,0 +1,22 @@
|
|||
#include <stdio.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
;
|
||||
}
|
||||
|
||||
/* this handles EOF as wall */
|
||||
int buffc = -1;
|
||||
|
||||
char getch()
|
||||
{
|
||||
char temp;
|
||||
return (buffc < 0) ? getchar() : (temp = buffc, buffc = -1, temp);
|
||||
}
|
||||
|
||||
void ungetch(int c)
|
||||
{
|
||||
if (buffc >= 0)
|
||||
printf("warning: overriding character buffer\n");
|
||||
buffc = c;
|
||||
}
|
|
@ -0,0 +1,37 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
void itoar(char *s, int x);
|
||||
|
||||
/* converts first arg to int, puts it to output with itoar */
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
if (argc < 2)
|
||||
return 1;
|
||||
int x = atoi(argv[1]);
|
||||
char s[strlen(argv[1])+1];
|
||||
itoar(s, x);
|
||||
printf("%s\n", s);
|
||||
return 0;
|
||||
}
|
||||
|
||||
char * utoar(char *s, unsigned x);
|
||||
|
||||
/* itoa: writes int x to string s in decimal
|
||||
* recursive implementation */
|
||||
void itoar(char *s, int x)
|
||||
{
|
||||
unsigned y = (x < 0) ? *(s++) = '-', -x : x;
|
||||
s = utoar(s, y);
|
||||
*s = '\0';
|
||||
}
|
||||
|
||||
/* utoar: helper for itoa */
|
||||
char * utoar(char *s, unsigned x)
|
||||
{
|
||||
if (x / 10)
|
||||
s = utoar(s, x / 10);
|
||||
*s = x % 10 + '0';
|
||||
return s+1;
|
||||
}
|
|
@ -0,0 +1,28 @@
|
|||
#include <stdio.h>
|
||||
|
||||
int reverse(char string[], int i);
|
||||
|
||||
/* reverses command line argument */
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
if (argc < 2)
|
||||
return 1;
|
||||
printf("length : %d\n", reverse(argv[1], 0));
|
||||
printf("reversed : %s\n", argv[1]);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* reverse: reverses string from index i, returns length
|
||||
* recursive implementation */
|
||||
int reverse(char s[], int i)
|
||||
{
|
||||
if (s[i] == '\0') { /* base case */
|
||||
return 0;
|
||||
}
|
||||
int j = reverse(s, i+1);
|
||||
if (j > i) { /* swap elements */
|
||||
char c;
|
||||
c = s[i], s[i] = s[j], s[j] = c;
|
||||
}
|
||||
return j+1;
|
||||
}
|
|
@ -0,0 +1,43 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
void qsort_book(int v[], int left, int right);
|
||||
|
||||
/* sorts command line arguments */
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
int i, v[argc-1];
|
||||
for (i = 1; i < argc; i++)
|
||||
v[i-1] = atoi(argv[i]);
|
||||
qsort_book(v, 0, argc-2);
|
||||
for (i = 0; i < argc-1; i++)
|
||||
printf("%d ", v[i]);
|
||||
putchar('\n');
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* swap: swap v[i] and v[j] */
|
||||
void swap(int *v, int i, int j)
|
||||
{
|
||||
int temp = v[i];
|
||||
v[i] = v[j], v[j] = temp;
|
||||
}
|
||||
|
||||
/* qsort: sort v[left]...v[right] into increasing order */
|
||||
void qsort_book(int v[], int left, int right)
|
||||
{
|
||||
int i, last;
|
||||
|
||||
if (left >= right) /* do nothing if array contains */
|
||||
return; /* fewer than two elements */
|
||||
swap(v, left, (left + right)/2); /* move partition elem */
|
||||
last = left; /* to v[0] */
|
||||
|
||||
for (i = left+1; i <= right; i++) /* partition */
|
||||
if (v[i] < v[left])
|
||||
swap(v, ++last, i);
|
||||
swap(v, left, last); /* restore partition elem */
|
||||
|
||||
qsort_book(v, left, last-1);
|
||||
qsort_book(v, last+1, right);
|
||||
}
|
Loading…
Reference in New Issue