Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Complex
//Rextester.Program.Main is the entry point for your code. Don't change it. //Compiler version 4.0.30319.17929 for Microsoft (R) .NET Framework 4.5 using System; using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; namespace Rextester { public class Program { public static void Main(string[] args) { //Your code goes here //Complex z = new Complex (1,1); Complex z = new Complex (1,180,false); Console.WriteLine(z.toRadian()); Complex a = new Complex (1,1); Complex b = new Complex (2,-3); Console.WriteLine(a*b); } } public struct Complex { public double real; public double imag; public double radius; public double arg; public bool polar, radian; public double Real { get { return real; } set { real = value; } } public double Imag { get { return imag; } set { imag = value; } } public Complex (double real, double imag) { this.real = real; this.imag = imag; this.radius = Math.Sqrt(Math.Pow(real, 2) + Math.Pow(imag, 2)); this.polar = false; this.radian = false; this.arg = Math.Atan(imag/real)*360/(2*Math.PI); } public Complex (double radius, double arg, bool radian) { this.radius = radius; this.arg = arg; this.polar = true; this.radian = radian; double argD = arg*2*Math.PI/360; if (!radian) { // 1st quadrant if (!radian && (arg%360) >=0 && (arg%360) <=90) { this.real = Math.Cos(argD%360)*radius; this.imag = Math.Sin(argD%360)*radius; } // 2nd quadrant else if (!radian && (arg%360)>90 && (arg%360) <=180) { this.real = Math.Cos(argD%360)*radius; this.imag = Math.Sin(argD%360)*radius; } // 3rd quadrant else if (!radian && (arg%360)>180 && (arg%360) <=270) { this.real = -Math.Cos(argD%360)*radius; this.imag = -Math.Sin(argD%360)*radius; } // 4th quadrant else {//if (!radian && (arg%360)>270 && (arg%360) <360) { this.real = Math.Cos(argD%360)*radius; this.imag = -Math.Sin(argD%360)*radius; } } else { // 1st quadrant if (!radian && (arg%360) >=0 && (arg%360) <=90) { this.real = Math.Cos(arg%360)*radius; this.imag = Math.Sin(arg%360)*radius; } // 2nd quadrant else if (!radian && (arg%360)>90 && (arg%360) <=180) { this.real = -Math.Cos(arg%360)*radius; this.imag = Math.Sin(arg%360)*radius; } // 3rd quadrant else if (!radian && (arg%360)>180 && (arg%360) <=270) { this.real = -Math.Cos(arg%360)*radius; this.imag = -Math.Sin(arg%360)*radius; } // 4th quadrant else {//if (!radian && (arg%360)>270 && (arg%360) <360) { this.real = Math.Cos(arg%360)*radius; this.imag = -Math.Sin(arg%360)*radius; } } } public Complex toPolar() { this.polar = true; return this; } public Complex toRect() { this.polar = false; return this; } public Complex toRadian() { if (radian) { return this; } else { radian = true; this.arg = this.arg*2*Math.PI/360; return this; } } public Complex toDegree() { if (!radian) { return this; } else { radian = false; this.arg = this.arg*360/(2*Math.PI); return this; } } public override string ToString() { if (!polar && Math.Abs(this.imag)!=1) { return real + " + " + imag + "i"; } else if (!polar && Math.Abs(this.imag)==1) { return real + " + " + "i"; } else if (polar && radian) { return "(" + radius + ", " + arg + "r)"; } else return "(" + radius + ", " + arg + "d)"; } public static Complex operator +(Complex left, Complex right) { if (left.polar) { left.toRect(); } if (right.polar) { right.toRect(); } Complex result = new Complex (left.real+right.real, left.imag+right.imag); return result; } public static Complex operator -(Complex left, Complex right) { if (left.polar) { left.toRect(); } if (right.polar) { right.toRect(); } Complex result = new Complex (left.real-right.real, left.imag-right.imag); return result; } public static Complex operator *(Complex left, Complex right) { if (left.polar) { left.toRect(); } if (right.polar) { right.toRect(); } Complex result = new Complex (left.real*right.real-left.imag*right.imag, left.real*right.imag+left.imag*right.real); return result; } public static Complex operator /(Complex left, Complex right) { if (left.polar) { left.toRect(); } if (right.polar) { right.toRect(); } Complex result = new Complex ((left.real*right.real + left.imag*right.imag)/(Math.Pow(right.real,2)+Math.Pow(right.imag,2)), (left.imag*right.real - left.real*right.imag)/(Math.Pow(right.real,2)+Math.Pow(right.imag,2)) ); return result; } public static Complex Pow(Complex z, int pow) { Complex result = z.toPolar(); result = new Complex (Math.Pow(z.radius,pow), pow*z.arg); return result; } } }
run
|
edit
|
history
|
help
0
Number to string
Anq Calc
2.2 Basic types: Dictionary
Nuzrath 284
Diagonal Difference
Int number to String Array
Derived static
10
axasxasxasaxsd
Encode URl C#