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 (Links to an external site.).

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 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( string inputfilename, string outputfilename )
{
ifstream in(inputfilename);
ofstream out(outputfilename);
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;
}

in.close();
out.close();
}

Requirements

You need to write a function that uses this header: void calculate( string inputfilename, string outputfilename );

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.7071067812
cos 45 0.7071067812
tan 45 1.0000000000

The output must contain 10 decimal places and 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 an input file and an output file. You can put the various trig functions in there for testing.

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()
{
calculate( "input.txt", "output.txt");
return 0;
}

calculate.h

#include < iostream>
#include < string>
#include < iomanip>
#include < fstream>

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

void calculate( string inputfilename, string outputfilename );
Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.