Using Using C# and visual Studio,Write a class that will allow for the addition,
ID: 3817556 • Letter: U
Question
Using Using C# and visual Studio,Write a class that will allow for the addition, subtraction, multiplication and division of 2 complex numbers. The class must use operator overloading for +, -, * and / operations. The class must have a ToString method that overrides the ToString method of the object class and properly formats the complex number ( 3.4 + 2i or -2.1 1.5i). You may use the following as a starting point for your class:
public class ComplexNumbers
{
public double Real
{
get ;
private set;
}
public double Imag
{
get ;
private set;
}
public ComplexNumbers(double r, double i)
{
Real = r;
Imag = i;
}
public static ComplexNumbers operator +(ComplexNumbers lhs, ComplexNumbers rhs)
{
double r = lhs.Real + rhs.Real;
double i = lhs.Imag + rhs.Imag;
return new ComplexNumbers(r, i);
}
Your console application to demonstrate the class's functioning must read the complex number components from a file called data.txt, add the first 2 complex numbers, subtract the next 2, multiply the next 2 and divide the last 2 and then write the results both to the screen and a file called outData.txt. Screen output should be properly formatted with the appropriate signs and the i for imaginary. The file output should be structured in the same manner as data.txt is formatted, with just the components. data.txt has the following contents and structure. The first value is the real component and the second is the imaginary component.
2 4
1.5 -3
2.3 -1.1
-1.8 2.5
3 5
7 -1
3 3
1 -2
Explanation / Answer
Here I am sending you a code for operator overloading in C#
This code given to my student hope it will work for you
class calculation
{
int a, b, c;
public calculation()
{
a = b = c = 0;
}
public calculation(int x, int y, int z)
{
a = x;
b = y;
c = z;
}
public static calculation operator +(calculation op1, calculation op2)
{
calculation calc = new calculation();
calc.a = op1.a + op2.a;
calc.b = op1.b + op2.b;
calc.c = op1.c + op2.c;
return calc;
}
public static calculation operator -(calculation op1, calculation op2)
{
calculation calc = new calculation();
calc.a = op1.a - op2.a;
calc.b = op1.b - op2.b;
calc.c = op1.c - op2.c;
return calc;
}
public static calculation operator *(calculation op1, calculation op2)
{
calculation calc = new calculation();
calc.a = op1.a * op2.a;
calc.b = op1.b * op2.b;
calc.c = op1.c * op2.c;
}
public static calculation operator /(calculation op1, calculation op2)
{
calculation calc = new calculation();
calc.a = op1.a / op2.a;
calc.b = op1.b / op2.b;
calc.c = op1.c / op2.c;
return calc;
}
public void ShowTheResult()
{
Console.WriteLine(a + "," + b + "," + c);
Console.ReadLine();
}
}
Here we define the operators and a function (ShowTheResult()) to show the output. After that we write the following code in the Program class. By this the addition will be performed and the result will be shown with the help of The ShowTheResult() function.
class Program
{
static void Main(string[] args)
{
calculation j = new calculation(5, 10, 3);
calculation k = new calculation();
k = i + j;
k.ShowTheResult();
Console.WriteLine();
}
}
class Program
{
static void Main(string[] args)
{
calculation i = new calculation(10, 20, 30);
calculation j = new calculation(5, 10, 3);
calculation k = new calculation();
k = i - j;
k.ShowTheResult();
Console.WriteLine();
}
}
Multiply
class Program
{
static void Main(string[] args)
{
calculation i = new calculation(10, 20, 30);
calculation j = new calculation(5, 10, 3);
calculation k = new calculation();
k = i * j;
k.ShowTheResult();
Console.WriteLine();
}
}
Division
class Program
{
static void Main(string[] args)
{
calculation i = new calculation(10, 20, 30);
calculation j = new calculation(5, 10, 3);
calculation k = new calculation();
k = i / j;
k.ShowTheResult();
Console.WriteLine();
}
}
D
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.