C程序设计语言(第二版)习题:第一章

时间:2021-12-20 17:06:56

第一章虽然感觉不像是个习题。但是我还是认真去做,去想,仅此而已!

练习 1-1

Run the "hello, world" program on your system. Experiment with leaving out parts of the program, to see what error messages you get. 

 #include <stdio.h>
int main(int argc, char const *argv[])
{
printf("Hello, world\n");
return ;
}

收获 : 学习一门新程序设计语言的唯一途径就是使用它编写程序!

练习 1-2

Experiment to find out what happens when printf 's argument string contains \c, where c is some character not listed above. 

 #include <stdio.h>
int main(int argc, char const *argv[])
{
printf("\c\n");
return ;
}

chap1.1.c:4:9: warning: unknown escape sequence '\c'
printf("\c\n");
^
1 warning generated.

练习 1-3

Modify the temperature conversion program to print a heading above the table.

 #include <stdio.h>
#include <stdlib.h> int main()
{
float fahr, celsius;
int lower, upper, step; lower = ;
upper = ;
step = ; fahr = lower;
printf("%3s\t%6s\n", "F", "C");
while(fahr <= upper) {
celsius = (5.0/9.0) *(fahr-32.0);
printf("%3.0f\t%6.1f\n", fahr, celsius);
fahr = fahr + step;
}
return ;
}

练习 1-4

Write a program to print the corresponding Celsius to Fahrenheit table.

 #include <stdio.h>
#include <stdlib.h> int main()
{
float fahr, celsius;
int lower, upper, step; lower = -;
upper = ;
step = ; celsius = lower;
printf("%3s\t%6s\n", "C", "F");
while(celsius <= upper) {
fahr = celsius * (9.0/5.0) + ;
printf("%3.0f\t%6.1f\n", celsius, fahr);
celsius = celsius + step;
}
return ;
}

练习 1-5

Modify the temperature conversion program to print the table in reverse order, that is, from 300 degrees to 0. 

 #include <stdio.h>
int main()
{
int fahr; for (fahr = ; fahr >= ; fahr = fahr - )
printf("%3d %6.1f\n", fahr, (5.0/9.0)*(fahr-)); return ;
}

练习 1-6

Verify that the expression getchar()!=EOF is 0 or 1. 

#include <stdio.h>

int main(void)
{
printf("getchar() != EOF evaluates to %d\n", getchar() != EOF);
return ;
}

Ps : 不输入是 0 , 输入是 1.

练习 1-7

Write a program to print the value of EOF

 #include <stdio.h>

 int main() {
printf("EOF = %d\n", EOF);
return ;
}

ps : EOF = -1

练习 1-8

Write a program to count blanks, tabs, and newlines.

 #include <stdio.h>

 int main(int argc, char const *argv[])
{
int c, nl, tab, space;
while((c = getchar()) != -){
if(c == '\n')
++nl;
else if(c == '\t')
++tab;
else if(c == ' ')
++space;
}
printf("nl: %d tab: %d space: %d\n", nl, tab, space);
return ;
}

练习 1-9

Write a program to copy its input to its output, replacing each string of one or more blanks by a single blank.

 #include <stdio.h>

 int main(int argc, char const *argv[])
{
int c, pre;
pre = -;
while((c = getchar()) != -){
if(c == ' ' && pre == ' '){
continue;
}
putchar(c);
pre = c;
}
return ;
}

ps : 可以用flag 实现

练习 1-10

Write a program to copy its input to its output, replacing each tab by \t , each backspace by \b , and each backslash by \\ . This makes tabs and backspaces visible in an unambiguous way.

 #include <stdio.h>

 int main(int argc, char const *argv[])
{
int c;
while((c = getchar()) != -){
if(c == '\t')
printf("\\t");
else if(c == '\b')
printf("\\b");
else if(c == '\\')
printf("\\\\");
else putchar(c);
}
return ;
}

练习 1-11

How would you test the word count program? What kinds of input are most likely to uncover bugs if there are any? 

 #include <stdio.h>

 int main(int argc, char const *argv[])
{
int c, nl, nw, nc, flag; flag = ;
nl = nw = nc = ;
while((c = getchar()) != -){
++nc;
if(c == '\n')
++nl;
if(c == ' ' || c == '\n' || c == '\t')
flag = ;
else if(flag == ){
flag = ;
++nw;
}
}
printf("%d %d %d\n", nl, nw, nc);
return ;
}

ps:

0. input file contains zero words
1. input file contains 1 enormous word without any newlines
2. input file contains all white space without newlines
3. input file contains 66000 newlines
4. input file contains word/{huge sequence of whitespace of different kinds}/word
5. input file contains 66000 single letter words, 66 to the line
6. input file contains 66000 words without any newlines
7. input file is /usr/dict contents (or equivalent)
8. input file is full collection of moby words
9. input file is binary (e.g. its own executable)
10. input file is /dev/nul (or equivalent)

练习 1-12

Write a program that prints its input one word per line.

 #include <stdio.h>

 int main(int argc, char const *argv[])
{
int c;
int pre;
pre = -;
while((c = getchar()) != -){
if(pre == - && c == ' ') continue;
if(c == ' ' && pre == ' ') continue;
else if(c == ' ') printf("\n");
else putchar(c);
pre = c;
}
return ;
}

练习 1-13

Write a program to print a histogram of the lengths of words in its input. It is easy to draw the histogram with the bars horizontal; a vertical orientation is more challenging.

 //水平直方图

 #include <stdio.h>
#define MAX 10 int main()
{
int len[MAX];
int c;
int num;
int flag = ;
int i; for(i = ; i < MAX; i++) len[i] = ; while((c = getchar()) != EOF){
if(c != ' ' && c != '\t' && c != '\n'){
if(flag == ) num = ;
else num++; flag = ;
}
else{
len[num]++;
num = ;
}
}
for(num = ; num < MAX; num++){
if(len[num] != ){
printf("%3d ", num);
for(i = ; i <= len[num]; i++)
putchar('*'); putchar('\n');
}
}
}
//垂直直方图
# include <stdio.h>
# define MAX
# define MAXLEN int main(void){
int length[MAX];
int c;
int num;
int i;
int r; for(i = ; i < MAX; i++){
length[i] = ;
}
num = ;
while((c = getchar()) != EOF){
if((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') ){
num++;
} else{
length[num]++;
num = ;
}
}
printf("1 2 3 4 5 6 7 8 9 10 ....\n");
for(r = ; r < MAXLEN; r++){
for(i = ; i < MAX; i++){
if(r <= length[i]){
printf("* ");
} else{
printf(" ");
}
}
putchar('\n');
}
}

练习 1-14

Write a program to print a histogram of the frequencies of different characters in its input. 

 //水平
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define N 256 void print() {
int r[N] = {};
int i, j, c;
while ((c = getchar()) != -) {
if(c == ' ' || c == '\t' || c == '\n' || c == '\r') continue;
r[c]++;
}
for (i = ; i < N; i++) {
if (r[i]) {
printf("%c| ", i);
for (j = ; j < r[i]; j++) printf("*");
printf("\n");
}
}
}
int main()
{ print();
return ;
}

练习 1-15

Rewrite the temperature conversion program of Section 1.2 to use a function for conversion. 

 /* 重新编写1.2 温度转换程序,使用函数实现温度转换程序 */

 #include <stdio.h>

 void change(int fahr){
printf("%3d %6.1f\n", fahr, (5.0/9.0)*(fahr-));
} int main(int argc, char const *argv[])
{
int fahr;
while(scanf("%d", &fahr) != -)
change(fahr); return ;
}

练习 1-16

Revise the main routine of the longest-line program so it will correctly print the length of arbitrarily long input lines, and as much as possible of the text.

 /* 修改打印最长行的主程序 main 使之可以打印任意长度的输入行的长度, 并尽可能多的打印 */

 #include <stdio.h>
#define MAXLINE 1000
void copy(char to[], char from[]){
int i;
i = ;
while((to[i] = from[i]) != '\0')
++i;
} int _getline(char s[], int lim){
int c, i;
for(i=;i<lim- && (c = getchar()) != - && c!='\n'; ++i)
s[i] = c;
s[i] = '\0';
return i;
} void printlength(int realLen){
int len;
char line[MAXLINE];
char realline[MAXLINE];
while((len = _getline(line, realLen)) > ){
printf("%s", line);
printf("\n");
}
} int main(int argc, char const *argv[])
{
printlength();
return ;
}

练习1-17

Write a program to print all input lines that are longer than 80 characters.

 /* 编写一个程序,打印长度大于80个字符的所有输入行 */

 #include <stdio.h>
#define MAXLINE 1000 int _getline(char s[], int lim){
int c, i;
for(i=; i<lim- && (c = getchar()) != - && c != '\n' ; ++i)
s[i] = c;
if(c == '\n'){
s[i] = c;
++i;
}
s[i] = '\0';
return i;
} void printfLen(){
char line[MAXLINE];
char longest[MAXLINE];
int len;
int tmp;
while((len = _getline(line, MAXLINE)) > )
if(len > )
printf("%s", line);
} int main(int argc, char const *argv[])
{
printfLen();
return ;
}

练习1-18

Write a program to remove all trailing blanks and tabs from each line of input, and to delete entirely blank lines.

/* Write a program to remove all trailing blanks and tabs from each line of input, and to delete entirely blank lines. */

#include <stdio.h>
#define MAXLENGTH 1000
int len, newline;
char line[MAXLENGTH];
int gline(char line[]);
int main()
{
while ((len = gline(line)) > ) {
if (line[len - ] == '\n') {
newline = ;
len--;
}
else
newline = ;
while (len > && (line[len - ] == ' ' || line [len - ] == '\t'))
len--;
if (len != )
{
if (newline == ) {
line[len] = '\n';
len++;
}
line[len] = ;
printf("%s", line);
}
}
return ;
}
int gline(char s[])
{
int c, i;
for (i = ; (c = getchar()) != EOF && c != '\n'; i++)
s[i] = c;
if (c == '\n') {
s[i] = c;
i++;
}
s[i] = '\0';
return i;
}

