Question

In: Computer Science

I need Calculator project : A complex number calculator program that allows the user to do...

I need Calculator project :
A complex number calculator program that allows the user to do either addition, subtraction, multiplication or
division where a complex number is a number of the form a + bi where a and b are real numbers and i equals √
−1.
1. It must consist of at least five functions excluding the main function.

2. There should be a function for each operation. Each of these functions should handle checking the validity of its operands
that must be parameters of the function, perform their given operation and simplify their result. They must not perform
any form of I/O. However, the functions can have additional parameters; but, you will be responsible for deciding the
return types of these functions, which do not have to be the same.

3. There must be a calculator function that takes no parameters that is responsible for displaying a menu of the operations to
the user, receiving the choice of the user, calling the appropriate function to perform the chosen operation, and displaying
the result of the operation in the format
a + b i or a - b i

where a and b are displayed with one decimal point and the second display is for when b is negative, or an error message.
Furthermore, if the user selects an invalid option display the message ''Invalid Operation''.

a.The program should only perform at most one operation per run.
b. The main function should only call the calculator function.
c. The program cannot use loops.
d. The program can only include the libraries iostream, string, and iomanip.

Solutions

Expert Solution

Please refer to screenshots

Code

Output

Code to copy

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

using namespace std;

// structure to hold complex number
struct complexnumber{
    double real;
    double imaginary;
};


complexnumber add(complexnumber a, complexnumber b){
    complexnumber z;
    z.real = a.real + b.real; // calculating real part
    z.imaginary = a.imaginary + b.imaginary; // calculating imaginary part
  
    return z;
}

complexnumber subtract(complexnumber a, complexnumber b){
    complexnumber z;
    z.real = a.real - b.real; // calculating real part
    z.imaginary = a.imaginary - b.imaginary; // calculating imaginary part
  
    return z;
}

complexnumber multiply(complexnumber a, complexnumber b){
    complexnumber z;
  
    // calculating real part
    z.real = (a.real * b.real) - (a.imaginary * b.imaginary);
  
    // calculating imaginary part
    z.imaginary = (a.real + a.imaginary) * (b.real + b.imaginary);
    z.imaginary = z.imaginary - ((a.real * b.real) + (a.imaginary * b.imaginary));
  
    return z;
}


complexnumber divide(complexnumber a, complexnumber b){
    // denominator of result, common for real and imaginary part
    double den = (b.real * b.real) + (b.imaginary * b.imaginary);
  
    complexnumber z;
  
    // calculating real part
    z.real = (a.real * b.real) + (a.imaginary * b.imaginary);
    z.real = z.real / den;
  
    // calculating imaginary part
    z.imaginary = (a.imaginary * b.real) - (a.real * b.imaginary);
    z.imaginary = z.imaginary / den;
  
    return z;
}

void calculator(){
    cout<<"Welcome to complex number calculator\n\n";
  
    cout<<"1 for Addition\n";
    cout<<"2 for Subtration\n";
    cout<<"3 for Multiplication\n";
    cout<<"4 for Division\n";
  
    complexnumber a, b;
  
    // Input first number
    cout<<"Enter First Complex Number\n";
    cout<<"Enter Real Part: ";
    cin>>a.real;
    cout<<"Enter Imaginary Part: ";
    cin>>a.imaginary;
  
    // Input second number
    cout<<"Enter Second Complex Number\n";
    cout<<"Enter Real Part: ";
    cin>>b.real;
    cout<<"Enter Imaginary Part: ";
    cin>>b.imaginary;
  
    cout<<"\nChoose your operation\n";
  
    int ch;
    cin>>ch; // input choice
  
    complexnumber z;
  
    switch(ch){
        // if user choose addition
        case 1: z = add(a, b);
                break;
              
                // if user choose subtract
        case 2: z = subtract(a, b);
                break;
              
                // if user choose multiply
        case 3: z = multiply(a, b);
                break;
              
                // if user choose division
        case 4: z = divide(a, b);
                break;
              
                // If user choose invalid option
        default: cout<<"Invalid Operation\n";
                return;
    }
  
    cout<<"Result of operation is\n";
    cout<< fixed << setprecision(1); // setting precision to one decimal point
    cout<<z.real;
  
    if(z.imaginary < 0){
        z.imaginary *= -1; // if imaginary part is negative
        cout<<" - ";
    } else {
        cout<<" + "; // if imaginary part is positive
    }
  
    cout<<z.imaginary<<'\n'; // printing imaginary part
}

