1
0
mirror of https://github.com/krislamo/knrc.git synced 2025-09-14 01:19:29 +00:00

Compare commits

..

23 Commits

Author SHA1 Message Date
da9de48bac Exercise 1-19. Write a function reverse(s) 2021-12-02 20:29:00 -05:00
c6070adb67 Exercise 1-18. Remove trailing blanks and tabs 2021-11-30 01:43:24 -05:00
42a0b5413b Exercise 1-17. Print input lines greater than 80 2021-11-30 00:04:36 -05:00
f254c6e67e Exercise 1-16. Print arbitrarily long input lines 2021-11-29 22:53:44 -05:00
9fd4d3db68 Character array example 2021-11-23 20:48:37 -05:00
7825fcd167 Exercise 1-15. Make temp. conversion function 2021-11-23 00:52:13 -05:00
984c019e14 Function example 2021-11-23 00:31:57 -05:00
f232d42ece Exercise 1-13. Vertical histogram 2021-11-20 02:40:30 -05:00
04ac11bea1 Exercise 1-13. Horizontal histogram 2021-11-20 00:26:15 -05:00
35dae8afaf Arrays example 2021-11-14 21:02:09 -05:00
0cc240323a Exercise 1-12. one word per line 2021-11-13 00:30:40 -05:00
0c2d0fae2b Exercise 1-11. test the word count program 2021-11-12 23:55:56 -05:00
d064db2b17 Word counting example 2021-11-12 22:58:29 -05:00
c24a724efb Simplify Makefile to compile all C sources 2021-11-12 21:02:45 -05:00
d8a70f8b58 Exercise 1-10. replace with escape codes 2021-11-11 23:30:43 -05:00
a8ca4de90f Exercise 1-9. Replace multiple blanks with one 2021-11-11 23:04:06 -05:00
93acdbe7e9 Exercise 1-8. count blanks, tabs, and newlines 2021-11-09 00:51:39 -05:00
6bf093f44f Line counting example 2021-11-09 00:36:42 -05:00
3512ae49ab Character counting example v2 2021-11-09 00:20:01 -05:00
72093b7232 Character counting example 2021-11-05 21:29:39 -04:00
979467565f Exercise 1-6 and 1-7. expression = 1, and EOF = -1 2021-11-05 21:17:39 -04:00
7099cc732e Example of a cleaner IO implementation 2021-11-05 21:01:14 -04:00
dcdc220259 Textbook example of copying input into output 2021-11-05 20:54:19 -04:00
14 changed files with 380 additions and 10 deletions

View File

@@ -4,6 +4,8 @@
#define UPPER 300
#define STEP 20
float convert(int f);
/* print Fahrenheit-Celsius table */
int main()
{
@@ -11,5 +13,10 @@ int main()
printf("Fahrenheit\tCelsius\n=======================\n");
for (fahr = UPPER; fahr >= LOWER; fahr = fahr - STEP)
printf("%3d\t\t%7.1f\n", fahr, (5.0/9.0)*(fahr-32));
printf("%3d\t\t%7.1f\n", fahr, convert(fahr));
}
float convert(int fahr)
{
return (5.0/9.0)*(fahr-32);
}

12
04-file-copying.c Normal file
View File

@@ -0,0 +1,12 @@
#include <stdio.h>
/* Exercise 1-6 and 1-7 */
int main()
{
int c;
printf("EOF = %d\n", EOF);
while (c = getchar() != EOF)
printf("c = %d\n", c);
}

10
05-char-counting.c Normal file
View File

@@ -0,0 +1,10 @@
#include <stdio.h>
int main()
{
double nc;
for (nc = 0; getchar() != EOF; ++nc)
;
printf("%.0f\n", nc);
}

21
06-line-count.c Normal file
View File

@@ -0,0 +1,21 @@
#include <stdio.h>
int main()
{
int c, b, t, n;
b = 0;
t = 0;
n = 0;
while ((c = getchar ()) != EOF) {
if (c == ' ')
++b;
if (c == '\t')
++t;
if (c == '\n')
++n;
}
printf("blanks = %d\n", b);
printf("tabs = %d\n", t);
printf("newlines = %d\n", n);
}

