create c# code: complex calclator app
- User
create c# code: complex calclator app
- Assistant
create c# code: complex calclator app
Creating a full application would be too long for this format. However, I can provide you with a basic implementation of a Complex Calculator in C# that you can use as a starting point for your application. You will need to create a new Console App project in Visual Studio and copy this code into the Program.cs file.
using System;
namespace ComplexCalculatorApp
{
public class ComplexNumber
{
public double Real { get; set; }
public double Imaginary { get; set; }
public ComplexNumber(double real, double imaginary)
{
Real = real;
Imaginary = imaginary;
}
public ComplexNumber Add(ComplexNumber other)
{
return new ComplexNumber(Real + other.Real, Imaginary + other.Imaginary);
}
public ComplexNumber Subtract(ComplexNumber other)
{
return new ComplexNumber(Real - other.Real, Imaginary - other.Imaginary);
}
public ComplexNumber Multiply(ComplexNumber other)
{
double real = Real * other.Real - Imaginary * other.Imaginary;
double imaginary = Real * other.Imaginary + Imaginary * other.Real;
return new ComplexNumber(real, imaginary);
}
public ComplexNumber Divide(ComplexNumber other)
{
double real = (Real * other.Real + Imaginary * other.Imaginary) / (other.Real * other.Real + other.Imaginary * other.Imaginary);
double imaginary = (Imaginary * other.Real - Real * other.Imaginary) / (other.Real * other.Real + other.Imaginary * other.Imaginary);
return new ComplexNumber(real, imaginary);
}
public override string ToString()
{
return $"{Real}{(Imaginary < 0 ? "" : "+")}{Imaginary}i";
}
}
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Complex Calculator");
Console.WriteLine("Enter first complex number (format: a+bi):");
var input1 = Console.ReadLine();
var complex1 = ParseComplex(input1);
Console.WriteLine("Enter second complex number (format: a+bi):");
var input2 = Console.ReadLine();
var complex2 = ParseComplex(input2);
Console.WriteLine("\nResults:");
Console.WriteLine($"{complex1} + {complex2} = {complex1.Add(complex2)}");
Console.WriteLine($"{complex1} - {complex2} = {complex1.Subtract(complex2)}");
Console.WriteLine($"{complex1} * {complex2} = {complex1.Multiply(complex2)}");
Console.WriteLine($"{complex1} / {complex2} = {complex1.Divide(complex2)}");
Console.ReadLine();
}
static ComplexNumber ParseComplex(string input)
{
input = input.Replace(" ", "").Replace("i", "");
string[] split = input.Split('+', '-');
bool hasTwoParts = split.Length == 2;
double real = 0, imaginary = 0;
if (hasTwoParts)
{
real = double.Parse(split[0]);
if (input.Contains("-"))
{
if (input.IndexOf('-') == 0)
{
real = -real;
imaginary = -double.Parse(split[1]);
}
else
{
imaginary = -double.Parse(split[1]);
}
}
else
{
imaginary = double.Parse(split[1]);
}
}
else
{
real = double.Parse(split[0]);
}
return new ComplexNumber(real, imaginary);
}
}
}
This code snippet provides a simple ComplexNumber class with basic mathematical operations (add, subtract, multiply, and divide) and a Console App entry point that will ask the user to enter two complex numbers, perform calculations, and print the results. Note that this code does not handle all edge cases and is not optimized for advanced calculations.
Not yet included in a published release.