mirror of
https://github.com/krislamo/knrc.git
synced 2025-09-14 01:19:29 +00:00
Compare commits
16 Commits
93acdbe7e9
...
main
Author | SHA1 | Date | |
---|---|---|---|
da9de48bac
|
|||
c6070adb67
|
|||
42a0b5413b
|
|||
f254c6e67e
|
|||
9fd4d3db68
|
|||
7825fcd167
|
|||
984c019e14
|
|||
f232d42ece
|
|||
04ac11bea1
|
|||
35dae8afaf
|
|||
0cc240323a
|
|||
0c2d0fae2b
|
|||
d064db2b17
|
|||
c24a724efb
|
|||
d8a70f8b58
|
|||
a8ca4de90f
|
@@ -4,6 +4,8 @@
|
|||||||
#define UPPER 300
|
#define UPPER 300
|
||||||
#define STEP 20
|
#define STEP 20
|
||||||
|
|
||||||
|
float convert(int f);
|
||||||
|
|
||||||
/* print Fahrenheit-Celsius table */
|
/* print Fahrenheit-Celsius table */
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
@@ -11,5 +13,10 @@ int main()
|
|||||||
|
|
||||||
printf("Fahrenheit\tCelsius\n=======================\n");
|
printf("Fahrenheit\tCelsius\n=======================\n");
|
||||||
for (fahr = UPPER; fahr >= LOWER; fahr = fahr - STEP)
|
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);
|
||||||
}
|
}
|
||||||
|
16
07-extra-blanks.c
Normal file
16
07-extra-blanks.c
Normal 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
17
08-unambiguous.c
Normal 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
25
09-word-count.c
Normal 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
14
10-one-word-per-line.c
Normal 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
26
11-arrays.c
Normal 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
55
12-histogram.c
Normal 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
24
13-functions.c
Normal 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
81
14-char-arrays.c
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
24
Makefile
24
Makefile
@@ -1,22 +1,10 @@
|
|||||||
all: hello mathvars c2f input count countlines
|
CC = gcc
|
||||||
|
OBJS = $(patsubst %.c,bin/%,$(wildcard *.c))
|
||||||
|
|
||||||
hello: 01-hello-world.c
|
all: $(OBJS)
|
||||||
gcc -o ./bin/01-helloworld 01-hello-world.c
|
|
||||||
|
|
||||||
mathvars: 02-vars-and-math.c
|
$(OBJS):
|
||||||
gcc -o ./bin/02-mathvars 02-vars-and-math.c
|
$(CC) -o ./$@ $(patsubst bin/%,%.c,$@) -ansi
|
||||||
|
|
||||||
c2f: 03-celsius-to-fahrenheit.c
|
|
||||||
gcc -o ./bin/03-c2f 03-celsius-to-fahrenheit.c
|
|
||||||
|
|
||||||
input: 04-file-copying.c
|
|
||||||
gcc -o ./bin/04-input 04-file-copying.c
|
|
||||||
|
|
||||||
count: 05-char-counting.c
|
|
||||||
gcc -o ./bin/05-count 05-char-counting.c
|
|
||||||
|
|
||||||
countlines: 06-line-count.c
|
|
||||||
gcc -o ./bin/06-line-count 06-line-count.c
|
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
$(RM) ./bin/*-*
|
$(RM) ./bin/*
|
||||||
|
65
README.adoc
65
README.adoc
@@ -152,3 +152,68 @@ https://github.com/krislamo/knrc/commit/588969b09fabab1e91ff4f4b1c37e87fc23cf76b
|
|||||||
https://github.com/krislamo/knrc/blob/588969b09fabab1e91ff4f4b1c37e87fc23cf76b/02-vars-and-math.c[(Source)]
|
https://github.com/krislamo/knrc/blob/588969b09fabab1e91ff4f4b1c37e87fc23cf76b/02-vars-and-math.c[(Source)]
|
||||||
|
|
||||||
Move some numbers around to reverse the table.
|
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
|
||||||
|
$
|
||||||
|
----
|
||||||
|
Reference in New Issue
Block a user