Help with ADT Implementation

Hello, I am trying to finish my assignment which requires me to play the role of an ADT implementor. I am to implement an ADT into a censoring program with the previous code provided. I've done all I can, and for some unknown reason my program isn't censoring all the words past the first.

For example:
----------------------------------------
John
Jane
===

John. Jane.

@@@@ Jane
----------------------------------------

Jane should be censored along with john.
I am ONLY suppose to implement my ADT onto this file. Everything else is provided for and should not be touched.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#include "message.h"

using namespace std;

///////////////////////////////////////////////////////////
//  "utility" functions to aid the implementation of the
//     Message member functions



bool isAlphanumeric (char c)
{
  return (c >= 'A' && c <= 'Z')
    || (c >= 'a' && c <= 'z')
    || (c >= '0' && c <= '9');
}



bool paragraphStart  (const string& msg, int pos)
{
  if (pos == 0)
    return true;
  if (pos == 1)
    return false;
  return msg[pos-1] == '\n' && msg[pos-2] == '\n';
}


bool sentencePunctuation (char c)
{
  return c == '.' || c == '?' || c == '!';
}


/*
 * Return the position that denotes the first character of the sentence
 * that begins at or before position pos and that ends at or after
 * that position.
 */
int findSentenceStart (const string& msg, int pos)
{
  while (pos > 0 && !paragraphStart(msg,pos) && !sentencePunctuation(msg[pos]))
    --pos;
  if (sentencePunctuation(msg[pos]))
    ++pos;
  return pos;
}


bool paragraphEnd  (const string& msg, int pos)
{
  if (pos == msg.length())
    return true;
  if (pos == msg.length()-1)
    return false;
  return msg[pos] == '\n' && msg[pos+1] == '\n';
}



/*
 * Return the position that denotes the last character of the sentence
 * that begins at or before position pos and that ends at or after
 * that position.
 */
int findSentenceStop (const string& msg, int pos)
{
  while (pos < msg.length()
	 && !paragraphEnd(msg,pos)
	 && !(pos > 0 && sentencePunctuation(msg[pos-1])))
    ++pos;
  return pos;
}

///////////////////////////////////////////////////////////
//  Member functions of the Message struct



/*
 * Add a line of text (and a line-terminating '\n') to the message.
 */
void Message::addLine (std::string line)
{
  messageText += line + "\n";
}


//** Insert your definitions for the remaining member functions of Message
//** here. All of these should work by doing appropriate things to the
//** data member messageText that, in the original version of the program,
//** would have been done to a string passed as a separate parameter.

  /*
   * How many characters are in this message?
   */
  int Message::length() const
  {

  }

  /*
   * Print a message to the indicated output
   */
  void Message::print (std::ostream& out)
 {
    for (int i =0; i < messageText.length(); i ++)
     out << messageText[i];

}

  /*
   * Return true if the character at position pos is the first character
   * of a word.
   */
bool Message::wordBeginsAt (int pos) const //DONE
{
  if (pos == 0)
    return true;

  return isAlphanumeric(messageText[pos]) && (!isAlphanumeric(messageText[pos-1]));
}

  /*
   * Return the string comprising the word that begins at
   * position beginningAt.
   */
  std::string Message::extractWord (int beginningAt) const
{
  int len = 0;
  while (beginningAt + len < messageText.length()
	 && isAlphanumeric(messageText[beginningAt+len]))
    {
      ++len;
    }
  string result = messageText.substr(beginningAt, len);
  return result;
}


  /*
   * Replace by '@' all characters (except '\n') of the sentence
   * that begins at or before position pos and that ends at or after
   * that position.
   */

  void Message::censorSentenceAt (int pos)
{
  int start = findSentenceStart (messageText, pos);
  int stop = findSentenceStop (messageText, pos);
  for (int i = start; i < stop; ++i)
    if (messageText[i] != '\n')
      messageText[i] = '@';

}








1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include <iostream>
#include <fstream>
#include <string>

#include "dictionary.h"
#include "message.h"


using namespace std;




void censorMessage (Message& msg, Dictionary& sensitiveWords)
{
  for (int i = 0; i < msg.length(); ++i)
    {
      if (msg.wordBeginsAt(i))
	{
	  string word = msg.extractWord(i);
	  if (sensitiveWords.contains(word))
	    msg.censorSentenceAt (i);
	}
    }
}



void readSensitiveWords (istream& in, Dictionary& sensitiveWords)
{
  string word;
  while ((in >> word) && (word != "==="))
    sensitiveWords.add (word);
  getline (in, word);
}


void readMessage (istream& in, Message& msg)
{
  string line;
  getline (in, line);
  while (in)
    {
      msg.addLine (line);
      getline (in, line);
    }
}



