//lab11.cxx //Author: Suzanne Gallagher //This is a possible solution to the problem for lab 11. Note that it is not //the only possible solution. In particular, there are at least 9 ways //that the Shoe struct could be set up. #include #include #include using namespace std; struct Character { string last; string first; char middle; }; //struct Shoe needs to store the following information: //1. Size of shoe (including potential half-sizes) //2. Whether it is a left or right shoe struct Shoe { int size; bool half; //This will be true if the size is a half size bool right; //This will be true for a right shoe and false for a left }; //function prototypes void init_character(Character& me, string last_name, string first_name, char middle_init); void print_result(Shoe s1, Shoe s2); Character last_character(Character us[], int size); void init_shoe(Shoe& mine, int size, bool half, bool right);//Parameters I want bool is_pair(Shoe s1, Shoe s2); int main() { Character cast[10]; Character last; Shoe evil1, evil2, ultimate_evil, not_quite_evil_enough; //initialize evil1: a right shoe of size 6 init_shoe(evil1, 6, false, true); //initialize evil2: a left shoe of size 6 init_shoe(evil2, 6, false, false); //initialize ultimate_evil: a left shoe of size 14 and a half init_shoe(ultimate_evil, 14, true, false); //initialize not_quite_evil_enough: a right shoe of size 14 init_shoe(not_quite_evil_enough, 14, false, true); init_character(cast[0], "Marple", "Jane", 'A'); init_character(cast[1], "Poirot", "Hercule", 'M'); init_character(cast[2], "Denmark", "Hamlet", 'P'); init_character(cast[3], "Montague", "Romeo", 'V'); init_character(cast[4], "Montague", "Benvolio", 'M'); init_character(cast[5], "Public", "John", 'E'); init_character(cast[6], "Public", "John", 'Q'); init_character(cast[7], "Stark", "Robb", 'G'); init_character(cast[8], "Stark", "Sansa", 'L'); init_character(cast[9], "Stark", "Arya", 'N'); last = last_character(cast, 5); cout << "The last of the first five characters is " << last.last << ", " << last.first << " " << last.middle << "." << endl; cout << "The last of the first five characters should be Poirot, Hercule M." << endl; last = last_character(cast, 7); cout << "The last of the first seven characters is " << last.last << ", " << last.first << " " << last.middle << "." << endl; cout << "The last of the first seven characters should be Public, John Q." << endl; last = last_character(cast, 10); cout << "The last of the characters is " << last.last << ", " << last.first << " " << last.middle << "." << endl; cout << "The last of the characters should be Stark, Sansa L." << endl; cout << "\nFirst test:" << endl; print_result(evil1, ultimate_evil); cout << "They should not be a pair." << endl; cout << "Second test:" << endl; print_result(evil1, evil1); cout << "They should not be a pair." << endl; cout << "Third test:" << endl; print_result(ultimate_evil, not_quite_evil_enough); cout << "They should not be a pair." << endl; cout << "Forth test:" << endl; print_result(evil1, evil2); cout << "They should be a pair." << endl; cout << "Fifth test:" << endl; print_result(evil1, evil2); cout << "They should be a pair." << endl; } void init_character(Character& me, string last_name, string first_name, char middle_init) { me.last = last_name; me.first = first_name; me.middle = middle_init; } void print_result(Shoe s1, Shoe s2) { if(is_pair(s1, s2)) { cout << "The shoes are a pair!" << endl; } else { cout << "The shoes are not a pair." << endl; } } //This function returns the character who would be last if the characters were listed //alphabetically by last name. //Any ties in the last name should be broken by looking at the first name //Any ties in both the last name and the first name should be broken by looking at the //middle initial Character last_character(Character us[], int size) { Character temp = us[0]; //Stores the last Character found so far int i; //Loop variable for(i=1; i temp.last) //Look at last names { temp = us[i]; } else if(us[i].last == temp.last) //If last names are the same { //then if(us[i].first > temp.first) //look at first names { temp = us[i]; } else if(us[i].first == temp.first) //If first names are the same { //then if(us[i].middle > temp.middle) //Look at middle initials { temp = us[i]; } } } } return temp; //Return the Character that we found } //This function initializes the value of a shoe //You should fill in the values for the parameters that you are going to want. //Remember to fill in parameters in prototype as well as calls in main function. //If you are stuck, look at the init_character function for ideas. void init_shoe(Shoe& mine, int size, bool half, bool right) { mine.size = size; mine.half = half; mine.right = right; } //This function determins whether or not two shoes are a pair. //Two shoes are a pair if they have the same size, but one is a left shoe //and the other is a right shoe. bool is_pair(Shoe s1, Shoe s2) { //Boolean expression will be true if and only if shoes are a pair return (s1.size==s2.size) && (s1.half==s2.half) && (s1.right != s2.right); }