"обыкновенная дробь" - C#

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

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

Создать класс "обыкн дробь", поля кл-числитель и знаменатель. В кл предусмотреть метод перегрузки операции умножение. Проверьте пож-ста на ошибки. При вычислении почему-то 0 выдаёт... programm.cs
namespace Drobi
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Введите числитель1 ");
            int a = Convert.ToInt16(Console.ReadLine());
            Console.WriteLine("Введите знаменатель1 ");
            int a1 = Convert.ToInt16(Console.ReadLine());
            Console.WriteLine("Введите числитель2 ");
            int b = Convert.ToInt16(Console.ReadLine());
            Console.WriteLine("Введите знаменатель2 ");
            int b1 = Convert.ToInt16(Console.ReadLine());
            if (a1 == 0 || b1 == 0) Console.WriteLine("знаменатель не может равняться 0!");
                 else
            {
                drob d1 = new drob(a, a1);
                drob d2 = new drob(b, b1);
                drob d = d1 * d2;
                Console.WriteLine("умножение="+d.ToString());
                Console.ReadLine();
            }
        }
    }
}
 
[COLOR="DarkRed"]drob.cs[/COLOR]
class drob
    {
        int a;
        public int A
        {
            get
            {
                return a;
            }
            set
            {
                a = value;
            }
        }
        int b;
        public int B
        {
            get
            {
                return b;
            }
            set
            {
                b = value;
            }
        }
        public drob(int a, int b)
        {       a = this.a;
            b = this.b;
 
        }
        public  override string ToString()
        {
            return "\n"+a.ToString() + "\n" + "--" + "\n" + b.ToString();
        }
        public static  drob operator * (drob d1, drob d2)
        {
            return new drob(d1.A * d2.A, d1.B * d2.B);
        }
 
    }                         
}

Решение задачи: «"обыкновенная дробь"»

textual
Листинг программы
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
 
class Program
{
    static void Main(string[] args)
    {
        Fraction f1 = new Fraction(1, 2);
        Fraction f2 = new Fraction(1, 3);
        Console.WriteLine(f1 + f2);
        Console.WriteLine(1.0 / 2.0 + 1.0 / 3.0);
        Console.WriteLine(f1 - f2);
        Console.WriteLine(1.0 / 2.0 - 1.0 / 3.0); 
        Console.WriteLine(f1 * f2);
        Console.WriteLine(1.0 / 2.0 * 1.0 / 3.0);
        Console.WriteLine(f1 / f2);
        Console.WriteLine((1.0 / 2.0) / (1.0 / 3.0));
 
        Fraction fracion = new Fraction(3, 9);
        Console.WriteLine(fracion);
        fracion.Reduce();
        Console.WriteLine(fracion);
 
        Console.ReadKey(true);
    }
}

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


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

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

15   голосов , оценка 3.867 из 5