cpp
#include <iostream>
#include <cstring>
#include <string>
using namespace std;
// chars are 8-bit, meaning there can only be 256 of them
const int NUMBER_OF_UNIQUE_CHARS = 256;
class CharSet {
public:
// constructors initializes the set to empty
CharSet() {
for (int i = 0; i < NUMBER_OF_UNIQUE_CHARS; i++)
isInSet = false;
}
// put a specific character into the set
// if the character is already in the set, no change
void insert(char c) {
}
// check if a specific character in in the set
bool isMember(char c) {
//YOUR CODE HERE
}
// remove a character from the set
// if the character is not in the set, no change
void remove(char c) {
}
}
// insert a null-terminated (c-string) of characters
// into the set
void insert(const char chars[]) {
//YOUR CODE HERE
}
// return a string containing all of the characters in
// this set in ASCII-value order
string toString() {
}
private:
// maps characters to the range 0-255 in a 1-1 manner
int indexFromChar(char c) {
}
}
// maps the range 0-255 into characters in a 1-1 manner
char charFromIndex(int i) {
unsigned char uc = i;
return (char)uc;
}
// an array of 256 bools, each reresents wheter a specific
// character is in the set or not
bool isInSet[NUMBER_OF_UNIQUE_CHARS];
};
////////////////////////////////////////////////////////////
//
// TEST CODE FOR CHARSET
//
////////////////////////////////////////////////////////////
int main() {
// BEGIN TEST ONE: RANDOM NAME
CharSet s1;
// insertions galore, some duplicates
s1.insert('B');
s1.insert('e');
s1.insert('n');
s1.insert('H');
s1.insert('e');
s1.insert('s');
s1.insert('c');
s1.insert('o');
s1.insert('t');
s1.insert('t');
// test the tostring method
// NOTE: we output what we expect to get when testing
cout << endl << "Expecting s1 to contains \"BHcenost\"" << endl;
cout << "s1 actually contains \"" << s1.toString() << "\"" << endl << endl;
// test the ismember method
cout << "Expecting: false - true - true" << endl;
cout << "Got: "<< s1.isMember('b') << " - " << s1.isMember('e')
<< " - " << s1.isMember('n') << endl << endl;
// BEGIN TEST TWO THE COMPLETE ALPHABET
CharSet s2;
s2.insert("The quick brown fox jumps over the lazy dog.");
s2.remove(' ');
s2.remove('.');
s2.insert('t');
s2.remove('T');
cout << "Expecting s2 to contain \"abcdefghijklmnopqrstuvwxyz\"" << endl;
cout << "s2 actually contains \"" << s2.toString() << "\"" << endl << endl;
}
Mod edit: Please
[code=cpp
] paste your code between these tags
[/code
]Thanks, gabehabe
This post has been edited by gabehabe: 31 Aug, 2008 - 10:47 AM