{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

SOLUTION_HW3

# SOLUTION_HW3 - CS 31 Introduction to Computer Science I...

This preview shows pages 1–4. Sign up to view the full content.

CS 31: Introduction to Computer Science I: Winter 2010: Homework 3 Solution Page 1 // UCLA Winter 2010 // CS 31 Homework 3: C Strings and Real Numbers // John A. Rohr // February 22, 2010 // // // This program evaluates real numbers input as C-strings and reports // the values of the numbers converted to double type data. // // All input is read from the file Hw3.In. All output is written to the // console and the file Hw3.Out. If either the input or the output file // cannot be opened, the program terminates. // // Input consists of lines of real numbers. Each number has // the following format: // // Optional leading white space // Optional + or - sign // Decimal digits for the integer part // Optional decimal point // Optional decimal digits for the fraction part // Terminal white space // // Numbers must have at least one decimal digit. // // Input lines are read a C-strings. // // Each value is first evaluated using the atof() function. // // Then each value is evaluated character-by-character. Initially, // the integer part of the value is converted to a number. Then the // fraction part, if any is present, is converted using two different // methods. One method multiplies each fraction digit by a negative // power of ten as it is encountered and immediately adds it to the // numeric value. The other method accumulates fraction digits as an // integer number, then divides by an appropriate power of ten before // adding the accumulated value to the number value. // // If the number is an integral value, two outputs are displayed. // // If the number has a fraction part, three outputs are displayed, // one for the atof() conversion and one for each of the two // fraction conversion methods. // // After the output display, the program displays a blank line and // repeats until an empty line is input. // // After an empty line is input, a termination message is displayed // and the program terminates. // // The getline() function in the iostream class is used to read input lines. // The atof() function in the cstdlib class is used for the first conversion. // All strings are C Strings.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
CS 31: Introduction to Computer Science I: Winter 2010: Homework 3 Solution Page 2 // Example Execution // // C STRING REAL NUMBER EVALUATOR // // Input:12 // Value:atof() function:12 // Value:Fraction digits:12 // // Input: -3695 // Value:atof() function:-3695 // Value:Fraction digits:-3695 // // Input: +429 // Value:atof() function:429 // Value:Fraction digits:429 // // Input:-0.365 // Value:atof() function:-0.36499999999999999 // Value:Fraction digits:-0.36499999999999999 // Value:Integer fraction:-0.36499999999999999 // // Input:+12345.987654321 // Value:atof() function:12345.987654320999 // Value:Fraction digits:12345.987654320999 // Value:Integer fraction:12345.987654320999 // // Input:0.12345678987654321 // Value:atof() function:0.12345678987654321 // Value:Fraction digits:0.12345678987654322 // Value:Integer fraction:0.1234567898765432 // // Input: + // ERROR: No decimal digits // // Input: 0.467z // ERROR: Invalid terminator at position 9 // // Input:-3658.97657* // ERROR: Invalid terminator at position 12 // // Input:The end // ERROR: Invalid terminator at position 1 // // Done.
CS 31: Introduction to Computer Science I: Winter 2010: Homework 3 Solution Page 3 #include <iostream> #include <fstream> #include <cstdlib> using namespace std; // ************************************************************************** // * * // * GLOBAL CONSTANTS * // * * // ************************************************************************** const char INFILE_NAME[] = "Hw3.In";

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 14

SOLUTION_HW3 - CS 31 Introduction to Computer Science I...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online