16
07-extra-blanks.c Normal file
View File

@@ -0,0 +1,16 @@
#include <stdio.h>
int main()
{
int c;
int l;
while ((c = getchar()) != EOF) {
if (c == ' ' && c == l) {
;
} else {
printf("%c", c);
l = c;
}
}
}

17
08-unambiguous.c Normal file
View File

@@ -0,0 +1,17 @@
#include <stdio.h>
int main()
{
int c;
while ((c = getchar()) != EOF) {
if (c == '\t')
printf("\\t");
else if (c == '\b')
printf("\\b");
else if (c == '\\')
printf("\\\\");
else
printf("%c", c);
}
}

25
09-word-count.c Normal file
View File

@@ -0,0 +1,25 @@
#include <stdio.h>
#define IN 1
#define OUT 0
/* count lines, words, and characters in input */
int main()
{
int c, nl, nw, nc, state;
state = OUT;
nl = nw = nc = 0;
while ((c = getchar()) != EOF) {
++nc;
if (c == '\n')
++nl;
if (c == ' ' || c == '\n' || c == '\t')
state = OUT;
else if (state == OUT) {
state = IN;
++nw;
}
}
printf("%d %d %d\n", nl, nw, nc);
}

14
10-one-word-per-line.c Normal file
View File

@@ -0,0 +1,14 @@
#include <stdio.h>
int main()
{
int c;
while((c = getchar()) != EOF) {
if (c == ' ' || c == '\n' || c == '\t')
printf("\n");
else
printf("%c", c);
}
}

26
11-arrays.c Normal file
View File

@@ -0,0 +1,26 @@
#include <stdio.h>
/* count digits, white space, others */
int main()
{
int c, i, nwhite, nother;
int ndigit[10];
nwhite = nother = 0;
for(i = 0; i < 10; ++i)
ndigit[i] = 0;
while ((c = getchar()) != EOF)
if (c >= '0' && c <= '9')
++ndigit[c-'0'];
else if (c == ' ' || c == '\n' || c == '\t')
++nwhite;
else
++nother;
printf("digits =");
for (i = 0; i < 10; ++i)
printf(" %d", ndigit[i]);
printf(", white space = %d, other = %d\n",
nwhite, nother);
}

55
12-histogram.c Normal file
View File

@@ -0,0 +1,55 @@
#include <stdio.h>
#define IN 1
#define OUT 0
#define MAXERROR "WARNING: Word length '%d' exceeded 99\n"
int main()
{
int c, i, j, state, count, top;
int wordl[100];
count = state = top = OUT;
for(i = 0; i < 100; ++i)
wordl[i] = 0;
while ((c = getchar()) != EOF) {
if (c == ' ' || c == '\n' || c == '\t') {
state = OUT;
if (count > 0 && count < 100)
++wordl[count];
else if (count >= 100)
printf(MAXERROR, count);
count = 0;
} else {
++count;
if (state == OUT)
state = IN;
}
}
if (count > 0 && count < 100)
++wordl[count];
else if (count >= 100)
printf(MAXERROR, count);
for (i = 0; i < 100; ++i)
if (wordl[i] > top)
top = wordl[i];
printf("\n");
for (i = 0; i < 100; ++i)
if (wordl[i] > 0)
printf("%2d ", i);
printf("\n");
for (i = 1; i <= top; ++i) {
for (j = 0; j < 100; ++j) {
if (wordl[j] >= i)
printf(" # ");
else if (wordl[j] > 0)
printf(" ");
}
printf("\n");
}
}

24
13-functions.c Normal file
View File

@@ -0,0 +1,24 @@
#include <stdio.h>
int power(int m, int n);
/* test power function */
int main()
{
int i;
for (i = 0; i < 10; ++i)
printf("%d %d %d\n", i, power(2,i), power(-3,i));
return 0;
}
/* power: raise base to n-th power; n >= 0 */
int power(int base, int n)
{
int i, p;
p = 1;
for (i = 1; i <= n; ++i)
p = p * base;
return p;
}

