Question

C++ ONLY Write a function calculator that takes two floating numbers and one operator and prints...

C++ ONLY

Write a function calculator that takes two floating numbers and one operator and prints out the answer based on arithmetic. Assume that there are no overflow, underflow and division by zero cases.

  • Your function should be named calculator
  • Your function takes three input parameter: two double numbers and one char operator
  • Your function does not return anything
  • Your function prints answer in the format specified below
  • Your function should set precision point to 2

Note: You must use a switch case for this problem. if/else statements are not allowed.

The format of the output should be <number 1> <operator> <number 2> = <answer> . If an invalid operator is passed to the function, it should print "Invalid operator!"

If the function is called with (3, 7, '+') as the input argument:

3 + 7 = 11

If the function is called with (3, 7, '-') as the input argument:

3 - 7 = -4

If the function is called with (3, 7, '*') as the input argument:

3 * 7 = 21

If the function is called with (3, 7, '/') as the input argument:

3 / 7 = 0.43

If the function is called with (3, 7, '!') as the input argument:

Invalid operator!

Note: You do not need to write the main(), the include, or namespace commands, you only need to write the function definition.

For example:

Test Result
calculator(3, -7, '+');
3 + -7 = -4
0 0
Add a comment Improve this question Transcribed image text
Answer #1
void calculator(double d1, double d2, char ch){
   double result;
   switch(ch){
      case '+':
         result=d1+d2;
         break;
      case '-':
         result=d1-d2;
         break;
      case '*':
         result=d1*d2;
         break;
      case '/':
         result=d1/d2;
         break;
      default:
         cout<<"Invalid operator"<<ch<<endl;
         return;
   }
   cout<<d1<<" "<<ch<<" "<<d2<<" = "<<result<<endl;
}

Add a comment
Know the answer?
Add Answer to:
C++ ONLY Write a function calculator that takes two floating numbers and one operator and prints...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a program that takes an operation (+, -, /, or *) and two floating-point numbers,...

    Write a program that takes an operation (+, -, /, or *) and two floating-point numbers, and outputs the result of applying that operation to the two numbers. For example, if the input is: + 100 3.14 the output should be 100 + 3.14 = 103.14 Likewise, if the input is * 4 5 the output should be 4 * 5 = 20 You may assume the input is well-formed; that is, the first string is one of the four...

  • C++ 9) Write a recursive function printAll that takes an int num argument and prints all...

    C++ 9) Write a recursive function printAll that takes an int num argument and prints all the numbers in the range [num, 0]. The function returns nothing. e.g. printAll(5) prints 5, 4, 3, 2, 1, 0. Call your function in the main

  • Please Use Python and provide explanations!!!! 1. Write a function called multiply_and_print that takes two numbers,...

    Please Use Python and provide explanations!!!! 1. Write a function called multiply_and_print that takes two numbers, multiplies them together, and prints out the sentence '____ times ____ is ____'. Ask the user for two numbers, and call multiply_and_print with their two numbers as arguments. 2.Write a function called roll_dice that rolls two six-sided dice and prints out the result of each die. Call it three times. (Remember, you can import the random module with the statement import random, and then...

  • Question is two parts In R(Programming language). Define and write a function which takes one number...

    Question is two parts In R(Programming language). Define and write a function which takes one number as input and detects whether the number is even. Return TRUE if it is even, and FALSE otherwise. You need to first detect if the input is an integer; if not, return NA. Note 1: TRUE and FALSE are logical values, not strings! Note 2: No need to get user input from the console. Test your function using your_function_name(1), your_function_name(2), and your_function_name(1.5). Hint: What...

  • Write a C function called display_dollar which takes a floating point number as input, and displays...

    Write a C function called display_dollar which takes a floating point number as input, and displays this is a dollar value to the screen. This means that it should have a ‘$’ in front of it, and be displayed to exactly two decimal places in non-scientific form. Your function should not put spaces or newlines before or after the output?

  • C++ 2. (a) Write a function, printdixisors, that takes a single integer parameter and prints all...

    C++ 2. (a) Write a function, printdixisors, that takes a single integer parameter and prints all the numbers less that the parameter that are divisors of the parameter (i.e. divides it without a remainder) including 1. So printdivisors(6) will print 1,2,3. Note you may use a wrapper function or default parameters. (b) Write a function, sumdixisors, that takes a single integer parameter and returns the sum of all the divisors of the parameter (including 1). So sumdivisors(6) will return 6...

  • You must write a C program that prompts the user for two numbers (no command line...

    You must write a C program that prompts the user for two numbers (no command line input) and multiplies them together using “a la russe” multiplication. The program must display your banner logo as part of a prompt to the user. The valid range of values is 0 to 6000. You may assume that the user will always enter numerical decimal format values. Your program should check this numerical range (including checking for negative numbers) and reprompt the user for...

  • Section 3: Create a Calculator For this problem, please create a "calculator" function that takes two...

    Section 3: Create a Calculator For this problem, please create a "calculator" function that takes two integers and an operator and does the math operation corresponding to the numbers and the operator. For instance, calculate(5,"+", 6) should return 11. Please support the following operators: +- X/^(addition, subtraction, multiplication, division, exponentiation) 1 2 3 function calculate (numi, operator, num2) { // Do the calculation } RUN CODE Parameters Expected Result Actual Result 1,"+", 4 1,"x", 0 20,"7", 5 3,"", 3

  • Using matlab Write a function that receives a vector as an input argument and prints the...

    Using matlab Write a function that receives a vector as an input argument and prints the individual elements from the vector in a sentence format. One example of calling this function on the command window looks like >>printvec([1.12 23 9.245]) Element 1 is 1.12 Element 2 is 23.00 Element 3 is 9.25 1. (Note: your function should work for any vector.)

  • Please use C !!! Write a C function matrixTranspose that takes a two-dimensional array as its...

    Please use C !!! Write a C function matrixTranspose that takes a two-dimensional array as its input argument then transposes its elements and prints the results. Develop the matrixTranspose method with two different methods: 1. Assume the matrix is a square matrix. This will make finding the transpose a simple swapping of the arrays’ elements and it can be done in place. 2. Generalize your function to work with any NxM matrix where N≠M. You will need to properly handle...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT