博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
《The C Programming Language》答案(第一章)
阅读量:4169 次
发布时间:2019-05-26

本文共 9410 字,大约阅读时间需要 31 分钟。

《The C Programming Language》答案(第一章)

P1

#include 
main(){
printf("hello, world\n");}

P2

warning: unknown escape sequence: '\c'

P3

#include 
main(){
float fahr,celsius; float lower,upper,step; lower = 0; upper = 300; step = 20; fahr = lower; printf("fahr celsius\n"); while(fahr<=upper){
celsius = (5.0/9.0)*(fahr-32.0); printf("%3.0f %6.1f\n",fahr,celsius); fahr = fahr+step; }}

P4

#include 
main(){
float fahr,celsius; float lower,upper,step; lower = 0; upper = 300; step = 20; celsius = lower; printf("celsius fahr\n"); while(celsius<=upper){
//celsius = (5.0/9.0)*(fahr-32.0); fahr=(9.0/5.0)*celsius+32.0; printf("%3.0f %6.1f\n",celsius,fahr); celsius = celsius+step; }}

P5

#include 
main(){
float fahr,celsius; float lower,upper,step; lower = 0; upper = 300; step = 20; fahr = upper; printf("fahr celsius\n"); for(fahr;fahr>=lower;fahr-=step){
celsius = (5.0/9.0)*(fahr-32.0); //fahr=(9.0/5.0)*celsius+32.0; printf("%3.0f %6.1f\n",fahr,celsius); //celsius = celsius+step; }}

P6

#include 
//the answer is 1main(){
printf("%d\n",getchar()!=EOF);}

P7

#include 
//the answer is -1main(){
printf("%d\n",EOF);}

P8

#include 
//press ctrl+Z in windows, ctrl+D in Linuxmain(){
int tmp,emptyCount,tableCount,lineCount; emptyCount=0; tableCount=0; lineCount=0; while(1){
tmp=getchar(); if(tmp==EOF) break; if(tmp==' ') ++emptyCount; if(tmp=='\t') ++tableCount; if(tmp=='\n') ++lineCount; } printf("Empties: %d\nTables: %d\nLines: %d\n",emptyCount,tableCount,lineCount);}

P9

#include 
//ctrl+Z in windows, ctrl+D in Linux to end the inputmain(void){
int c, blank_flag; blank_flag = 0; while (1){
c = getchar(); if(c==EOF) break; if (c == ' ') {
if (!blank_flag) {
blank_flag = 1; putchar(c); } } else {
blank_flag = 0; putchar(c); } }}

P10

#include 
//ctrl+Z in windows, ctrl+D in Linux to end the input.main(void){
int c; while (1){
c = getchar(); if(c==EOF) break; if (c == '\t') printf("\\t"); else if (c == '\b') printf("\\b"); else if (c == '\\') printf("\\\\"); else printf("%c", c); }}

P11

行首空格,空行

P12

#include 
//ctrl+Z in windows, ctrl+D in Linux to end the inputint main(void){
int c, lineflag; lineflag = 0; while (1){
c = getchar(); if(c==EOF) break; if (c == ' ' || c == '\t' || c == '\n') {
if (!lineflag) {
putchar('\n'); lineflag = 1; } } else {
putchar(c); lineflag = 0; } } return 0;}

P13

#include 
#define MAX_LENGTH 15#define IN_WORD 1#define OUT_WORD 0int main(void){
int c, word_in_flag; int word_length[MAX_LENGTH + 1]; int l; int i, j; unsigned int max_count; for (i = 0; i <= MAX_LENGTH; ++i) word_length[i] = 0; word_in_flag = OUT_WORD; while (1) {
c = getchar(); if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) {
if (word_in_flag == OUT_WORD) {
l = 0; word_in_flag = IN_WORD; } ++l; } else {
if (word_in_flag == IN_WORD) {
if (l <= MAX_LENGTH) ++word_length[l - 1]; else ++word_length[MAX_LENGTH]; word_in_flag = OUT_WORD; } if (c == EOF) break; } }//horizon for (i = 0; i <= MAX_LENGTH; ++i) {
if (i != MAX_LENGTH) printf(" %2d | ", i + 1); else printf(" >%2d | ", MAX_LENGTH); for (j = 0; j < word_length[i]; ++j) putchar('+'); putchar('\n'); }//vertical max_count = 0; for (i = 0; i <= MAX_LENGTH; ++i) if (word_length[i] > max_count) max_count = word_length[i]; for (i = 0; i < max_count; ++i) {
printf(" %2u | ", max_count - i); for (j = 0; j <= MAX_LENGTH; ++j) if (word_length[j] >= max_count - i) printf(" +"); else printf(" "); printf("\n"); } printf(" ------"); for (i = 0; i <= MAX_LENGTH; ++i) printf("---"); printf("--\n"); printf(" "); for (i = 0; i < MAX_LENGTH;) printf(" %2u", ++i); printf(" >%2u", MAX_LENGTH); printf("\n"); return 0;}

P14

#include 
#define NUM_CHARS 128#define IN_WORD 1#define OUT_WORD 0//ctrl+Z in windows, ctrl+D in Linux to finish inputint main(void){
int c; int char_frequencies[NUM_CHARS + 1]; int i; for (i = 0; i <= NUM_CHARS; ++i) char_frequencies[i] = 0; while ((c = getchar()) != EOF) ++char_frequencies[c]; printf("\n ASCII | Count\n"); for (i = 0; i <= NUM_CHARS; ++i) if (char_frequencies[i] > 0) printf(" %5d : %5d\n", i, char_frequencies[i]); return 0;}

P15

P16

