I'm trying to inset a char into a data set. They way it has to be done is the following.
The char is sent from int main to a class and inserted with a function called insert. Here's what it looks like in int main.
1 2 3 4
CharSet s1;
s1.insert('z');
s1.insert('a');
The constructor and insert function in the CharSet class look like this:
1 2 3 4 5 6 7 8 9 10
CharSet() {
for (int i = 0; i < UNIQUECHARS; i++)
isInSet[i] = false;
}
// put a specific character into the set
// if the character is already in the set, no change
void insert(char c) {
}
There's nothing in the insert function because I'm really unsure how to do it. I've made a similar program before but that was using the +operator which we aren't using here. Any help would be greatly appreciated. Thank you.
constint UNIQUECHARS = 256;
class CharSet {
public:
// constructors initializes the set to empty
CharSet() {
for (int i = 0; i < UNIQUECHARS; i++)
isInSet[i] = false;
}
// put a specific character into the set
// if the character is already in the set, no change
void insert(char c) {
//code
}
// check if a specific character in in the set
bool isMember(char c) {
//code
}
// remove a character from the set
// if the character is not in the set, no change
void remove(char c) {
//code
}
// insert a null-terminated (c-string) of characters
// into the set
void insert(constchar chars[]) {
//code
}
// return a string containing all of the characters in
// this set in ASCII-value order
// note: this will act funny with non-printable characters
string toString() {
//code
}
private:
// maps characters to the range 0-255 in a 1-1 manner
// remember, signed chars have value -128 to 127
int indexFromChar(char c) {
}
// maps the range 0-255 into characters in a 1-1 manner
char charFromIndex(int i) {
unsignedchar uc = i;
return (char)uc;
}
// an array of 256 bools, each reresents wheter a specific
// character is in the set or not
bool isInSet[UNIQUECHARS];
};
For indexFromChar, cast that char to unsigned char and then to integer.
Insert would do isInSet[ indexFromChar(c) ] = true; all others are almost the same. Except toString, maybe.
I honestly don't know how to do that. Is that required before I can add the chars from int main into the set?
I haven't coded in about 4 months so I'm rusty but I never learned how to do these things in my previous class.
It could be just return (unsignedchar)c;, but I wrote it to look exactly like charFromIndex. Did you write the code you have yourself? If no, you may want to see a tutorial.
No I didn't write the code, I'm just supposed to fill in the blanks and make it work. I've been spending some time in the reference section but I can't seem to find exactly what I'm looking for.