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 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
|
#include <cstdlib> // For exit()
// What header file must be included to access the abs(), pow(), sqrt() functions?
#include <cmath>
// What header file must be included to access cin, cout, and endl?
#include <iostream>
// What header file must be included to use the string class?
#include <string>
using namespace std;
// Constants for converting inch to cm and lb to kg.
const double CM_PER_INCH = 2.54;
const double LB_PER_KG = 2.20462262;
// Eye color menu constants. NOTE: THESE ARE NOT THE VALUES OF VARIABLE e IN THE FORMULA.
const int MENU_BLACK = 1;
const int MENU_BLUE = 2;
const int MENU_GREEN = 3;
const int MENU_BROWN = 4;
const int MENU_GRAY = 5;
const int MENU_HAZEL = 6;
const int MENU_RED = 7;
const int MENU_PINK = 8;
const int MENU_VIOLET = 9;
void ValidateEyeColor();
// FUNCTION: CalcLucky()
//
// DESCRIPTION
// Calculates and returns the customer's lucky number using the Even Better Lucky Number Formula(tm).
//
// PARAMS
// d int date
// h int height in cm
// m int month
// w int weight in kg
// y int year
// e int eye value based on eye color
// f int finger value
//
// RETURNS
// An int which is the lucky number.
// Write the header for this function and the opening left brace for the body.
int CalcLucky(int d, int h, int m, int w, int y, int e, int f) {
// Define double variable term1 and assign to it: 2 m^2.2 + 3 d^3.3 + 5 sqrt(y).
// Note: if you are using MSVS, you will have to typecase year to a double before passing it to sqrt().
double term1 = (2 * pow(m,2.2)) + (3 * pow(d,3.3)) + (5 * sqrt(static_cast<double>(y)));
// Define int variable term2 and assign to it: (e h) / 3 performing integer division.
int term2 = (e / h) / 3;
// Define int variable term3 and assign to it: (11 w) / 5 performing integer divison.
int term3 = (11 * w) / 5;
// Define double variable term4 and assign to it: term1 + f (term2 + term3).
int term4 = term1 + (f * (term2 + term3));
// Calculate and return the lucky number using term4 and the lucky number formula. You must use the abs()
// function from the math library and type cast the result to int before performing the % 10 operation.
int l = static_cast<int>((term4 % 10) + 1);
return l; }
// FUNCTION: ConvertInchToCm()
double ConvertInchToCm(double inches)
{
return inches * CM_PER_INCH;
}
double ConvertLbToKg(double lbs)
{
return lbs / LB_PER_KG;
}
// FUNCTION: GetDouble()
//
// DESCRIPTION
// Display a prompt and read a number (as a double) from the keyboard. Return the number.
double GetDouble(string prompt)
{
double n;
cout << prompt;
cin >> n;
return n;
}
int GetInt(string prompt)
{
int n;
cout << prompt;
cin >> n;
return n;
}
// Write the header for this function and the opening left brace for the body.
int GetEyeColor() {
// Write cout statements to display the eye color menu per the project document.
cout << "1. Black" << endl << "2. Blue" << endl << "3. Green" << endl << "4. Brown" << endl << "5. Gray" << endl << "6. Hazel" << endl << "7. Red" << endl << "8. Pink" << endl << "9. Violet" << endl;
// Define an int variable named choice. Call GetInt() and pass "Select Eye Color: " as the parameter
// string. Assign the value returned by GetInt() to variable choice.
int choice;
GetInt("Select Eye Color: ") {
n = choice; }
// Call ValidateEyeColor() and pass choice as a parameter. If the user's menu choice was invalid (less than
// 1 or greater than 9) then ValidateEyeColor() will display an error message and terminate the program.
ValidateEyeColor(choice); {
if (choice < 1 || choice > 9) {
cout << "INVALID eye color, Program will terminate";
return 0; } }
// Define an int variable named e.
int e;
// Write an if-elseif-elseif-... statement to set e to the correct value depending on the value of choice.
// That is, you must check to see if choice is MENU_BLACK. If it is, then set e to 3. Otherwise, check to
// see if choice is MENU_BLUE. If it is, then set e to -2. Otherwise, check to see if choice is MENU_GREEN.
// If it is, then set e to 17. Otherwise...
if (choice == MENU_BLACK) {
e = 3; }
else if (choice == MENU_BLUE) {
e = -2; }
else if (choice == MENU_GREEN) {
e = 17; }
else if (choice == MENU_BROWN) {
e = 5; }
else if (choice == MENU_GRAY) {
e = 12; }
else if (choice == MENU_HAZEL) {
e = -8; }
else if (choice == MENU_RED) {
e = 4; }
else if (choice == MENU_PINK) {
e = 0; }
else if (choice == MENU_VIOLET) {
e = 11; }
// Return e.
return e; }
string GetString(string prompt)
{
string s;
cout << prompt;
cin >> s;
return s;
}
int Round(double n)
{
// Define an int variable named r.
int r;
// Write an if-else statement that rounds n up or down to the nearest integer and assign the rounded value
// to r. Hint: this will involve a static_cast<int> operation and you will need to add or subtract 0.5 to
// or from n.
if (n > 1) {
static_cast<int>(n); }
// Return r.
return r;
}
void ValidateEyeColor(int pChoice)
{
if (pChoice < MENU_BLACK || pChoice > MENU_VIOLET) {
cout << "An invalid eye color was selected. The program is terminating..." << endl;
exit(-1);
}
}
int main()
{
cout << "Zelda's Lucky Number Calculator" << endl << endl;
// Define these int local variables: date, e, f, heightCm, heightIn, lucky, month, weightKg, weightLb, year.
int date, e, f, heightCm, heightIn, lucky, month, weightKg, weightLb, year;
// Define double local variables: index, ring.
double index, ring;
// Define string object: name.
string name;
// Call GetString() asking for the customer's first name. Assign the return value to name.
GetString("What is your name? ") {
name = s; }
// Call GetInt() asking for the month of the customer's birthdate and assign the return value to month.
GetInt("What is your birth month? ") {
month = s; }
// Call GetInt() asking for the date of the customer's birthdate and assign the return value to date.
GetInt("What is your birth date? ") {
date = s; }
// Call GetInt() asking for the year of the customer's birthdate and assign the return value to year.
GetInt("What is your birth year? ") {
year = s; }
// Call GetInt() asking for the customer's height in inches and assign the return value to heightIn.
GetInt("What is your birth date? ") {;
heightIn = s; }
// Call GetInt() asking for the customer's weight in pounds and assign the return value to weightLb.
GetInt("What is your weight in pounds? ") {
weightLb = s; }
// Call ConvertInchToCm() passing heightIn as the parameter. Pass the return value from ConvertInchToCm()
// as the input parameter to Round(). Assign the return value from Round() to heightCm.
ConvertInchToCm(heightIn) {
Round(return) {
return heightCm; }
// Call ConvertLbToKg() passing weightLb as the parameter. Pass the return value from ConvertLbToKg() as
// the input parameter to Round(). Assign the return value from Round() to weightKg.
ConvertLbToKg(weightLb) {
Round(return) {
return weightKg; }
// Call GetEyeColor() to display the eye color menu and get the value of e. Assign the return value to e.
GetEyeColor() {
e = n; }
// Call GetDouble() asking for the length of the person's index finger and assign the return value
// to index.
GetDouble("What is your index finger length in inches? ") {
index = n; }
// Call GetDouble() asking for the length of the person's ring finger and assign the return value
// to ring.
GetDouble("What is your ring finger length in inches? ") {
ring = n; }
// Write an if-elseif-elseif statement that assigns 0 to f when index is equal to ring, -1 to f
// when index is less than ring, and 1 to f otherwise.
if (index == ring) {
f = 0; }
else if (index < ring) {
f = -1; }
else {
f = 1; }
// Call CalcLucky() passing date, heightCm, month, weightKg, year, e, and f as parameters. Assign the
// returned value to lucky.
CalcLucky(date, heightCm, month, weightKg, year, e, f) {
lucky = l; }
// Display the lucky number and return from main().
cout << lucky;
return 0;
}
|