#include 
#define MAXLINE 1000 /* maximum input line length */int get_line(char line[], int maxline);void copy(char to[], char from[]);/* print the longest input line */int main(void){
int len; /* current line length */ int max; /* maximum length seen so far */ char line[MAXLINE]; /* current input line */ char longest[MAXLINE]; /* longest line saved here */ max = 0; while ((len = get_line(line, MAXLINE)) > 0) if (len > max) {
max = len; copy(longest, line); } if (max > 0) {
printf("The longest line is:\n"); printf("%s\n", longest); printf("The length of it is %d.\n", max); } return 0;}/* get_line: read a line into s, return length */int get_line(char s[], int lim){
int c, i, l; for (i = 0, l = 0; (c = getchar()) != EOF && c != '\n'; ++i) {
if (i < lim - 1) s[l++] = c; } if (c == '\n') {
if (l < lim - 1) s[l++] = c; ++i; } s[l] = '\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;}

P17

#include 
#define MAXLINE 1000#define MAXLENGTH 10int get_line(char s[],int lim){
int c,i,l; for(i=0,l=0;(c=getchar())!=EOF&&c!='\n';++i){
if(i
0) if(len>MAXLENGTH) printf("%s\n",line); return 0;}

P18

#include 
#define MAXLINE 1000#define MAXLENGTH 10int get_line(char s[],int lim){
int c,i,l; for(i=0,l=0;(c=getchar())!=EOF&&c!='\n';++i){
if(i
0){
if(len==1&&line[0]=='\n') continue; printf("%s\n",line); } return 0;}

P19

#include 
#define MAXLINE 1000#define MAXLENGTH 10int get_line(char s[],int lim){
int c,i,l; for(i=0,l=0;(c=getchar())!=EOF&&c!='\n';++i){
if(i
0){
reverse(line); printf("%s\n",line); } return 0;}

P20

#include 
#define MAXLINE 1000#define TAB_WIDTH 4int getchars(char s[],int lim){
int c,i,l; for(i=0,l=0;(c=getchar())!=EOF&&c!='\n';++i) if(i

P21

#include 
#define MAXLINE 1000#define TAB_WIDTH 8int getchars(char s[],int lim){
int c,i,l; for(i=0,l=0;(c=getchar())!=EOF&&c!='\n';++i) if(i

P22

#include 
#define MAXLINE 1000#define TAB_WIDTH 8#define LINE_WIDTH 10int getchars(char s[],int lim){
int c,i,l; for(i=0,l=0;(c=getchar())!=EOF&&c!='\n';++i) if(i
=linewidth){
s2[l++]='\n'; w=0; i=k; }else{
s2[l++]='\t'; w+=tabwidth; } }else{
s2[l++]=c; if(w+1==linewidth){
s2[l++]='\n'; w=0; }else{
k=i; ++w; } } ++i; } s2[l++]='\0';}int main(){
char s1[MAXLINE]; char s2[MAXLINE]; while(getchars(s1,MAXLINE)==0) ; fold(s1,s2,LINE_WIDTH,TAB_WIDTH); printf("%s\n",s2); return 0;}

P23

#include 
#define MAXLENGTH 5000//ctrl+Z in windows, ctrl+D in Linux to end the inputint _getline(char s[], int max){
int c, i, l; for (i = 0, l = 0; (c = getchar()) != EOF; ++i) if (i < max - 1) s[l++] = c; s[l] = '\0'; return l;}int main(void){
int len, i; char s[MAXLENGTH]; while ((len = _getline(s, MAXLENGTH)) > 0) {
i = 0; while (s[i] != '\0') {
if (s[i] == '/' && s[i+1] == '/') {
i += 2; while (s[i] != '\n' && s[i] != '\0') ++i; } else if (s[i] == '/' && s[i+1] == '*') {
i += 2; while (s[i] != '\0' && s[i+1] != '\0' && (s[i] != '*' || s[i+1] != '/')) ++i; if (s[i] != '\0' && s[i+1] == '\0') ++i; if (s[i] == '*') i += 2; } else if (s[i] == '\"') {
putchar('\"'); ++i; while (s[i] != '\0' && (s[i-1] == '\\' || s[i] != '\"')) putchar(s[i++]); } else {
putchar(s[i++]); } } } return 0;}

P24

太长不写,括号匹配可以用栈实现。

第一章 完

转载地址:http://jfwai.baihongyu.com/

你可能感兴趣的文章
5月英语总结--I will do it well.
查看>>
认识JS
查看>>
Google浏览器--翻译一定要“出去”吗?
查看>>
bash:ifconfig:未找到命令
查看>>
送给毕业的歌
查看>>
嵌入式100题(017):malloc的底层实现
查看>>
嵌入式100题(018):在1G内存的计算机中能否malloc(1.2G)?为什么?
查看>>
嵌入式100题(019):指针与引用的相同和区别;如何相互转换?
查看>>
嵌入式100题(040):什么是三次握手
查看>>
嵌入式100题(037):Http1.1和Http1.0的区别
查看>>
嵌入式100题(038):HTTPS与HTTP的一些区别
查看>>
嵌入式100题(042):为什么服务端易受到SYN攻击?
查看>>
嵌入式100题(043):什么是四次挥手
查看>>
嵌入式100题(044):为什么客户端最后还要等待2MSL?
查看>>
嵌入式100题(045):为什么建立连接是三次握手,关闭连接确是四次挥手呢?...
查看>>
嵌入式100题(028):static的用法(定义和用途)
查看>>
嵌入式100题(027):char和int之间的转换
查看>>
嵌入式100题(029):const常量和#define的区别(编译阶段、安全性、内存占用等)...
查看>>
嵌入式100题(030):volatile作用和用法
查看>>
嵌入式100题(033):TCP、UDP的优缺点
查看>>