Write a C# program in which you will create a class Complex that will allow the
ID: 3637798 • Letter: W
Question
Write a C# program in which you will create a class Complex that will allow the user enter two complex numbers and add, subtract, multiply or divide them.The class should overload the operators of +, -, * or / rather than create methods for this.
Use a menu from which the user can select which operation they want to do. Then once the operation is selected, they are to get the complex numbers. You should allow users to enter any complex number. On the division, you need to be sure that the divisor is not 0+0i. If it is, then have the user enter another complex number.
You should print the complex numbers, the operation, the = sign and the result to the screen. The problem should also be displayed to an output file with a problem number attached. Allow the user to enter more than one problem. Have an exit option on the menu.
Sample output to the output file is
1. 2+3i + 4 -8i = 6-5i
2. 2+3i – (4-8i) = -2 +11i
3. (2+3i) * (4-8i) =32-4i
4. (2+3i)/(4-8i)=-.2+.35i
You should also display the output to the screen but minus a problem number.
Explanation / Answer
//****************************************************
//ComplexNumber.cs
//****************************************************
using System;
namespace CramsterComplexNumber
{
class ComplexNumber
{
private double real;
private double imaginary;
public ComplexNumber()
{
real = 0;
imaginary = 0;
}
public ComplexNumber(double real, double imaginary)
{
this.real = real;
this.imaginary = imaginary;
}
public double Real
{
get
{
return real;
}
}
public double Imaginary
{
get
{
return imaginary;
}
}
public static ComplexNumber operator +(ComplexNumber lhs, ComplexNumber rhs)
{
return new ComplexNumber(lhs.Real + rhs.Real, lhs.Imaginary + rhs.Imaginary);
}
public static ComplexNumber operator -(ComplexNumber lhs, ComplexNumber rhs)
{
return new ComplexNumber(lhs.Real - rhs.Real, lhs.Imaginary - rhs.Imaginary);
}
public static ComplexNumber operator *(ComplexNumber lhs, ComplexNumber rhs)
{
double realNumerator = (lhs.Real * rhs.Real - lhs.Imaginary * rhs.Imaginary);
double imaginaryNumerator = (lhs.Imaginary * rhs.Real + lhs.Real * rhs.Imaginary);
return new ComplexNumber(realNumerator, imaginaryNumerator);
}
//http://upload.wikimedia.org/wikipedia/en/math/4/e/7/4e7f7f4252952bfa20559a0d388a5bfa.png
public static ComplexNumber operator /(ComplexNumber lhs, ComplexNumber rhs)
{
if (rhs.Real != 0 && rhs.Imaginary != 0)
{
ComplexNumber rhsConjugate = new ComplexNumber(rhs.Real, -rhs.Imaginary);
double realNumerator = (lhs.Real * rhs.Real + lhs.Imaginary * rhs.Imaginary);
double imaginaryNumerator = (lhs.Imaginary * rhs.Real - lhs.Real * rhs.Imaginary);
double denominator = (rhs.Real * rhs.Real + rhs.Imaginary * rhs.Imaginary);
return new ComplexNumber(realNumerator / denominator, imaginaryNumerator / denominator);
}
else
{
throw new Exception("Divided by zero.");
}
}
public override string ToString()
{
if (this.imaginary > 0)
{
return String.Format("{0:0.##}+{1:0.##}i", this.real, this.imaginary);
}
else if (this.imaginary < 0)
{
return String.Format("{0:0.##}{1:0.##}i", this.real, this.imaginary);
}
else
{
return String.Format("{0:0.##}", this.real);
}
}
}
}
//****************************************************
//Program.cs
//****************************************************
using System;
using System.IO;
namespace CramsterComplexNumber
{
class Program
{
static void Main(string[] args)
{
int response = -1;
int aa;
int bb;
int cc;
int dd;
ComplexNumber first;
ComplexNumber second;
string ans = "";
string[] answers;
StreamWriter fout = new StreamWriter("answer.txt");
int count = 1;
while (response != 0)
{
//Print out menu
Console.WriteLine("1. Addition");
Console.WriteLine("2. Subtraction");
Console.WriteLine("3. Multiplication");
Console.WriteLine("4. Division");
Console.WriteLine("0. Exit");
Console.Write("Enter your choice: ");
response = int.Parse(Console.ReadLine());
if (response > 0 && response < 5)
{
//Get first complex number
Console.WriteLine("Enter the first complex number a+bi:");
Console.Write(" a = ");
aa = int.Parse(Console.ReadLine());
Console.Write(" b = ");
bb = int.Parse(Console.ReadLine());
first = new ComplexNumber(aa, bb);
//Get second complex number
Console.WriteLine("Enter the second complex number c+di:");
Console.Write(" c = ");
cc = int.Parse(Console.ReadLine());
Console.Write(" d = ");
dd = int.Parse(Console.ReadLine());
second = new ComplexNumber(cc, dd);
//Add the result string to ans, using '#' as delimiter
if (response == 1)
{
ans += String.Format("#{0} + {1} = {2}",
first.ToString(), second.ToString(),
(first + second).ToString());
}
else if (response == 2)
{
ans += String.Format("#{0} - ({1}) = {2}",
first.ToString(), second.ToString(),
(first - second).ToString());
}
else if (response == 3)
{
ans += String.Format("#({0}) * ({1}) = {2}",
first.ToString(), second.ToString(),
(first * second).ToString());
}
else
{
try
{
ans += String.Format("#({0}) / ({1}) = {2}",
first.ToString(), second.ToString(),
(first / second).ToString());
}
catch (Exception e)
{
Console.WriteLine("ERROR: " + e.Message);
ans += String.Format("#({0}) / ({1}) = {2}",
first.ToString(), second.ToString(),
e.Message);
}
}
Console.WriteLine("Problem added. Proceed to next problem. ");
}
else if (response > 4)
{
Console.WriteLine("Wrong choice. Please enter a number between 1 and 4 (0 to exit). ");
}
}
//Split the answers. Ignore first character '#'
answers = ans.Substring(1).Split('#');
//Print out the answers to the screen and the file
Console.WriteLine(" The answers are: ");
foreach (string answer in answers)
{
Console.WriteLine(answer);
fout.WriteLine("{0}. {1}", count++, answer);
}
fout.Close();
Console.ReadLine();
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.