Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Comp 3
//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; using MathSpace; namespace Rextester { public class Program { public static Complex ComplexF (Fraction real, Fraction imag) { Complex z = new Complex(); z.fraction = true; z.real = real.num/real.denom; z.imag = imag.num/imag.denom; z.radius = Math.Sqrt(Math.Pow(z.real, 2) + Math.Pow(z.imag, 2));; z.polar = false; z.radian = false; z.arg = Math.Atan(z.imag/z.real)*360/(2*Math.PI); z.realF = real; z.imagF = imag; return z; } 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 = ComplexF(new Fraction(1,2),new Fraction(1,4)); // Complex b = new Complex (4,-1); Complex z = new Complex(new Fraction(1,2),new Fraction(5,5)); Complex y = new Complex(3,5); Console.WriteLine(z); //Add(z,y); Console.WriteLine((new Fraction (5,5))); } public struct Complex { public double real; public double imag; public double radius; public double arg; public bool polar, radian; public Fraction realF, imagF; public bool fraction; 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); this.fraction = false; this.realF = new Fraction(0,0); this.imagF = new Fraction(0,0); } public Complex (Fraction real, Fraction imag) { this.fraction = true; this.real = real.num/real.denom; this.imag = imag.num/imag.denom; this.radius = Math.Sqrt(Math.Pow(this.real, 2) + Math.Pow(this.imag, 2));; this.polar = false; this.radian = false; this.arg = Math.Atan(this.imag/this.real)*360/(2*Math.PI); this.realF = real; this.imagF = imag; } public Complex (double radius, double arg, bool radian) { this.realF = new Fraction(0,0); this.imagF = new Fraction(0,0); this.radius = radius; this.arg = arg; this.polar = true; this.radian = radian; double argD = arg*2*Math.PI/360; this.fraction = false; 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 static Complex Add(Complex a, Complex b) { return a+b; } 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() { //*** Rectangular Form // real not 0, imag not 1 or -1 if (!polar && !this.fraction && this.real !=0 && (Math.Abs(this.imag)!=1 || Math.Abs(this.imag)!=-1) ) { return this.real + " + " + this.imag + "i"; } // real not 0, imag is 1 else if (!polar && !this.fraction && this.real!=0 && this.imag==1) { return this.real + " + i"; } // real not 0, imag is -1 else if (!polar && !this.fraction && this.real!=0 && this.imag==-1) { return this.real + " - i"; } // real is 0, imag is 1 else if (!polar && !this.fraction && this.real ==0 && this.imag==1) { return "i"; } // real is 0, imag is -1 else if (!polar && !this.fraction && this.real ==0 && this.imag==1) { return "-i"; } // real is 0, imag not 1 or -1 else if (!polar && !this.fraction && this.real==0 && Math.Abs(this.imag)!=1) { return this.imag + "i"; } // real not 0, imag is 0 else if (!polar && !this.fraction && this.real!=0 && this.imag==0) { return this.real.ToString(); } // real 0 and imag 0 else if (!polar && !this.fraction && this.real == 0 && this.imag == 0) { return this.real.ToString(); } //*** Fraction else if (!polar && this.fraction && this.realF.num%this.realF.denom!=0 && this.imagF.num!=this.imagF.denom) { return this.realF + " " + this.imagF + "i"; } // real not 0, imag is 1 else if (!polar && this.fraction && this.realF.num%this.realF.denom!=0 && this.imagF.num==this.imagF.denom) { return this.realF + " + i"; } // real not 0, imag is -1 else if (!polar && !this.fraction && this.real!=0 && this.imag==-1) { return this.real + " - i"; } //*** Polar Form else if (polar && radian && !this.fraction ) { 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 = ComplexF (new Fraction((int)(left.real*right.real + left.imag*right.imag), (int)(Math.Pow(right.real,2)+Math.Pow(right.imag,2))), new Fraction((int)(left.imag*right.real - left.real*right.imag), (int)(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; } } } } namespace MathSpace { public struct Fraction { public long num; public long denom; public long integer; public long Num { get { return num; } set { num = value; } } public long Denom { get { return denom; } set { denom = value; } } public long Integer { get { return integer; } set { integer = value; } } public Fraction(int num, int denom) { this.num = num; this.denom = denom; this.integer = 0; } public Fraction(long num, long denom) { this.num = num; this.denom = denom; this.integer = 0; } public Fraction(int integer, int num, int denom) { this.integer = integer; this.num = num; this.denom = denom; } public Fraction(long integer, long num, long denom) { this.integer = integer; this.num = num; this.denom = denom; } public Fraction toMixed() { if (this.integer==0) { return new Fraction (this.num/this.denom, this.num%this.denom, this.denom); } else return new Fraction (this.integer+this.num/this.denom,this.num%this.denom,this.denom); } public Fraction toImproper() { if (this.integer!=0 && this.integer<0) { return new Fraction (-(Math.Abs(this.integer)*Math.Abs(this.denom)+Math.Abs(this.num)),Math.Abs(this.denom)); } else if (this.integer!=0 && this.integer>0) { return new Fraction (Math.Abs(this.integer)*Math.Abs(this.denom)+Math.Abs(this.num),Math.Abs(this.denom)); } else return new Fraction (this.num,this.denom); } public override string ToString() { if (this.denom == 0) { return "Div by 0"; } // if fraction is not whole number but not mixed if (this.denom != 1 && this.integer == 0 && this.num%this.denom!=0) { // if fraction is negative (either num or denom negative but not both) if ((this.num < 0 || this.denom < 0 ) && !(this.num<0 && this.denom<0) ) { this.num = this.num; this.denom = -this.denom; return this.num + "/" + Math.Abs(this.denom); } // if fraction is positive else return num + "/" + denom; // + ", " + num/denom + " " + num%denom + "/" + denom; } //if fraction is not whole number and mixed number else if (this.denom != 1 && this.integer != 0) { var new_integer = this.integer + this.num/this.denom; var new_num = this.num % this.denom; // if fraction is negative (either num or denom negative but not both) if ((this.num < 0 || this.denom < 0) && !(this.num<0 && this.denom<0) ) { return new_integer + " " + Math.Abs(new_num) + "/" + Math.Abs(this.denom); } // if fraction is positive else return new_integer + " " + new_num + "/" + denom; } // if denom not 1 and numerator is a multiple of the denominator (ie. 4/2 ==> 2) and not mixed number else if (this.num % this.denom == 0 &&this.integer==0) { return (this.num / this.denom).ToString(); } // if denom not 1 and numerator is a multiple of the denominator (ie. 4/2 ==> 2) and mixed number else if (this.num % this.denom == 0 && this.integer!=0) { return (this.integer+(this.num / this.denom)).ToString(); } // if denominator is 1, ie., whole number and not mixed number else if (this.integer!=0) { return (integer+num).ToString(); } else return num.ToString(); } public Fraction Simplify() { int divisor = Convert.ToInt32(GCD(this.num, this.denom)); if (this.denom != 0 && this.integer==0 && this.num >0) { return new Fraction(this.num / divisor, this.denom / divisor); } else if (this.denom != 0 && this.integer==0 && this.num <0) { return new Fraction(-Math.Abs(this.num / divisor), Math.Abs(this.denom / divisor)); } else if (this.denom != 0 && this.integer!=0) { return new Fraction(this.integer, this.num / divisor, this.denom / divisor); } else return new Fraction (this.num, this.denom); } public static long GCD(long a, long b) { return b == 0 ? a : GCD(b, a % b); } public Fraction Reciprocal() { return new Fraction(this.Denom, this.Num); } public static Fraction operator +(Fraction left, Fraction right) { Fraction result; if (left.integer==0 &&right.integer==0){ result = new Fraction(left.num * right.denom + right.num * left.denom, left.denom * right.denom); } else { //result = new Fraction((left.integer*left.denom+left.num)*right.denom+(right.integer*right.denom+right.num)*left.denom, left.denom * right.denom); result = (new Fraction(left.integer+right.integer, left.num * right.denom + right.num * left.denom, left.denom * right.denom)).toMixed(); } return result.toMixed().Simplify(); } public static Fraction operator +(int left, Fraction right) { Fraction result; if (right.integer==0) { result = new Fraction(left * right.denom + right.num, right.denom); } else { result = (new Fraction(left+right.integer, right.num, right.denom)).toMixed(); } return result.Simplify(); } public static Fraction operator +(Fraction left, int right) { Fraction result; if (left.integer==0) { result = new Fraction(left.num + left.denom * right, left.denom); } else { result = (new Fraction(left.integer+right, left.denom)).toMixed(); } return result.Simplify(); } public static Fraction operator -(Fraction frac) { if (frac.integer==0) { return new Fraction(-frac.num,frac.denom); } else return (new Fraction (-frac.integer,frac.num,frac.denom)).toMixed(); } public static Fraction operator -(Fraction left, Fraction right) { Fraction result; if (left.integer==0 && right.integer==0) { result = new Fraction(left.num * right.denom - right.num * left.denom, left.denom * right.denom); } else { if (((left.integer*left.denom+left.num)/left.denom)>=((right.integer*right.denom+right.num)/right.denom)) { result = (left.toImproper()-right.toImproper()).toMixed(); } else result = (left.toImproper()-right.toImproper()).toMixed(); } return result.Simplify(); } public static Fraction operator -(int left, Fraction right) { Fraction result; if (right.integer==0) { result = new Fraction(left * right.denom - right.num, right.denom); } else result = (new Fraction(left * right.denom - right.toImproper().num, right.denom)).toMixed(); return result.Simplify(); } public static Fraction operator -(Fraction left, int right) { Fraction result; if (left.integer==0) { result = new Fraction(left.num - left.denom * right, left.denom); } // if mixed numbers else result = (new Fraction(left.toImproper().num - left.denom * right, left.denom)).toMixed(); return result.Simplify(); } public static Fraction operator *(Fraction left, Fraction right) { Fraction result; if (left.integer==0 && right.integer==0){ result = new Fraction(left.num * right.num, Math.Abs(left.denom * right.denom)); } // if mixed numbers else result = (left.toImproper()*right.toImproper()).toMixed(); return result.Simplify(); } public static Fraction operator *(int left, Fraction right) { Fraction result; if (right.integer==0){ result = new Fraction(left * right.num, right.denom); } else result = (new Fraction(right.toImproper().num * left, right.denom)).toMixed(); return result.Simplify(); } public static Fraction operator *(Fraction left, int right) { Fraction result; // if improper fraction if (left.integer==0){ result = new Fraction(left.num * right, left.denom); } // if mixed numbers else result = (new Fraction(left.toImproper().num * right, left.denom)).toMixed(); return result.Simplify(); } public static Fraction operator /(Fraction left, Fraction right) { Fraction result; if (left.integer==0 &&right.integer==0){ result = left * right.Reciprocal(); } else result = (left.toImproper() * right.toImproper().Reciprocal()).toMixed(); return result.Simplify(); } public static Fraction operator /(int left, Fraction right) { Fraction result; if (right.integer==0) { result = left * right.Reciprocal(); } else result = left * right.toImproper().Reciprocal(); return result.Simplify(); } public static Fraction operator /(Fraction left, int right) { Fraction rightRecip = new Fraction(1,right); Fraction result; if (left.integer==0) { result = left * rightRecip; } else { result = left.toImproper() * rightRecip; } return result.Simplify(); } public static int LCM(int a, int b) { int lcm; int a_r = a; int b_r = b; int d = max(a_r, b_r); while (d > 1) { if (a_r % d == 0 && b_r % d == 0) { int a_r_temp = a_r / d; int b_r_temp = b_r / d; a_r = a_r_temp; b_r = b_r_temp; lcm = d * a_r * b_r; break; } d--; } lcm = d * a_r * b_r; return lcm; } public static int LCM(long a, long b) { int lcm; long a_r = a; long b_r = b; int d = Convert.ToInt32(max(a_r, b_r)); while (d > 1) { if (a_r % d == 0 && b_r % d == 0) { long a_r_temp = a_r / d; long b_r_temp = b_r / d; a_r = a_r_temp; b_r = b_r_temp; lcm = Convert.ToInt32(d * a_r * b_r); break; } d--; } lcm = Convert.ToInt32(d * a_r * b_r); return lcm; } public static int max(int a, int b) { if (a > b) { return a; } else if (b > a) { return b; } else { return a; } } public static long max(long a, long b) { if (a > b) { return a; } else if (b > a) { return b; } else { return a; } } public static Fraction ToFrac(double deci) { /*List<double> d = new List<double>(); List<double> z = new List<double>(); List<double> n = new List<double>();*/ double[] d = new double[6]; double[] z = new double[6]; double[] n = new double[6]; z[1] = deci; //X d[0] = 0; d[1] = 1; for (int i = 1; i < 5; i++) { z[i + 1] = 1 / (z[i] - Math.Floor(z[i])); d[i + 1] = d[i] * Math.Floor(z[i + 1]) + d[i - 1]; n[i + 1] = Math.Round(z[1] * d[i + 1]); } int num, denom; num = (int)n[5]; denom = (int)d[5]; return new Fraction(num, denom); } /* public static Fraction RealToFraction(double value, double error) { if (error <= 0.0 || error >= 1.0) { throw new ArgumentOutOfRangeException("error", "Must be between 0 and 1 (exclusive)."); } int sign = Math.Sign(value); if (sign == -1) { value = Math.Abs(value); } if (sign != 0) { // error is the maximum relative error; convert to absolute error *= value; } int n = (int)Math.Floor(value); value -= n; if (value < error) { return new Fraction(sign * n, 1); } if (1 - error < value) { return new Fraction(sign * (n + 1), 1); } // The lower fraction is 0/1 int lower_n = 0; int lower_d = 1; // The upper fraction is 1/1 int upper_n = 1; int upper_d = 1; while (true) { // The middle fraction is (lower_n + upper_n) / (lower_d + upper_d) int middle_n = lower_n + upper_n; int middle_d = lower_d + upper_d; if (middle_d * (value + error) < middle_n) { // real + error < middle : middle is our new upper upper_n = middle_n; upper_d = middle_d; } else if (middle_n < (value - error) * middle_d) { // middle < real - error : middle is our new lower lower_n = middle_n; lower_d = middle_d; } else { // Middle is our best fraction return new Fraction((n * middle_d + middle_n) * sign, middle_d); } } }*/ public static Fraction Parse(string input) { if (String.IsNullOrWhiteSpace(input)) { throw new ArgumentNullException(); } //var instance = new Fraction(); // checks if there is more than one "/" if (input.IndexOf("/") != input.LastIndexOf("/")) { throw new ArgumentException(); } //return instance; int v; // if (input.IndexOf("/") != -1) { if (int.TryParse(input.Substring(0, input.IndexOf("/")), out v) && int.TryParse(input.Substring(input.IndexOf("/") + 1), out v)) { int num1 = int.Parse(input.Substring(0, input.IndexOf("/"))); int denom1 = int.Parse(input.Substring(input.IndexOf("/") + 1)); var frac = new Fraction(num1, denom1); return frac; } else throw new IndexOutOfRangeException(); } else // if { if (int.TryParse(input, out v)) { return new Fraction(int.Parse(input), 1); } else throw new IndexOutOfRangeException(); } } public static bool TryParse(string input, out Fraction frac) { if (string.IsNullOrWhiteSpace(input)) { frac = new Fraction(0, 0); return false; } if (input.IndexOf("/") == -1 || input.IndexOf("/") != input.LastIndexOf("/")) { frac = new Fraction(0, 0); return false; } long v; if (long.TryParse(input.Substring(0, input.IndexOf("/")), out v) && long.TryParse(input.Substring(input.IndexOf("/") + 1), out v)) { frac = new MathSpace.Fraction(1, 1); v = 1; return true; } else frac = new Fraction(0, 0); return false; } public static Fraction ConvertFrom(object obj) { Fraction frac = new MathSpace.Fraction(0, 0); if (obj.GetType() == frac.GetType()) { frac = (Fraction) obj; return frac; } else throw new NotSupportedException(); } public static double ConvertToDecimal(Fraction frac) { return Convert.ToDouble(frac.Num) / Convert.ToDouble(frac.Denom); } public static double operator +(Fraction left, double right) { double result = ConvertToDecimal(left) + right; return result; } public static double operator +(double left, Fraction right) { double result = left + ConvertToDecimal(right); return result; } public static double operator -(Fraction left, double right) { double result = ConvertToDecimal(left) - right; return result; } public static double operator -(double left, Fraction right) { double result = left - ConvertToDecimal(right); return result; } public static double operator *(Fraction left, double right) { double result = ConvertToDecimal(left) * right; return result; } public static double operator *(double left, Fraction right) { double result = left * ConvertToDecimal(right); return result; } public static double operator /(Fraction left, double right) { double result = ConvertToDecimal(left) / right; return result; } public static double operator /(double left, Fraction right) { double result = left / ConvertToDecimal(right); return result; } public static Fraction Pow(Fraction frac, int n) { long num_new = (long) Math.Pow(frac.Num, n); long denom_new = (long) Math.Pow(frac.Denom, n); return new MathSpace.Fraction(num_new, denom_new); } } }
run
|
edit
|
history
|
help
0
ANQ Bug with Feb month display
Amma
a5
remove special char c#
const
IEquatable doubt resolved!!
tool
AKASH B AND HIS CODES\\
Project Euler Problem 6
Expression Tree