联系方式

  • QQ:99515681
  • 邮箱:99515681@qq.com
  • 工作时间:8:00-23:00
  • 微信:codinghelp

您当前位置:首页 >> javajava

日期:2018-08-29 10:15


Computer Programming 2 Assignment 1

Assignment 1: Number Words

Most first graders know that nine hundred and ninety nine plus one is one thousand, but C++

doesn't! Sure, a computer can easily compute 999 + 1 and get 1000. But reading and writing the

numbers in words is not so easy.

Consider the following code, which implements a very simple 4-function calculator:

float n1, n2;

char op;

while (cin >> n1 >> op >> n2) {

switch (op) {

case '+': cout << n1 + n2 << endl; break;

case '-': cout << n1 - n2 << endl; break;

case '*': cout << n1 * n2 << endl; break;

case '/': cout << n1 / n2 << endl; break;

}

}

The code will read input consisting of simple arithmetic expressions, and for each it will display the

result. For example, it could compute

2.1 + 3.7 // answer: 5.8

1.732906E-9 / 4.5870021E-25 // answer: 3.77786e+15

By simply changing the type of the numbers n1 and n2 from float to some other type, the

calculator can compute the answers for various kinds of number. For example, changing the type to

int will mean that numbers are computed and displayed according to the rules of integer

arithmetic, so you could compute that 7 / 2 is 3.

Task

Your task for this assignment is to define a class, named Wordnum, that will read and write

numbers expressed in word form. To use the class in the calculator, simply include the class header

file and change the type of the numbers n1 and n2 to Wordnum. Then the program can read input

consisting of expressions such as

five + six

ninety_nine - ninety_nine

one_thousand_two_hundred_thirty_four * five_hundred_forty_three

one million / negative_one hundred

and for each line it will write the result in words:

eleven

zero

six_hundred_seventy_thousand_sixty_two

negative_ten_thousand

Download the file wordnum_0.zip, which contains the calculator code and a skeleton version of

the Wordnum class. The class defines (but doesn't fully implement) appropriate constructors to

initialise instances of Wordnum, extract and insert operators for input and output of Wordnum

values, and of course arithmetic operators to compute the results.

Note that for a numeric type such as this, the arithmetic operators and IO operators are best be

defined as friend functions. The extract and insert operators are the most difficult to implent

(indeed, they are the sole reason for defining this class), but if the number's value is represented

using an int, the arithmetic operators are trival to implement.

Flinders University / College of Science and Engineering 1

Computer Programming 2 Assignment 1

Background

For this assignment, numbers should be written as English words separated by underscore

characters, with the default wording following the US conventions (because they are simpler to

program). Thus 1001 is written "one_ thousand_ one" and 123000009 is "one_ hundred_

twenty_ three_ million_ nine". If British format is in effect (from level 6), tens and units

are separated by a hyphen and preceded by the word "and". For example in British format 123

would be written one_ hundred_ and_ twenty-three.

Because of the way numbers are written as words in English, you'll probably find it easiest if you

break a number up into groups of 3 digits, called triads, and deal with each triad separately. For

example, 12345678 is best though of as 12_ million_ 345_ thousand_ 678. Because the

code for converting a triad to and from words is the same for each group, you can convert a multitriad

number like this:

work out how many millions (m), thousands (t) and units (u)

write triad for m followed by "million"

write triad for t followed by "thousand"

write triad for u

and to input a number you would reverse the process:

try to read a triad followed by "million" and set m to the

result

try to read a triad followed by "thousand" and set t to the

result

try to read a triad and set u to the result

the number is m * 1000000 + t * 1000 + u

To input or output a triad, the simplest strategy is to use arrays of strings for the various words, with

the array indexes carefully arranged to correspond to the numeric values of the words. Then you

can convert a number to words by indexing the arrays with the appropriate value, and you can

convert words to a number by looping though the arrays looking for a match. Of course, you'll need

to deal with the case where the hundreds, tens, or units digits in the triad are zero.

Rather than trying to read input and write output directly, you'll probably find it easier to convert

numbers to and from an array of strings, with each element in the array representing one word.

Then the extract and insert operators can simply read and write the string arrays. For example, the

insert operator would output each word separated by an underscore, and the input operator would

tokenise the input using the underscore as a delimiter, storing the tokens into successive array

elements.

Function Points

1. In the skeleton program, the extract and insert operators are implemented using helper

functions write_number and read_number, which currently contain placeholder code.

Implement a simple version of the helpers so that the program can handle input and output of

numbers from 1 to 9.

2. Extend the class so that it is insensitive to the case of the input. Output should always be in

lower case.

3. Extend the class so that the program can handle input and output of numbers from 1 to 100.

Don't forget that forty is spelt without a 'u'.

4. Extend the class so that the program can handle input and output of negative numbers, such as

negative_twenty_three, and zero.

Flinders University / College of Science and Engineering 2

Computer Programming 2 Assignment 1

5. Extend the class so that the program can handle output of all numbers with magnitude less

than 1000. For this level, input numbers will always be less than 100.

6. Extend the class so that the program can also handle input of all numbers with magnitude less

than 1000.

7. Extend the program so that if the command-line option -b is specified, input and output are in

British format.

8. Extend the class so that the program can handle output of all numbers with magnitude less

than 1,000,000,000. For this level, input numbers will always be less than 1000.

9. Extend the class so that the program can also handle input of all numbers with magnitude less

than 1,000,000,000.

10. Extend the class to deal sensibly with invalid input. Specifically, it should process the longest

valid prefix of the input and ignore the remainder. As a special case, if none of the input is

valid, the number should be interpreted as zero. For example, the input

six_squillion + more

should produce the output

six

Assessment

Pairing

You are encouraged to work on this assignment in pairs. If you work as a pair, you must both hand

in a solution, which should be identical. All work handed in will be checked using a similarity

checker; if more that 2 solutions appear similar, marks will be withheld pending investigation.

Automatic assessment

The function points for this task will be assessed automatically using the demo and try programs

under the task name wordnum. You can run the demo version using demo wordnum and you can test

your work using try wordnum <<filename>>. As the source code consists of more than one file,

you'll need to submit your work as a zip file.

Scoring

The assignment will be submitted and assessed twice: once following the "core" hand-in date, and

again following the "extension" hand-in date. The core submission will be given a score out of 10,

based on the first 5 function points. The extension submission will also be given a score out of 10,

based on the last 5 function points.

Flinders University / College of Science and Engineering 3


版权所有:留学生程序网 2020 All Rights Reserved 联系方式:QQ:99515681 电子信箱:99515681@qq.com
免责声明:本站部分内容从网络整理而来,只供参考!如有版权问题可联系本站删除。