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运行库. ...
随机推荐
- js之按键总结
js 实现键盘记录 兼容FireFox和IE 2009-01-07 11:43 作者:羽殇仁 转载请注明出处,谢谢. 本篇文章是我的第一百篇blog文章,恭喜一下! 这两天突然想弄弄js的键盘记录,所 ...
- Linux:闪光的宝石,智慧(下一个)
2005年4月7日.Linus Torvalds公布了一款新型通用工具软件包,叫做"Git"(the Git source code management system).&quo ...
- React实践(一)
该实践取自官方教程:https://github.com/reactjs/react-tutorial 主要是自实现的过程以及一些心得体会 该实践是实现一个评论框. 一个展示所有评论的视图 一个提交评 ...
- ASP.NET MVC+EF框架+EasyUI实现权限管理系列(14)-主框架搭建
原文:ASP.NET MVC+EF框架+EasyUI实现权限管理系列(14)-主框架搭建 ASP.NET MVC+EF框架+EasyUI实现权限管系列 (开篇) (1):框架搭建 (2 ...
- Unobtrusive JavaScript 是什么?
Unobtrusive JavaScript 是什么? <!--以下是常规Javascript下写出来的Ajax--><div id="test">< ...
- 解决Unity3d 4.3 动画系统带来的烦恼
近期有非常多同学问我关于unity3d 4.3更新之后动画系统和曾经不一样了,并且之前用的非常熟练的创建动画和修修改画非常多操作都不好用了,那么在这里和大家分享一下三杀的个人经验,方便大家使用unit ...
- FindBugs:Compiler output path for module can not be null. check your module/project settings问题原因
这可能是很多人在使用Android studio 该插件会发现此错误信息:Compiler output path for module can not be null. check your mod ...
- IIS7伪静态化URL Rewrite模块
原文 IIS7伪静态化URL Rewrite模块 在Win7安装了IIS7.5之后,搭建一些网站或者博客,但是IIS7.5本身没有URL Rewrite功能,也就是无法实现网址的伪静态化. 从网上找了 ...
- 关于UtilTimerStack类的使用--XWork2、Struts2内置性能诊断类
关于UtilTimerStack类的使用--XWork2.Struts2内置性能诊断类 一.UtilTimerStack做什么用的? 这个本来是Xwork2(Struts2的核心)的相关的工具类,可以 ...
- Linux Mysql 权限相关信息 来源于网络
查看用户权限 show grants for 你的用户 比如: show grants for root@’localhost’; mysql> use mysql; Database chan ...