int main(){
    calculator();
  
    return 0;
}

Thank you


Related Solutions

Fat Percentage Calculator Create a C++ program that allows the user to enter the number of...
Fat Percentage Calculator Create a C++ program that allows the user to enter the number of calories and fat grams in a food. The application should display the percentage of the calories that come from fat. If the calories from fat are less than 30% of the total calories of the food, it should also display a message indicating the food is low in fat. One gram of fat has 9 calories, so: Calories from fat = fat grams *...
Write a MATLAB program to do the following: 1- Allows the user to enter unlimited number...
Write a MATLAB program to do the following: 1- Allows the user to enter unlimited number of data set. 2- Allows the user to exit the program at any time by entering zero. 3- Calculates and displays the following statistics for the entered data set: a- Count of positive, negative, and total numbers. b- Maximum and Minimum numbers. c- Sum and Average of positive numbers. d- Sum and Average of negative numbers. e- Overall Sum and overall average of all...
Write a program that runs on SPIM that allows the user to enter the number of...
Write a program that runs on SPIM that allows the user to enter the number of hours, minutes and seconds and then prints out the total time in seconds. Name the source code file “seconds.asm
Write a program that runs on SPIM that allows the user to enter the number of...
Write a program that runs on SPIM that allows the user to enter the number of hours, minutes and seconds and then prints out the total time in seconds. Name the source code file “seconds.asm Explain step by step
I need a java code Write a simple program to prompt the user for a number...
I need a java code Write a simple program to prompt the user for a number between 1 and 12 (inclusive) and print out the corresponding month. For example:   The 12th month is December. Use a java "switch" statement to convert from the number (1-12) to the month. Also use a "do while" loop and conditional checking to validate that the number entered is between 1 and 12 inclusive and if not, prompt the user again until getting the correct...
Java Programming: Write a program that allows the user to compute the power of a number...
Java Programming: Write a program that allows the user to compute the power of a number or the product of two numbers. Your program should prompt the user for the following information: • Type of operation to perform • Two numbers (the arguments) for the operation to perform The program then outputs the following information: • The result of the mathematical operation selected. Menu to be displayed for the user: MATH TOOL 1. Compute the power of a number 2....
Write a program that allows the user to search the array to find the number entered
Write a program that allows the user to search the array to find the number entered
Time Calculator Create a C++ program that lets the user enter a number of seconds and...
Time Calculator Create a C++ program that lets the user enter a number of seconds and produces output according to the following criteria: • There are 60 seconds in a minute. If the number of seconds entered by the user is greater than or equal to 60, the program should display the number of minutes in that many seconds. • There are 3,600 seconds in an hour. If the number of seconds entered by the user is greater than or...
In Python, write a program that allows the user to enter a number between 1-5 and...
In Python, write a program that allows the user to enter a number between 1-5 and returns the vitamins benefits based on what the user entered. The program should: Continue to run until the user quits and Validate the user’s input (the only acceptable input is 0, 1, 2, 3, 4, or 5), If the user enters zero “0” the program should terminate You can use the following facts: 1- Vitamin A protects eyes from night blindness and age-related decline...
In Python, write a program that allows the user to enter a number between 1-5 and...
In Python, write a program that allows the user to enter a number between 1-5 and returns the vitamins benefits based on what the user entered. The program should: Continue to run until the user quits and Validate the user’s input (the only acceptable input is 0, 1, 2, 3, 4, or 5), If the user enters zero “0” the program should terminate You can use the following facts: 1- Vitamin A protects eyes from night blindness and age-related decline...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT