Комплексные числа, округление, ввод тригонометрической формы со строки - C#
Формулировка задачи:
Не знаю, как округлить значения, к примеру до 2-3 символа. Еще не понимаю как сделать метод для преобразования строки в тригонометрическую форму. Помогите пожалуйста.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace ConsoleApplication5
{
class Program
{
class Complex
{
double re = 0, im;
double p, q;
//конструктор и его перегрузки
public Complex()
{
}
public Complex(double r) // конструктор для триг
{
re = r;
im = 0;
}
public Complex(double r, double i) // конструктор для алгебр
{
re = r;
im = i;
}
public Complex(double r, double i, string flag)
{
if (flag == "alg")
{
re = r;
im = i;
}
if (flag == "trig")
{
p = r;
q = i;
}
}
public static Complex AlgToTrig(Complex f)
{
Complex f1 = new Complex();
double ABS = f.abs();
double b = Math.Atan(f.im / f.re) + Math.PI;
f1.p = ABS;
f1.q = b;
return f1;
}
public static Complex TrigToAlg(Complex f)
{
Complex f1 = new Complex();
f1.re = f.p * Math.Cos(f.q) * -1;
f1.im = f.p * Math.Sin(f.q) * -1;
return f1;
}
//метод преобразования в строку
public string getComplexStrAlg() // в алг
{
string gc;
if (im >= 0)
{
gc = re + "+" + im + "i";
}
else
{
gc = re + "" + im + "i";
}
return gc;
}
public string getComplexStrTrig() // в триг
{
string gc;
gc = String.Format("z={0:n3}(cos{1:n3}+sin{1:n3})", p, q);
return gc;
}
#region перезагрузки арифметических операторов
public static Complex operator +(Complex f1, Complex f2) // сложение
{
if (f1.p != 0 && f2.p != 0)
{
Complex ff1 = Complex.TrigToAlg(f1);
Complex ff2 = Complex.TrigToAlg(f2);
Complex th = new Complex();
th.re = ff1.re + ff2.re;
th.im = ff1.im + ff2.im;
return th;
}
else
{
Complex th = new Complex();
th.re = f1.re + f2.re;
th.im = f1.im + f2.im;
return th;
}
}
public static Complex operator -(Complex f1, Complex f2) //вычитание
{
if (f1.p != 0 && f2.p != 0)
{
Complex ff1 = Complex.TrigToAlg(f1);
Complex ff2 = Complex.TrigToAlg(f2);
Complex th = new Complex();
th.re = ff1.re - ff2.re;
th.im = ff1.im - ff2.im;
return th;
}
else
{
Complex th = new Complex();
th.re = f1.re - f2.re;
th.im = f1.im - f2.im;
return th;
}
}
public static Complex operator *(Complex f1, Complex f2)// умножение
{
if (f1.p != 0 && f2.p != 0)
{
Complex ff1 = Complex.TrigToAlg(f1);
Complex ff2 = Complex.TrigToAlg(f2);
Complex th = new Complex();
th.re = ff1.re * ff2.re - ff1.im * ff2.im;
th.im = ff1.re * ff2.im + ff1.im * ff2.re;
return th;
}
else
{
Complex th = new Complex();
th.re = f1.re * f2.re - f1.im * f2.im;
th.im = f1.re * f2.im + f1.im * f2.re;
return th;
}
}
public static Complex operator /(Complex f1, Complex f2)// деление
{
if (f1.p != 0 && f2.p != 0)
{
Complex ff1 = Complex.TrigToAlg(f1);
Complex ff2 = Complex.TrigToAlg(f2);
Complex th = new Complex();
th.re =(ff1.re * ff2.re + ff1.im * ff2.im) / (Math.Pow(ff2.re, 2) + Math.Pow(ff2.im, 2));
th.im = (ff2.re * ff1.im - ff1.re * ff2.im) / (Math.Pow(ff2.re, 2) + Math.Pow(ff2.im, 2));
return th;
}
else
{
Complex th = new Complex();
th.re = (f1.re * f2.re + f1.im * f2.im) / (Math.Pow(f2.re, 2) + Math.Pow(f2.im, 2));
th.im = (f2.re * f1.im - f1.re * f2.im) / (Math.Pow(f2.re, 2) + Math.Pow(f2.im, 2));
return th;
}
}
#endregion
public double abs()
{
double r;
r = Math.Sqrt(Math.Pow(re, 2) + Math.Pow(im, 2));
return r;
}
#region перезагрузки операторов сравнения
public static bool operator >(Complex f1, Complex f2)
{
double r1, r2;
if (f1.p != 0 && f2.p != 0)
{
Complex ff1 = Complex.TrigToAlg(f1);
Complex ff2 = Complex.TrigToAlg(f2);
r1 = ff1.abs();
r2 = ff2.abs();
}
else
{
r1 = f1.abs();
r2 = f2.abs();
}
if (r1 > r2)
{
return true;
}
else
{
return false;
}
}
public static bool operator <(Complex f1, Complex f2)
{
double r1, r2;
if (f1.p != 0 && f2.p != 0)
{
Complex ff1 = Complex.TrigToAlg(f1);
Complex ff2 = Complex.TrigToAlg(f2);
r1 = ff1.abs();
r2 = ff2.abs();
}
else
{
r1 = f1.abs();
r2 = f2.abs();
}
if (r1 < r2)
{
return true;
}
else
{
return false;
}
}
public static bool operator <=(Complex f1, Complex f2)
{
double r1, r2;
if (f1.p != 0 && f2.p != 0)
{
Complex ff1 = Complex.TrigToAlg(f1);
Complex ff2 = Complex.TrigToAlg(f2);
r1 = ff1.abs();
r2 = ff2.abs();
}
else
{
r1 = f1.abs();
r2 = f2.abs();
}
if (r1 <= r2)
{
return true;
}
else
{
return false;
}
}
public static bool operator >=(Complex f1, Complex f2)
{
double r1, r2;
if (f1.p != 0 && f2.p != 0)
{
Complex ff1 = Complex.TrigToAlg(f1);
Complex ff2 = Complex.TrigToAlg(f2);
r1 = ff1.abs();
r2 = ff2.abs();
}
else
{
r1 = f1.abs();
r2 = f2.abs();
}
if (r1 >= r2)
{
return true;
}
else
{
return false;
}
}
public static bool operator ==(Complex f1, Complex f2)
{
double r1, r2;
if (f1.p != 0 && f2.p != 0)
{
Complex ff1 = Complex.TrigToAlg(f1);
Complex ff2 = Complex.TrigToAlg(f2);
r1 = ff1.abs();
r2 = ff2.abs();
}
else
{
r1 = f1.abs();
r2 = f2.abs();
}
if (r1 == r2)
{
return true;
}
else
{
return false;
}
}
public static bool operator !=(Complex f1, Complex f2)
{
double r1, r2;
if (f1.p != 0 && f2.p != 0)
{
Complex ff1 = Complex.TrigToAlg(f1);
Complex ff2 = Complex.TrigToAlg(f2);
r1 = ff1.abs();
r2 = ff2.abs();
}
else
{
r1 = f1.abs();
r2 = f2.abs();
}
if (r1 >= r2)
{
return true;
}
else
{
return false;
}
}
#endregion
public static Complex StringToComplex(string s)//статический метод из строки в комплексное число
{
Complex f = new Complex();
String pattern = @"[-+]?(\d+)([-+*/])(\d+)";
foreach (Match m in Regex.Matches(s, pattern))
{
f.re = Int32.Parse(m.Groups[1].Value);
f.im = Int32.Parse(m.Groups[3].Value);
if (m.Groups[0].Value == "-")
{
f.re = f.re * -1;
}
if (m.Groups[2].Value == "-")
{
f.im *= -1;
}
}
return f;
}
~Complex()
{
Console.WriteLine("Экземпляр уничтожен");
}
}
static void Main(string[] args)
{
Complex cmx1 = new Complex();
Complex cmx2 = new Complex();
Complex cmx = new Complex();
Console.WriteLine("Введите первое комплексное число в алгебраической форме:");
string comp1 = Console.ReadLine();
cmx1 = Complex.StringToComplex(comp1);
Console.WriteLine("Введите второе комплексное число в алгебраической форме:");
string comp2 = Console.ReadLine();
cmx2 = Complex.StringToComplex(comp2);
Console.WriteLine("ОПЕРАТОРЫ");
cmx = cmx1 + cmx2;
Console.WriteLine(cmx1.getComplexStrAlg() + "+" + cmx2.getComplexStrAlg() + "=" + cmx.getComplexStrAlg());
cmx = cmx1 - cmx2;
Console.WriteLine(cmx1.getComplexStrAlg() + "-" + cmx2.getComplexStrAlg() + "=" + cmx.getComplexStrAlg());
cmx = cmx1 * cmx2;
Console.WriteLine(cmx1.getComplexStrAlg() + "*" + cmx2.getComplexStrAlg() + "=" + cmx.getComplexStrAlg());
cmx = cmx1 / cmx2;
Console.WriteLine(cmx1.getComplexStrAlg() + "/" + cmx2.getComplexStrAlg() + "=" + cmx.getComplexStrAlg());
if (cmx1 > cmx2)
{
Console.WriteLine(cmx1.getComplexStrAlg() + ">" + cmx2.getComplexStrAlg() + "=true");
}
else
if (cmx1 < cmx2)
{
Console.WriteLine(cmx1.getComplexStrAlg() + "<" + cmx2.getComplexStrAlg() + "=true");
}
else
{
Console.WriteLine(cmx1.getComplexStrAlg() + "==" + cmx2.getComplexStrAlg() + "=true");
}
Console.WriteLine("ТРИГОНОМЕТРИЧЕСКАЯ ФОРМА:");
Console.WriteLine("Первое число в тригонометрической форме:");
cmx = Complex.AlgToTrig(cmx1);
Console.WriteLine(cmx.getComplexStrTrig());//
Console.WriteLine("Второе число в тригонометрической форме:");
cmx = Complex.AlgToTrig(cmx2);
Console.WriteLine(cmx.getComplexStrTrig());//
Console.WriteLine("Введите аргумент комплексного числа:");
double p = Convert.ToDouble(Console.ReadLine());
Console.WriteLine("Введите угол комплексного числа:");
double q = Convert.ToDouble(Console.ReadLine());
Complex tcmx = new Complex(p, q, "trig");
Console.WriteLine("Ваше число в тригонометрической и алгеброической формах");
Console.WriteLine(tcmx.getComplexStrTrig());
cmx = Complex.TrigToAlg(tcmx);
Console.WriteLine(cmx.getComplexStrAlg());
Console.ReadLine();
}
}
}Решение задачи: «Комплексные числа, округление, ввод тригонометрической формы со строки»
textual
Листинг программы
//метод преобразования в строку
public string getComplexStrAlg() // в алг
{
return string.Format("{0:0.00} {1} {2:0.00}i", re, im >= 0 ? "+" : "-", Math.Abs(im));
}