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

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

C程序设计语言(第二版)习题:第一章的更多相关文章

  1. 笔记-Python基础教程(第二版)第一章

    第一章 快速改造:基础知识 01:整除.乘方 (Python3.0之前 如2.7版本) >>> 1/2 ==>0 1/2整除,普通除法: 解决办法1: 1.0/2.0  ==& ...

  2. Java程序设计(2021春)——第一章课后题(选择题+编程题)答案与详解

    Java程序设计(2021春)--第一章课后题(选择题+编程题)答案与详解 目录 Java程序设计(2021春)--第一章课后题(选择题+编程题)答案与详解 第一章选择题 1.1 Java与面向对象程 ...

  3. Java程序设计(2021春)——第一章续笔记与思考

    Java程序设计(2021春)--第一章续笔记与思考 目录 Java程序设计(2021春)--第一章续笔记与思考 Java数据类型 基本数据类型 引用类型 基本数据类型--整数类型的细节 基本数据类型 ...

  4. javaSE习题 第一章 JAVA语言概述

    转眼就开学了,正式在学校学习SE部分,由于暑假放视频过了一遍,略感觉轻松,今天开始,博客将会记录我的课本习题,主要以文字和代码的形式展现,一是把SE基础加强一下,二是课本中有很多知识是视频中没有的,做 ...

  5. JavaScript高级程序设计(第4版)-第一章学习

    第一章 什么是Javascript 一.历史 JavaScript的名字怎么来的 首先,我们从javascript的历史开始了解,在以前的时候网页要验证某个必填字段是否填写,或者是判断输入的值的正确与 ...

  6. 【转】apue《UNIX环境高级编程第三版》第一章答案详解

    原文网址:http://blog.csdn.net/hubbybob1/article/details/40859835 大家好,从这周开始学习apue<UNIX环境高级编程第三版>,在此 ...

  7. C语言编程入门之--第一章初识程序

    第一章 初识程序 导读:计算机程序无时不刻的影响着人类的生活,现代社会已经离不开程序,程序的作用如此巨大,那么程序到底是什么呢?本章主要讨论程序的概念,唤起读者对程序的兴趣,同时对C语言程序与其它语言 ...

  8. 《JavaScript高级程序设计》(第二版)

    这本书的作者是 Nicholas C.Zakas ,博客地址是 http://www.nczonline.net/ ,大家可以去多关注,雅虎的前端工程师,是YUI的代码贡献者,可想而知这本书得含金量, ...

  9. CSAPP深入理解计算机系统(第二版)第三章家庭作业答案

    <深入理解计算机系统(第二版)>CSAPP 第三章 家庭作业 这一章介绍了AT&T的汇编指令 比较重要 本人完成了<深入理解计算机系统(第二版)>(以下简称CSAPP) ...

  10. C#高级编程 (第六版) 学习 第一章:.Net体系结构

    第一章 .Net体系结构 1,公共语言运行库(Common Language Runtime, CLR) .Net Framework的核心是其运行库的执行环境,称为公共语言运行库,或.Net运行库. ...

随机推荐

  1. 使用 CodeIgniter 框架快速开发 PHP 应用(三)

    原文:使用 CodeIgniter 框架快速开发 PHP 应用(三) 分析网站结构既然我们已经安装 CI ,我们开始了解它如何工作.读者已经知道 CI 实现了MVC式样. 通过对目录和文件的内容进行分 ...

  2. SpringMVC1

    itRed You are never too old to set another goal or to dream a new dream. SpringMVC一路总结(一) SpringMVC听 ...

  3. 隐藏Console形式无效(继续1)

    [2014/10/19  23:57 ] :由port主机遥控. 该程序的执行后,计划自己主动开放之机999port,其他计算机将能够通过999port机器的操作. 程序中使用的到的命令: telne ...

  4. Microsoft dotnetConf 2015

    Microsoft dotnetConf 2015 一些整理   dotnetConf 2015 直播计划: 第一天 (10 个主讲) - .NET Product Teams 主讲人为 .NET 产 ...

  5. TDD

    初识TDD 首先说一下名词解释,TDD,英文名称Test-Driven Development,中文名称测试驱动开发,简单的断下句“测试/驱动/开发”,简单的理解一下,就是测试驱动着开发,大白话就是说 ...

  6. GetDirectories 出错的解决方法

    我想找到D盘里面所有 "*.pst文件,类似 windows 下的磁盘搜索功能, using System.IO; Directory.GetFiles(@"d:\", ...

  7. PHP-微信公众平台开发-接收用户输入消息类型并响应

    原文:PHP-微信公众平台开发-接收用户输入消息类型并响应 <?php // 该代码块用于接收用户消息,根据用户输入的消息类型进行判断,文本,图片,视频,位置,链接,语音等,并取得值,处理后给予 ...

  8. 【Unity Shaders】Mobile Shader Adjustment —— 为手机定制Shader

    本系列主要參考<Unity Shaders and Effects Cookbook>一书(感谢原书作者),同一时候会加上一点个人理解或拓展. 这里是本书全部的插图.这里是本书所需的代码和 ...

  9. Oracle SQL in 超过1000 的解决方案

    处理 Oracle SQL in 超过1000 的解决方案 处理oracle sql 语句in子句中(where id in (1, 2, ..., 1000, 1001)),如果子句中超过1000项 ...

  10. Serialization performance analysis

    Serialization performance analysis http://www.skyscanner.net/blogs/serialization-performance-analysi ...