1 <!DOCTYPE HTML PUBLIC
"-//W3O//DTD W3 HTML 2.0//EN">
4 <link rev=
"owner" href=
"mailto:scs@eskimo.com">
5 <link rev=
"made" href=
"mailto:scs@eskimo.com">
6 <title>Assignment #
3 Answers
</title>
9 <H1>Assignment #
3 Answers
</H1>
15 <B>Intermediate C Programming
18 UW Experimental College
21 <B>Assignment #
3 ANSWERS
25 <I>Add some other commands, such as ``break'' or
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.)
34 else if(strcmp(verb,
"break") ==
0)
38 printf(
"You must tell me what to break.\n");
41 if(cmd-
>preposition == NULL || strcmp(cmd-
>preposition,
"with") !=
0 ||
42 cmd-
>xobject == NULL)
44 printf(
"You must tell me what to break with.\n");
47 if(!contains(player-
>contents, cmd-
>xobject))
49 printf(
"You have no %s.\n", cmd-
>xobject-
>name);
52 printf(
"Oh, dear. Now the %s is broken.\n", objp-
>name);
55 else if(strcmp(verb,
"cut") ==
0)
59 printf(
"You must tell me what to cut.\n");
62 if(cmd-
>preposition == NULL || strcmp(cmd-
>preposition,
"with") !=
0 ||
63 cmd-
>xobject == NULL)
65 printf(
"You must tell me what to cut with.\n");
68 if(!contains(player-
>contents, cmd-
>xobject))
70 printf(
"You have no %s.\n", cmd-
>xobject-
>name);
73 printf(
"The %s is now cut in two.\n", objp-
>name);
76 else if(strcmp(verb,
"read") ==
0)
80 printf(
"You must tell me what to read.\n");
84 printf(
"There isn't much to read on the %s.\n", objp-
>name);
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:
93 #include
<string.h
>
101 static char retbuf[MAXRETBUF];
102 strcpy(retbuf, word);
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)
115 cmd-
>object = NULL;
116 else if((cmd-
>object = findobject(actor, av[
1])) == NULL)
118 printf(
"I don't see any %s.\n", plural(av[
1]));
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,
126 <p>To keep my compiler happy,
127 I also added the prototype
129 extern char *plural(char *);
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,
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.
143 since
<TT>plural
</TT>'s function is relatively unimportant,
144 I chose to have it return the original word,
146 rather than printing error messages or aborting or anything.)
148 #include
<string.h
>
156 static char retbuf[MAXRETBUF];
157 int len = strlen(word);
158 if(len +
3 >= MAXRETBUF)
160 strcpy(retbuf, word);
161 if(word[len-
1] != 's')
165 /* maybe we should add
"es" */
166 strcat(retbuf,
"es");
167 /* (or maybe it'a already plural and we should add nothing...) */
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>