Commit Diff


commit - 93cf4d1819cdeaafea9da8c38d283e5e15e6ba77
commit + 32aa96c343d4f605621e53ee51b976d1805428d8
blob - /dev/null
blob + 3b14f78450102b40204d1a806f45d6e12e1618de (mode 644)
--- /dev/null
+++ 1-15.c
@@ -0,0 +1,28 @@
+/* 1-15 Rewrite the temperature conversion program of Section 1.2 to use a
+ * function for conversion. */
+
+#include <stdio.h>
+
+/* convert fahrenheit to celsius */
+
+float ftoc(float fahr) {
+	return (5.0/9.0) * (fahr-32.0);
+}
+
+/* print Fahrenheit-Celsius table
+    for fahr = 0, 20, ..., 300; floating-point version */
+
+int main() {
+	float fahr, celsius;
+	int lower, upper, step;
+
+	lower = 0;		/* lower limit of temperature table */
+	upper = 300;	/* upper limit */
+	step = 20;		/* step size */
+
+	fahr = lower;
+	while (fahr <= upper) {
+		printf("%3.0f %6.1f\n", fahr, ftoc(fahr));
+		fahr = fahr + step;
+	}
+}
blob - /dev/null
blob + ecbbbcc629877aa2ae5565c01e1eb94d595c4e4b (mode 644)
--- /dev/null
+++ 1-16-text
@@ -0,0 +1,47 @@
+THE WAY TO WEALTH

+

