Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
I am working on a c++ project and I am having trouble understanding. This should be pretty as each code you need to insert should only be one or two lines. Here is what I have to do and what I have already figured out on my own, if anyone could help I would really appreciate it.
You will write the member functions of the class set. For the simplicity of lab you can do these functions inline. There are 6 functions for write their prototypes are:
void insert(char c);
void insert(const char chars[]);
bool isMember(char c);
void remove(char c);
string toSrting();
in indexFromChar(char c)put a specific character into the set, if the character is already in the set, there are no changes
void insert(char c)a function that removes a character from the set
remove(char c)insert a null-terminated array of characters into the set. This is a c-string, you can insert character by character into the set.
vod insert(const char chars[])a function that takes all of the characters that are in the set and converts them to a c++ string.
toString()a function that returns a string containing all of the characters in this set is ASCII-valus order. NOTE this will act funny with non-printable characters
string toString()a function that maps characters 1-1 to their position in the array (range 0-255) remember that the signed chars have valu -128 to 127.
int indexFormChar(char c)THIS IS THE CODE. I PUT A COMMENT OF CODE WHERE I NEED THE HELP.
#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[i] = false;
}// put a specific character into the set
// if the character is already in the set, no change
void insert(char c) {
//YOUR CODE HERE
}// 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) {
//YOUR CODE HERE
}}
// 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 orderstring toString() {
//YOUR CODE HERE
}private:
// maps characters to the range 0-255 in a 1-1 manner
int indexFromChar(char c) {
//YOUR CODE HERE
}
}// 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;
}

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |