您好,欢迎来到九壹网。
搜索
您的当前位置:首页C程序设计语言 (第二版) 课后答案第一章

C程序设计语言 (第二版) 课后答案第一章

来源:九壹网
Chapter 1

Exercise 1-1

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

#include int main() {

printf(\"hello, \"); printf(\"world\"); printf(\"\\n\"); return 0; }

Exercise 1-2

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

Exercise 1-3

Modify the temperature conversion program to print a heading above the table. #include int main() {

float fahr, celsius;

float lower, upper, step; lower = 0; upper = 300; step = 20; fahr = lower;

printf(\"Fahrenheit temperatures and their centigrade or Celsius equivalents\\n\"); while (fahr <= upper) {

celsius = (5.0/9.0) * (fahr-32.0); printf(\"%3.0f %6.1f\\n\ fahr = fahr + step; } return 0; }

Exercise 1-4

Write a program to print the corresponding Celsius to Fahrenheit table. #include int main() {

float fahr, celsius;

float lower, upper, step; lower = 0; upper = 300; step = 20;

celsius = lower;

while (celsius<= upper) {

fahr = 9.0*celsius/5.0+32;

printf(\"%3.0f %6.1f\\n\ celsius = celsius + step; } return 0; }

Exercise 1-5

Modify the temperature conversion program to print the table in reverse order, that is, from 300 degrees to 0. #include int main() {

float fahr, celsius;

float lower, upper, step; lower = 0; upper = 300; step = 20; fahr = upper;

printf(\"Fahrenheit temperatures and their centigrade or Celsius equivalents\\n\"); while (fahr >= lower) {

celsius = (5.0/9.0) * (fahr-32.0); printf(\"%3.0f %6.1f\\n\ fahr = fahr - step; } return 0; }

Exercise 1-6

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

#include main() {

int c;

c =(getchar() != EOF); printf(\"%d\ return 0; }

Exercise 1-7

Write a program to print the value of EOF . #include int main() {

printf(\"%d\return 0; }

Exercise 1-8

Write a program to count blanks, tabs, and newlines. #include int main(void) {

int blanks, tabs, newlines; int c;

int done = 0; int lastchar = 0; blanks = 0; tabs = 0;

newlines = 0;

printf(\"输入0查看结果\\n\"); while(done == 0) {

c = getchar();

if(c == ' ') ++blanks; if(c == '\') ++tabs; if(c == '\\n') ++newlines; if(c == '0') {

if(lastchar != '\\n')

{

++newlines; }

done = 1; }

lastchar = c; }

printf(\"空格的个数为: %d\\n制表符的个数为: %d\\n换行符的个数为: %d\\n\newlines); return 0; }

Exercise 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 int main(void) {

int c;

int inspace; inspace = 0;

while((c = getchar()) != EOF) {

if(c == ' ') {

if(inspace == 0) {

inspace = 1; putchar(c); } }

if(c != ' ') {

inspace = 0; putchar(c); } }

return 0; }

Exercise 1-10

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

#include int main() { int c;

while((c=getchar())!=EOF) {

if (c=='\')

printf(\"\\"); if (c=='\\b')

printf(\"\\b\"); if (c=='\\\\')

printf(\"\\\\\\\\\"); if (c!='\')

if (c!='\\b') if (c!='\\\\')

putchar(c); }

return 0; }

Exercise 1-11

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

Exercise 1-12

Write a program that prints its input one word per line. #include int main(void) {

int c; int asd;

asd = 0;

while((c = getchar()) != EOF) {

if(c == ' ' || c == '\' || c == '\\n') {

if(asd == 0) {

asd = 1;

putchar('\\n'); } }

else {

asd = 0; putchar(c); } }

return 0; }

Exercise 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

#define MAXWORDLEN 10 int main(void) {

int c;

int inspace = 0;

long lengtharr[MAXWORDLEN + 1]; int wordlen = 0;

int firstletter = 1; long thisval = 0; long maxval = 0; int thisidx = 0; int done = 0;

for(thisidx = 0; thisidx <= MAXWORDLEN; thisidx++) {

lengtharr[thisidx] = 0; }

while(done == 0) {

c = getchar();

if(c == ' ' || c == '\' || c == '\\n' || c == EOF) {

if(inspace == 0) {

firstletter = 0; inspace = 1;

if(wordlen <= MAXWORDLEN) {

if(wordlen > 0) {

thisval = ++lengtharr[wordlen - 1]; if(thisval > maxval) {

maxval = thisval; } } } else {

thisval = ++lengtharr[MAXWORDLEN]; if(thisval > maxval) {

maxval = thisval; } } }

if(c == EOF) {

done = 1; } } else {

if(inspace == 1 || firstletter == 1) {

wordlen = 0; firstletter = 0; inspace = 0; }

++wordlen; } }

for(thisval = maxval; thisval > 0; thisval--) {

printf(\"%4d | \

for(thisidx = 0; thisidx <= MAXWORDLEN; thisidx++) {

if(lengtharr[thisidx] >= thisval) {

printf(\"* \"); } else {

printf(\" \"); } }

printf(\"\\n\"); }

printf(\" +\");

for(thisidx = 0; thisidx <= MAXWORDLEN; thisidx++) {

printf(\"---\"); }

printf(\"\\n \");

for(thisidx = 0; thisidx < MAXWORDLEN; thisidx++) {

printf(\"%2d \ }

printf(\">%d\\n\

return 0; }

Exercise 1-14

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

#include

#define NUM_CHARS 256 int main(void) {

int c;

long freqarr[NUM_CHARS + 1]; long thisval = 0; long maxval = 0; int thisidx = 0;

for(thisidx = 0; thisidx <= NUM_CHARS; thisidx++) {

freqarr[thisidx] = 0; }

while((c = getchar()) != EOF) {

if(c < NUM_CHARS) {

thisval = ++freqarr[c]; if(thisval > maxval) {

maxval = thisval; } } else {

thisval = ++freqarr[NUM_CHARS]; if(thisval > maxval) {

maxval = thisval; } } }

for(thisval = maxval; thisval > 0; thisval--) {

printf(\"%4d |\

for(thisidx = 0; thisidx <= NUM_CHARS; thisidx++) {

if(freqarr[thisidx] >= thisval) {

printf(\"*\"); }

else if(freqarr[thisidx] > 0) {

printf(\" \"); } }

printf(\"\\n\"); }

printf(\" +\");

for(thisidx = 0; thisidx <= NUM_CHARS; thisidx++) {

if(freqarr[thisidx] > 0) {

printf(\"-\"); } }

printf(\"\\n \");

for(thisidx = 0; thisidx < NUM_CHARS; thisidx++) {

if(freqarr[thisidx] > 0)

{

printf(\"%d\ } }

printf(\"\\n \");

for(thisidx = 0; thisidx < NUM_CHARS; thisidx++) {

if(freqarr[thisidx] > 0) {

printf(\"%d\ } }

printf(\"\\n \");

for(thisidx = 0; thisidx < NUM_CHARS; thisidx++) {

if(freqarr[thisidx] > 0) {

printf(\"%d\ } }

if(freqarr[NUM_CHARS] > 0) {

printf(\">%d\\n\ }

printf(\"\\n\"); return 0; }

Exercise 1-15

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

#include float FtoC(float f) {

float c;

c = (5.0 / 9.0) * (f - 32.0); return c; }

int main(void) {

float fahr, celsius; int lower, upper, step; lower = 0; upper = 300;

step = 20;

printf(\"F C\\n\\n\"); fahr = lower;

while(fahr <= upper) {

celsius = FtoC(fahr);

printf(\"%3.0f %6.1f\\n\ fahr = fahr + step; }

return 0; }

Exercise 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. #include #define MAXLINE 1000

int getline(char line[ ], int maxline); void copy(char to[ ], char from[ ]); int main(void) {

int len; int max; char line[MAXLINE]; char longest[MAXLINE]; max = 0;

while((len = getline(line, MAXLINE)) > 0) {

printf(\"%d: %s\ if(len > max) {

max = len;

copy(longest, line); } }

if(max > 0) {

printf(\"Longest is %d characters:\\n%s\ }

printf(\"\\n\"); return 0; }

int getline(char s[], int lim)

{

int c, i, j;

for(i = 0, j = 0; (c = getchar())!=EOF && c != '\\n'; ++i) {

if(i < lim - 1) {

s[j++] = c; } }

if(c == '\\n') {

if(i <= lim - 1) {

s[j++] = c; } ++i; }

s[j] = '\\0'; return i; }

void copy(char to[], char from[]) {

int i;

i = 0;

while((to[i] = from[i]) != '\\0') {

++i; } }

Exercise 1-17

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

#define MINLENGTH 81 int readbuff(char *buffer) { size_t i=0; int c;

while (i < MINLENGTH) { c = getchar();

if (c == EOF) return -1; if (c == '\\n') return 0; buffer[i++] = c; }

return 1; }

int copyline(char *buffer) { size_t i; int c;

int status = 1;

for(i=0; iputchar(c); }

putchar('\\n'); return status; }

int main(void) {

char buffer[MINLENGTH]; int status = 0;

while (status != -1) {

status = readbuff(buffer); if (status == 1)

status = copyline(buffer); }

return 0; }

Exercise 1-18

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

#define MAXQUEUE 1001 int advance(int pointer) {

if (pointer < MAXQUEUE - 1) return pointer + 1; else

return 0; }

int main(void) {

char blank[MAXQUEUE]; int head, tail; int nonspace; int retval; int c;

retval = nonspace = head = tail = 0; while ((c = getchar()) != EOF) { if (c == '\\n') { head = tail = 0; if (nonspace) putchar('\\n'); nonspace = 0; }

else if (c == ' ' || c == '\') { if (advance(head) == tail) { putchar(blank[tail]); tail = advance(tail); nonspace = 1;

retval = EXIT_FAILURE; }

blank[head] = c;

head = advance(head); }

else {

while (head != tail) { putchar(blank[tail]); tail = advance(tail); }

putchar(c); nonspace = 1; } }

return retval; }

Exercise 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. #include

#define MAX_LINE 1024

void discardnewline(char s[]) {

int i;

for(i = 0; s[i] != '\\0'; i++) {

if(s[i] == '\\n') s[i] = '\\0'; } }

int reverse(char s[]) {

char ch; int i, j;

for(j = 0; s[j] != '\\0'; j++) { }

--j;

for(i = 0; i < j; i++) {

ch = s[i]; s[i] = s[j]; s[j] = ch; --j; }

return 0; }

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

s[i] = '\\0';

return i; }

int main(void) {

char line[MAX_LINE];

while(getline(line, sizeof line) > 0) {

discardnewline(line); reverse(line);

printf(\"%s\\n\ }

return 0; }

Exercise 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? #include #include #include

#define MAX_BUFFER 1024 #define SPACE ' ' #define TAB '\'

int CalculateNumberOfSpaces(int Offset, int TabSize) {

return TabSize - (Offset % TabSize); }

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

int main(void) {

char Buffer[MAX_BUFFER]; int TabSize = 5; int i, j, k, l;

while(getline(Buffer, MAX_BUFFER) > 0) {

for(i = 0, l = 0; Buffer[i] != '\\0'; i++) {

if(Buffer[i] == TAB) {

j = CalculateNumberOfSpaces(l, TabSize); for(k = 0; k < j; k++) {

putchar(SPACE); l++; } } else {

putchar(Buffer[i]); l++; } } }

return 0; }

Exercise 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

#define MAXLINE 1000 #define TAB2SPACE 4

char line[MAXLINE];

int getline(void); int main() {

int i,t;

int spacecount,len;

while (( len = getline()) > 0 ) {

spacecount = 0;

for( i=0; i < len; i++) { if(line[i] == ' ') spacecount++; if(line[i] != ' ') spacecount = 0; if(spacecount == TAB2SPACE) { i -= 3; len -= 3; line[i] = '\'; for(t=i+1;tprintf(\"%s\ }

return 0; }

int getline(void) {

int c, i;

extern char line[];

for ( i=0;iline[i] = c; ++i; }

line[i] = '\\0'; return i; }

Exercise 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

#define MAXLINE 1000

char line[MAXLINE]; int getline(void); int main() {

int t,len;

int location,spaceholder; const int FOLDLENGTH=70; while (( len = getline()) > 0 ) {

if( len < FOLDLENGTH ) { }

else

{ t = 0; location = 0; while(tprintf ( \"%s\ }

return 0; }

int getline(void) {

int c, i;

extern char line[];

for ( i=0;iline[i] = c; ++i; }

line[i] = '\\0'; return i; }

Exercise 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

#define MAXLINE 1000 char line[MAXLINE]; int getline(void); int main() {

int in_comment,len; int in_quote; int t;

in_comment = in_quote = t = 0; while ((len = getline()) > 0 ) {

t=0;

while(t < len) { if( line[t] == '\"') in_quote = 1; if( ! in_quote ) {

if( line[t] == '/' && line[t+1] == '*') { t=t+2; in_comment = 1; }

if( line[t] == '*' && line[t+1] == '/') { t=t+2; in_comment = 0; }

if(in_comment == 1) { t++; } else { printf (\"%c\ t++; } } else {

printf (\"%c\

t++; } } }

return 0; }

int getline(void) {

int c, i;

extern char line[];

for ( i=0;iline[i] = c; ++i; }

line[i] = '\\0'; return i; }

Exercise 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

#define MAXLINE 1000 char line[MAXLINE]; int getline(void); int main() {

int len=0; int t=0;

int brace=0, bracket=0, parenthesis=0; int s_quote=1, d_quote=1;

while ((len = getline()) > 0 )

{

t=0;

while(t < len) { if( line[t] == '[') { brace++; } if( line[t] == ']') { brace--; } if( line[t] == '(') { parenthesis++; } if( line[t] == ')') { parenthesis--; } if( line[t] == '\\'') { s_quote *= -1; } if( line[t] == '\"') { d_quote *= -1; } t++; } }

if(d_quote !=1)

printf (\"Mismatching double quote mark\\n\"); if(s_quote !=1)

printf (\"Mismatching single quote mark\\n\"); if(parenthesis != 0)

printf (\"Mismatching parenthesis\\n\"); if(brace != 0)

printf (\"Mismatching brace mark\\n\"); if(bracket != 0)

printf (\"Mismatching bracket mark\\n\");

if( bracket==0 && brace==0 && parenthesis==0 && s_quote == 1 && d_quote == 1)

printf (\"Syntax appears to be correct.\\n\");

return 0; }

int getline(void) {

int c, i;

extern char line[];

for ( i=0;iline[i] = c; ++i; }

line[i] = '\\0'; return i; }

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- 91gzw.com 版权所有 湘ICP备2023023988号-2

违法及侵权请联系:TEL:199 18 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务