+(From "Father Abraham's Speech," forming the preface to Poor _Richard's Almanac_ for 1758.)

+

+It would be thought a hard Government that should tax its People one-tenth Part of their _Time_, to be employed in its Service. But _Idleness_ taxes many of us much more, if we reckon all that is spent in absolute _Sloth_, or doing of nothing, with that which is spent in idle Employments or Amusements, that amount to nothing. _Sloth_, by bringing on Diseases, absolutely shortens Life. _Sloth, like Rust, consumes faster than Labor wears; while the used key is always bright, as Poor Richard says. But dost thou love Life, then do not squander Time, for that's the stuff Life is made of, as Poor Richard_ says. How much more than is necessary do we spend in sleep, forgetting that _The sleeping Fox catches no Poultry_, and that _There will be sleeping enough in the Grave_, as _Poor Richard_ says.

+

+_If Time be of all Things the most precious, wasting Time must be, as Poor Richard_ says, _the_ _greatest Prodigality_; since, as he elsewhere tells us, _Lost Time is never found again; and what we call Time enough, always proves little enough_: Let us then up and be doing, and doing to the Purpose; so by Diligence shall we do more with less Perplexity. _Sloth makes all Things difficult, but Industry all easy_, as _Poor Richard_ says; and _He that riseth late must trot all Day, and shall scarce overtake his Business at Night; while Laziness travels so slowly, that Poverty soon overtakes him_, as we read in _Poor Richard_, who adds, _Drive thy Business, let not that drive thee_; and _Early to Bed, and early to rise, makes a Man healthy, wealthy, and wise._

+

+_Industry need not wish, and he that lives upon Hope will die fasting._

+

+_There are no Gains without Pains._

+

+_He that hath a Trade hath an Estate; and he that hath a Calling, hath an Office of Profit and Honor_; but then the _Trade_ must be worked at, and the _Calling_ well followed, or neither the _Estate_ nor the _Office_ will enable us to pay our Taxes.

+

+What though you have found no Treasure, nor has any rich Relation left you a Legacy, _Diligence is the Mother of Good-luck_, as _Poor Richard_ says, _and God gives all Things to Industry_.

+

+_One To-day is worth two To-morrows_, and farther, _Have you somewhat to do To-morrow, do it To-day_.

+

+If you were a Servant, would you not be ashamed that a good Master should catch you idle? Are you then your own Master, _be ashamed to catch yourself idle_.

+

+Stick to it steadily; and you will see great Effects, for _Constant Dropping wears away Stones_, and by _Diligence and Patience the Mouse ate in two the Cable_; and _Little Strokes fell great Oaks_.

+

+Methinks I hear some of you say, _Must a Man afford himself no Leisure_? I will tell thee, my friend, what _Poor Richard_ says, _Employ thy Time well, if thou meanest to gain Leisure; and, since thou art not sure of a Minute, throw not away an Hour_. Leisure, is Time for doing something useful; this Leisure the diligent Man will obtain, but the lazy Man never; so that, as _Poor Richard_ says, _A Life of Leisure and a Life of Laziness are two things_.

+

+_Keep thy Shop, and thy Shop will keep thee_; and again, _If you would have your business done, go; if not, send._

+

+If you would have a faithful Servant, and one that you like, serve yourself.

+

+_A little Neglect may breed great Mischief:_ adding, _for want of a Nail the Shoe was lost; for want of a Shoe the Horse was lost; and for want of a Horse the Rider was lost, being overtaken and slain by the Enemy; all for the want of Care about a Horse-shoe Nail_.

+

+So much for Industry, my Friends, and Attention to one's own Business; but to these we must add _Frugality_.

+

+_What maintains one Vice, would bring up two Children_. You may think perhaps, that a _little_ Tea, or a _little_ Punch now and then, Diet a _little_ more costly, Clothes a _little_ finer, and a _little_ Entertainment now and then, can be no _great_ Matter; but remember what _Poor Richard_ says, _Many a Little makes a Mickle._

+

+_Beware of little expenses; A small Leak will sink a great Ship_; and again, _Who Dainties love, shall Beggars prove_; and moreover, _Fools make Feasts, and wise Men eat them._

+

+Buy what thou hast no Need of, and ere long thou shalt sell thy Necessaries.

+

+If you would know the Value of Money, go and try to borrow some; for, he that goes a borrowing goes a sorrowing.

+

+The second Vice is Lying, the first is running in Debt.

+

+_Lying rides upon Debt's Back_.

+

+Poverty often deprives a Man of all Spirit and Virtue: '_Tis hard for an empty Bag to stand upright_.

+

+And now to conclude, _Experience keeps a dear School, but Fools will learn in no other, and scarce in that_; for it is true, _we may give Advice, but we cannot give Conduct_, as _Poor Richard_ says: However, remember this, _They that won't be counseled, can't be helped_, as _Poor Richard_ says: and farther, That _if you will not hear Reason, she'll surely rap your Knuckles_.

blob - /dev/null
blob + b1927ea83c79c6c1be5c355d329fb05886e54f78 (mode 644)
--- /dev/null
+++ 1-16.c
@@ -0,0 +1,55 @@
+/* 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. */
+
+#include <stdio.h>
+#define MAXLINE 1000		/* maximum input line size */
+
+int getlin(char line[], int maxline);
+void copy(char to[], char from[]);
+
+/* print longest input line */
+int main() {
+	int len;				/* current line length */
+	int max;				/* maximum length seen so far */
+	char line[MAXLINE];		/* current input line */
+	char longest[MAXLINE];	/* longest line saved here */
+
+	max = 0;
+	while ((len = getlin(line, MAXLINE)) > 0) {
+		if (len > max) {
+			max = len;
+			copy(longest, line);
+		}
+	}
+	if (max > 0)	/* there was a line */
+		printf("%d chars: %s", max, longest);
+	return 0;
+}
+
+/* getlin: read at most lim chars of a line into s, return the exact length */
+int getlin(char s[], int lim) {
+	int c, i;
+
+	for (i=0; i<lim-1 && (c=getchar())!=EOF && c!='\n'; ++i)
+		s[i] = c;
+	if (c == '\n') {
+		s[i++] = c;
+		s[i] = '\0';
+		return i;
+	} else {
+		s[i] = '\0';
+		while ((c = getchar()) != EOF && c != '\n')
+			++i;
+		return i;
+	}
+}
+
+/* copy: copy 'from' into 'to'; assume to is big enough */
+void copy(char to[], char from[]) {
+	int i;
+
+	i = 0;
+	while ((to[i] = from[i]) != '\0')
+		++i;
+}
blob - /dev/null
blob + a456cc0652db49e56e15c9ee4fe7dbf0179a58ed (mode 644)
--- /dev/null
+++ 1-17-text
@@ -0,0 +1,98 @@
+THE WAY TO WEALTH

+

+(From "Father Abraham's Speech," forming

+the preface to Poor _Richard's Almanac_ for 1758.)

+

+It would be thought a hard Government that should tax its People

+one-tenth Part of their _Time_, to be employed in its Service. But

+_Idleness_ taxes many of us much more, if we reckon all that is spent

+in absolute _Sloth_, or doing of nothing, with that which is spent in

+idle Employments or Amusements, that amount to nothing. _Sloth_, by

+bringing on Diseases, absolutely shortens Life. _Sloth, like Rust,

+consumes faster than Labor wears; while the used key is always bright,

+as Poor Richard says. But dost thou love Life, then do not squander

+Time, for that's the stuff Life is made of, as Poor Richard_ says. How

+much more than is necessary do we spend in sleep, forgetting that _The

+sleeping Fox catches no Poultry_, and that _There will be sleeping

+enough in the Grave_, as _Poor Richard_ says.

+

+_If Time be of all Things the most precious, wasting Time must be, as

+Poor Richard_ says, _the_ _greatest Prodigality_; since, as he

+elsewhere tells us, _Lost Time is never found again; and what we call

+Time enough, always proves little enough_: Let us then up and be

+doing, and doing to the Purpose; so by Diligence shall we do more with

+less Perplexity. _Sloth makes all Things difficult, but Industry all

+easy_, as _Poor Richard_ says; and _He that riseth late must trot all

+Day, and shall scarce overtake his Business at Night; while Laziness

+travels so slowly, that Poverty soon overtakes him_, as we read in

+_Poor Richard_, who adds, _Drive thy Business, let not that drive

+thee_; and _Early to Bed, and early to rise, makes a Man healthy,

+wealthy, and wise._

+

+_Industry need not wish, and he that lives upon Hope will die

+fasting._

+

+_There are no Gains without Pains._

+

+_He that hath a Trade hath an Estate; and he that hath a Calling, hath

+an Office of Profit and Honor_; but then the _Trade_ must be worked

+at, and the _Calling_ well followed, or neither the _Estate_ nor the

+_Office_ will enable us to pay our Taxes.

+

+What though you have found no Treasure, nor has any rich Relation left

+you a Legacy, _Diligence is the Mother of Good-luck_, as _Poor

+Richard_ says, _and God gives all Things to Industry_.

+

+_One To-day is worth two To-morrows_, and farther, _Have you somewhat to do To-morrow, do it To-day_.

+

+If you were a Servant, would you not be ashamed that a good Master should catch you idle? Are you then your own Master, _be ashamed to catch yourself idle_.

+

+Stick to it steadily; and you will see great Effects, for _Constant Dropping wears away Stones_, and by _Diligence and Patience the Mouse ate in two the Cable_; and _Little Strokes fell great Oaks_.

+

+Methinks I hear some of you say, _Must a Man afford himself no

+Leisure_? I will tell thee, my friend, what _Poor Richard_ says,

+_Employ thy Time well, if thou meanest to gain Leisure; and, since

+thou art not sure of a Minute, throw not away an Hour_. Leisure, is

+Time for doing something useful; this Leisure the diligent Man will

+obtain, but the lazy Man never; so that, as _Poor Richard_ says, _A

+Life of Leisure and a Life of Laziness are two things_.

+

+_Keep thy Shop, and thy Shop will keep thee_; and again, _If you would have your business done, go; if not, send._

+

+If you would have a faithful Servant, and one that you like, serve

+yourself.

+

+_A little Neglect may breed great Mischief:_ adding, _for want of a

+Nail the Shoe was lost; for want of a Shoe the Horse was lost; and for

+want of a Horse the Rider was lost, being overtaken and slain by the

+Enemy; all for the want of Care about a Horse-shoe Nail_.

+

+So much for Industry, my Friends, and Attention to one's own Business; but to these we must add _Frugality_.

+

+_What maintains one Vice, would bring up two Children_. You may think

+perhaps, that a _little_ Tea, or a _little_ Punch now and then, Diet a

+_little_ more costly, Clothes a _little_ finer, and a _little_

+Entertainment now and then, can be no _great_ Matter; but remember

+what _Poor Richard_ says, _Many a Little makes a Mickle._

+

+_Beware of little expenses; A small Leak will sink a great Ship_; and

+again, _Who Dainties love, shall Beggars prove_; and moreover, _Fools

+make Feasts, and wise Men eat them._

+

+Buy what thou hast no Need of, and ere long thou shalt sell thy

+Necessaries.

+

+If you would know the Value of Money, go and try to borrow some; for, he that goes a borrowing goes a sorrowing.

+

+The second Vice is Lying, the first is running in Debt.

+

+_Lying rides upon Debt's Back_.

+

+Poverty often deprives a Man of all Spirit and Virtue: '_Tis hard for an empty Bag to stand upright_.

+

+And now to conclude, _Experience keeps a dear School, but Fools will

+learn in no other, and scarce in that_; for it is true, _we may give

+Advice, but we cannot give Conduct_, as _Poor Richard_ says: However,

+remember this, _They that won't be counseled, can't be helped_, as

+_Poor Richard_ says: and farther, That _if you will not hear Reason,

+she'll surely rap your Knuckles_.