the switch statement selection revisited. problem using ocd, design and implement a program that...

28
The switch Statement The switch Statement Selection Revisited Selection Revisited

Upload: beverly-campbell

Post on 24-Dec-2015

215 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: The switch Statement Selection Revisited. Problem Using OCD, design and implement a program that allows the user to perform an arbitrary temperature conversion

The switch StatementThe switch Statement

Selection RevisitedSelection Revisited

Page 2: The switch Statement Selection Revisited. Problem Using OCD, design and implement a program that allows the user to perform an arbitrary temperature conversion

ProblemProblem

Using OCD, design and implement a Using OCD, design and implement a program that allows the user to perform an program that allows the user to perform an arbitrary temperature conversion (Celsius-arbitrary temperature conversion (Celsius-to-Kelvin, Kelvin-to-Celsius, Fahrenheit-to-to-Kelvin, Kelvin-to-Celsius, Fahrenheit-to-Celsius, Celsius-to-Fahrenheit, Fahrenheit-Celsius, Celsius-to-Fahrenheit, Fahrenheit-to-Kelvin, or Kelvin-to-Fahrenheit).to-Kelvin, or Kelvin-to-Fahrenheit).

Page 3: The switch Statement Selection Revisited. Problem Using OCD, design and implement a program that allows the user to perform an arbitrary temperature conversion

Preliminary AnalysisPreliminary Analysis

One way to solve the problem is to provide a One way to solve the problem is to provide a menumenu of choices, from which the user of choices, from which the user selects the operation they want to perform:selects the operation they want to perform:

Please enter: a - to convert Fahrenheit to Celsius b - to convert Celsius to Fahrenheit c - to convert Fahrenheit to Kelvin d - to convert Kelvin to Fahrenheit e - to convert Celsius to Kelvin f - to convert Kelvin to Celsius q - to quit-->

Page 4: The switch Statement Selection Revisited. Problem Using OCD, design and implement a program that allows the user to perform an arbitrary temperature conversion

Behavior: main()Behavior: main()

Our program should display its purpose Our program should display its purpose and a menu of choices. It should then and a menu of choices. It should then read the user’s choice. It should then read the user’s choice. It should then prompt for and read the temperature prompt for and read the temperature to be converted. It should then to be converted. It should then compute the converted temperature compute the converted temperature by applying to the input temperature by applying to the input temperature the particular conversion specified by the particular conversion specified by the user’s choice. It should then the user’s choice. It should then display the converted temperature.display the converted temperature.

Page 5: The switch Statement Selection Revisited. Problem Using OCD, design and implement a program that allows the user to perform an arbitrary temperature conversion

Objects: main()Objects: main()

Description Type Kind NameDescription Type Kind Name

menu string constant MENUchoice char variable choiceinput temp. double variable tempInoutput temp. double variable tempOut

purpose string constant --

Page 6: The switch Statement Selection Revisited. Problem Using OCD, design and implement a program that allows the user to perform an arbitrary temperature conversion

Operations: main()Operations: main()

Description Predefined? Library? NameDescription Predefined? Library? Name

display strings yes iostream <<read a double yes iostream >>convert a temp. no -- --display double yes iostream <<

Page 7: The switch Statement Selection Revisited. Problem Using OCD, design and implement a program that allows the user to perform an arbitrary temperature conversion

Algorithm: main()Algorithm: main()

0. Display via cout the purpose of the program.0. Display via cout the purpose of the program.

1. Loop1. Loop

a. Display a. Display MENUMENU via via coutcout..

b. Read b. Read choicechoice from from cincin..

c. If c. If choicechoice is ‘q’, terminate repetition. is ‘q’, terminate repetition.

d. Prompt for and read d. Prompt for and read tempIntempIn from from cincin..

e. Compute e. Compute tempOut tempOut by converting by converting tempIntempIn using using choicechoice..

f. Display f. Display tempOuttempOut..

End loop.End loop.

Page 8: The switch Statement Selection Revisited. Problem Using OCD, design and implement a program that allows the user to perform an arbitrary temperature conversion

OrganizationOrganization

We’ll need:We’ll need:– a main functiona main function

