For this lab, we are going to be working with functions from the math header lib
ID: 3751234 • Letter: F
Question
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.
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:
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 don’t have to use this, but if your code doesn’t work I’m going to ask why you didn’t 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.
Now just a few words about the output. You do not need to match my spacing. Where I have space you must have at least one space. Where I don’t have space, e.g. [Argument 2], you may not have space. I like my tables to look pretty so I took time to tab it out. You do not need to do this, unless you are like me and want it to look pretty. (The html made this look way worse then it does on Powershell.)
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.
Explanation / Answer
Here is your C++ well commented , self explainable, code -
#include "bits/stdc++.h" //including this , you dont need to include any other header file
using namespace std;
void mathCalculator(){
double res; // to store final result
string command;
while(cin >> command){ // reading until end of file
if(command == "othercommand"){
double val;
cin >> val;
cout << "Unknown command:" << " " << command << " ";
continue;
}
if( command != "raised-to-the-power" ){ // checking for just one number in corresponding command
double val;
cin >> val;
if(command == "exponential"){
res = exp(val);
}else if(command == "natural-log"){
res = log (val);
}else if(command == "log"){
res = log10 (val);
}else if(command == "square-root"){
res = sqrt(val);
}else if(command == "ceiling"){
res = ceil(val);
}else if(command == "floor"){
res = floor(val);
}
cout << command << " " << val << " "<< res << " ";
}else if(command == "raised-to-the-power"){
double val1, val2;
cin >> val1 >> val2;
res = pow(val1, val2);
cout << command << " " << val1 << " " << val2 << " " << res << " ";
}
}
}
int main(int argc, char const *argv[])
{
mathCalculator();
return 0;
}
any doubts, please comment
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.