.NET 4.x Исправить расчет и вывод дроби - C#

Узнай цену своей работы

Формулировка задачи:

Здравствуйте! Помогите пожалуйста!

Нужно исправить вывод. Вожу например: 5 7 * 5 7 и на выходе получается 25/0. Вот как мне сделать чтобы считало знаменатель?
Листинг программы
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace lab_6_8
  7. {
  8. class Program
  9. {
  10. static void Main(string[] args)
  11. {
  12. try
  13. {
  14. NewClass a1 = new NewClass();
  15. a1.a = 2.5;
  16. double tmp = (double)a1;
  17. int tmp1 = (int)a1;
  18. YoungClass young = new YoungClass();
  19. OldClass old = new OldClass();
  20. string[] StrMass = new string[5];
  21. string str;
  22. double a, b, answe = 0, answe_1 = 0, a_1, a_2, b_1, b_2;
  23. Console.WriteLine("Введите через пробел числа a_1 a_2 b_1 b_2 (a_1/a_2 знак b_1/b_2):");
  24. str = Console.ReadLine();
  25. StrMass = str.Split(' ');
  26. a_1 = Convert.ToDouble(StrMass[0]);
  27. a_2 = Convert.ToDouble(StrMass[1]);
  28. a = a_1 / a_2;
  29. b_1 = Convert.ToDouble(StrMass[3]);
  30. b_2 = Convert.ToDouble(StrMass[4]);
  31. b = b_1 / b_2;
  32. if (StrMass[2] == "/")
  33. {
  34. //answe = a / b;
  35. answe = a_1 * b_2;
  36. answe_1 = b_1 * a_2;
  37. }
  38. if (StrMass[2] == "*")
  39. //answe = a * b;
  40. answe = a_1 * b_1;
  41. answe_1 = a_2 * b_2;
  42. if (StrMass[2] == "+")
  43. //answe = a + b;
  44. answe = a_1 + b_1;
  45. answe_1 = a_2 + b_2;
  46. if (StrMass[2] == "-")
  47. //answe = a - b;
  48. answe = a_1 - b_1;
  49. answe_1 = a_2 - b_2;
  50. Console.WriteLine(tmp);
  51. Console.WriteLine(tmp1);
  52. Console.WriteLine(a.CompareTo(b));
  53. Console.WriteLine("Вывод: {0}/{1}",(int)answe,(int)answe_1);
  54. young.Wow += old.old;
  55. young.Count(a);
  56. }
  57. catch (Exception e)
  58. {
  59. Console.WriteLine(e.Message);
  60. }
  61. finally
  62. {
  63. Console.WriteLine("конец");
  64. }
  65. Console.ReadKey();
  66. }
  67. }
  68. class NewClass : IComparable
  69. {
  70. public double a;
  71. public double b;
  72. public int CompareTo(object obj)
  73. {
  74. var sort = (NewClass)obj;
  75. return a.CompareTo(sort.a);
  76. }
  77. public static NewClass operator +(NewClass obj1, NewClass obj2)
  78. {
  79. NewClass arr = new NewClass();
  80. arr.a = obj1.a + obj2.a;
  81. arr.b = obj1.b + obj2.b;
  82. return arr;
  83. }
  84. public static NewClass operator /(NewClass obj1, NewClass obj2)
  85. {
  86. NewClass arr = new NewClass();
  87. arr.a = obj1.a / obj2.a;
  88. arr.b = obj1.b / obj2.b;
  89. return arr;
  90. }
  91. public static NewClass operator -(NewClass obj1, NewClass obj2)
  92. {
  93. NewClass arr = new NewClass();
  94. arr.a = obj1.a - obj2.a;
  95. arr.b = obj1.b - obj2.b;
  96. return arr;
  97. }
  98. public static NewClass operator *(NewClass obj1, NewClass obj2)
  99. {
  100. NewClass arr = new NewClass();
  101. arr.a = obj1.a * obj2.a;
  102. arr.b = obj1.b * obj2.b;
  103. return arr;
  104. }
  105. public static explicit operator double(NewClass obj) { return (double)(obj.a); }
  106. public static implicit operator int(NewClass obj) { return (int)(obj.a); }
  107. }
  108. class YoungClass
  109. {
  110. public delegate void Method();
  111. public event Method Wow;
  112. public double Count(double a)
  113. {
  114. double ten = 10.0;
  115. if (a < ten)
  116. Wow();
  117. if (a > ten)
  118. Wow();
  119. return 0;
  120. }
  121. }
  122. class OldClass
  123. {
  124. public void old()
  125. {
  126. // Console.WriteLine("число a меньше 10");
  127. }
  128. }
  129. }

Решение задачи: «.NET 4.x Исправить расчет и вывод дроби»