81
14-char-arrays.c Normal file
View File

@@ -0,0 +1,81 @@
#include <stdio.h>
#define MAXLINE 1000 /* maximum input line size */
#define MINLINE 0
int getline(char line[], int maxline);
void copy(char to[], char from[]);
void trim(char line[], int len);
void reverse(char to[], char from[], int len);
/* print longest input line */
main()
{
int len;
int max;
int c;
char line[MAXLINE];
char enil[MAXLINE];
max = 0;
while((len = getline(line, MAXLINE)) > 0) {
if (line[len-1] != '\n') {
while ((c=getchar())!=EOF && c!='\n')
++len;
if (c == '\n')
++len;
}
if (len >= MINLINE) {
reverse(enil, line, len);
printf("%s", enil);
}
}
return 0;
}
/* getline: read a line into s, return length */
int getline(char s[], int lim)
{
int c, i;
for (i=0; i<lim-1 && (c=getchar())!=EOF && c!='\n'; ++i)
s[i] = c;
if (c == '\n') {
s[i] = c;
++i;
}
s[i] = '\0';
return i;
}
/* copy: copy 'from' into 'to': assume to is big enough */
void copy(char to[], char from[])
{
int i;
i = 0;
while ((to[i] = from[i]) != '\0')
++i;
}
/* trim: remove extraneous blanks and tabs */
void trim(char s[], int len)
{
int i;
for (i = len-2; i >= 0; --i)
if (s[i] == ' ' || s[i] == '\t')
s[i] = '\0';
else
return;
}
void reverse(char to[], char from[], int len)
{
int i;
i = 0;
while ((to[i] = from[len-1]) != '\0') {
++i;
--len;
}
}

View File

@@ -1,13 +1,10 @@
all: hello mathvars c2f
CC = gcc
OBJS = $(patsubst %.c,bin/%,$(wildcard *.c))
hello: 01-hello-world.c
gcc -o ./bin/01-helloworld 01-hello-world.c
all: $(OBJS)
mathvars: 02-vars-and-math.c
gcc -o ./bin/02-mathvars 02-vars-and-math.c
c2f: 03-celsius-to-fahrenheit.c
gcc -o ./bin/03-c2f 03-celsius-to-fahrenheit.c
$(OBJS):
$(CC) -o ./$@ $(patsubst bin/%,%.c,$@) -ansi
clean:
$(RM) ./bin/*-*
$(RM) ./bin/*

View File

@@ -152,3 +152,68 @@ https://github.com/krislamo/knrc/commit/588969b09fabab1e91ff4f4b1c37e87fc23cf76b
https://github.com/krislamo/knrc/blob/588969b09fabab1e91ff4f4b1c37e87fc23cf76b/02-vars-and-math.c[(Source)]
Move some numbers around to reverse the table.
09-word-count.c
~~~~~~~~~~~~~~~~
https://github.com/krislamo/knrc/commit/d064db2b171e3da74fd5082ae29456f64caeafeb[(Diff)]
https://github.com/krislamo/knrc/blob/d064db2b171e3da74fd5082ae29456f64caeafeb/09-word-count.c[(Source)]
Textbook example of counting lines, words, and characters from input.
Exercise 1-11: test the word count program
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
No input:
----
$ ./bin/09-word-count
0 0 0
$
----
Just 3 newlines:
----
$ ./bin/09-word-count
3 0 3
$
----
Just 3 tabs:
----
$ ./bin/09-word-count
0 0 3
$
----
Just 3 spaces:
----
$ ./bin/09-word-count
0 0 3
$
----
Just a single word per line:
----
$ ./bin/09-word-count
one
word
per
line
4 4 18
$
----
Three blanks before and after:
----
$ ./bin/09-word-count
three blanks before/after 0 3 31
$
----