– six conversion functionssix conversion functions

– a function to apply the right conversiona function to apply the right conversion

The conversion functions seem likely to The conversion functions seem likely to be reuseable someday, so we’ll create be reuseable someday, so we’ll create a library named Heat in which to store a library named Heat in which to store them.them.

Page 9: The switch Statement Selection Revisited. Problem Using OCD, design and implement a program that allows the user to perform an arbitrary temperature conversion

Behavior: Behavior: FahrenheitToCelsius()FahrenheitToCelsius()

Our function should receive from its Our function should receive from its caller the temperature to be caller the temperature to be converted. It should return the result converted. It should return the result of subtracting 32.0 from that of subtracting 32.0 from that temperature, and then dividing the temperature, and then dividing the difference by 1.8.difference by 1.8.

Page 10: The switch Statement Selection Revisited. Problem Using OCD, design and implement a program that allows the user to perform an arbitrary temperature conversion

Objects: Objects: FahrenheitToCelsius()FahrenheitToCelsius()

Description Type Kind NameDescription Type Kind Name

the converted double variable -- temperature

the original double variable originalTemp temperature

We can use this as a pattern for each of We can use this as a pattern for each of the temperature conversion functions the temperature conversion functions -- they differ only in the particular -- they differ only in the particular formula they use.formula they use.

Page 11: The switch Statement Selection Revisited. Problem Using OCD, design and implement a program that allows the user to perform an arbitrary temperature conversion

Coding: Coding: Conversion PrototypesConversion Prototypes

/* Heat.h * ... */

double FahrenheitToCelsius(double originalTemp);

double CelsiusToFahrenheit(double originalTemp);

double FahrenheitToKelvin(double originalTemp);

double KelvinToFahrenheit(double originalTemp);

double CelsiusToKelvin(double originalTemp);

double KelvinToCelsius(double originalTemp);

Page 12: The switch Statement Selection Revisited. Problem Using OCD, design and implement a program that allows the user to perform an arbitrary temperature conversion

Operations: Operations: FahrenheitToCelsius()FahrenheitToCelsius()

Description Predefined? Library? NameDescription Predefined? Library? Name

receive a double yes built-in subtract doubles yes built-in -divide doubles yes built-in /return a double yes built-in return

Page 13: The switch Statement Selection Revisited. Problem Using OCD, design and implement a program that allows the user to perform an arbitrary temperature conversion

Algorithm: Algorithm: FahrenheitToCelsius()FahrenheitToCelsius()

0. Receive 0. Receive originalTemporiginalTemp..

1. Return (1. Return (originalTemporiginalTemp - 32.0) / 1.8. - 32.0) / 1.8.

We can use this algorithm as a pattern We can use this algorithm as a pattern for each of the temperature conversion for each of the temperature conversion functions -- each differs only in the functions -- each differs only in the particular formula used.particular formula used.

Page 14: The switch Statement Selection Revisited. Problem Using OCD, design and implement a program that allows the user to perform an arbitrary temperature conversion

Coding: Coding: Conversion Definitions Conversion Definitions

/* Heat.cpp * ... */

double FahrenheitToCelsius(double originalTemp){ return (originalTemp - 32.0) / 1.8;}

double CelsiusToFahrenheit(double originalTemp){ return originalTemp * 1.8 + 32.0;}

double FahrenheitToKelvin(double originalTemp){ return (originalTemp - 32.0) / 1.8 + 273.0;}

// ... other conversion function definitions

Page 15: The switch Statement Selection Revisited. Problem Using OCD, design and implement a program that allows the user to perform an arbitrary temperature conversion

Organization (ii)Organization (ii)

Once our library is completed, we can design Once our library is completed, we can design and implement a function to perform the and implement a function to perform the particular conversion specified by the user’s particular conversion specified by the user’s menu choice.menu choice.

We will call this function Convert().We will call this function Convert().

Since it is less likely to be reuseable than our Since it is less likely to be reuseable than our conversion functions, we will store its conversion functions, we will store its prototype and definition in the same file as prototype and definition in the same file as our main function.our main function.