int main()
{
  Dictionary sensitiveWords;
  readSensitiveWords(cin, sensitiveWords);
  Message msg;
  readMessage(cin, msg);
  censorMessage (msg, sensitiveWords);
  msg.print (cout);
  cout << flush;
  return 0;
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#ifndef MESSAGE_H
#define MESSAGE_H

#include <string>
#include <iostream>


//
// A message is a single long string
// containing \n characters separating the lines.
// A message can be divided into words and sentences.


struct Message {

  /*
   * Add a line of text (and a line-terminating '\n') to the message.
   */
  void addLine (std::string line);


  /*
   * How many characters are in this message?
   */
  int length() const;


  /*
   * Print a message to the indicated output
   */
  void print (std::ostream& out);


  /*
   * Return true if the character at position pos is the first character
   * of a word.
   */
  bool wordBeginsAt (int pos) const;

  /*
   * Return the string comprising the word that begins at
   * position beginningAt.
   */
  std::string extractWord (int beginningAt) const;

  /*
   * Replace by '@' all characters (except '\n') of the sentence
   * that begins at or before position pos and that ends at or after
   * that position.
   */
  void censorSentenceAt (int pos);


  /* ********************************************************** */
private:
  // The following are considered to be "hidden" or private. No code outside
  // of message.cpp should access the data below.

  std::string messageText;



};


#endif


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#ifndef VECTORUTILS_H
#define VECTORUTILS_H

#include <vector>




// Add value into vectr[index], shifting all elements already in positions
//    index..size-1 up one, to make room.

template <typename T>
void addElement (std::vector<T>& vectr, int& size, int index, T value)
{
  // Make room for the insertion
  int toBeMoved = vectr.size() - 1;
  vectr.push_back(value);

  // Shift elements up to make room
  while (toBeMoved >= index) {
    vectr[toBeMoved+1] = vectr[toBeMoved];
    --toBeMoved;
  }
  // Insert the new value
  vectr[index] = value;
}


// Assume the elements of the vector are already in order
// Find the position where value could be added to keep
//    everything in order, and insert it there.
// Return the position where it was inserted

template <typename T>
int addInOrder (std::vector<T>& vectr, T value)
{
  // Make room for the insertion
  int toBeMoved = vectr.size() - 1;
  vectr.push_back(value);

  // Shift elements up to make room
  while (toBeMoved >= 0 && value < vectr[toBeMoved]) {
    vectr[toBeMoved+1] = vectr[toBeMoved];
    --toBeMoved;
  }
  // Insert the new value
  vectr[toBeMoved+1] = value;
  return toBeMoved+1;
}


// Search a vector for a given value, returning the index where 
//    found or -1 if not found.
template <typename T>
int seqSearch(const std::vector<T>& list, T searchItem)
{
    int loc;

    for (loc = 0; loc < list.size(); loc++)
        if (list[loc] == searchItem)
            return loc;

    return -1;
}


// Search an ordered vector for a given value, returning the index where 
//    found or -1 if not found.
template <typename T>
int seqOrderedSearch(const std::vector<T> list, T searchItem)
{
    int loc = 0;

    while (loc < list.size() && list[loc] < searchItem)
      {
       ++loc;
      }
    if (loc < list.size() && list[loc] == searchItem)
       return loc;
    else
       return -1;
}


// Removes an element from the indicated position in the vector, moving
// all elements in higher positions down one to fill in the gap.
template <typename T>
void removeElement (T* vectr, int& size, int index)
{
  int toBeMoved = index + 1;
  while (toBeMoved < size) {
    vectr[toBeMoved] = vectr[toBeMoved+1];
    ++toBeMoved;
  }
  --size;
}



// Search an ordered vector for a given value, returning the index where 
//    found or -1 if not found.
template <typename T>
int binarySearch(const std::vector<T> list, T searchItem)
{
    int first = 0;
    int last = list.size() - 1;
    int mid;

    bool found = false;

    while (first <= last && !found)
    {
        mid = (first + last) / 2;

        if (list[mid] == searchItem)
            found = true;
        else 
            if (searchItem < list[mid])
                last = mid - 1;
            else
                first = mid + 1;
    }

    if (found) 
        return mid;
    else
        return -1;
}





#endif


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#ifndef DICTIONARY_H
#define DICTIONARY_H

#include <string>
#include <vector>

/**
 * A Dictionary is a collection of words. The primary operations
 * are adding new words to the dictionary and checking to see if some
 * word is already in there.
 */


struct Dictionary {

  /*
   *  Add a word to the dictionary
   */
  void add (std::string word);


  /*
   *  Check to see if a word is in the dictionary
   */
  bool contains (std::string word) const;



  /* ********************************************************** */

  // The following are considered to be "hidden" or private. No code outside
  // of dictionary.cpp should access the data and functions below.

  std::vector<std::string> words;


};


#endif


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include "dictionary.h"
#include "vectorUtils.h"

using namespace std;


string toLowerCase (string word)
{
  string result = word;
  for (int i = 0; i < word.length(); ++i)
    if (word[i] >= 'A' && word[i] <= 'Z')
      result[i] = result[i] + 'a' - 'A';
  return result;
}

/*
 *  Add a word to the dictionary
 */
void Dictionary::add (std::string word)
{
  addInOrder (words, toLowerCase(word));
}


/*
 *  Check to see if a word is in the dictionary
 */
bool Dictionary::contains (std::string word) const
{
  int k = binarySearch (words, toLowerCase(word));
  return k >= 0; // binarySearch returns -1 if not found
}
Topic archived. No new replies allowed.