C程序设计语言(第二版)习题:第一章
第一章虽然感觉不像是个习题。但是我还是认真去做,去想,仅此而已!
练习 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程序设计语言(第二版)习题:第一章的更多相关文章
- 笔记-Python基础教程(第二版)第一章
第一章 快速改造:基础知识 01:整除.乘方 (Python3.0之前 如2.7版本) >>> 1/2 ==>0 1/2整除,普通除法: 解决办法1: 1.0/2.0 ==& ...
- Java程序设计(2021春)——第一章课后题(选择题+编程题)答案与详解
Java程序设计(2021春)--第一章课后题(选择题+编程题)答案与详解 目录 Java程序设计(2021春)--第一章课后题(选择题+编程题)答案与详解 第一章选择题 1.1 Java与面向对象程 ...
- Java程序设计(2021春)——第一章续笔记与思考
Java程序设计(2021春)--第一章续笔记与思考 目录 Java程序设计(2021春)--第一章续笔记与思考 Java数据类型 基本数据类型 引用类型 基本数据类型--整数类型的细节 基本数据类型 ...
- javaSE习题 第一章 JAVA语言概述
转眼就开学了,正式在学校学习SE部分,由于暑假放视频过了一遍,略感觉轻松,今天开始,博客将会记录我的课本习题,主要以文字和代码的形式展现,一是把SE基础加强一下,二是课本中有很多知识是视频中没有的,做 ...
- JavaScript高级程序设计(第4版)-第一章学习
第一章 什么是Javascript 一.历史 JavaScript的名字怎么来的 首先,我们从javascript的历史开始了解,在以前的时候网页要验证某个必填字段是否填写,或者是判断输入的值的正确与 ...
- 【转】apue《UNIX环境高级编程第三版》第一章答案详解
原文网址:http://blog.csdn.net/hubbybob1/article/details/40859835 大家好,从这周开始学习apue<UNIX环境高级编程第三版>,在此 ...
- C语言编程入门之--第一章初识程序
第一章 初识程序 导读:计算机程序无时不刻的影响着人类的生活,现代社会已经离不开程序,程序的作用如此巨大,那么程序到底是什么呢?本章主要讨论程序的概念,唤起读者对程序的兴趣,同时对C语言程序与其它语言 ...
- 《JavaScript高级程序设计》(第二版)
这本书的作者是 Nicholas C.Zakas ,博客地址是 http://www.nczonline.net/ ,大家可以去多关注,雅虎的前端工程师,是YUI的代码贡献者,可想而知这本书得含金量, ...
- CSAPP深入理解计算机系统(第二版)第三章家庭作业答案
<深入理解计算机系统(第二版)>CSAPP 第三章 家庭作业 这一章介绍了AT&T的汇编指令 比较重要 本人完成了<深入理解计算机系统(第二版)>(以下简称CSAPP) ...
- C#高级编程 (第六版) 学习 第一章:.Net体系结构
第一章 .Net体系结构 1,公共语言运行库(Common Language Runtime, CLR) .Net Framework的核心是其运行库的执行环境,称为公共语言运行库,或.Net运行库. ...
随机推荐
- HTTP必知必会(转)
HTTP协议作为网络传输的基本协议,有着广泛的应用.HTTP协议的完整内容很多,但是其核心知识却又简单精炼.学习者应该掌握其基本结构,并且能够举一反三.这篇文章所列的,就是在实际开发中必须知道必须掌握 ...
- 四:redis的sets类型 - 相关操作(有序和无序集合)
================四十五种(有序和无序集合):sets种类(它是一个集)============= 简介: set它代表的集合.加入是随意添加----->无序集合 ...
- Linux/Unix
Linux/Unix 新手和专家教程 你正在找一些高质量的Linux 和 UNIX 的教程吗?如果是,这篇文章会告诉你到哪去找到这些教程.这里我们将给出超过30个相当的不错的 Linux 和 UNIX ...
- MVC推荐教程和文章列表
着手Getting Started Getting Started with ASP.NET MVC 5 (共11部分) Pluralsight ASP.NET MVC 5 Fundamentals( ...
- EF中的EntityState几个状态的说明
之前使用EF,我们都是通过调用SaveChanges方法把增加/修改/删除的数据提交到数据库,但是上下文是如何知道实体对象是增加.修改还是删除呢?答案是通过EntityState枚举来判断的,我们看一 ...
- MongoDB详解学习历程
MongoDB是一个基于分布式文件存储的数据库,它是介于关系数据库和非关系数据库之间的产品. MongoDB支持的数据结构非常松散,类似json的bjson格式,因此可以存储比较复杂的数据类型.Mon ...
- 使用Visifire+ArcGIS API for Silverlight实现Graphic信息的动态图表显示
原文:使用Visifire+ArcGIS API for Silverlight实现Graphic信息的动态图表显示 首先来看一看实现的效果: PS:原始的程序中更新曲线数据时添加了过渡的效果,具体可 ...
- 【LeetCode】 sort list 单清单归并
称号:Sort a linked list in O(n log n) time using constant space complexity. 思路:要求时间复杂度O(nlogn) 知识点:归并排 ...
- UVA - 817 According to Bartjens
Description According to Bartjens The wide dissemination of calculators and computers has itsdisad ...
- Lucene.Net简介和分词
Lucene.net站内搜索—2.Lucene.Net简介和分词 2015-03-24 23:10 by 邹琼俊, 118 阅读, 1 评论, 收藏, 编辑 Lucene.Net简介 Lucene.N ...