Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Linq;
- namespace _4
- {
- internal class Complex
- {
- public Complex(string r, string i)
- {
- if (r.All(x => char.IsDigit(x) && x == '-' && x == '+' && x == ',') &&
- i.All(x => char.IsDigit(x) && x == '-' && x == '+' && x == ','))
- {
- throw new Exception("real и imagine могут быть только числом или символами + и -");
- }
- Real = r;
- Imagine = i;
- }
- private string Real { get; set; }
- private string Imagine { get; set; }
- public Complex Add(Complex complex)
- {
- return new Complex(
- Convert.ToString(Convert.ToDouble(Real) + Convert.ToDouble(complex.Real)),
- Convert.ToString(Convert.ToDouble(Imagine) + Convert.ToDouble(complex.Imagine)));
- }
- public Complex Multiply(Complex z1)
- {
- Imagine = Convert.ToString(Convert.ToDouble(Real) * Convert.ToDouble(z1.Imagine) +
- Convert.ToDouble(Imagine) * Convert.ToDouble(z1.Real));
- Real = Convert.ToString(Convert.ToDouble(Real) * Convert.ToDouble(z1.Real) -
- Convert.ToDouble(Imagine) * Convert.ToDouble(z1.Imagine));
- return new Complex(Real, Imagine);
- }
- public void Print()
- {
- Console.Write(Convert.ToDouble(Imagine) < 0 ? $"{Real}{Imagine}i" : $"{Real}+{Imagine}i");
- }
- }
- internal class Program
- {
- private static void Main(string[] args)
- {
- string tempReal;
- string tempImagine;
- Console.Write("Введите операцию (сложение, умножение): ");
- var tip = Convert.ToString(Console.ReadLine());
- switch (tip.ToLower())
- {
- case "умножение":
- {
- Console.Write("Введите реальную часть z1: ");
- tempReal = Console.ReadLine();
- Console.Write("Введите мнимую часть z1: ");
- tempImagine = Console.ReadLine();
- var c1 = new Complex(tempReal, tempImagine);
- Console.Write("Введите реальную часть z2: ");
- tempReal = Console.ReadLine();
- Console.Write("Введите мнимую часть z2: ");
- tempImagine = Console.ReadLine();
- var c2 = new Complex(tempReal, tempImagine);
- var c3 = c1.Multiply(c2);
- Console.WriteLine("z3 = ");
- c3.Print();
- break;
- }
- case "сложение":
- {
- Console.Write("Введите реальную часть z1: ");
- tempReal = Console.ReadLine();
- Console.Write("Введите мнимую часть z1: ");
- tempImagine = Console.ReadLine();
- var c1 = new Complex(tempReal, tempImagine);
- Console.Write("Введите реальную часть z2: ");
- tempReal = Console.ReadLine();
- Console.Write("Введите мнимую часть z2: ");
- tempImagine = Console.ReadLine();
- var c2 = new Complex(tempReal, tempImagine);
- var c3 = c1.Add(c2);
- Console.Write("z3 = ");
- c3.Print();
- break;
- }
- default:
- Console.WriteLine("Такого типа не существует!");
- break;
- }
- Console.ReadKey();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement