How should I insert a char into a set?

Aug 24, 2011 at 5:40pm
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.
Aug 24, 2011 at 5:56pm
Post the whole class declaration.
Aug 24, 2011 at 6:45pm
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
const int 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(const char 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) {
		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[UNIQUECHARS];
};
Last edited on Aug 24, 2011 at 6:45pm
Aug 24, 2011 at 7:51pm
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.
Aug 24, 2011 at 9:06pm
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.
Aug 25, 2011 at 5:58am
You don't know how to do what?
Aug 25, 2011 at 1:35pm
cast a char to an unsigned char. Why would I want to make it an integer (if that's what you're saying)?
Aug 25, 2011 at 2:31pm
Here's the implementation of indexFromChar:
1
2
unsigned char uc = c;
return uc;

It could be just return (unsigned char)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.
Aug 25, 2011 at 2:45pm
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.
Topic archived. No new replies allowed.