1Boolean Functions The keypad on your oven is used to enter
1.(Boolean Functions) 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 column no longer function. In other words, the digits 1, 4, 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, computer the next larest 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. Similarily, if the desired temperature is 375, then the program should output 380 and 369.
Write a function named containDigit to determine 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. Other wise, the function should return false. Your program should use this function to find the closest numbers that can be entered on the keypad.
| 1 | 2 | 3 |
| 4 | 5 | 6 |
| 7 | 8 | 9 |
| 0 |
Solution
// C++ code
#include <fstream> // file processing
#include <iostream> // cin and cout
#include <cctype> // toupper
#include <iomanip> // setw
#include <cstring> // cstring functions strlen, strcmp, strcpy stored in string.h
#include <string.h> // string class
#include <stdlib.h>
#include <vector>
using namespace std;
bool containsDigit(int number, int digit)
{
int x = number/100;
int y = (number/10)%10;
int z = number%10;
if (x == digit || y == digit || z == digit)
return true;
else
return false;
}
int main()
{
int temperature;
cout << \"Enter the temperature: \";
cin >> temperature;
// input validation
if (temperature < 0 && temperature >999)
{
cout << \"Please enter a valid temperature. \" << endl;
cin >> temperature;
}
int t1 = temperature;
while(containsDigit(t1, 1) || containsDigit(t1, 4) || containsDigit(t1, 7))
t1--;//keeps decrementing temperature until it doesn\'t contain 1 ,4 nor 7
int t2 = temperature;
while(containsDigit(t2, 1) || containsDigit(t2, 4) || containsDigit(t2, 7))
t2++; //keeps incrementing temperature until it doesn\'t contain 1 ,4 nor 7
cout << \"The correct temperature :\" << t1 << \" and \" << t2 << endl;
return 0;
}
/*
output:
Enter the temperature; 450
The correct temperature :399 and 500
Enter the temperature: 375
The correct temperature :369 and 380
*/

