required functions for program 3 int readuntilvalidbaseread( ); int readnumbersreturningvalue( int...

20
Required Functions for Program 3 int readUntilValidBaseRead( ); int readNumbersReturningValue( int base ); int decimalValueOf( char chDigit ); bool isValid( char chDigit, int base ); MUST NOT Change the Prototypes! You can have other functions. 1

Upload: eric-leonard

Post on 03-Jan-2016

251 views

Category:

Documents


2 download

TRANSCRIPT

Required Functions for Program 3

int readUntilValidBaseRead( );

int readNumbersReturningValue( int base );

int decimalValueOf( char chDigit );

bool isValid( char chDigit, int base );

MUST NOT Change the Prototypes!

You can have other functions.

1

Required Functions for Program 3//---------------------------------------------------------------------

// This function reads bases until a valid base is read or eof occurs.

// If an invalid base is read, an error message is displayed and the

// rest of the line is ignored and another attempt to read a base value

// will be attempted.

// -1 is returned if eof occurs otherwise a valid base value is

// returned.

//---------------------------------------------------------------------

int readUntilValidBaseRead( )

Is base a char, int, float, or string?

int!

Input for Test Run 2:

1 hello

10 this is bad too

-1 as well as this

2

Required Functions for Program 3

//---------------------------------------------------------------------

// This function reads in a sequence of characters that represent

// a number in the given base. A valid sequence is given in a

// "backwards" format such that the rightmost digit is given first,

// the second to the rightmost digit is next, etc.

// This function returns the value of this sequence of characters if

// it is a valid sequence. If it is not valid it returns -1.

// params: ( )

//---------------------------------------------------------------------

int readNumbersReturningValue( int base )

Do we know the base now?

Yes! And it’s a valid base!

Sample Input:

2 1101

3

Pseudocode for main()Set totalSum to zero

Read until a valid base is read or end of file

while not at end of file

write "For the given base ", base

Assign to numberValue the translated number from the input

if numberValue is not valid

Write " the number is NOT valid!"

else

Accumulate numberValue into totalSum

Write numberValue appropriately labelled

Read until another valid base is read or end of file

Write totalSum appropriately labelled

4

Calling Functions from main()Set totalSum to zero

Call function readUntilValidBaseRead

while not at end of file

write "For the given base ", base

Call function readNumbersReturningValue to get numberValue

if numberValue is not valid

Write " the number is NOT valid!"

else

Accumulate numberValue into totalSum

Write numberValue appropriately labelled

Call readUntilValidBaseRead to get next base

Write totalSum appropriately labelled

5

6

Functions//---------------------------------------------------------------------// This function reads in a sequence of characters that represent// a number in the given base. A valid sequence is given in a // "backwards" format such that the rightmost digit is given first,// the second to the rightmost digit is next, etc. // This function returns the value of this sequence of characters if// it is a valid sequence. If it is not valid it returns -1. // params: ( )//---------------------------------------------------------------------int readNumbersReturningValue( int base )

How to read ‘\n’?cin.get(ch)

How to read the first digit? cin >> ch;

Input for Run 1: 2 1101 3 1212 5 66 2 1111

7

Functions

int readNumbersReturningValue( int base ) { char ch;

cin >> ch;

while (ch != ‘\n’) { // process digit

cin.get(ch) }}

How to check range?

8

int readNumbersReturningValue( int base ) { char ch; bool valid; int total;

cin >> ch; // call isValid() while (ch != ‘\n’ && valid) { // process digit // Call function decimalValueOf

cin.get(ch); // call isValid() }

if (! valid) { cin.ignore( MAX_LINE, '\n' ); // MAX_LINE: 256 return -1; } else return total;}

How to check range?Call function! bool isValid( char chDigit, int base )

Required Functions for Program 3//---------------------------------------------------------------------

// This function returns true if chDigit is a valid digit in the given

// base, it returns false otherwise.

// params: (in, in)

//---------------------------------------------------------------------

bool isValid( char chDigit, int base )

Base 2:

Valid digits: 0, 1

Base 5:

Valid digits: 0, 1, 2, 3, 4

Any base:

Valid digits: 0, 1, . . . (base – 1)

9

10

ASCII Code Table

All digits 0 through 9 are together

0 1 2 3 4 5 6 7 8 9

4 0 1

5 2 3 4 5 6 7 8 9

6 A B C D E

7 F G H I J K L M N O

8 P Q R S T U V W X Y

9 Z a b c

10 d e f

11

ASCII Code

Char ASCII Code

‘0’: 48

‘5’: ?

Any base:

Valid digits: 0, 1, . . . (base – 1)

>= ‘0’ and

< (‘0’ + base)

0, 1 and -1 are not magic numbers!

2 and 9 are!

Required Functions for Program 3

//---------------------------------------------------------------------

// This function returns the numeric value of the character digit that

// is stored in chDigit.

// params: (in)

//---------------------------------------------------------------------

int decimalValueOf( char chDigit )

Assume chDigit is a digit

Base 5:

chDigit ‘3’ to 3 (int)

chDigit – ‘0’

12

13

int readNumbersReturningValue( int base ) { char ch; bool valid; int total;

cin >> ch; // call isValid() while (ch != ‘\n’ && valid) { // process digit // Call function decimalValueOf

cin.get(ch); // call isValid() }

if (! valid) { cin.ignore( MAX_LINE, '\n' ); // MAX_LINE: 256 return -1; } else return total;}

14

Binary Numbers

27 + 23 + 22 + 21 + 20 128 + 8 + 4 + 2 + 1

Decimal Number: 143

1 0 0 0 1 1 1 1

27 26 25 24 23 22 21 20

Base 2Two digits: 0, 1

15

Binary Numbers (may not be a byte)

23 22 21 20

1 1 0 1

= 23 + 22 + 0 + 20

= 8 + 4 + 0 + 1

= 13

Base 2Two digits: 0, 1

16

Decimal Numbers

Base 10

How many digits?Ten digits: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9

143 = 1 * 102 + 4 * 101 + 3 * 100

17

Other Bases

Base 5

How many digits?Five digits: 0, 1, 2, 3, 4

143 in base 5 1 * 52 + 4 * 51 + 3 * 50

= 25 + 20 + 3 = 48

Base 10143 = 1 * 102 + 4 * 101 + 3 * 100

18

Backwards

Why?Input one char at a time!

1 1 1 1 0 0 0 120 + 21 + 22 + 23 + 27

1 + 2 + 4 + 8 + 128= 143

27 26 25 24 23 22 21 20

20 21 22 23 24 25 26 27

19

Backwards

What’s the value of the following number? 143

Base 5 1 * 50 + 4 * 51 + 3 * 52

= 1 + 20 + 75 = 96

Base 10 1 * 100 + 4 * 101 + 3 * 102

= 1 + 40 + 300 = 341

20

Backwards

What’s the value of the following number? 4 1 3 0 2

Base 2 Invalid!

Base 5 4 * 50 + 1 * 51 + 3 * 52 + 0 * 53 + 2 * 54

= 4 * 1 + 1 * 5 + 3 * 25 + 0 * 125 + 2 * 625 = 4 + 5 + 75 + 0 + 1250 = 1334

Base 10 20,314