- 博客(17)
- 收藏
- 关注
原创 《C程序设计语言》第二版 Exercise1-15
Exercise 1.15. Rewrite the temperature conversion program of Section 1.2 to use a function for conversion.Let us see the Fahr-to-Celsius program in Section 1.2 first:#include <stdio.h>/* print Fahrenheit-Celsius table for fahr = 0, 20, ...
2022-05-09 10:50:42
178
原创 《C程序设计语言》第二版 Exercise1-14
Exercise 1-14. Write a program to print a histogram of the frequencies of different characters in its input.This Exercise is much like the exercise before. Let us solve it step by step.First of course we need an array to store the occurrence number of
2022-05-04 12:43:23
310
原创 《C程序设计语言》第二版 Exercise1-13-Extra
In the last article we describe how to print a horizontal histogram of the lenths of words. In this article we will describe how to print a vertical one.Let's review how we draw the horizontal histogram:Exercise1_13_1.c#include <stdio.h>#def
2022-05-02 11:06:51
218
原创 《C程序设计语言》第二版 Exercise1-13
Exercise 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.比起之前的几道题,这一题开始作者给我们上难度了。作者让我们输出读取到的(伪)单词长度的直方图。笔者认为作者的意思是:先.
2022-04-30 21:58:50
457
原创 《C程序设计语言》第二版 Exercise1-12
Exercise 1-12. Write a program that prints its input one word per line.作者要求我们对输入的文本中的(伪)单词按行输出,每行输出一个。有个小问题,如果是以空白符开头的输入,是否要保留开头的空白符。笔者在自己的程序中做了不保留的处理。为了达到“每当(伪)单词结束就换行”的效果,笔者这里用了一个状态记录变量 state 。如果当前记录的字符不是空白符,那么我们将 state 的值设置为 IN 。那么如果我们当 state
2022-04-29 00:04:40
329
原创 《C程序设计语言》第二版 Exercise1-11
Exercise 1-11. How would you test the word count program? What kinds of input are most likely to uncover bugs if there are any?这里作者问我们如何测试课文中的(伪)单词计数的例子。笔者认为最好的办法是将想要测试的文本全部写在一个文本文档中,让后运行程序时将输入流用 < 控制符切换成从文档中读取输入,这样我们可以直接看到结果,不用一个个敲键盘输入字符然后用 control +
2022-04-28 23:53:16
176
原创 《C程序设计语言》第二版 Exercise1-10
Exercise 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.这一题甚至比 Exercise1-9 还要简单一些。我们只需要模仿课文中的输出复制的例子,加入一些判断语句,将
2022-04-28 23:38:29
263
原创 《C程序设计语言》第二版 Exercise1-9
Exercise 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>#define NEW_BLANK
2022-04-28 23:29:29
327
原创 《C程序设计语言》第二版 Exercise1-8
Exercise 1-8. Write a program to count blanks, tabs, and newlines.参考课文。作者是如何记录换行符个数的,我们就照猫画虎的记录空格和制表符个数。完整代码如下:#include <stdio.h>int main(void) { int c; int nb, nt, nl; nb = nt = nl = 0; while ((c = getchar()) != EOF) {
2022-04-27 17:49:05
359
原创 《C程序设计语言》第二版 Exercise1-7
Exercise 1-7. Write a program to print the value of EOF.比 Exercise1_6 还简单。用 printf() 输出即可,记得用整数 %d 。完整代码如下:#include <stdio.h>int main(void) { printf("The value of EOF is %d.\n", EOF); return 0;}运行后可以看到 EOF 的值是 -1 :> ./Exerci
2022-04-27 17:37:26
223
原创 《C程序设计语言》第二版 Exercise1-6
Exercsise 1-6. Verify that the expression getchar() != EOF is 0 or 1.很简单,用一个 printf() 函数输出即可。完整代码如下:#include <stdio.h>int main(void) { printf("getchar() != EOF is: %d\n", (getchar() != EOF)); return 0;}运行结果如下:> ./Exercise1_
2022-04-27 16:21:17
157
原创 《C程序设计语言》第二版 Exercise1-5
Exercise 1-5. Modify the temperature conversion program to print the table in reverse order, that is, from 300 degrees to 0.作者要求我们反转华氏-摄氏转换表,从300华氏度开始向下输出。在这里我们利用 for 循环,初始化条件改成 fahr = UPPER ,循环条件改为 fahr >= LOWER ,步长运算从增加一个 STEP 改成减少一个 STEP 即可。完整代
2022-04-27 16:13:32
169
原创 C++多文件编译及连接
在学习清华大学郑莉老师的《C++语言程序设计(第四版》,刚学到第六章,课本里讲到了编写多个源文件代码并且编译连接的过程,课本只讲了源代码内容和结构,对于如何编译连接多个源文件并生成最终的可执行文件没有说明。本人摸索了一下,记录于此方便以后回顾。追求简单粗暴可直接跳到最后。源代码是关于数组对象的应用举例,包含三个文件:1. Point.h类 Point 的头文件,存放一些不需要分配空间的声明以及内联函数,代码如下://Point.h#ifndef _POINT_H#define _P
2022-04-26 23:59:54
6359
原创 《C程序设计语言》第二版 Exercise1-4
Exercise 1-4. Write a program to print the corresponding Celsius to Fahrenheit table.写一个摄氏度转换成华氏度的程序,这里我们还是假定从0-300,步长20。根据华氏度转换摄氏度的公式:得到摄氏度转换成华氏度的公式为:所以我们的转换语句可以改成:fahr = (9.0 * celsius / 5.0) + 32;完整代码为:#include <stdio.h>main(
2022-04-26 17:29:15
411
原创 《C程序设计语言》第二版 Exercise1-3
Exercise 1-3. Modify the temperature conversion program to print a heading above the table.习题要求我们给华氏-摄氏温度转换表增加一个heading。笔者不清楚作者的意思是让读者增加一个“华氏-摄氏转换表”这样的图表标题,还是给表的每一列再列顶端加上列标题。笔者索性就按照后者来进行解题。当然了,如果是第一种情况,只需要在 main() 函数进入循环之前添加下面的语句即可:printf("Fahr-Ce
2022-04-26 17:12:35
139
原创 《C程序设计语言》第二版 Exercise1-2
Exercise 1-2. Experiment to find out what happens when prints's argument string contains \c, where c is some character not listed above.让读者自己探索用 printf() 输出 \c 后会出现什么。笔者用的代码如下:#include <stdio.h>int main(void) { printf("\chello, world\n");
2022-04-26 16:53:13
290
原创 《C程序设计语言》第二版 Exercise1-1
用一个最原始的hello, world程序让初学者了解各个程序片段分别都有什么用。但由于是本书的第一个程序,main函数写的不规范,没有返回类型,参数和返回值,所以编译过程中不可避免的会遇见以下错误:warning: return type defaults to ‘int’ [-Wimplicit-int] 3 | main() { | ^~~~第一章后面几小节的程序也会有此类报错,不必过多在意。进入后面的章节之后作者会恢复到标准的main函数写法,这个warning就不
2022-04-26 16:44:17
1059
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人