#include
#include
#include
using namespace std;
class DateProfile {
private:
// private instance variables
char gender, searchGender;
int romance, finance;
string name;
public:
// public static constants
static const int MIN_ROMANCE;
static const int MAX_ROMANCE;
static const int MIN_FINANCE;
static const int MAX_FINANCE;
static const int MIN_NAME_LEN;
static const int MAX_NAME_LEN;
static const int DEFAULT_ROMANCE;
static const int DEFAULT_FINANCE;
static const char DEFAULT_GEND;
static const char DEFAULT_SEARCH_GEND;
static const string DEFAULT_NAME;
static const double VERY_BAD_FIT;
static const double PERFECT_FIT;
// constructor
DateProfile();
DateProfile(char gender,char searchGender,int romance, int finance,string name);
// mutators
bool setGender(char gdr);
bool setSearchGender(char schgdr);
bool setRomance(int rmc);
bool setFinance(int fnc);
bool setName(string nm);
void setAll(char gender,char searchGender,int romance, int finance,string name);
void setDefaults();
// accessors
char getGender();
char getSearchGender();
int getRomance();
int getFinance();
string getName();
// public methods
double fitValue(DateProfile partner);
private:
// private instance methods
double determineGenderFit(DateProfile partner);
double determineRomanceFit(DateProfile partner);
double determineFinanceFit(DateProfile partner);
};
// define static constants
int const DateProfile::MIN_ROMANCE = 1;
int const DateProfile::MAX_ROMANCE = 10;
int const DateProfile::MIN_FINANCE = 1;
int const DateProfile::MAX_FINANCE = 10;
int const DateProfile::MIN_NAME_LEN = 3;
int const DateProfile::MAX_NAME_LEN = 20;
int const DateProfile::DEFAULT_ROMANCE = 1;
int const DateProfile::DEFAULT_FINANCE = 1;
char const DateProfile::DEFAULT_GEND = 'M';
char const DateProfile::DEFAULT_SEARCH_GEND = 'F';
string const DateProfile::DEFAULT_NAME = "(undefined)";
double const DateProfile::VERY_BAD_FIT = 0;
double const DateProfile::PERFECT_FIT = 1;
// a global scope method
void displayTwoProfiles(DateProfile profile1, DateProfile profile2);
// client --------------------------------------------------------------------- int main() {
// Create 4 DateProfile objects
DateProfile applicant1('M', 'M', 3, 9, "Chandler Bing");
DateProfile applicant2('F', 'M', 10, 3, "Phoebe Buffay");
DateProfile applicant3('F', 'M', 5, 7, "Rachel Greens");
DateProfile applicant4('M', 'F', 9, 2, "Joey Tribbiani");
// display the fits with the others
displayTwoProfiles(applicant1, applicant1);
displayTwoProfiles(applicant1, applicant2);
displayTwoProfiles(applicant1, applicant3);
displayTwoProfiles(applicant1, applicant4);
cout << endl;
displayTwoProfiles(applicant2, applicant1);
displayTwoProfiles(applicant2, applicant2);
displayTwoProfiles(applicant2, applicant3);
displayTwoProfiles(applicant2, applicant4);
cout << endl;
displayTwoProfiles(applicant3, applicant1);
displayTwoProfiles(applicant3, applicant2);
displayTwoProfiles(applicant3, applicant3);
displayTwoProfiles(applicant3, applicant4);
cout << endl;
displayTwoProfiles(applicant4, applicant1);
displayTwoProfiles(applicant4, applicant2);
displayTwoProfiles(applicant4, applicant3);
displayTwoProfiles(applicant4, applicant4);
cout << endl; }
// DateProfileg class methods
// constructors
DateProfile::DateProfile() {
gender = DEFAULT_GEND;
searchGender = DEFAULT_SEARCH_GEND;
romance = DEFAULT_ROMANCE;
finance = DEFAULT_FINANCE;
name = DEFAULT_NAME; }
DateProfile::DateProfile(char gender,char searchGender,int romance, int finance,string name) {
if (!setGender(gender))
gender = DEFAULT_GEND;
if (!setSearchGender(searchGender))
searchGender = DEFAULT_SEARCH_GEND;
if (!setRomance(romance))
romance = DEFAULT_ROMANCE;
if (!setFinance(finance))
finance = DEFAULT_FINANCE;
if (!setName(name))
name = DEFAULT_NAME; }
// mutators
bool DateProfile::setGender(char gdr) {
if (gdr != 'M' && gdr != 'F' )
return false;
gender = gdr;
return true; }
bool DateProfile::setSearchGender(char schgdr) {
if (schgdr!= 'M' && schgdr!= 'F' )
return false;
searchGender = schgdr;
return true; }
bool DateProfile::setRomance(int rmc) {
if (rmc < MIN_ROMANCE || rmc > MAX_ROMANCE )
return false;
romance = rmc;
return true; }
bool DateProfile::setFinance(int fnc) {
if (fnc < MIN_FINANCE || fnc > MAX_FINANCE )
return false;
finance = fnc;
return true; }
bool DateProfile::setName(string nm) {
if (nm.length()< MIN_NAME_LEN || nm.length() > MAX_NAME_LEN)
return false;
name = nm;
return true; }
void DateProfile::setAll(char gender,char searchGender,int romance,
int finance,string name)
{ setGender(gender);
setSearchGender(searchGender);
setRomance(romance);
setFinance(finance);
setName(name); }
void DateProfile::setDefaults() { gender = DEFAULT_GEND;
searchGender = DEFAULT_SEARCH_GEND;
romance = DEFAULT_ROMANCE;
finance = DEFAULT_FINANCE;
name = DEFAULT_NAME; }
// accessors
char DateProfile::getGender() {
return gender; }
char DateProfile::getSearchGender() {
return searchGender; }
int DateProfile::getRomance() {
return romance; }
int DateProfile::getFinance() {
return finance; }
string DateProfile::getName() {
return name; }
// private methods
double DateProfile:: determineGenderFit(DateProfile partner) {
if (this->gender != partner.searchGender || partner.gender != this->searchGender)
return VERY_BAD_FIT;
return PERFECT_FIT; }
double DateProfile:: determineRomanceFit(DateProfile partner) {
double difference = this->romance - partner.romance;
double absDifference = difference >= 0 ? difference : -difference;
double maxDifference = MAX_ROMANCE - MIN_ROMANCE + 0.1;
return 1 - (absDifference / maxDifference); }
double DateProfile:: determineFinanceFit(DateProfile partner) {
double difference = this->finance - partner.finance;
double absDifference = difference >= 0 ? difference : -difference;
double maxDifference = MAX_FINANCE - MIN_FINANCE + 0.1;
return 1 - (absDifference / maxDifference); }
// public methods double DateProfile:: fitValue(DateProfile partner) {
if (determineGenderFit(partner)< PERFECT_FIT)
return 0;
else
return (determineRomanceFit(partner) + determineFinanceFit(partner)) / 2; }
// print the names of the two objects and show the fit value
void displayTwoProfiles(DateProfile profile1, DateProfile profile2) {
cout << "Fit between " << profile1.getName() << " and " << profile2.getName() << ":" << profile1.fitValue(profile2) << endl; }
/* ------------------------ RUN ------------------------
Fit between Chandler Bing and Chandler Bing:1
Fit between Chandler Bing and Phoebe Buffay:0
Fit between Chandler Bing and Rachel Greens:0
Fit between Chandler Bing and Joey Tribbiani:0
Fit between Phoebe Buffay and Chandler Bing:0
Fit between Phoebe Buffay and Phoebe Buffay:0
Fit between Phoebe Buffay and Rachel Greens:0
Fit between Phoebe Buffay and Joey Tribbiani:0.9
Fit between Rachel Greens and Chandler Bing:0
Fit between Rachel Greens and Phoebe Buffay:0
Fit between Rachel Greens and Rachel Greens:0
Fit between Rachel Greens and Joey Tribbiani:0.55
Fit between Joey Tribbiani and Chandler Bing:0
Fit between Joey Tribbiani and Phoebe Buffay:0.9
Fit between Joey Tribbiani and Rachel Greens:0.55
Fit between Joey Tribbiani and Joey Tribbiani:0
Program ended with exit code: 0
---------------------------------------------------------- */
C++: Free Frozen Yogurt
#include
#include
#include
using namespace std;
//define the getKeyCharacter method
char getKeyCharacter()
{
string keyCharacter;
bool validated;
int length;
char letter;
// Get input from console, until it passes our tests
validated = false;
while (!validated)
{
cout << "Please enter a SINGLE character to act as key:";
getline(cin, keyCharacter);
length = keyCharacter.length();
// test for reasonable length
if (length == 1)
{
letter = keyCharacter[0];
break;
}else
continue;
}
return letter;
}
//define the getString method
string getString()
{
string theString;
bool validated;
int length;
const int MIN_LENGTH = 6;
const int MAX_LENGTH = 160;
// Get input from console, until it passes our tests
validated = false;
while (!validated)
{
cout << "Please enter a phrase or sentence > = " <<MIN_LENGTH << " and <= " << MAX_LENGTH << " characters:";
getline(cin, theString);
length = theString.length();
// test for reasonable length
if (length >= MIN_LENGTH && length <= MAX_LENGTH)
break;
else
continue;
}
return theString;
}
//define the maskCharacter method
string maskCharacter(string theString, char keyCharacter)
{
for (int i=0; i<= theString.length(); i++)
{
if (theString[i] == keyCharacter)
theString[i] = '#';
}
return theString;
}
//define the removeCharacter method
string removeCharacter(string theString, char keyCharacter)
{
string newString="";
for (int i=0; i<=theString.length(); i++)
{
if (theString[i]!= keyCharacter)
newString = newString + theString[i] ;
}
return newString;
}
//define the countKey method
int countKey(string theString, char keyCharacter)
{
int countKey=0;
for(int i=0; i<=theString.length(); i++)
{
if (theString[i] == keyCharacter)
countKey++;
}
return countKey;
}
// main
int main()
{
string userInput;
while(true)
{
//Ask the user to enter a key character
char keyCharacter = getKeyCharacter();
//Ask the user to enter a target string (phrase, sentence, etc.)
string theString = getString();
cout<<endl;
//has each occurrence of the key character replaced by a number sign char, '#'
string stringMasked = maskCharacter(theString, keyCharacter);
cout << "String with key character, '" << keyCharacter << "' masked:"<< endl;
cout << stringMasked <<endl << endl;
//remove the key character
string stringRemoved = removeCharacter(theString, keyCharacter);
cout << "String with '" << keyCharacter << "' removed:" << endl;
cout <<stringRemoved << endl << endl;
// count the number of key character
int occurence = countKey(theString, keyCharacter);
cout << "# of occurrences of key character, '" <<keyCharacter << "': " << occurence << endl;
cout <<"Press any key to continue . . ." <<endl << endl;
getline(cin,userInput);
}
return 0; }
/* ------------------------ RUN ------------------------
Please enter a SINGLE character to act as key:123
Please enter a SINGLE character to act as key:abc
Please enter a SINGLE character to act as key:
Please enter a SINGLE character to act as key:a
Please enter a phrase or sentence > = 6 and < = 160 characters:i like apple banana pineapple %
String with key character, 'a' masked:
i like #pple b#n#n# pine#pple %
String with 'a' removed:
i like pple bnn pinepple %
# of occurrences of key character, 'a': 5
Press any key to continue . . .
2
Please enter a SINGLE character to act as key:NM
Please enter a SINGLE character to act as key:,,,
Please enter a SINGLE character to act as key:8
Please enter a phrase or sentence > = 6 and < = 160 characters:18888999923089009238998795338
String with key character, '8' masked:
1####9999230#900923#99#79533#
String with '8' removed:
199992309009239979533
# of occurrences of key character, '8': 8
Press any key to continue . . .
3
Please enter a SINGLE character to act as key:M
Please enter a phrase or sentence > = 6 and < = 160 characters:NM
Please enter a phrase or sentence > = 6 and < = 160 characters:8889
Please enter a phrase or sentence > = 6 and < = 160 characters:
Please enter a phrase or sentence > = 6 and < = 160 characters:888899999IU
String with key character, 'M' masked:
888899999IU
String with 'M' removed:
888899999IU
# of occurrences of key character, 'M': 0
Press any key to continue . . .
4
Please enter a SINGLE character to act as key:N
Please enter a phrase or sentence > = 6 and < = 160 characters:NMNNNNNM?bcd3457n2wennnNNNN%
String with key character, 'N' masked:
#M#####M?bcd3457n2wennn####%
String with 'N' removed:
MM?bcd3457n2wennn%
# of occurrences of key character, 'N': 10
Press any key to continue . . .
5
Please enter a SINGLE character to act as key:#
Please enter a phrase or sentence > = 6 and < = 160 characters:nmnmn8979990989#ssss#ssss#3333#####
String with key character, '#' masked:
nmnmn8979990989#ssss#ssss#3333#####
String with '#' removed:
nmnmn8979990989ssssssss3333
# of occurrences of key character, '#': 8
Press any key to continue . . .
6
Please enter a SINGLE character to act as key:#
Please enter a phrase or sentence > = 6 and < = 160 characters:###########################
String with key character, '#' masked:
###########################
String with '#' removed:
# of occurrences of key character, '#': 27
Press any key to continue . . .
7
Please enter a SINGLE character to act as key:nnnn
Please enter a SINGLE character to act as key:n
Please enter a phrase or sentence > = 6 and < = 160 characters:jnmnjnmnjnmnnmnjhgvbnmjbnhjnmhjiouytuiioonhjgutrednbmjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjnnnnnmmkkkkkkkkkkkkkkkkkkkkkiiooooooooooooppppplllllllllllllllllllllllllllllljjiiijkkjhjjkkklmnhjbnjkkmbbbbbbbbbbbbbbbbbbbbbbbbbbbbbnmjhnmjhnmghbnm
Please enter a phrase or sentence > = 6 and < = 160 characters:nmnmjkhuiyjihikn
String with key character, 'n' masked:
#m#mjkhuiyjihik#
String with 'n' removed:
mmjkhuiyjihik
# of occurrences of key character, 'n': 3
Press any key to continue . . .
8
Please enter a SINGLE character to act as key://///
Please enter a SINGLE character to act as key:0000
Please enter a SINGLE character to act as key:.
Please enter a phrase or sentence > = 6 and < = 160 characters:?.,./,./{]][;=-=;'}
String with key character, '.' masked:
?#,#/,#/{]][;=-=;'
String with '.' removed:
?,/,/{]][;=-=;'
# of occurrences of key character, '.': 3
Press any key to continue . . .
Program ended with exit code: 0
---------------------------------------------------------- */