Question

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 operators, and the next two items are valid floating-point numbers.

Hint: Read a char and two doubles, then use a switch statement to select the proper operation. Note that there is no need for a loop in this exercise; you only have one line of input.

Please help me as soon as possible!!!!! Thank you!!!

0 0
Add a comment Improve this question Transcribed image text
Answer #1


#include <iostream>
using namespace std;
int main()
{
    char ch;
    double num1, num2,result=0;

   
    //storing operator and double values in variables
    cin >>ch >> num1 >> num2;
   
    switch(ch)
    {
        //based on operator corresponding operation will be performed
        case '+':result = num1+num2;
                 break;
        case '-':result = num1-num2;
                 break;
        case '*':result = num1*num2;
                 break;
        case '/':result = num1/num2;
                 break;
        default :printf("Invalid operator");
                 break;
    }
    // displayed final result
    cout << num1<<" "<< ch <<" "<<num2 <<" = "<<result;
}

CppDroid project_jun17a.cpp Editor Navigator 1 2#include <iostream> 3 using namespace std; 4 int main( 5 char ch; double num1CppDroid project_jun17a.cpp Editor Navigator double num1, num2,result=0; 8 9 //storing operator and double values in |variabl10:12 AM CppDroid terminal Stopped + 100 3.14 100 + 3.14 = 103.14 Get premium! Remove ad banner O V10:12 AM CppDroid terminal < Stopped * 4 5 4 * 5 = 20 Terminal session stopped Get premium! Remove ad banner O V

Add a comment
Know the answer?
Add Answer to:
Write a program that takes an operation (+, -, /, or *) and two floating-point numbers,...
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
  • 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...

  • USING C# 1. Write a program that takes outputs a string, an integer and a floating-point number s...

    USING C# 1. Write a program that takes outputs a string, an integer and a floating-point number separated by commas. Sample output: Bob Marley, 20, 5.2 2. 2. Write a program that asks the user for a string of letters of any size (no spaces), and finally a special character (values 33 to 47 in the Ascii table, or ‘!’ to ‘/’). Generate a random number of any size, integer or floating point, and combine those three pieces of information...

  • In MIPS: Write an assembly program that reads two lists of floating point numbers A and...

    In MIPS: Write an assembly program that reads two lists of floating point numbers A and B from users, and displays the measures given above on the simulator’s console. The program’s specifications are given below: • Each input vector should be of size 10, i.e., N=10 • The program should use PROCEDURES to compute dot product. Sample input vectors: A = [0.11 0.34 1.23 5.34 0.76 0.65 0.34 0.12 0.87 0.56] B = [7.89 6.87 9.89 7.12 6.23 8.76 8.21...

  • write a program that will read a file containing floating point numbers, store the numbers into...

    write a program that will read a file containing floating point numbers, store the numbers into an array, count how many numbers were in the file, then output the numbers in reverse order. Your program should only use one array and that array should not be changed after the numbers have been read. You should assume that the file does not contain more than 100 floating point numbers. Numbers found in file 100.7362 105.2896 66.3493 105.6688 91.6180 64.8770 73.9249 87.3441...

  • Write a program which: - prompts the user for 2 numerical inputs - stores their two...

    Write a program which: - prompts the user for 2 numerical inputs - stores their two inputs in variables as floats - Use a while in loop to ask a user for a valid operation: (if the user's input operation isn't one of those listed above, instead print a message telling them so, and continue asking for the valid operation) - prompts the user for an arithmetic operation ( + - * / %) - stores the user's input as...

  • C Programming: Write a program that inputs two strings that represent floating-point values, convert the strings...

    C Programming: Write a program that inputs two strings that represent floating-point values, convert the strings to double values. Calculate the sum,difference,and product of these values and print them. int main(){    // character string value array for user    char stringValue[10];       double sum=0.0;// initialize sum to store the results from 2 double values       double difference=0.0; // initialize difference to store the results from 2 double values       double product = 0.0; // intitialzie product...

  • c++ no pointers just follow instructions Write a program (array.cpp) that read positive floating point numbers...

    c++ no pointers just follow instructions Write a program (array.cpp) that read positive floating point numbers into an array of capacity 100. The program will then ask for one of three letters(A, M or S). if the user inputs something other than one of these letters, then the program should ask for that input until an A, M, or S is entered. A do-while loop should be used for this. If the user inputs an A, then the program should...

  • Write a program **(IN C)** that displays all the phone numbers in a file that match the area code...

    Write a program **(IN C)** that displays all the phone numbers in a file that match the area code that the user is searching for. The program prompts the user to enter the phone number and the name of a file. The program writes the matching phone numbers to the output file. For example, Enter the file name: phone_numbers.txt Enter the area code: 813 Output: encoded words are written to file: 813_phone_numbers.txt The program reads the content of the file...

  • Write a program that writes a series of random numbers to a file. Each random number...

    Write a program that writes a series of random numbers to a file. Each random number should be in the range of 1 through 500 inclusive. 1.Use a file called randoms.txt as a input file and output file a. If you go to open the file and its not there then ask the user to create the file     and then quit 2. Ask the user to specify how many random numbers the file will hold. a.Make sure that the...

  • Write a C program that takes two sets of characters entered by the user and merge...

    Write a C program that takes two sets of characters entered by the user and merge them character by character. Enter the first set of characters: dfn h ate Enter the second set of characters: eedtecsl Output: defend the castle Your program should include the following function: void merge(char *s3, char *s1, char *s2); The function expects s3 to point to a string containing a string that combines s1 and s2 letter by letter. The first set might be longer...

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