Week 2

word.c

1:	 #include <string.h>
2:	 
3:	 /* word.c - represent a word in memory as a structure */
4:	 
5:	 typedef struct word_struct{
6:	   char* letters;
7:	   int length;
8:	 } word;
9:	 
10:	 word* init_word(char* charstr) {
11:	   word* new_word;
12:	   new_word = (word*) malloc(sizeof(word));
13:	   new_word->length = strlen(charstr);
14:	   new_word->letters = (char*) malloc(sizeof(char)*new_word->length);
15:	   strncpy(new_word->letters, charstr);  
16:	 }
17:	 
18:	 void free_word(word* w) {
19:	   free(w->letters);
20:	   free(w);
21:	 }
22:	 
23:	 int equals(word* a, word* b) {
24:	   if (a->length != b->length) {
25:	     return 0;
26:	   } 
27:	   if (strcmp(a->letters, b->letters) == 0) {
28:	     return 1;
29:	   } else {
30:	     return 0;
31:	   }
32:	 }
33:	 
34:	 void print(void) {
35:	   printf("%s\n", letters);
36:	 }
word.cpp
1:	 #include <string.h>
2:	 #include <iostream.h>
3:	 
4:	 // word.cpp - represent a word as a Word object type
5:	 
6:	 class Word {
7:	 public: 
8:	   
9:	   Word(char* charstr) {
10:	     length = strlen(charstr);
11:	     letters = new char[length];
12:	     strncpy(letters, charstr, length);
13:	   }
14:	 
15:	   ~Word() {
16:	     delete letters;
17:	   }
18:	   
19:	   int equals(Word* w) {
20:	     if (w->length != length) {
21:	       return 0;
22:	     } 
23:	     if (strcmp(w->letters, letters) == 0) {
24:	       return 1;
25:	     } else {
26:	       return 0;
27:	     }
28:	   }
29:	 
30:	   int equals(Word* w1, Word* w2) {
31:	     if (w1->length != w2->length) {
32:	       return 0;
33:	     }
34:	     if (strcmp(w1->letters, w2->letters) == 0) {
35:	       return 1;
36:	     } else {
37:	       return 0;
38:	     }
39:	   }
40:	 
41:	   void print() {
42:	     cout << letters << endl;
43:	   }
44:	 
45:	 private:
46:	   char* letters; 
47:	   int length; 
48:	 };

  1. Find 5 things different between word.c and word.cpp.
  2. How do you make a multi-line comment in C? in C++?
  3. How would you print the length of a word?
  4. How do use a get and use a new word? Free up a old word?
  5. How do you access the length of a word?
  6. Why are there two equals() methods in the cpp file?
  7. How do you make a copy of a word?

Debugging

How to use DDD

Programming Assignment

To submit your project:
  1. Make sure it compiles with 'make' and creates a 'netlist' binary. If this doesn't work, you won't get any credit for the assignment. If you created new files for the assignment, remember to add them to the OBJ and SRC lines of the Makefile.
  2. Clean out your binaries with 'make clean'
  3. Create a README file. Put your name, SID, how many hours you spent on this assignment and your favorite riddle/joke in the file.
  4. Use handin from the CSIF machines. Run 'handin cs40at pa1 filenames' where filenames is the list of all of code files, including headers, Makefile and README. If your directory only has your code files, you can run 'handin cs40at pa1 *'.
  5. Example:
    [hsuf@ta1 proj1]$ make
    [hsuf@ta1 proj1]$ ls -l netlist
    -rwx------    1 hsuf     users       23776 Oct 13 15:45 netlist
    [hsuf@ta1 proj1]$ make clean
    rm *.o netlist
    [hsuf@ta1 proj1]$ cat > README
    Francis Hsu
    990000000
    4 hours
    Two hydrogen atoms bumped into each other recently.
    One said: "Why do you look so sad?"
    The other responded: "I lost an electron."
    Concerned, One asked "Are you sure?"
    The other replied "I'm positive."
    ^D
    [hsuf@ta1 proj1]$ handin cs40at pa1 *
    Submitting BoardComponent.c... ok
    Submitting BoardComponent.h... ok
    Submitting BoardLocation.h... ok
    Submitting DataTypes.h... ok
    Submitting Makefile... ok
    Submitting NetList.c... ok
    Submitting NetList.h... ok