* remove "\r" nonsense
[mascara-docs.git] / C / the.ansi.c.programming.language / notes.accompany.ansi.c / homework / PS3a.html
blobc3db0cc6409ab53c0bba5a4b81cab30ba4f9ffb8
1 <!DOCTYPE HTML PUBLIC "-//W3O//DTD W3 HTML 2.0//EN">
2 <html>
3 <head>
4 <link rev="owner" href="mailto:scs@eskimo.com">
5 <link rev="made" href="mailto:scs@eskimo.com">
6 <title>Assignment #3 Answers</title>
7 </head>
8 <body>
9 <H1>Assignment #3 Answers</H1>
15 <B>Intermediate C Programming
16 <br>
17 <br>
18 UW Experimental College
19 </B><br>
20 <br>
21 <B>Assignment #3 ANSWERS
22 </B><br>
23 <br>
24 <p>Exercise 2.
25 <I>Add some other commands, such as ``break'' or
26 ``cut'' or ``read''.
27 </I><p>Here is the code I added
28 (to <TT>commands.c</TT>, of course)
29 to implement all three commands.
30 They are all straightforward but quite boring,
31 because they don't really <em>do</em> anything yet.
32 (Soon we'll be able to make them more interesting.)
33 <pre>
34 else if(strcmp(verb, "break") == 0)
36 if(objp == NULL)
38 printf("You must tell me what to break.\n");
39 return FALSE;
41 if(cmd-&gt;preposition == NULL || strcmp(cmd-&gt;preposition, "with") != 0 ||
42 cmd-&gt;xobject == NULL)
44 printf("You must tell me what to break with.\n");
45 return FALSE;
47 if(!contains(player-&gt;contents, cmd-&gt;xobject))
49 printf("You have no %s.\n", cmd-&gt;xobject-&gt;name);
50 return FALSE;
52 printf("Oh, dear. Now the %s is broken.\n", objp-&gt;name);
55 else if(strcmp(verb, "cut") == 0)
57 if(objp == NULL)
59 printf("You must tell me what to cut.\n");
60 return FALSE;
62 if(cmd-&gt;preposition == NULL || strcmp(cmd-&gt;preposition, "with") != 0 ||
63 cmd-&gt;xobject == NULL)
65 printf("You must tell me what to cut with.\n");
66 return FALSE;
68 if(!contains(player-&gt;contents, cmd-&gt;xobject))
70 printf("You have no %s.\n", cmd-&gt;xobject-&gt;name);
71 return FALSE;
73 printf("The %s is now cut in two.\n", objp-&gt;name);
76 else if(strcmp(verb, "read") == 0)
78 if(objp == NULL)
80 printf("You must tell me what to read.\n");
81 return FALSE;
84 printf("There isn't much to read on the %s.\n", objp-&gt;name);
87 </pre>
88 <p>Exercise 3.
89 <I>Write a function <TT>plural</TT>
90 so that the game can print slightly more interesting messages.
91 </I><p>Here is my first <TT>plural</TT> function:
92 <pre>
93 #include &lt;string.h&gt;
94 #include "game.h"
96 #define MAXRETBUF 20
98 char *
99 plural(char *word)
101 static char retbuf[MAXRETBUF];
102 strcpy(retbuf, word);
103 strcat(retbuf, "s");
104 return retbuf;
106 </pre>
107 Notice that the <TT>retbuf</TT> array is declared <TT>static</TT>,
108 so that it will persist after <TT>plural</TT> returns,
109 so that the pointer that <TT>plural</TT> returns will remain valid.
110 <p>Here is the change to
111 (the newer version of)
112 <TT>parser.c</TT>:
113 <pre>
114 if(ac &lt; 2)
115 cmd-&gt;object = NULL;
116 else if((cmd-&gt;object = findobject(actor, av[1])) == NULL)
118 printf("I don't see any %s.\n", plural(av[1]));
119 return FALSE;
121 </pre>
122 (The only change is to the <TT>printf</TT> call.
123 You could also make a similar change to the code just below
124 which checks for the presence of the object of the preposition,
125 if any.)
126 <p>To keep my compiler happy,
127 I also added the prototype
128 <pre>
129 extern char *plural(char *);
130 </pre>
131 to <TT>game.h</TT>.
132 <p>Finally, here is an improved version of the <TT>plural</TT> function.
133 If the last character of the word being pluralized is s,
134 it tacks on ``es''.
135 (Unfortunately, if the word is <em>already</em> plural,
136 this means that it ends up changing
137 ``marbles'' to ``marbleses,''
138 and sounding like Gollum.)
139 Since this version has to compute
140 the length of the original word anyway,
141 it's easy to make sure that it won't overflow the return buffer.
142 (In this case,
143 since <TT>plural</TT>'s function is relatively unimportant,
144 I chose to have it return the original word,
145 unpluralized,
146 rather than printing error messages or aborting or anything.)
147 <pre>
148 #include &lt;string.h&gt;
149 #include "game.h"
151 #define MAXRETBUF 20
153 char *
154 plural(char *word)
156 static char retbuf[MAXRETBUF];
157 int len = strlen(word);
158 if(len + 3 &gt;= MAXRETBUF)
159 return word;
160 strcpy(retbuf, word);
161 if(word[len-1] != 's')
162 strcat(retbuf, "s");
163 else {
164 /* It ends in s. */
165 /* maybe we should add "es" */
166 strcat(retbuf, "es");
167 /* (or maybe it'a already plural and we should add nothing...) */
169 return retbuf;
171 </pre>
172 <hr>
173 <hr>
175 This page by <a href="http://www.eskimo.com/~scs/">Steve Summit</a>
176 // <a href="copyright.html">Copyright</a> 1995-9
177 // <a href="mailto:scs@eskimo.com">mail feedback</a>
178 </p>
179 </body>
180 </html>