Page 16: The switch Statement Selection Revisited. Problem Using OCD, design and implement a program that allows the user to perform an arbitrary temperature conversion

Behavior: Convert()Behavior: Convert()

Our function should receive from its Our function should receive from its caller the temperature to be converted caller the temperature to be converted and the user’s menu choice. Based on and the user’s menu choice. Based on the menu choice, our function should the menu choice, our function should return the result of applying the return the result of applying the appropriate temperature conversion appropriate temperature conversion function to the received temperature.function to the received temperature.

Page 17: The switch Statement Selection Revisited. Problem Using OCD, design and implement a program that allows the user to perform an arbitrary temperature conversion

Objects: Convert()Objects: Convert()

Description Type Kind NameDescription Type Kind Name

the converted double variable -- temperature

the temp. double variable origTemp being converted

Our prototype is thus:Our prototype is thus:

double Convert(double origTemp, char choice);

a menu choice char variable choice

Page 18: The switch Statement Selection Revisited. Problem Using OCD, design and implement a program that allows the user to perform an arbitrary temperature conversion

Operations: Convert()Operations: Convert()

Description Defined? Library? NameDescription Defined? Library? Name

return a double yes built-in return

convert F-to-C yes Heat ...

convert C-to-F yes Heat ...

convert F-to-K yes Heat ...

convert K-to-F yes Heat ...

convert C-to-K yes Heat ...

convert K-to-C yes Heat ... perform selection yes built-in switch based on a char

Page 19: The switch Statement Selection Revisited. Problem Using OCD, design and implement a program that allows the user to perform an arbitrary temperature conversion

Algorithm: Convert()Algorithm: Convert()

0. Receive origTemp, choice.0. Receive origTemp, choice.1. If choice is ‘a’: return FahrenheitToCelsius(origTemp);1. If choice is ‘a’: return FahrenheitToCelsius(origTemp); Else if choice is ‘b’: return Else if choice is ‘b’: return

CelsiusToFahrenheit(origTemp);CelsiusToFahrenheit(origTemp); Else if choice is ‘c’: return FahrenheitToKelvin(origTemp);Else if choice is ‘c’: return FahrenheitToKelvin(origTemp); Else if choice is ‘d’: return KelvinToFahrenheit(origTemp);Else if choice is ‘d’: return KelvinToFahrenheit(origTemp); Else if choice is ‘e’: return CelsiusToKelvin(origTemp);Else if choice is ‘e’: return CelsiusToKelvin(origTemp); Else if choice is ‘f’: return KelvinToCelsius(origTemp);Else if choice is ‘f’: return KelvinToCelsius(origTemp); Else display error message and return 0.0.Else display error message and return 0.0.

We could implement this algorithm with a multi-We could implement this algorithm with a multi-branch if, but let’s learn something new.branch if, but let’s learn something new.

Page 20: The switch Statement Selection Revisited. Problem Using OCD, design and implement a program that allows the user to perform an arbitrary temperature conversion

Coding: Convert()Coding: Convert()

/* main.cpp * ... */

// rest of main.cpp

double Convert(double origTemp, char choice){ switch (choice) { case ‘a’: return FahrenheitToCelsius(origTemp); case ‘b’: return CelsiusToFahrenheit(origTemp); case ‘c’: return FahrenheitToKelvin(origTemp); case ‘d’: return KelvinToFahrenheit(origTemp); case ‘e’: return CelsiusToKelvin(origTemp); case ‘f’: return KelvinToCelsius(origTemp); default : cerr << “\nUnsupported menu choice: “ << choice << “ in Convert()” << endl; return 0.0; }}

Page 21: The switch Statement Selection Revisited. Problem Using OCD, design and implement a program that allows the user to perform an arbitrary temperature conversion

DiscussionDiscussion

double Convert(double origTemp, char choice){ switch (choice) { case ‘a’: return FahrenheitToCelsius(origTemp); case ‘b’: return CelsiusToFahrenheit(origTemp); case ‘c’: return FahrenheitToKelvin(origTemp); case ‘d’: return KelvinToFahrenheit(origTemp); case ‘e’: return CelsiusToKelvin(origTemp); case ‘f’: return KelvinToCelsius(origTemp); default : cerr << “\nUnsupported menu choice: “ << choice << “ in Convert()” << endl; return 0.0; }}

The switch statement provides an alternative means The switch statement provides an alternative means of doing multi-branch selection.of doing multi-branch selection.

Page 22: The switch Statement Selection Revisited. Problem Using OCD, design and implement a program that allows the user to perform an arbitrary temperature conversion

Organization (iii)Organization (iii)

Once our library and conversion function have Once our library and conversion function have been designed and completed, we can been designed and completed, we can proceed to implement the algorithm for our proceed to implement the algorithm for our main function.main function.

The primary control structure is a forever loop The primary control structure is a forever loop that continues repetition until the user that continues repetition until the user enters menu choice ‘q’...enters menu choice ‘q’...

Page 23: The switch Statement Selection Revisited. Problem Using OCD, design and implement a program that allows the user to perform an arbitrary temperature conversion

Coding: main()Coding: main()#include <iostream> // cin, cout, <<, >>, ...using namespace std;

#include “Heat.h” // FahrenheitToCelsius(), ...

double Convert(double originalTemp, char menuChoice);

int main(){ const MENU = “\nPlease enter:\n“ “ a - to convert Fahrenheit to Celsius\n” “ b - to convert Celsius to Fahrenheit\n” “ c - to convert Fahrenheit to Kelvin\n” “ d - to convert Kelvin to Fahrenheit\n” “ e - to convert Celsius to Kelvin\n” “ f - to convert Kelvin to Celsius\n” “ q - to quit\n” “--> “;

cout << “\nThis program converts temperatures.\n”;

Page 24: The switch Statement Selection Revisited. Problem Using OCD, design and implement a program that allows the user to perform an arbitrary temperature conversion

Coding: main()Coding: main() char choice; double tempIn, tempOut;

for (;;) { cout << MENU; cin >> choice;

if (choice == ‘q’ || choice == ‘Q’) break;

cout << “\nEnter the temperature to be converted: “; cin >> tempIn;

tempOut = Convert(tempIn, choice);

cout << “\nThe converted temperature is “ << tempOut << endl; }}

Page 25: The switch Statement Selection Revisited. Problem Using OCD, design and implement a program that allows the user to perform an arbitrary temperature conversion

TestingTestingThis program converts temperatures.

Please enter: a - to convert Fahrenheit to Celsius b - to convert Celsius to Fahrenheit c - to convert Fahrenheit to Kelvin d - to convert Kelvin to Fahrenheit e - to convert Celsius to Kelvin f - to convert Kelvin to Celsius q - to quit\n --> a

Enter the temperature to be converted: 212

The converted temperature is 100

Page 26: The switch Statement Selection Revisited. Problem Using OCD, design and implement a program that allows the user to perform an arbitrary temperature conversion

TestingTestingPlease enter: a - to convert Fahrenheit to Celsius b - to convert Celsius to Fahrenheit c - to convert Fahrenheit to Kelvin d - to convert Kelvin to Fahrenheit e - to convert Celsius to Kelvin f - to convert Kelvin to Celsius q - to quit\n --> b

Enter the temperature to be converted: 0

The converted temperature is 32

Page 27: The switch Statement Selection Revisited. Problem Using OCD, design and implement a program that allows the user to perform an arbitrary temperature conversion

TestingTestingPlease enter: a - to convert Fahrenheit to Celsius b - to convert Celsius to Fahrenheit c - to convert Fahrenheit to Kelvin d - to convert Kelvin to Fahrenheit e - to convert Celsius to Kelvin f - to convert Kelvin to Celsius q - to quit\n --> c

Enter the temperature to be converted: 32

The converted temperature is 273

...

Page 28: The switch Statement Selection Revisited. Problem Using OCD, design and implement a program that allows the user to perform an arbitrary temperature conversion

SummarySummary

To perform multibranch selection, C++ To perform multibranch selection, C++ provides an alternative statement called the provides an alternative statement called the switch statement.switch statement.

We’ll learn more of the details of the if and We’ll learn more of the details of the if and switch statements next time, including when switch statements next time, including when you should use which statement.you should use which statement.