SOLUTION_5 - CS 31: Introduction to Computer Science I: / /...

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

View Full Document Right Arrow Icon
CS 31: Introduction to Computer Science I: Winter 2010: Project 5 Solution Page 1 // UCLA Winter 2010 // CS 31 Project 5 // John A. Rohr // February 22, 2010 // // // C-String Expression Evaluator // // // This program evaluates arithmetic expressions input as C-strings and // reports the values of each of the expressions stored as double type data. // // All input is read from the file File5.In. All output is written to the // console and the file File5.Out. If either the input file or the output // file cannot be opened, the program terminates. // // Input consists of lines of real numbers and the arithmetic operators // +, -, *, and /. // // 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. // // Each expression must have alternating operands and operators. // // Operators must be preceded and followed by white space. // White space after a number can be the same as that before a following operator. // // Normal mathematical precedence is used for the operators. // // Parentheses are not supported. // // Input lines are read a C-strings. // // After displaying the result of an expression evaluation, 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. // // No functions are used in the evaluation of numbers, operators, and expressions. // //
Background image of page 1

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

View Full DocumentRight Arrow Icon
CS 31: Introduction to Computer Science I: Winter 2010: Project 5 Solution Page 2 // Example Execution // // C STRING ARITHMETIC EXPRESSION EVALUATOR // // Input:24 // Value:24 // // Input:-35.98 * 87.36 // Value:-3143.2127999999998 // // Input:436.893 + 356 / -0.06 // Value:-5496.4403333333339 // // Input:54366 - 3463 + 453 * .01253 // Value:50908.676090000001 // // Input:86.634 * 684.6 / 438.74 + 826.684 - 536.3266 // Value:425.53913952682683 // // Input:- // ERROR: Invalid number! // // ERROR: Invalid number! // // Input:974376.9863 @ // ERROR: Invalid operator! // // Input:4326 + 864 * // ERROR: Invalid expression! // // Input:346 + 54 / 0 + 37 // ERROR: Divide by zero! // // Done.
Background image of page 2
CS 31: Introduction to Computer Science I: Winter 2010: Project 5 Solution Page 3 //#define DEBUG #include <iostream> #include <fstream> using namespace std; // ************************************************************************** // * * // * GLOBAL CONSTANTS * // * * // ************************************************************************** const char INFILE_NAME[] = "File5.In"; const char OUTFILE_NAME[] = "File5.Out"; const char TITLE[] = "C STRING ARITHMETIC EXPRESSION EVALUATOR"; const char LEADIN[] = "Input:"; const char LEADOUT[] = "Value:"; const char ERROR1[]
Background image of page 3

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

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 03/08/2010 for the course COM SCI 31 taught by Professor Kinsey during the Spring '98 term at UCLA.

Page1 / 22

SOLUTION_5 - CS 31: Introduction to Computer Science I: / /...

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

View Full Document Right Arrow Icon
Ask a homework question - tutors are online