/*--- CS 111 - Week 10 Lecture 2 - 2024-10-31 by: Sharon Tuttle last modified: 2024-10-31 ---*/ #include <cstdlib> #include <iostream> #include <string> #include <cmath> using namespace std; /*=== signature: next_sound: string -> string purpose: expects an animal sound from the early part of Sandra Boyton's book "Moo, Baa, La La La", and returns the next sound from that book as follows: * for moo, return baa * for baa, return la la la * for la la la, return oink * for oink, return moo * for anything else, return ??? tests: next_sound("moo") == "baa" next_sound("baa") == "la la la" next_sound("la la la") == "oink" next_sound("oink") == "moo" next_sound("meow-rowr") == "???" ===*/ string next_sound(string animal_sound) { if (animal_sound == "moo") return "baa"; else if (animal_sound == "baa") return "la la la"; else if (animal_sound == "la la la") return "oink"; else if (animal_sound == "oink") return "moo"; else return "???"; } /*=== signature: porridge_state: double -> string purpose: expects a porridge temperature (in Fahrenheit), and returns a string, "mama-approved", "papa-approved", or "baby-approved" based on the following: Mama Bear's pref: < 50 degrees Papa Bear's pref: > 80 degrees Baby Bear's pref: in [50, 80] tests: porridge_state(34) == "mama-approved" porridge_state(50) == "baby-approved" porridge_state(65) == "baby-approved" porridge_state(80) == "baby-approved" porridge_state(99) == "papa-approved" ====*/ string porridge_state(double porridge_temp) { if (porridge_temp < 50) { return "mama-approved"; } else if (porridge_temp <= 80) { return "baby-approved"; } else { return "papa-approved"; } } /*--- copied from Week 10 Lecture 1 in-class examples ---*/ /*=== signature: name_length: string string -> int purpose: expects a first name and a last name, and returns the total length of the full name (first and last combined) tests: name_length("Jonathan", "Vex") == 11 name_length("Holly", "Dawn") == 9 ===*/ int name_length(string first_name, string last_name) { return first_name.length() + last_name.length(); } /*--- end of part copied from Week 10 Lecture 1 in-class examples ---*/ //===== // desired maximum total length of a first+last name // before we would prefer a first initial be used const int NAME_LENGTH_MAX = 30; /*=== signature: pretty_name_2: string string -> string purpose: expects a first name and a last name, and if the combined length of both is less than NAME_LENGTH_MAX, return a string with the last name, a comma and blank, and the first name, OTHERWISE, it returns a string with the last name, a comma and blank, and the first letter in the first name (their initial) followed by a period tests: pretty_name_2("Jo", "Williams") == "Williams, Jo" pretty_name_2("Elizabetha-Lisa", "Wendell-Johnson") == "Wendell-Johnson, E." pretty_name_2("Alexander", "Graham-Bell-Johnson") == "Graham-Bell-Johnson, A." ===*/ string pretty_name_2(string first_name, string last_name) { if (name_length(first_name, last_name) < NAME_LENGTH_MAX) { return last_name + ", " + first_name; } else { return last_name + ", " + first_name.at(0) + '.'; } } /*--- test the functions above ---*/ int main() { cout << boolalpha; cout << "*** Testing: next_sound ***" << endl; cout << (next_sound("moo") == "baa") << endl; cout << (next_sound("baa") == "la la la") << endl; cout << (next_sound("la la la") == "oink") << endl; cout << (next_sound("oink") == "moo") << endl; cout << (next_sound("meow-rowr") == "???") << endl; cout << "Just for fun: " << next_sound("oink") << endl; cout << "*** Testing: porridge_state ***" << endl; cout << (porridge_state(34) == "mama-approved") << endl; cout << (porridge_state(50) == "baby-approved") << endl; cout << (porridge_state(65) == "baby-approved") << endl; cout << (porridge_state(80) == "baby-approved") << endl; cout << (porridge_state(99) == "papa-approved") << endl; cout << "Just to demo: " << porridge_state(65) << endl; cout << "*** Testing: pretty_name_2 ***" << endl; cout << (pretty_name_2("Jo", "Williams") == "Williams, Jo") << endl; cout << (pretty_name_2("Elizabetha-Lisa", "Wendell-Johnson") == "Wendell-Johnson, E.") << endl; cout << (pretty_name_2("Alexander, Jr.", "Graham-Bell-Johnson") == "Graham-Bell-Johnson, A.") << endl; cout << "Demo this: " << endl; cout << pretty_name_2("Elizabetha-Lisa", "Wendell-Johnson") << endl; return EXIT_SUCCESS; }