Help needed Details This program is intended to compute a ba
Help needed!
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 (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 <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:
Requirements:
You need to write a function that uses this header: void calculate( string inputfilename, string outputfilename );
If the someone types the input file contained
sin 45 cos 45 tan 45
or
sin 45
cos 45
tan 45
, then you should output a nice table like this:
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
calculate.h
Solution
#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 == \"sin\" )
{
std::cout << \"Operation\" << \" \" << \"Angle\" << \" \"<< \"Answer\" << std::endl;
out << \"sin\" <<\" \"<< degrees<<\" \" <<std::setprecision(10) << sin(degrees) << std::endl;
}
else if ( oper == \"cos\" )
{
std::cout << \"Operation\" << \" \" << \"Angle\" << \" \"<< \"Answer\" << std::endl;
out << \"cos\" <<\" \"<< degrees<<\" \" <<std::setprecision(10) << cos(degrees) << std::endl;
}
else if ( oper == \"tan\" )
{
std::cout << \"Operation\" << \" \" << \"Angle\" << \" \"<< \"Answer\" << std::endl;
out<< \"tan\" <<\" \"<< degrees<<\" \" <<std::setprecision(10) << tan(degrees) << std::endl;
}
in >> oper;
in >> degrees;
}
in.close();
out.close();
}