练习1-19

Write a function reverse(s) that reverses the character string s . Use it to write a program that reverses its input a line at a time.

/* Write a function reverse(s) that reverses the character string s . Use it to write a program that reverses its input a line at a time */

#include <stdio.h>
#include <string.h>
char s[];
void swap(int a, int b){
int tmp;
tmp = s[a];
s[a] = s[b];
s[b] = tmp;
} void reverse(char a[]){
int i, p, len;
len = strlen(a);
for(i=,p=len-;i<p;++i,--p){
printf("%d ", p);
swap(i, p);
}
} int main(int argc, char const *argv[])
{
scanf("%s", s);
reverse(s);
printf("%s", s);
printf("\n"); return ;
}

练习1-20

Write a program detab that replaces tabs in the input with the proper number of blanks to space to the next tab stop. Assume a fixed set of tab stops, say every n columns. Should n be a variable or a symbolic parameter?

不是很懂

练习1-21

Write a program entab that replaces strings of blanks with the minimum number of tabs and blanks to achieve the same spacing. Use the same stops as for detab . When either a tab or a single blank would suffice to reach a tab stop, which should be given preference? 

 #include <stdio.h>

 int main() {
int i, c, nwhite, ntab; nwhite = ;
while((c = getchar()) != EOF) {
if(c == ' ') {
++nwhite;
}
else {
putchar(c);
if(nwhite != ) {
ntab = nwhite / ;
nwhite = nwhite % ;
for(i = ; i < ntab; i++)
putchar('\t');
for(i = ; i < nwhite; i++)
putchar(' ');
nwhite = ;
}
}
}
return ;
}

练习1-22

Write a program to "fold" long input lines into two or more shorter lines after the last non-blank character that occurs before the n -th column of input. Make sure your program does something intelligent with very long lines, and if there are no blanks or tabs before the specified column. 

 #include <stdio.h>

 #define MAXLINE 1000
char line[MAXLINE];
int getline(void);
int main()
{
int t,len;
int location,spaceholder;
const int FOLDLENGTH=; /* The max length of a line */ while (( len = getline()) > )
{
if( len < FOLDLENGTH )
{
}
else
{ t = ;
location = ;
while(t<len)
{
if(line[t] == ' ')
spaceholder = t; if(location==FOLDLENGTH)
{
line[spaceholder] = '\n';
location = ;
}
location++;
t++;
}
}
printf ( "%s", line);
}
return ;
} int getline(void)
{
int c, i;
extern char line[]; for ( i=;i<MAXLINE- && ( c=getchar()) != EOF && c != '\n'; ++i)
line[i] = c;
if(c == '\n')
{
line[i] = c;
++i;
}
line[i] = '\0';
return i; }

练习1-23

Write a program to remove all comments from a C program. Don't forget to handle quoted strings and character constants properly. C comments do not nest. 

 #include <stdio.h>
#define IN 1
#define OUT 0 int main() {
int c, state;
int s1, s2, e1, e2; state = OUT; while((c = getchar()) != EOF) {
if(c == '/')
s1 = ;
else if(c != '*' && s1 == )
s1 = ;
else if(c == '*' && s1 == )
state = IN;
else if(c == '*' && state == IN)
e1 = ;
else if(c != '/' && e1 == )
e1 = ;
else if(c == '/' && e1 == )
state == OUT;
else if(state == OUT)
putchar(c);
}
return ;
}

练习1-24

Write a program to check a C program for rudimentary syntax errors like unbalanced parentheses, brackets and braces. Don't forget about quotes, both single and double, escape sequences, and comments. (This program is hard if you do it in full generality.) 

 #include <stdio.h>
#define MAXLINE 1000 char line[MAXLINE];
int getline(void); int main()
{
int len=;
int t=;
int brace=, bracket=, parenthesis=;
int s_quote=, d_quote=; while ((len = getline()) > )
{
t=;
while(t < len)
{
if( line[t] == '[')
{
brace++;
}
if( line[t] == ']')
{
brace--;
}
if( line[t] == '(')
{
parenthesis++;
}
if( line[t] == ')')
{
parenthesis--;
}
if( line[t] == '\'')
{
s_quote *= -;
}
if( line[t] == '"')
{
d_quote *= -;
}
t++;
}
}
if(d_quote !=)
printf ("Mismatching double quote mark\n");
if(s_quote !=)
printf ("Mismatching single quote mark\n");
if(parenthesis != )
printf ("Mismatching parenthesis\n");
if(brace != )
printf ("Mismatching brace mark\n");
if(bracket != )
printf ("Mismatching bracket mark\n");
if( bracket== && brace== && parenthesis== && s_quote == && d_quote == )
printf ("Syntax appears to be correct.\n");
return ;
} int getline(void)
{
int c, i;
extern char line[]; for ( i=;i<MAXLINE- && ( c=getchar()) != EOF && c != '\n'; ++i)
line[i] = c;
if(c == '\n')
{
line[i] = c;
++i;
}
line[i] = '\0';
return i; }