Lab 4

This week you will be writing a short program to calculate standard trig functions. The program should read from the user one of the 3 standard trig functions, sin, cos, or tan and then read an integer that represents an angle in degrees.

Please make sure you read this entire page and if you have questions please ask.

Details

This program is intended to compute a basic trig function, either sine, cosine or tangent. Remember these functions are in the header file cmath and they expect their input to be a double that represents an angle in radians. So you will have to convert the number you read in from degrees to radians. I suggest you use M_PI and to do that you need to #define _USE_MATH_DEFINES before you #include <cmath>. You can get more information about cmath at http://www.cplusplus.com.

You can use simple extraction to read these two pieces of input. You should enter either, sin, cos, or tan. You should read this into a string variable. To use strings you need to #include <string> and using std::string. You can extract data into a string, e.g. in >> myString; You can also use strings in an if condition. Take a look at this example for how to read a string and use it in a comparison. This is similar to what you will need to do in project 2.

We will have multiple commands each with a trig function and an angle ended with a newline. There will be an unknown number of commands and your program should run until it exhausts the input.

Example:


#include "calculate.h"

void calculate( istream& in, ostream& out )
{
        string oper;
        int degrees;
        double radians;
        double answer;
        in >> oper;
        in >> degrees;
        while ( !in.fail() )
        {
            if ( oper == "somecommand" )
            { 
                //compute the answer and print it
            }
            else if ( oper == "someothercommand" )
            {
                 //compute the answer and print it
            }
            else if ( oper == "yetsomeothercommand" )
            {
                //compute the answer and print it
            }
            in >> oper;
            in >> degrees;
        }   
}

Requirements:

You need to write a function that uses this header: void calculate( istream& in, ostream& out );

If the someone types

sin 45 cos 45 tan 45

or

sin 45<retrun>cos<return>45<return>tan 45

, where <return> is the enter or return key, then you should output a nice table like this:

Operation     Angle     Answer  
sin           45        0.707107
cos           45        0.707107
tan           45        1

You need to repeat the function they wanted and the original angle in degrees. Your program will calculate the answer. The text must be as it is in the samples I have up both the calculate.h and the main.cpp files you can use for this lab. The main file is used simply for testing. It will just call the calculate function using cin and cout. You should not turn in main.cpp and you should write your code in a separate cpp fie.

Additional Code

For this to all work together, you need a main and a way to have main call your function. We'll do this by using one file for main, let's call that main.cpp, a header file that contains the function declaration, let's call that calculate.h, and the last file will contain your implementation of the function, let's call that calculate.cpp.

main.cpp

#include "calculate.h"
#include <iostream>

using std::cout;
using std::cin;

int main()
{
    cout << "Please enter a number of trig commands followed by an angle in degrees.  For example: sin 45 cos 45 tan 45: \n";
    calculate( cin, cout );
    return 0;
}

calculate.h

#define _USE_MATH_DEFINES
#include <cmath>
#include <iostream>
#include <string>
#include <iomanip>

using std::string;
using std::istream;
using std::ostream;
using std::setprecision;
using std::fixed;
using std::showpoint;
using std::setw;

void calculate( istream& in, ostream& out );

Grading

Zip up your calculate.h and your implementation file, e.g. calculate.cpp and turn them in to Web-CAT. Lab is officially due Tuesday September 23 at 6:50PM; however I will accept lab without penalty until Friday at noon.