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

练习 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. mongodb迁移

    A机器上有mongodb服务,A机器要废,于是迁至B. 简单起见,依旧是在A上ps auxwww|grep mongo找到正在执行的进程: /home/admin/mongodb/mongodb-li ...

  2. iframe参数

    iframe参数: <iframe src="test.jsp" width="100″ height="50″ frameborder="no ...

  3. Qt Quick 组件和动态创建的对象具体的解释

    在<Qt Quick 事件处理之信号与槽>一文中介绍自己定义信号时,举了一个简单的样例.定义了一个颜色选择组件,当用户在组建内点击鼠标时,该组件会发出一个携带颜色值的信号,当时我使用 Co ...

  4. oracle_面试题01

    完成下列操作,写出相应的SQL语句 创建表空间neuspace,数据文件命名为neudata.dbf,存放在d:\data目录下,文件大小为200MB,设为自动增长,增量5MB,文件最大为500MB. ...

  5. oracle_利用闪回功能恢复数据

    方便起见一般:执行如下即可不用往下看: ① 启用行移动功能 alter table tbl_a enable row movement; ② 闪回表数据到某个时间点 flashback table t ...

  6. linux_cp_远程copy

    1:远程copy  [linux对linux 远程拷贝]   scp 文件名  root@远程ip:/路径/    将本地home目录下的test.tar的文件拷贝到远程主机192.168.1.23的 ...

  7. NEFUOJ 500 二分法+最大流量

    http://acm.nefu.edu.cn/JudgeOnline/problemshow.php?problem_id=500 description 在这个信息化的时代.网购成为了最流行的购物方 ...

  8. easyui datagrid shift 多选

    <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <meta na ...

  9. js checkbox多选值采集

    var objs = document.getElementsByTagName("input"); for (var i = 0; i < objs.length; i++ ...

  10. Atitit.ALT+TAB没反应车and 点击任务栏程序闪烁可是不能切换

    Atitit.ALT+TAB没反应车and 点击任务栏程序闪烁可是不能切换 1. 可能你的Alt+Tab键被别人禁用了,试下以下的方法: 1 2. 为什么要禁用Alt+Tab 1 3. ALT+TAB ...