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
|
// Do Programming Project 11 from Chapter 3 except write a function named
//containsDigit that determines if a number contains a particular digit. The
//header should look like: bool containsDigit(int number, int digit);
// If number contains digit , then the function should return true . Otherwise,
//the function should return false . Your program should use this function
//to find the closest numbers that can be entered on the keypad.
//The keypad on your oven is used to enter the desired baking temperature
//and is arranged like the digits on a phone:
//1 2 3
//4 5 6
//7 8 9
//0
//Unfortunately the circuitry is damaged and the digits in the leftmost col-
//umn no longer function. In other words, the digits 1, 4, and 7 do not work.
//If a recipe calls for a temperature that can’t be entered, then you would
//like to substitute a temperature that can be entered. Write a program that
//inputs a desired temperature. The temperature must be between 0 and 999
//degrees. If the desired temperature does not contain 1, 4, or 7, then output
//the desired temperature. Otherwise, compute the next largest and the next
//smallest temperature that does not contain 1, 4, or 7 and output both.
//For example, if the desired temperature is 450, then the program
//should output 399 and 500. Similarly, if the desired temperature is
//375, then the program should output 380 and 369.
#include <iostream>
bool isNum(int number, int digit);
bool containsDigit(int number, int digit);
int main()
{
using namespace std;
int temp;
int ones_digit, tens_digit, hundreds_digit;
bool contains_147;
bool num;
// get valid temp
do {
cout << "Please enter temp" << endl;
cin >> temp;
}
while (num = false);
// check if temp has a 1, 4, or 7
int lowertemp = temp;
ones_digit = lowertemp % 10;
tens_digit = (lowertemp/10) % 10;
hundreds_digit = (lowertemp/100);
if ((ones_digit==1) || (ones_digit ==4) || (ones_digit ==7) ||
(tens_digit==1) || (tens_digit ==4) || (tens_digit ==7) ||
(hundreds_digit==1) || (hundreds_digit ==4) || (hundreds_digit ==7))
contains_147 = true;
else
contains_147 = false;
while (contains_147)
{
// lowertemp contains the closest lower temp without 1,4, or 7
lowertemp--;
ones_digit = lowertemp % 10;
tens_digit = (lowertemp/10) % 10;
hundreds_digit = (lowertemp/100);
if ((ones_digit==1) || (ones_digit ==4) || (ones_digit ==7) ||
(tens_digit==1) || (tens_digit ==4) || (tens_digit ==7) ||
(hundreds_digit==1) || (hundreds_digit ==4) || (hundreds_digit ==7))
contains_147 = true;
else
contains_147 = false;
}
int highertemp = temp;
ones_digit = highertemp % 10;
tens_digit = (highertemp/10) % 10;
hundreds_digit = (highertemp/100);
if ((ones_digit==1) || (ones_digit ==4) || (ones_digit ==7) ||
(tens_digit==1) || (tens_digit ==4) || (tens_digit ==7) ||
(hundreds_digit==1) || (hundreds_digit ==4) || (hundreds_digit ==7))
contains_147 = true;
else
contains_147 = false;
while (contains_147)
{
highertemp++;
ones_digit = highertemp % 10;
tens_digit = (highertemp/10) % 10;
hundreds_digit = (highertemp/100);
if ((ones_digit==1) || (ones_digit ==4) || (ones_digit ==7) ||
(tens_digit==1) || (tens_digit ==4) || (tens_digit ==7) ||
(hundreds_digit==1) || (hundreds_digit ==4) || (hundreds_digit ==7)){
contains_147 = true;
}
else
{
contains_147 = false;
}
}
cout << "The next closest lower temperature is " << lowertemp << endl;
cout << "The next closest higher temperature is " << highertemp << endl;
char ch;
cin >> ch;
return 0;
}
bool isNum(int number, int digit)
{
bool num;
if ((digit==1) || (digit ==2) || (digit ==3) || (digit ==4) || (digit ==5) ||
(digit==6) || (digit ==7) || (digit ==8) || (digit ==9) || (digit ==0))
num = true;
else
num = false;
return num;
bool containsDigit(int number, int digit)
{
if ((digit==1) || (digit ==4) || (digit ==7))
contains_147 = true;
else
contains_147 = false;
}
|