textual
Листинг программы
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace lab_6_8
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             try
  14.             {
  15.                 NewClass a1 = new NewClass();
  16.                 a1.a = 2.5;
  17.                 double tmp = (double)a1;
  18.  
  19.                 int tmp1 = (int)a1;
  20.                 YoungClass young = new YoungClass();
  21.                 OldClass old = new OldClass();
  22.                 string[] strMass = new string[5];
  23.                 string str;
  24.                 double a, b, answe = 0, answe_1 = 0, a_1, a_2, b_1, b_2;
  25.                 Console.WriteLine("Введите через пробел числа a_1 a_2 b_1 b_2 (a_1/a_2 знак b_1/b_2):");
  26.                 str = Console.ReadLine();
  27.                 strMass = str.Split(' ');
  28.                 a_1 = Convert.ToDouble(strMass[0]);
  29.                 a_2 = Convert.ToDouble(strMass[1]);
  30.                 a = a_1 / a_2;
  31.                 b_1 = Convert.ToDouble(strMass[3]);
  32.                 b_2 = Convert.ToDouble(strMass[4]);
  33.                 b = b_1 / b_2;
  34.                 switch (strMass[2])
  35.                 {
  36.  
  37.                     case "/":
  38.                         answe = a_1 * b_2;
  39.                         answe_1 = b_1 * a_2;
  40.                         break;
  41.                     case "*":
  42.                         answe = a_1 * b_1;
  43.                         answe_1 = a_2 * b_2;
  44.                         break;
  45.                     case "+":
  46.                         answe = a_1 + b_1;
  47.                         answe_1 = a_2 + b_2;
  48.                         break;
  49.                     case "-":
  50.                         answe = a_1 - b_1;
  51.                         answe_1 = a_2 - b_2;
  52.                         break;
  53.                     default :
  54.                         throw new Exception(string.Format("Ошибка ввода, вводить можно только "/", "*", "+", "-" ,а введено {0}", strMass[2]));
  55.                 }
  56.                 Console.WriteLine(tmp);
  57.                 Console.WriteLine(tmp1);
  58.                 Console.WriteLine(a.CompareTo(b));
  59.                 Console.WriteLine("Вывод: {0}/{1}", (int)answe, (int)answe_1);
  60.                 young.Wow += old.old;
  61.                 young.Count(a);
  62.             }
  63.             catch (Exception e)
  64.             {
  65.                 Console.WriteLine(e.Message);
  66.             }
  67.             finally
  68.             {
  69.  
  70.                 Console.WriteLine("конец");
  71.             }
  72.             Console.ReadKey();
  73.         }
  74.     }
  75.     class NewClass : IComparable
  76.     {
  77.         public double a;
  78.         public double b;
  79.  
  80.         public int CompareTo(object obj)
  81.         {
  82.             var sort = (NewClass)obj;
  83.             return a.CompareTo(sort.a);
  84.         }
  85.  
  86.         public static NewClass operator +(NewClass obj1, NewClass obj2)
  87.         {
  88.             NewClass arr = new NewClass();
  89.             arr.a = obj1.a + obj2.a;
  90.             arr.b = obj1.b + obj2.b;
  91.             return arr;
  92.         }
  93.         public static NewClass operator /(NewClass obj1, NewClass obj2)
  94.         {
  95.             NewClass arr = new NewClass();
  96.             arr.a = obj1.a / obj2.a;
  97.             arr.b = obj1.b / obj2.b;
  98.             return arr;
  99.         }
  100.         public static NewClass operator -(NewClass obj1, NewClass obj2)
  101.         {
  102.             NewClass arr = new NewClass();
  103.             arr.a = obj1.a - obj2.a;
  104.             arr.b = obj1.b - obj2.b;
  105.             return arr;
  106.         }
  107.         public static NewClass operator *(NewClass obj1, NewClass obj2)
  108.         {
  109.             NewClass arr = new NewClass();
  110.             arr.a = obj1.a * obj2.a;
  111.             arr.b = obj1.b * obj2.b;
  112.             return arr;
  113.         }
  114.  
  115.         public static explicit operator double(NewClass obj) { return (double)(obj.a); }
  116.         public static implicit operator int(NewClass obj) { return (int)(obj.a); }
  117.     }
  118.  
  119.     class YoungClass
  120.     {
  121.         public delegate void Method();
  122.         public event Method Wow;
  123.  
  124.         public double Count(double a)
  125.         {
  126.             double ten = 10.0;
  127.             if (a < ten)
  128.                 Wow();
  129.             if (a > ten)
  130.                 Wow();
  131.             return 0;
  132.         }
  133.     }
  134.     class OldClass
  135.     {
  136.         public void old()
  137.         {
  138.             // Console.WriteLine("число a меньше 10");
  139.         }
  140.     }
  141. }

ИИ поможет Вам:


  • решить любую задачу по программированию
  • объяснить код
  • расставить комментарии в коде
  • и т.д
Попробуйте бесплатно

Оцени полезность:

7   голосов , оценка 4.571 из 5

Нужна аналогичная работа?

Оформи быстрый заказ и узнай стоимость

Бесплатно
Оформите заказ и авторы начнут откликаться уже через 10 минут
Похожие ответы