Advertisement
Guest User

Untitled

a guest
Jun 18th, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.05 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3.  
  4. namespace _4
  5. {
  6. internal class Complex
  7. {
  8. public Complex(string r, string i)
  9. {
  10. if (r.All(x => char.IsDigit(x) && x == '-' && x == '+' && x == ',') &&
  11. i.All(x => char.IsDigit(x) && x == '-' && x == '+' && x == ','))
  12. {
  13. throw new Exception("real и imagine могут быть только числом или символами + и -");
  14. }
  15.  
  16. Real = r;
  17. Imagine = i;
  18. }
  19.  
  20. private string Real { get; set; }
  21. private string Imagine { get; set; }
  22.  
  23. public Complex Add(Complex complex)
  24. {
  25. return new Complex(
  26. Convert.ToString(Convert.ToDouble(Real) + Convert.ToDouble(complex.Real)),
  27. Convert.ToString(Convert.ToDouble(Imagine) + Convert.ToDouble(complex.Imagine)));
  28. }
  29.  
  30. public Complex Multiply(Complex z1)
  31. {
  32. Imagine = Convert.ToString(Convert.ToDouble(Real) * Convert.ToDouble(z1.Imagine) +
  33. Convert.ToDouble(Imagine) * Convert.ToDouble(z1.Real));
  34. Real = Convert.ToString(Convert.ToDouble(Real) * Convert.ToDouble(z1.Real) -
  35. Convert.ToDouble(Imagine) * Convert.ToDouble(z1.Imagine));
  36. return new Complex(Real, Imagine);
  37. }
  38.  
  39. public void Print()
  40. {
  41. Console.Write(Convert.ToDouble(Imagine) < 0 ? $"{Real}{Imagine}i" : $"{Real}+{Imagine}i");
  42. }
  43. }
  44.  
  45. internal class Program
  46. {
  47. private static void Main(string[] args)
  48. {
  49. string tempReal;
  50. string tempImagine;
  51. Console.Write("Введите операцию (сложение, умножение): ");
  52. var tip = Convert.ToString(Console.ReadLine());
  53. switch (tip.ToLower())
  54. {
  55. case "умножение":
  56. {
  57. Console.Write("Введите реальную часть z1: ");
  58. tempReal = Console.ReadLine();
  59. Console.Write("Введите мнимую часть z1: ");
  60. tempImagine = Console.ReadLine();
  61. var c1 = new Complex(tempReal, tempImagine);
  62.  
  63. Console.Write("Введите реальную часть z2: ");
  64. tempReal = Console.ReadLine();
  65. Console.Write("Введите мнимую часть z2: ");
  66. tempImagine = Console.ReadLine();
  67. var c2 = new Complex(tempReal, tempImagine);
  68.  
  69. var c3 = c1.Multiply(c2);
  70. Console.WriteLine("z3 = ");
  71. c3.Print();
  72. break;
  73. }
  74.  
  75. case "сложение":
  76. {
  77. Console.Write("Введите реальную часть z1: ");
  78. tempReal = Console.ReadLine();
  79. Console.Write("Введите мнимую часть z1: ");
  80. tempImagine = Console.ReadLine();
  81. var c1 = new Complex(tempReal, tempImagine);
  82.  
  83. Console.Write("Введите реальную часть z2: ");
  84. tempReal = Console.ReadLine();
  85. Console.Write("Введите мнимую часть z2: ");
  86. tempImagine = Console.ReadLine();
  87. var c2 = new Complex(tempReal, tempImagine);
  88.  
  89. var c3 = c1.Add(c2);
  90. Console.Write("z3 = ");
  91. c3.Print();
  92. break;
  93. }
  94.  
  95. default:
  96. Console.WriteLine("Такого типа не существует!");
  97. break;
  98. }
  99.  
  100. Console.ReadKey();
  101. }
  102. }
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement