Lecture 3: I/O Investigation of Rational Numbers



I/O Investigation and Pseudo-code of Rational Numbers

Notes:

1. If we consider the user interface, we might want to think about how users enter information via keyboard. If the program acts friendly, it must carry more work for itself. Convenience for users might be not simple for programmers.

2. We are aware that users can enter a rational number as an example 123/45. It is a mixed between numbers and characters. Hence it is treated as a string. We must learn how to extract the sub-string before slash and another after slash. Looking for all possible cases with examples will help us do classify this kind of input.

I/O Investigation:

Input: from keyboard as string.

Output: Extract numerator and denominator as integers.

Here are the typical cases:

Case # Input Expected Output Simplest forms

1 "4/14" 4 14 2 7

"-2/-7" -2 -7 2 7

2 "125" 125 1 125 1

3 "-125" -125 1 -125 1

4 "+125" 125 1 125 1

5 "+125/-12" 125 -12 -125 12

6 "+125/0" "ERROR"

Note 3. In this section, we choose to work from input to Expected Output. The next step from Expected Output to Simplest Forms" can be considered later. This technique of divide and conquer would help us to focus on a narrow solid subject matter.

Strategy to handle these cases, pseudo-code:

Phase 1: The simplest normal case (Case 2) : StrToNum will convert a string-number to a number.

Phase 2: Expension of Phase 1 to cover more cases: Case 3, 4. Theses cases are Case 2 with a sign either + or – in front. Here is the pseudo-code with input as inputString and output as outputNum

Pseudocode: StrToInt (input: inputString; output: outputNum)

Get the firstChar from inputString

Case firstChar is

‘+’: sign is set positive

reduce inputString by firstChar

‘-’: sign is set negative

reduce inputString by firstChar

others: sign is set positive;

Do not reduce inputString

OutputNum ................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download