Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I'm stuck on the process portion of the code. I have the header file (lab5.h) and main file (main.cpp) complete. Any help would be greatly

I'm stuck on the process portion of the code. I have the header file (lab5.h) and main file (main.cpp) complete. Any help would be greatly appreciated.

For this lab we are adding to our selection structure a looping structure. This is going to be a pattern we use a few times in lab and in our projects so pay attention to how we set this up. Theres some sample code below that sets up the structure.

Details: For this lab, we are going to be working with functions from the math header library. Specifically, we are going to write a program that will accept a series of math functions followed by 1 or 2 numbers. Then we will output the function, the number arguments and the answer. If we are given unknown command we will also log that as well.

Input: The input will be a series of commands followed by 1 or two numbers. Only the raise-to-the-power command will have two numbers. All other commands will have 1 number. The commands correspond to the functions from the cmath header file. Essentially, we are you to read the command, decide what command was issued and call the corresponding function. Then you output the command, the arguments that were given and the answer. See the sample output below.

exponential 3.14 natural-log 10 log 10 raised-to-the-power 2.5 3 square-root 36.0 ceiling 43.2 floor 43.2 othercommand 10.0

There might be a single command in the file or there might be several. The input controlled failure loop is what we will be using to read until there are no more commands in the file. It is outlined below:

lab5.cpp

#include "lab5.h" void mathCalculator( string input, string output ) { ifstream in(input); ofstream out(output);

string command; double argument1, argument2, answer;

in >> command; //prime while( !in.fail() ) //test { //process in >> command;//re-prime } }

While there are other ways to accomplish what we want, it is my experience that this is the best way to get this done. You dont have to use this, but if your code doesnt work Im going to ask why you didnt use what was given.

You will be adding the selection statement in the process section to determine the command and call the correct function. Output:

So if you are given the above input file, this is the corresponding output file.

Command Argument1 [Argument2] Answer exponential 3.14 23.1039 natural-log 10 2.30259 log 10 1 raised-to-the-power 2.5 3 15.625 square-root 36 6 ceiling 43.2 44 floor 43.2 43 Unknown command: othercommand I have used the default number of decimal places on my doubles, you do not need to do anything to your output, e.g. setprecision, fixed or showpoint, to get this effect.

Requirements: 1. You must use the header file lab5.h 2. In your lab5.h header file you must declare the function: void mathCalculator( string input, string output ); 3. Dont store the data, just process the data as it is read.

lab5.h

#include //this allows us to use files for input and output #include //this gives us access to the math functions #include //this gives us access to string for our input and output variables

using std::string; //these let the compiler know what to using std::ifstream; //do when it encounters these words using std::ofstream; using std::endl;

// This is a function declaration.

void mathCalculator( string input, string output )

main.cpp

#include "lab5.h" //this gives the main file access

//to the function we declared in the header

//so it can call it.

int main()

{

mathCalculator( "input.txt", "output.txt" );

//here's where we name the input and

//output files.

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Concepts

Authors: David M Kroenke, David J Auer

6th Edition

0132742926, 978-0132742924

Students also viewed these Databases questions