├── Chapter5 ├── include │ ├── exercise5.10.h │ ├── exercise5.2.h │ ├── exercise5.1.h │ ├── exercise5.3.h │ ├── exercise5.4.h │ └── exercise5.5.h └── src │ ├── main │ ├── exercise5.10 │ ├── obj │ ├── main.o │ ├── exercise5.1.o │ ├── exercise5.2.o │ ├── exercise5.3.o │ ├── exercise5.4.o │ ├── exercise5.5.o │ └── exercise5.10.o │ ├── exercise5.2.c │ ├── main.c │ ├── makefile │ ├── exercise5.3.c │ ├── exercise5.4.c │ ├── exercise5.1.c │ ├── exercise5.5.c │ └── exercise5.10.c ├── .gitignore ├── Chapter1 ├── a.out ├── hello.c ├── exercise1.7.c ├── exercise1.6.c ├── exercise1.9.c ├── exercise1.5.c ├── exercise1.10.c ├── exercise1.5.1.c ├── exercise1.12.c ├── guide_exercise1.3.c ├── exercise1.3.c ├── exercise1.8.c ├── exercise1.4.c ├── exercise1.15.c ├── exercise1.13.c ├── exercise1.14.c ├── exercise1.18.c ├── exercise1.17.c ├── exercise1.19.c └── print_longest_line.c ├── Chapter2 ├── a.out ├── exercise2.10.c ├── exercise2.2.c ├── exercise2.6.c ├── exercise2.9.c ├── solution_from_internet2.8.c ├── exercise2.5.c ├── exercise2.4.c ├── exercise2.3.c ├── exercise2.1.c ├── exercise2.7.c └── exercise2.8.c ├── Chapter3 ├── a.out ├── exercise3.1.1.c ├── exercise3.1.c ├── exercise3.2.c └── exercise3.3.c └── Chapter4 ├── src ├── main ├── exercises ├── obj │ ├── main.o │ ├── exercise4.1.o │ ├── exercise4.2.o │ ├── exercise4_1.o │ ├── exercise4_2.o │ └── exercise4_3_8.o ├── main.c ├── makefile ├── exercise4_2.c ├── exercise4_1.c └── exercise4_3_to_5.c └── include ├── exercise4_2.h ├── exercise4_1.h └── exercise4_3_to_5.h /Chapter5/include/exercise5.10.h: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .idea 2 | CMakeLists.txt 3 | cmake-build-debug 4 | -------------------------------------------------------------------------------- /Chapter5/include/exercise5.2.h: -------------------------------------------------------------------------------- 1 | #include "stdio.h" 2 | 3 | extern int exercise5_2(void); -------------------------------------------------------------------------------- /Chapter1/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/double-o-z/TheCProgramming_KandR/HEAD/Chapter1/a.out -------------------------------------------------------------------------------- /Chapter2/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/double-o-z/TheCProgramming_KandR/HEAD/Chapter2/a.out -------------------------------------------------------------------------------- /Chapter3/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/double-o-z/TheCProgramming_KandR/HEAD/Chapter3/a.out -------------------------------------------------------------------------------- /Chapter1/hello.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | main() 4 | { 5 | printf("hello, world\n"); 6 | } 7 | -------------------------------------------------------------------------------- /Chapter4/src/main: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/double-o-z/TheCProgramming_KandR/HEAD/Chapter4/src/main -------------------------------------------------------------------------------- /Chapter5/src/main: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/double-o-z/TheCProgramming_KandR/HEAD/Chapter5/src/main -------------------------------------------------------------------------------- /Chapter4/src/exercises: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/double-o-z/TheCProgramming_KandR/HEAD/Chapter4/src/exercises -------------------------------------------------------------------------------- /Chapter4/src/obj/main.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/double-o-z/TheCProgramming_KandR/HEAD/Chapter4/src/obj/main.o -------------------------------------------------------------------------------- /Chapter5/src/exercise5.10: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/double-o-z/TheCProgramming_KandR/HEAD/Chapter5/src/exercise5.10 -------------------------------------------------------------------------------- /Chapter5/src/obj/main.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/double-o-z/TheCProgramming_KandR/HEAD/Chapter5/src/obj/main.o -------------------------------------------------------------------------------- /Chapter4/src/obj/exercise4.1.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/double-o-z/TheCProgramming_KandR/HEAD/Chapter4/src/obj/exercise4.1.o -------------------------------------------------------------------------------- /Chapter4/src/obj/exercise4.2.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/double-o-z/TheCProgramming_KandR/HEAD/Chapter4/src/obj/exercise4.2.o -------------------------------------------------------------------------------- /Chapter4/src/obj/exercise4_1.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/double-o-z/TheCProgramming_KandR/HEAD/Chapter4/src/obj/exercise4_1.o -------------------------------------------------------------------------------- /Chapter4/src/obj/exercise4_2.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/double-o-z/TheCProgramming_KandR/HEAD/Chapter4/src/obj/exercise4_2.o -------------------------------------------------------------------------------- /Chapter5/src/obj/exercise5.1.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/double-o-z/TheCProgramming_KandR/HEAD/Chapter5/src/obj/exercise5.1.o -------------------------------------------------------------------------------- /Chapter5/src/obj/exercise5.2.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/double-o-z/TheCProgramming_KandR/HEAD/Chapter5/src/obj/exercise5.2.o -------------------------------------------------------------------------------- /Chapter5/src/obj/exercise5.3.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/double-o-z/TheCProgramming_KandR/HEAD/Chapter5/src/obj/exercise5.3.o -------------------------------------------------------------------------------- /Chapter5/src/obj/exercise5.4.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/double-o-z/TheCProgramming_KandR/HEAD/Chapter5/src/obj/exercise5.4.o -------------------------------------------------------------------------------- /Chapter5/src/obj/exercise5.5.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/double-o-z/TheCProgramming_KandR/HEAD/Chapter5/src/obj/exercise5.5.o -------------------------------------------------------------------------------- /Chapter4/src/obj/exercise4_3_8.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/double-o-z/TheCProgramming_KandR/HEAD/Chapter4/src/obj/exercise4_3_8.o -------------------------------------------------------------------------------- /Chapter5/src/obj/exercise5.10.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/double-o-z/TheCProgramming_KandR/HEAD/Chapter5/src/obj/exercise5.10.o -------------------------------------------------------------------------------- /Chapter5/src/exercise5.2.c: -------------------------------------------------------------------------------- 1 | #include "../include/exercise5.2.h" 2 | 3 | int exercise5_2(){ 4 | printf("bamba\n"); 5 | return 0; 6 | } -------------------------------------------------------------------------------- /Chapter1/exercise1.7.c: -------------------------------------------------------------------------------- 1 | #include 2 | /* copy input to output; 2nd version */ 3 | main(){ 4 | printf("This is EOF char: %d\n", EOF); 5 | } 6 | -------------------------------------------------------------------------------- /Chapter5/include/exercise5.1.h: -------------------------------------------------------------------------------- 1 | #include "stdio.h" 2 | #include "ctype.h" 3 | 4 | #define SIZE 100 5 | 6 | int getint(int *); 7 | 8 | extern int exercise5_1(void); -------------------------------------------------------------------------------- /Chapter1/exercise1.6.c: -------------------------------------------------------------------------------- 1 | #include 2 | /* copy input to output; 2nd version */ 3 | main(){ 4 | int c; 5 | while (c = getchar() != EOF) 6 | putchar(c); 7 | } 8 | -------------------------------------------------------------------------------- /Chapter4/include/exercise4_2.h: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | double sum; 5 | char line[MAXLINE]; 6 | 7 | double atof_custom(char s[]); 8 | int exercise4_2(); -------------------------------------------------------------------------------- /Chapter5/include/exercise5.3.h: -------------------------------------------------------------------------------- 1 | #ifndef SRC_EXERCISE5_3_H 2 | #define SRC_EXERCISE5_3_H 3 | #include "stdio.h" 4 | extern int exercise5_3(); 5 | char *strcatp(char *s, char *t); 6 | #endif 7 | -------------------------------------------------------------------------------- /Chapter5/include/exercise5.4.h: -------------------------------------------------------------------------------- 1 | #ifndef SRC_EXERCISE5_4_H 2 | #define SRC_EXERCISE5_4_H 3 | #include "stdio.h" 4 | extern int exercise5_4(); 5 | int strend(char s[], char t[]); 6 | #endif 7 | -------------------------------------------------------------------------------- /Chapter1/exercise1.9.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main(){ 3 | int c; 4 | int last_char; 5 | while ((c = getchar()) != EOF){ 6 | if(!(last_char == ' ' && c == ' ')) 7 | printf("%c", c); 8 | last_char = c; 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /Chapter1/exercise1.5.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /* print Celsius-Fahrenheit table */ 4 | 5 | main(){ 6 | int fahr; 7 | 8 | for (fahr = 300; fahr >= 0; fahr = fahr - 20) 9 | printf("%3d %6.1f\n", fahr, (5.0/9.0)*(fahr-32)); 10 | } 11 | -------------------------------------------------------------------------------- /Chapter4/include/exercise4_1.h: -------------------------------------------------------------------------------- 1 | #include 2 | #define MAXLINE 1000 3 | 4 | int getline_f(char line[], int max); 5 | int strindex(char source[], char searchfor[]); 6 | int strrindex(char source[], char searchfor[], int len); 7 | int exercise4_1(); -------------------------------------------------------------------------------- /Chapter4/src/main.c: -------------------------------------------------------------------------------- 1 | #include "exercise4_1.h" 2 | #include "exercise4_2.h" 3 | #include "exercise4_3_to_5.h" 4 | 5 | int main(){ 6 | /* 7 | exercise4_1(); 8 | exercise4_2(); 9 | exercise4_3_to_5(); 10 | */ 11 | return 0; 12 | } -------------------------------------------------------------------------------- /Chapter1/exercise1.10.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main(){ 3 | int c; 4 | while ((c = getchar()) != EOF){ 5 | if (c == '\t') 6 | printf("\\t"); 7 | else if (c == '\b') 8 | printf("\\b"); 9 | else if (c == '\\') 10 | printf("\\\\"); 11 | else 12 | printf("%c", c); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /Chapter1/exercise1.5.1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define LOWER 0 4 | #define UPPER 300 5 | #define STEP 20 6 | 7 | /* print Celsius-Fahrenheit table */ 8 | 9 | main(){ 10 | int fahr; 11 | 12 | for (fahr = UPPER; fahr >= LOWER; fahr = fahr - STEP) 13 | printf("%3d %6.1f\n", fahr, (5.0/9.0)*(fahr-32)); 14 | } 15 | -------------------------------------------------------------------------------- /Chapter2/exercise2.10.c: -------------------------------------------------------------------------------- 1 | #include 2 | int lower(int c); 3 | int main(){ 4 | printf("lower result on: %c is %c\n", 'D', lower('D')); 5 | printf("lower result on: %c is %c\n", 'g', lower('g')); 6 | printf("lower result on: %c is %c\n", '5', lower('5')); 7 | } 8 | int lower(int c){ 9 | return (c>= 'A' && c<= 'Z') ? (c + 'a' - 'A') : c; 10 | } 11 | -------------------------------------------------------------------------------- /Chapter5/include/exercise5.5.h: -------------------------------------------------------------------------------- 1 | #ifndef CHAPTER5_EXERCISE5_5_H 2 | #define CHAPTER5_EXERCISE5_5_H 3 | #include "stdio.h" 4 | #include "stdlib.h" 5 | #include 6 | 7 | extern int exercise5_5(); 8 | char *strncpyVersion(char *s, char *ct, int n); 9 | char *strncatVersion(char *s, char *ct, int n); 10 | int strncmpVersion(char *cs, char *ct, int n); 11 | #endif 12 | -------------------------------------------------------------------------------- /Chapter1/exercise1.12.c: -------------------------------------------------------------------------------- 1 | #include 2 | #define IN 1 3 | #define OUT 0 4 | main(){ 5 | int c, state; 6 | state = OUT; 7 | while ((c = getchar()) != EOF){ 8 | if (c == ' ' || c == '\t' || c == '\n'){ 9 | if (state == IN){ 10 | putchar('\n'); 11 | state = OUT; 12 | } 13 | } 14 | else{ 15 | putchar(c); 16 | state = IN; 17 | } 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /Chapter1/guide_exercise1.3.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | main(){ 4 | float fahr, celsius; 5 | int lower, upper, step; 6 | lower = 0; 7 | upper = 300; 8 | step = 20; 9 | fahr = lower; 10 | while (fahr <= upper) 11 | { 12 | celsius = (5.0 / 9.0) * (fahr - 32.0); 13 | printf ("%d, %3.1f, %6.1f\n", fahr, celsius); 14 | /* change of format */ 15 | fahr = fahr + step; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /Chapter1/exercise1.3.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /* print Fahrenheit-Celsius table */ 4 | 5 | main(){ 6 | float fahr, celsius; 7 | int lower, upper, step; 8 | 9 | lower = 0; 10 | upper = 300; 11 | step = 20; 12 | 13 | fahr = lower; 14 | printf("Fahr Celsius\n"); 15 | while (fahr <= upper) { 16 | celsius = (5/9.0) * (fahr-32); 17 | printf("%3.0f %6.1f\n", fahr, celsius); 18 | fahr = fahr + step; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /Chapter1/exercise1.8.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main(){ 3 | int blanks, tabs, newlines, c; 4 | blanks = 0; 5 | tabs = 0; 6 | newlines = 0; 7 | while ((c = getchar()) != EOF){ 8 | if (c == ' ') 9 | ++blanks; 10 | else if (c == '\t') 11 | ++tabs; 12 | else if (c == '\n') 13 | ++newlines; 14 | } 15 | printf("Number of blanks: %d\nNumber of tabs: %d\nNumber of new lines: %d\n", blanks, tabs, newlines); 16 | } 17 | -------------------------------------------------------------------------------- /Chapter4/src/makefile: -------------------------------------------------------------------------------- 1 | IDIR =../include 2 | CC=gcc 3 | CFLAGS=-I$(IDIR) -Wall -ansi -pedantic 4 | 5 | ODIR=obj 6 | LDIR =../lib 7 | 8 | _DEPS = 9 | DEPS = $(patsubst %,$(IDIR)/%,$(_DEPS)) 10 | 11 | _OBJ = exercise4_1.o exercise4_2.o exercise4_3_8.o main.o 12 | OBJ = $(patsubst %,$(ODIR)/%,$(_OBJ)) 13 | 14 | 15 | $(ODIR)/%.o: %.c $(DEPS) 16 | $(CC) -c -o $@ $< $(CFLAGS) 17 | 18 | main: $(OBJ) 19 | $(CC) -o $@ $^ $(CFLAGS) 20 | -------------------------------------------------------------------------------- /Chapter1/exercise1.4.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /* print Celsius-Fahrenheit table */ 4 | 5 | main(){ 6 | float fahr, celsius; 7 | int lower, upper, step; 8 | 9 | lower = 0; 10 | upper = 300; 11 | step = 20; 12 | 13 | celsius = lower; 14 | printf("Celsius\tFahr\n"); 15 | while (celsius <= upper) { 16 | fahr = (celsius * 9 / 5.0) + 32.0; 17 | printf("%3.0f\t%6.1f\n", celsius, fahr); 18 | celsius = celsius + step; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /Chapter5/src/main.c: -------------------------------------------------------------------------------- 1 | #include "../include/exercise5.1.h" 2 | #include "../include/exercise5.2.h" 3 | #include "../include/exercise5.3.h" 4 | #include "../include/exercise5.4.h" 5 | #include "../include/exercise5.5.h" 6 | #include "../include/exercise5.10.h" 7 | 8 | int main(int argc, char *argv[]) 9 | { 10 | /*exercise5_3();*/ 11 | /*exercise5_4();*/ 12 | /*exercise5_5();*/ 13 | /*exercise5_10();*/ 14 | /*exercise5_2();*/ 15 | exercise5_1(); 16 | return 0; 17 | } -------------------------------------------------------------------------------- /Chapter2/exercise2.2.c: -------------------------------------------------------------------------------- 1 | #include 2 | #define LIM 1000 3 | enum stop {NO, YES}; 4 | int main(){ 5 | char s[LIM], c; 6 | int stop = NO, i = 0; 7 | 8 | while (!stop){ 9 | stop = YES; 10 | if (i 2 | #include /* for atof_custom */ 3 | #include 4 | #include 5 | 6 | #define BUFSIZE 100 7 | #define MAXOP 100 /* max size of operand or operator */ 8 | #define NUMBER '0' /* signal that a number was found */ 9 | #define MAXVAL 100 /* max depth of val stack */ 10 | 11 | int getop (char []); 12 | void push (double); 13 | double pop (void); 14 | int my_getch(void); 15 | void my_ungetch (int); 16 | int exercise4_3_8(); -------------------------------------------------------------------------------- /Chapter5/src/makefile: -------------------------------------------------------------------------------- 1 | IDIR =../include 2 | CC=gcc 3 | CFLAGS=-I$(IDIR) -Wall -ansi -pedantic 4 | 5 | ODIR=obj 6 | LDIR =../lib 7 | 8 | _DEPS = exercise5.10.h exercise5.5.h exercise5.4.h exercise5.3.h exercise5.2.h exercise5.1.h 9 | DEPS = $(patsubst %,$(IDIR)/%,$(_DEPS)) 10 | 11 | _OBJ = main.o exercise5.10.o exercise5.5.o exercise5.4.o exercise5.3.o exercise5.2.o exercise5.1.o 12 | OBJ = $(patsubst %,$(ODIR)/%,$(_OBJ)) 13 | 14 | 15 | $(ODIR)/%.o: %.c $(DEPS) 16 | $(CC) -c -o $@ $< $(CFLAGS) 17 | 18 | main: $(OBJ) 19 | $(CC) -o $@ $^ $(CFLAGS) 20 | -------------------------------------------------------------------------------- /Chapter1/exercise1.15.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /* print Fahrenheit-Celsius table */ 4 | int fahr_to_celsius(int fahr); 5 | 6 | int main(){ 7 | float fahr, celsius; 8 | int lower, upper, step; 9 | 10 | lower = 0; 11 | upper = 300; 12 | step = 20; 13 | 14 | fahr = lower; 15 | printf("Fahr Celsius\n"); 16 | while (fahr <= upper) { 17 | celsius = fahr_to_celsius(fahr); 18 | printf("%3.0f %6.1f\n", fahr, celsius); 19 | fahr = fahr + step; 20 | } 21 | return 0; 22 | } 23 | 24 | int fahr_to_celsius(int fahr){ 25 | int celsius; 26 | celsius = (5/9.0) * (fahr-32); 27 | return celsius; 28 | } 29 | -------------------------------------------------------------------------------- /Chapter1/exercise1.13.c: -------------------------------------------------------------------------------- 1 | #include 2 | #define MAX_WORD_LENGTH 20 3 | main(){ 4 | int nwords[MAX_WORD_LENGTH]; 5 | int c, lw, i, j; 6 | for (i = 1; i <= MAX_WORD_LENGTH; ++i) 7 | nwords[i] = 0; 8 | lw = 0; 9 | while ((c = getchar()) != EOF){ 10 | if (c == ' ' || c == '\t' || c == '\n'){ 11 | ++nwords[lw]; 12 | lw = 0; 13 | } else { 14 | ++lw; 15 | } 16 | } 17 | printf("Histogram of words length:\n"); 18 | for (i = 1; i<= MAX_WORD_LENGTH; ++i) 19 | if (nwords[i] > 0){ 20 | printf("%d ", i); 21 | for (j = 1; j <= nwords[i]; ++j) 22 | putchar('='); 23 | putchar('\n'); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /Chapter5/src/exercise5.3.c: -------------------------------------------------------------------------------- 1 | #include "../include/exercise5.3.h" 2 | 3 | int exercise5_3(){ 4 | char s[] = "the", t[] = " batman"; 5 | char *result; 6 | printf("exercise 5.3\n"); 7 | printf("calling strcatp on s: '%s', t: '%s'\n", s , t); 8 | result = strcatp(s, t); 9 | printf("result: \n'"); 10 | for (; *result != '\0'; result++) { 11 | putchar(*result); 12 | } 13 | printf("'\n"); 14 | return 0; 15 | } 16 | 17 | char *strcatp(char *s, char *t){ 18 | char *keep = s; 19 | while (*s != '\0') 20 | s++; 21 | while ((*s++ = *t++) != '\0') 22 | ; 23 | return keep; 24 | } -------------------------------------------------------------------------------- /Chapter1/exercise1.14.c: -------------------------------------------------------------------------------- 1 | #include 2 | #define CHARACTERS 52 3 | #define LOWER_CHARS 26 4 | main(){ 5 | int nchars[CHARACTERS]; 6 | int c, i, j; 7 | for (i = 0; i < CHARACTERS; ++i) 8 | nchars[i] = 0; 9 | while ((c = getchar()) != EOF){ 10 | if (c >= 'a' && c <= 'z') 11 | ++nchars[c-'a']; 12 | else if (c >= 'A' && c <= 'Z') 13 | ++nchars[c-'A'+LOWER_CHARS]; 14 | } 15 | printf("Histogram of frequency of different characters:\n"); 16 | for (i = 0; i < CHARACTERS; ++i){ 17 | if (nchars[i] > 0){ 18 | if (i < LOWER_CHARS) 19 | printf("%c: ", 'a'+i); 20 | else 21 | printf("%c: ", 'A'+i-LOWER_CHARS); 22 | for (j = 1; j <= nchars[i]; ++j) 23 | putchar('='); 24 | putchar('\n'); 25 | } 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /Chapter2/exercise2.6.c: -------------------------------------------------------------------------------- 1 | #include 2 | unsigned setbits(unsigned x, int p, int n, unsigned y); 3 | int main(){ 4 | unsigned int x = 31; // 11111 5 | unsigned int y = 0; // 00000 6 | unsigned int z = setbits(x, 3, 2, y); 7 | printf("x after setbits: %d\n", z); // 31-12 = 19. 8 | return 0; 9 | } 10 | 11 | unsigned setbits(unsigned x, int p, int n, unsigned y){ 12 | // (y & 07) -- is the 3 left most bits of y 13 | // ~(~0 << n) -- gives me the the n right most bits turned on 14 | // (y & ~(~0 << n)) -- gives me the n right most bits of y 15 | // (y & ~(~0 << n)) << (p+1-n) -- gives me y's n bits from p location right adjusted 16 | // (~0 << p+1) | ~(~0 << n) 17 | return ((y & ~(~0 << n)) << (p+1-n)) | (x & ((~0 << p+1) | ~(~0 << n))); 18 | } 19 | -------------------------------------------------------------------------------- /Chapter1/exercise1.18.c: -------------------------------------------------------------------------------- 1 | #include 2 | #define MAXLINE 1000 3 | int getline_f(char line[], int maxline); 4 | 5 | int main(){ 6 | int len; 7 | int max; 8 | char line[MAXLINE]; 9 | 10 | while ((len = getline_f(line, MAXLINE)) > 0){ 11 | printf("%s", line); 12 | } 13 | return 0; 14 | } 15 | 16 | int getline_f(char s[], int lim){ 17 | int c, i; 18 | 19 | for (i=0; i 0){ 23 | while ((i-1) >= 0 && (s[i-1] == ' ' || s[i-1] == '\t')) 24 | --i; /* remove trailing blanks and tabs */ 25 | s[i] = c; 26 | ++i; 27 | } 28 | } 29 | s[i] = '\0'; 30 | if (c == '\n' && i == 0) 31 | ++i; /* return 1 for blank lines, but with empty line for print */ 32 | return i; 33 | } 34 | -------------------------------------------------------------------------------- /Chapter1/exercise1.17.c: -------------------------------------------------------------------------------- 1 | #include 2 | #define MAXLINE 80 3 | int getline_f(char line[], int maxline); 4 | void copy(char to[], char from[]); 5 | 6 | int main(){ 7 | int len; 8 | char line[MAXLINE]; 9 | 10 | while ((len = getline_f(line, MAXLINE)) > 0) 11 | if (len > MAXLINE) 12 | printf("Found line with length %d, first 100 chars are: %s", len, line); 13 | return 0; 14 | } 15 | 16 | int getline_f(char s[], int lim){ 17 | int c, i; 18 | 19 | for (i=0; i 2 | #define MAXLINE 1000 3 | int getline_f(char line[], int maxline); 4 | void reverse(char line[], int len); 5 | int main(){ 6 | int len; 7 | char line[MAXLINE]; 8 | 9 | while ((len = getline_f(line, MAXLINE)) > 0){ 10 | reverse(line, len); 11 | printf("%s", line); 12 | } 13 | return 0; 14 | } 15 | 16 | int getline_f(char s[], int lim){ 17 | int c, i; 18 | 19 | for (i=0; i 0) 24 | sizet--; 25 | 26 | return (sizet == 0); /* returns 1 if sp and tp where reversed back sizet times, otherwise 0. */ 27 | } -------------------------------------------------------------------------------- /Chapter2/exercise2.9.c: -------------------------------------------------------------------------------- 1 | #include 2 | int bitcount(signed x); 3 | int main(){ 4 | signed int x = 127; // 0111 in two's complement (signed) 5 | int b = bitcount(x); // expect 3 6 | printf("bit count of signed int x: %d, is %d\n", x, b); 7 | } 8 | 9 | int bitcount(signed x){ 10 | /* 11 | Count bits in signed integer. 12 | Each time we remove 1 from x, and & with itself, thus removing the right most bit. 13 | When x is decreased by 1, the right most bit becomes zero, and some or the bits on its right might become 1. 14 | But by using &, we remove them because x has zeros on the right most bits of x-1. 15 | This simplfies the bit count because its cheaper them shifting x by 1 each iteration. 16 | Simply deleting a bit or decreasing x by 1 is cheaper. 17 | */ 18 | int b; 19 | for (b = 0; x != 0; x &= x -1) 20 | b++; 21 | return b; 22 | } 23 | -------------------------------------------------------------------------------- /Chapter1/print_longest_line.c: -------------------------------------------------------------------------------- 1 | #include 2 | #define MAXLINE 1000 3 | int getline_f(char line[], int maxline); 4 | void copy(char to[], char from[]); 5 | 6 | int main(){ 7 | int len; 8 | int max; 9 | char line[MAXLINE]; 10 | char longest[MAXLINE]; 11 | 12 | max = 0; 13 | while ((len = getline_f(line, MAXLINE)) > 0) 14 | if (len > max){ 15 | max = len; 16 | copy(longest, line); 17 | } 18 | if (max > 0) 19 | printf("%s", longest); 20 | return 0; 21 | } 22 | 23 | int getline_f(char s[], int lim){ 24 | int c, i; 25 | 26 | for (i=0; i 2 | #define INT_BITS 32 3 | 4 | /*Function to left rotate n by d bits*/ 5 | int leftRotate(int n, unsigned int d) 6 | { 7 | /* In n<>(INT_BITS - d) */ 9 | return (n << d)|(n >> (INT_BITS - d)); 10 | } 11 | 12 | /*Function to right rotate n by d bits*/ 13 | int rightRotate(int n, unsigned int d) 14 | { 15 | /* In n>>d, first d bits are 0. To put last 3 bits of at 16 | first, do bitwise or of n>>d with n <<(INT_BITS - d) */ 17 | return (n >> d)|(n << (INT_BITS - d)); 18 | } 19 | 20 | /* Driver program to test above functions */ 21 | int main() 22 | { 23 | int n = 873; 24 | int d = 3; 25 | printf("Left Rotation of %d by %d is ", n, d); 26 | printf("%d", leftRotate(n, d)); 27 | printf("\nRight Rotation of %d by %d is ", n, d); 28 | printf("%d", rightRotate(n, d)); 29 | getchar(); 30 | } 31 | 32 | -------------------------------------------------------------------------------- /Chapter2/exercise2.5.c: -------------------------------------------------------------------------------- 1 | #include 2 | #define MAX_STRING 1000 3 | int any(char s1[], char s2[]); 4 | int main(){ 5 | int i = 0, res; 6 | char s1[MAX_STRING], s2[MAX_STRING], c; 7 | while ((c=getchar())!='\n' && c != EOF) 8 | s1[i++] = c; 9 | s1[i]='\0'; 10 | //printf("Got string s1: %s\n", s1); 11 | i = 0; 12 | while ((c=getchar())!='\n' && c != EOF) 13 | s2[i++] = c; 14 | s2[i]='\0'; 15 | //printf("Got string s2: %s\n", s2); 16 | res = any(s1,s2); 17 | printf("Result: %d\n", res); 18 | return 0; 19 | } 20 | 21 | int any(char s1[], char s2[]){ 22 | int i, k; 23 | char c, d; 24 | for (i = 0; (c = s1[i]) != '\0'; i++){ 25 | //printf("looking at s1 char: %c\n", c); 26 | for (k = 0; (d = s2[k]) != '\0' && d != c; k++) 27 | ; 28 | //printf("looking at s2 char: %c\n", d); 29 | if (d != '\0'){ 30 | //printf("Found s2 char: %c, in s1 at location: %d\n", s2[k], i); 31 | return i; 32 | } 33 | } 34 | return -1; 35 | } 36 | -------------------------------------------------------------------------------- /Chapter2/exercise2.4.c: -------------------------------------------------------------------------------- 1 | #include 2 | #define MAX_STRING 1000 3 | void squeeze(char s1[], char s2[]); 4 | int main(){ 5 | int i = 0; 6 | char s1[MAX_STRING], s2[MAX_STRING], c; 7 | while ((c=getchar())!='\n' && c != EOF) 8 | s1[i++] = c; 9 | s1[i]='\0'; 10 | //printf("Got string s1: %s\n", s1); 11 | i = 0; 12 | while ((c=getchar())!='\n' && c != EOF) 13 | s2[i++] = c; 14 | s2[i]='\0'; 15 | //printf("Got string s2: %s\n", s2); 16 | squeeze(s1,s2); 17 | printf("Result of s1 is now: %s\n", s1); 18 | return 0; 19 | } 20 | 21 | void squeeze(char s1[], char s2[]){ 22 | int i, j, k; 23 | char c, d; 24 | for (i = j = 0; (c = s1[i]) != '\0'; i++){ 25 | //printf("looking at s1 char: %c\n", c); 26 | for (k = 0; (d = s2[k]) != '\0' && d != c; k++) 27 | ; 28 | //printf("looking at s2 char: %c\n", d); 29 | if (d != c){ 30 | //printf("Not skipping this char: %c\n", s1[i]); 31 | s1[j++] = s1[i]; 32 | } 33 | } 34 | s1[j] = '\0'; 35 | } 36 | -------------------------------------------------------------------------------- /Chapter5/src/exercise5.1.c: -------------------------------------------------------------------------------- 1 | #include "../include/exercise5.1.h" 2 | 3 | /* getint: get next integer from input into *pn */ 4 | int getint(int *pn){ 5 | int c, sign; 6 | while (isspace(c = getc(stdin))) /* skip white space */ 7 | ; 8 | if (!isdigit(c) && c != EOF && c != '+' && c != '-'){ 9 | ungetc(c, stdin); /* it's not a number */ 10 | return 0; 11 | } 12 | sign = (c == '-') ? -1 : 1; 13 | if (c== '+' || c == '-') 14 | c = getc(stdin); 15 | for (*pn = 0; isdigit(c); c = getc(stdin)) 16 | *pn = 10 * *pn + (c - '0'); 17 | *pn *= sign; 18 | if (c != EOF) 19 | ungetc(c, stdin); 20 | return c; 21 | } 22 | 23 | int exercise5_1(){ 24 | int i, n, array[SIZE]; 25 | printf("Exercise 5.1\n"); 26 | for (n = 0; n < SIZE && getint(&array[n]) != EOF; n++) 27 | ; 28 | for (i = 0; i < n; i++) { 29 | printf("array[%d]: %d\n", i, *(array+i)); 30 | } 31 | return 0; 32 | } -------------------------------------------------------------------------------- /Chapter2/exercise2.3.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | int htoi(char s[]); 5 | int i = 0; 6 | int main(){ 7 | int result; 8 | extern int i; 9 | char s[1000], c; 10 | while ((c = getchar()) != '\n') 11 | s[i++] = c; 12 | s[i] = '\0'; 13 | //printf("got hex number: %s\n", s); 14 | result = htoi(s); 15 | printf("result: %d\n", result); 16 | return 0; 17 | } 18 | 19 | int htoi(char s[]){ 20 | extern int i; 21 | int hexPower = 1; 22 | char c; 23 | int res = 0; 24 | //printf("length of string: %d\n", i); 25 | for (--i; i >= 2; --i){ // Go from last char to second 26 | c = s[i]; 27 | //printf("working on char: %c\n", c); 28 | if (isdigit(c)) 29 | c = c - '0'; 30 | else // a-f/A-F 31 | c = tolower(c) - 'a' + 10; 32 | //printf("weight of hex char: %d\n", c); 33 | res = res + (c * hexPower); 34 | //printf("res is currently: %d\n", res); 35 | hexPower = hexPower * 16; 36 | //printf("hexPower is currently: %d\n", hexPower); 37 | } 38 | return res; 39 | } 40 | -------------------------------------------------------------------------------- /Chapter2/exercise2.1.c: -------------------------------------------------------------------------------- 1 | /** 2 | * C program to get minimum and maximum range of a type using C library constants 3 | */ 4 | #include 5 | #include 6 | #include 7 | 8 | int main() 9 | { 10 | printf("Range of signed char %d to %d\n", SCHAR_MIN, SCHAR_MAX); 11 | printf("Range of unsigned char 0 to %d\n\n", UCHAR_MAX); 12 | 13 | printf("Range of signed short int %d to %d\n", SHRT_MIN, SHRT_MAX); 14 | printf("Range of unsigned short int 0 to %d\n\n", USHRT_MAX); 15 | 16 | printf("Range of signed int %d to %d\n", INT_MIN, INT_MAX); 17 | printf("Range of unsigned int 0 to %u\n\n", UINT_MAX); 18 | 19 | printf("Range of signed long int %ld to %ld\n", LONG_MIN, LONG_MAX); 20 | printf("Range of unsigned long int 0 to %lu\n\n", ULONG_MAX); 21 | 22 | printf("Range of float %e to %e\n", FLT_MIN, FLT_MAX); 23 | printf("Range of double %e to %e\n", DBL_MIN, DBL_MAX); 24 | printf("Range of long double %e to %e\n", LDBL_MIN, LDBL_MAX); 25 | 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /Chapter3/exercise3.1.1.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #define SIZE 1000000 4 | #define BILLION 1000000000.0 5 | int binsearch(int x, int v[], int n); 6 | int main(){ 7 | struct timespec start, end; 8 | clock_gettime(CLOCK_REALTIME, &start); 9 | int v[SIZE], x = 9; 10 | for (int i = 0; i v[mid]) 29 | low = mid+1; 30 | else 31 | return mid; 32 | } 33 | return -1; 34 | } 35 | -------------------------------------------------------------------------------- /Chapter4/src/exercise4_2.c: -------------------------------------------------------------------------------- 1 | #include "exercise4_2.h" 2 | 3 | double atof_custom(char s[]){ 4 | double val, power, exponent; 5 | int i, sign, e_sign, j; 6 | 7 | for (i = 0; isspace(s[i]); i++) 8 | ; 9 | sign = (s[i] == '-') ? -1 : 1; 10 | if (s[i] == '+' || s[i] == '-') 11 | i++; 12 | for (val = 0; isdigit(s[i]); i++) 13 | val = 10.0 * val + (s[i] - '0'); 14 | if (s[i] == '.') 15 | i++; 16 | for (power = 1.0; isdigit(s[i]); i++) { 17 | val = 10.0 * val + (s[i] - '0'); 18 | power *= 10.0; 19 | } 20 | if (tolower(s[i++]) == 'e' && ((e_sign = s[i++]) == '+' || e_sign == '-') && isdigit(s[i])){ 21 | for (j = s[i]-'0', exponent = 1.0; j > 0; j--) 22 | exponent *= 10.0; 23 | if (e_sign == '+') 24 | power /= exponent; 25 | else 26 | power *= exponent; 27 | } 28 | return sign * val / power; 29 | } 30 | 31 | int exercise4_2(){ 32 | sum = 0; 33 | printf("Input lines of doubles, with exponent.\n"); 34 | while (getline_f(line, MAXLINE) > 0) 35 | printf("\t%g\n", sum += atof_custom(line)); 36 | return 0; 37 | } 38 | -------------------------------------------------------------------------------- /Chapter2/exercise2.7.c: -------------------------------------------------------------------------------- 1 | #include 2 | unsigned invert(unsigned x, int p, int n); 3 | int main(){ 4 | unsigned int x = 873; // 1101101001 = 873 5 | // 1100011001 = 793 6 | printf("x after invert(x, 4, 3): %d\n", invert(x, 4, 3)); // 793 7 | return 0; 8 | } 9 | 10 | /* returns x with n bits that begin at position p inverted, leaving the others unchanged. */ 11 | unsigned invert(unsigned x, int p, int n){ 12 | // 1101101001 x 13 | // 1100011001 result 14 | // first make a mask of 1's at bits other the the n after p: 15 | unsigned int left_mask = ~0 << n+p; // 1110000000 16 | unsigned int right_mask = ~(~0 << p); // 0000001111 17 | unsigned int full_mask = left_mask | right_mask; // 1110001111 18 | unsigned int reset_n_bits_of_x = x & full_mask; // 1100001001 19 | 20 | unsigned int revert_full_mask = ~(full_mask); // 0001110000 21 | unsigned int only_n_bits_of_x = x & revert_full_mask; // 0001100000 22 | unsigned int n_bits_of_x_revert = ~(only_n_bits_of_x); // 1110011111 23 | n_bits_of_x_revert = n_bits_of_x_revert & revert_full_mask; // 0000010000 24 | return reset_n_bits_of_x | n_bits_of_x_revert; // 1100011001 25 | } 26 | -------------------------------------------------------------------------------- /Chapter2/exercise2.8.c: -------------------------------------------------------------------------------- 1 | #include 2 | unsigned rightrot(unsigned x, int n); 3 | int main(){ 4 | unsigned int x = 873; // 1101101001 = 873 5 | int n = 3; 6 | printf("x is %d, n is %d\n", x, n); 7 | printf("x after rightrot(x, 3): %d\n", rightrot(x, n)); // 0011101101 = 237 8 | return 0; 9 | } 10 | 11 | /* returns x rotated by n bits to the right. */ 12 | unsigned rightrot(unsigned x, int n){ 13 | // 1101101001 original x 873 14 | // 0011101101 wanted result 237 15 | // 0001101111 actual result 111 16 | int x_bits = sizeof(x) * 8; 17 | //printf("x_bits: %d\n", x_bits); 18 | unsigned int x_n_shifted_right = x >> n; // 0001101101 x n shifted to the right 19 | //printf("x_n_shifted_right: %u\n", x_n_shifted_right); 20 | unsigned int x_n_right_most_bits = (~(~0 << n)) & x; // 0000000001 x's right most n bits 21 | unsigned int n_bits_left_shifted = x_n_right_most_bits << (x_bits - n); // 0010000000 right most n bits, left shifted 22 | unsigned int result = (x_n_shifted_right | n_bits_left_shifted); // 0011101101 result 237 23 | /* can be shorted.. simple left shift x by (x_bits - n) and use | with x right shifted by n: (x << (x_bits-n)) | (x >> n); */ 24 | //return result; 25 | return (x << (x_bits-n)) | (x >> n); 26 | } 27 | -------------------------------------------------------------------------------- /Chapter3/exercise3.1.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #define SIZE 1000000 4 | #define BILLION 1000000000.0 5 | int binsearch(int x, int v[], int n); 6 | int main(){ 7 | struct timespec start, end; 8 | clock_gettime(CLOCK_REALTIME, &start); 9 | int v[SIZE], x = 9; 10 | for (int i = 0; i 0 && (c=getchar()) != EOF && c != '\n') 10 | s[i++] = c; 11 | if (c == '\n') 12 | s[i++] = c; 13 | s[i] = '\0'; 14 | return i; 15 | } 16 | 17 | int strindex(char s[], char t[]){ 18 | int i, j, k; 19 | for (i = 0; s[i] != '\0'; i++){ 20 | for (j=i, k=0; t[k] != '\0' && s[j]==t[k]; j++, k++) 21 | ; 22 | if (k > 0 && t[k] == '\0') 23 | return i; 24 | } 25 | return -1; 26 | } 27 | 28 | int strrindex(char s[], char t[], int len){ 29 | int i, j, k; 30 | for (i = len-1; i >= 0; i--){ 31 | for (j=i, k=p_len-1; k >= 0 && s[j]==t[k]; j--, k--) 32 | ; 33 | if (k < 0) 34 | return i-p_len+1; 35 | } 36 | return -1; 37 | } 38 | 39 | int exercise4_1(){ 40 | char line[MAXLINE]; 41 | int found = 0; 42 | int i, j; 43 | printf("Please write some lines. All lines with pattern: '%s', are printed back." 44 | "Finish by sending Ctrl+D.\n", pattern); 45 | while ((i = getline_f(line, MAXLINE)) > 0) 46 | if ((j = strrindex(line, pattern, i)) >= 0){ 47 | printf("found pattern in line:\n%sat index: %d\n", line, j); 48 | found++; 49 | } 50 | return found; 51 | } -------------------------------------------------------------------------------- /Chapter3/exercise3.2.c: -------------------------------------------------------------------------------- 1 | #include 2 | void escape(char s[], char t[]); 3 | void escape_revert(char t[], char u[]); 4 | int main (){ 5 | char s[100] = "bamba\tbatman\nwow\0"; 6 | char t[100]; 7 | char u[100]; 8 | printf("result of running escape() on %s\n", s); 9 | escape(s, t); 10 | printf("%s\n", t); 11 | printf("result of runnign escape_revert() on %s\n", t); 12 | escape_revert(t, u); 13 | printf("%s\n", u); 14 | return 0; 15 | } 16 | void escape(char s[], char t[]){ 17 | int i, j; 18 | char c; 19 | for (i = j = 0; (c = s[i]) != '\0'; i++) 20 | switch (c) { 21 | case '\t': 22 | t[j++] = '\\'; 23 | t[j++] = 't'; 24 | break; 25 | case '\n': 26 | t[j++] = '\\'; 27 | t[j++] = 'n'; 28 | break; 29 | default: 30 | t[j++] = c; 31 | break; 32 | } 33 | t[j] = '\0'; 34 | } 35 | void escape_revert(char t[], char u[]){ 36 | int i, j; 37 | char c; 38 | char last_c = '0'; 39 | for (i = j = 0; (c = t[i]) != '\0'; i++){ 40 | switch (c) { 41 | case 'n': 42 | case 't': 43 | printf("In case 't' with char: %c and last_c: %c\n", c, last_c); 44 | if (last_c == '\\'){ 45 | printf("In if\n"); 46 | u[--j] = (c == 'n') ? '\n' : '\t'; 47 | j++; 48 | last_c = c; 49 | break; 50 | } 51 | default: 52 | printf("In default with char: %c and last_c: %c\n", c, last_c); 53 | u[j++] = c; 54 | last_c = c; 55 | break; 56 | } 57 | printf("u: %s\n", u); 58 | } 59 | u[j] = '\0'; 60 | } 61 | -------------------------------------------------------------------------------- /Chapter5/src/exercise5.5.c: -------------------------------------------------------------------------------- 1 | #include "../include/exercise5.5.h" 2 | 3 | int exercise5_5(){ 4 | char s[100] = "0123456789", t[] = "6666666666"; 5 | char *cpy, *cat; 6 | int cmp; 7 | printf("string source: '%s', target: '%s'\n", s, t); 8 | printf("calling strncpy('%s', '%s', %d).\n", s, t, 2); 9 | cpy = strncpyVersion(s, t, 2); 10 | printf("result: '%s'\n", cpy); 11 | printf("calling strncat('%s', '%s', %d).\n", s, t, 4); 12 | cat = strncatVersion(s, t, 4); 13 | printf("result: '%s'\n", cat); 14 | printf("calling strncmp('%s', '%s', %d).\n", s, t, 5); 15 | cmp = strncmpVersion(s, t, 5); 16 | printf("result: %d\n", cmp); 17 | return 0; 18 | } 19 | /* copy at most n characters of string ct to string s. return s. */ 20 | char *strncpyVersion(char *s, char *ct, int n){ 21 | char *ps = s, *pct = ct; 22 | while(n-- > 0 && (*ps++ = *pct++) != '\0') 23 | ; 24 | while (n-- > 0) 25 | *ps = '\0'; 26 | return s; 27 | } 28 | /* copy at most n characters of string ct to end of string s. return s. */ 29 | char *strncatVersion(char *s, char *ct, int n){ 30 | char *ps = s; 31 | strncpyVersion(ps + strlen(ps), ct, n); 32 | return s; 33 | } 34 | /* compare at most n characters of string cs to string ct. 35 | * return <0 if cs0 if cs>ct. */ 36 | int strncmpVersion(char *cs, char *ct, int n){ 37 | char *ps = cs, *pct = ct; 38 | while (n > 0 && *ps == *pct) { 39 | n--; 40 | ps++; 41 | pct++; 42 | } 43 | if (n == 0) 44 | return 0; 45 | return *ps - *pct; 46 | } -------------------------------------------------------------------------------- /Chapter3/exercise3.3.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | void expand(char s1[], char s2[]); 5 | int should_expand(char next_c, char last_c); 6 | int main(){ 7 | char s1[] = "a-g-123-9BF-H-s-w-xZ33T-AB-d"; 8 | char s2[100]; 9 | memset(s2, 0, sizeof s2); 10 | printf("doing expand on string: \n%s\n", s1); 11 | expand(s1, s2); 12 | printf("%s\n", s2); 13 | return 0; 14 | } 15 | void expand(char s1[], char s2[]){ 16 | char c, last_c, next_c, d; 17 | int i, j, k, se; 18 | for (i = j = 0; (c = s1[i]) != '\0'; i++){ 19 | //printf("c: %c\n", c); 20 | switch (c){ 21 | case '-': 22 | //printf("In '-'\n"); 23 | next_c = s1[++i]; 24 | se = should_expand(next_c, last_c); 25 | //printf("last_c: %c, next_c: %c, se: %d\n", last_c, next_c, se); 26 | if (se){ 27 | for (last_c++; last_c <= next_c; last_c++){ 28 | //printf("In for, last_c is: %c\n", last_c); 29 | s2[j++] = last_c; 30 | } 31 | c = --last_c; 32 | break; 33 | } 34 | else 35 | i--; // don't break, put '-' in string. 36 | default: 37 | s2[j++] = c; 38 | break; 39 | 40 | } 41 | last_c = c; 42 | //printf("s2: %s\n", s2); 43 | } 44 | } 45 | /* 46 | Check wether both last and next chars are: 47 | 1. digits, and next is higher or equal, return 1 48 | 2. alphabet, same case, and next is higher or equal, return 1 49 | 3. other cases, return 0 50 | */ 51 | int should_expand(char n, char l){ 52 | if (isdigit(l) && isdigit(n) && l <= n) 53 | return 1; 54 | else if (isalpha(l) && isalpha(n) && isupper(l) == isupper(n) && l <= n) 55 | return 1; 56 | else 57 | return 0; 58 | } 59 | -------------------------------------------------------------------------------- /Chapter5/src/exercise5.10.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #define STK_SIZE 20 5 | static int stk_ptr = 0; 6 | static int stack[STK_SIZE]; /* stack and stack pointer */ 7 | void push (int val) 8 | { 9 | if (stk_ptr >= STK_SIZE) 10 | { 11 | printf ("push: stack overflow\n"); 12 | exit (1); 13 | } 14 | else 15 | stack[stk_ptr++] = val; 16 | } 17 | int pop(void) 18 | { 19 | if (stk_ptr <= 0) 20 | { 21 | printf("pop: stack underflow\n"); 22 | exit(2); 23 | } 24 | return(stack[--stk_ptr]); 25 | } 26 | int exercise5_10(int argc, char *argv[]) 27 | { 28 | int i; 29 | int tmp; 30 | if (argc == 1) /* no arguments */ 31 | return (0); 32 | for (i = 1; i < argc; i++){ 33 | printf("argv[%d]: %c\n", i, *argv[i]); 34 | if (isdigit(*argv[i])) 35 | push(atoi(argv[i])); 36 | else 37 | switch(*argv[i]) 38 | { 39 | case '+': 40 | push (pop( ) + pop( )); 41 | break; 42 | case 'x': 43 | push (pop( ) * pop( )); 44 | break; 45 | case '-': 46 | tmp = pop( ); 47 | push (pop( ) - tmp); 48 | break; 49 | case '/': 50 | tmp = pop( ); 51 | push (pop( ) / tmp); 52 | break; 53 | case ' ': 54 | break; 55 | default: 56 | printf ("Unknown operator %c\n", *argv[i]); 57 | break; 58 | } 59 | } 60 | 61 | printf ("%d\n", pop( )); 62 | return 0; 63 | } -------------------------------------------------------------------------------- /Chapter4/src/exercise4_3_to_5.c: -------------------------------------------------------------------------------- 1 | #include "exercise4_3_to_5.h" 2 | 3 | int sp = 0; /* next free stack position */ 4 | double val [MAXVAL]; /* value stack */ 5 | char buf[BUFSIZE]; /* buffer for ungetch */ 6 | int bufp = 0; /* next free position in buf */ 7 | double t; /* varialbe for top element */ 8 | 9 | /* push: push f onto value stack */ 10 | void push (double f) 11 | { 12 | if (sp < MAXVAL) 13 | val[sp++] = f; 14 | else 15 | printf ("Error: stack is full, can't push %g\n", f); 16 | } 17 | 18 | /* pop: pop and return top value from stack */ 19 | double pop(void) 20 | { 21 | if (sp > 0) 22 | return (val[--sp]); 23 | else 24 | { 25 | printf ("Error: stack empty\n"); 26 | return 0.0; 27 | } 28 | } 29 | 30 | /* top: print top element without poping it. */ 31 | double top(){ 32 | t = '\0'; 33 | if (sp > 0){ 34 | t = val[--sp]; 35 | sp++; 36 | printf("\t%.8g\n", t); 37 | } 38 | else 39 | printf ("Error: stack empty\n"); 40 | return t; 41 | } 42 | 43 | /* duplicate: print top element, then push a copy of it. */ 44 | void duplicate(){ 45 | t = top(); 46 | if (t != '\0') 47 | push(t); 48 | } 49 | 50 | /* swap: swap top two elements (if they exist), and print them. */ 51 | void swap(){ 52 | if (sp > 1){ 53 | sp -= 2; 54 | t = val[sp]; 55 | printf("\t%.8g ", (val[sp] = val[++sp])); 56 | printf("%.8g\n", (val[sp++] = t)); 57 | } 58 | else 59 | printf ("Error: stack doesn't contain two elements.\n"); 60 | } 61 | 62 | /* clear: clears the stack. */ 63 | void clear(){ 64 | sp = 0; 65 | val[sp] = '\0'; 66 | printf ("Stack is cleared.\n"); 67 | } 68 | 69 | /* getop: get next operator or numeric operand */ 70 | int getop (char s[]) 71 | { 72 | int i, c; 73 | while ((s[0] = c = my_getch()) == ' ' || c == '\t') 74 | ; 75 | s[1] = '\0'; 76 | if (!isdigit(c) && c != '.' && c != '-') 77 | return c; /* not a number */ 78 | i = 0; 79 | if (isdigit(c)) /* collect integer part */ 80 | while (isdigit(s[++i] = c = my_getch())) 81 | ; 82 | if (c == '-') 83 | { 84 | while (isdigit(s[++i] = c = my_getch())) 85 | ; 86 | if (i == 1 && c != '.') /* minus sign alone */ 87 | { 88 | my_ungetch(c); 89 | return '-'; 90 | } 91 | } 92 | if (c == '.') /* collect fraction part */ 93 | while (isdigit(s[++i] = c = my_getch())) 94 | ; 95 | s[i] = '\0'; 96 | if (c != EOF) 97 | my_ungetch (c); 98 | return NUMBER; 99 | } 100 | 101 | int my_getch(void) /* get a (possibly pushed back) character */ 102 | { 103 | return (bufp > 0 ? buf[--bufp] : getchar()); 104 | } 105 | 106 | void my_ungetch (int c) /* push character back on input */ 107 | { 108 | if (bufp >= BUFSIZE) 109 | printf ("ungetch: too many characters\n"); 110 | else 111 | buf[bufp++] = c; 112 | } 113 | 114 | int exercise4_3_8() /* reverse Polish calculator */ 115 | { 116 | int type; 117 | double op2; 118 | char s[MAXOP]; 119 | char last_type; 120 | while((type = getop(s)) != EOF){ 121 | switch(type) 122 | { 123 | case NUMBER: 124 | push(atof(s)); 125 | break; 126 | case '+': 127 | push(pop() + pop()); 128 | break; 129 | case '*': 130 | push(pop() * pop()); 131 | break; 132 | case '-': 133 | op2 = pop(); 134 | push(pop() - op2); 135 | break; 136 | case '/': 137 | op2 = pop(); 138 | if (op2 != 0.0) 139 | push(pop() / op2); 140 | else 141 | printf("Error: zero devisor\n"); 142 | break; 143 | case '%': 144 | op2 = pop(); 145 | if (op2 != 0.0) 146 | push((int) pop() % (int) op2); 147 | else 148 | printf("Error: zero devisor\n"); 149 | break; 150 | case '\n': 151 | if (last_type != 't' && last_type != 'd' && last_type != 's' && last_type != 'c') 152 | printf("\t%.8g\n", pop()); 153 | break; 154 | case 't': /* print top element */ 155 | (void) top(); 156 | break; 157 | case 'd': /* duplicate top element */ 158 | duplicate(); 159 | break; 160 | case 's': /* swap top two elemnts */ 161 | swap(); 162 | break; 163 | case 'c': /* clear stack */ 164 | clear(); 165 | break; 166 | default: 167 | printf("Error: unknown command\n"); 168 | break; 169 | } 170 | last_type = type; 171 | } 172 | 173 | return 0; 174 | } --------------------------------------------------------------------------------