1
0
mirror of https://github.com/krislamo/knrc.git synced 2025-09-16 18:19:28 +00:00

Compare commits

...

11 Commits

7 changed files with 209 additions and 2 deletions

View File

@@ -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);
} }

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

@@ -4,7 +4,7 @@ OBJS = $(patsubst %.c,bin/%,$(wildcard *.c))
all: $(OBJS) all: $(OBJS)
$(OBJS): $(OBJS):
$(CC) -o ./$@ $(patsubst bin/%,%.c,$@) $(CC) -o ./$@ $(patsubst bin/%,%.c,$@) -ansi
clean: clean:
$(RM) ./bin/* $(RM) ./bin/*