c#试验报告 构造分数类

4 篇文章 0 订阅
1 篇文章 0 订阅

using System;
using System.Collections.Generic;
using System.Text;

namespace 分数类
{
    class Fraction
    {
        private int numerator;//分子
        public int Numerator
        {
            get { return numerator; }
            set { numerator = value; }
        }
        private int denominator;//分母
        public int Denominator
        {
            get { return denominator; }
            set
            {
                if (value != 0)
                    denominator = value;
                else
                    throw new Exception("分母不能为0");
            }
        }

        public Fraction(int x, int y)//构造函数
        {
            Numerator = x;
            Denominator = y;
            YueFen(ref numerator, ref denominator);
            FunC(ref numerator, ref denominator);
        }
        public Fraction() { }//重载无参构造函数
        

        public void YueFen(ref int x, ref int y)//约分
        {
            
            int a, b, c;
            if (x > y)
            {
                a = x;
                b = y;
            }
            else
            {
                a = y;
                b = x;
            }
        
            c = a % b;//求最大公约数
            while (c != 0)
            {
                a = b;
                b = c;
                c = a % b;
            }
            x /= b;
            y /= b;
        }

        public void FunC(ref int x, ref int y)//分子分母异号时分子为负
        {
            if (x * y < 0)
            {
                x = -Math.Abs(x);
                y = Math.Abs(y);
            }
            else
            {
                x = Math.Abs(x);
                y = Math.Abs(y);
            }
        }

        public static void IntToFraction(object x, object y,out Fraction a,out Fraction b)//把int 类转换成fraction
        {
            a = new Fraction();
            b = new Fraction();
            if (x is int)
            {
                a.Numerator = (int)x;
                a.Denominator = 1;
            }
            else
                a = (Fraction)x;

            if (y is int)
            {
                b.Numerator = (int)y;
                b.Denominator = 1;
            }
            else
                b = (Fraction)y;
        }

        public void Compare(object x, object y)//比大小
        {
            Fraction a = new Fraction();
            Fraction b = new Fraction();
            Fraction.IntToFraction(x, y, out a, out b);
            Fraction result = new Fraction();
            result=result.Sub(a,b);
            if (result.Numerator == 0)
                Console.WriteLine("等于");
            else if (result.Numerator < 0)
                Console.WriteLine("第一个数小于第二个数");
            else
                Console.WriteLine("第一个数大于第二个数");
        }

        public double ToDecimal(Fraction x)//转换成小数
        {
            double a, b;
            a = x.Numerator;
            b = x.Denominator;
            return a / b;
        }

        public void OutPut()//输出
        {
            Console.WriteLine("{0}/{1}", Numerator, Denominator);
        }

        public Fraction Add(object a, object b)//加
        {
            Fraction x=new Fraction ();
            Fraction y=new Fraction ();
            Fraction.IntToFraction(a, b, out x, out y);
            Fraction result = new Fraction();
            result.Numerator = x.Numerator * y.Denominator + y.Numerator * x.Denominator;
            result.Denominator = x.Denominator * y.Denominator;
            result.YueFen(ref result.numerator, ref result.denominator);//进行约分
            result.FunC(ref result.numerator, ref result.denominator);//分子分母异号时分子为负
            return result;
        }


        public Fraction Sub(object a, object b)//减
        {
            Fraction x = new Fraction();
            Fraction y = new Fraction();
            Fraction.IntToFraction(a, b, out x, out y);
            Fraction result = new Fraction();
            result.Numerator = x.Numerator * y.Denominator - y.Numerator * x.Denominator;
            result.Denominator = x.Denominator * y.Denominator;
            result.YueFen(ref result.numerator, ref result.denominator);//进行约分
            result.FunC(ref result.numerator, ref result.denominator);//分子分母异号时分子为负
            return result;
        }


        public Fraction Mul(object a, object b)//乘
        {
            Fraction x = new Fraction();
            Fraction y = new Fraction();
            Fraction.IntToFraction(a, b, out x, out y);
            Fraction result = new Fraction();
            result.Numerator = x.Numerator * y.Numerator;
            result.Denominator = x.Denominator * y.Denominator;
            result.YueFen(ref result.numerator, ref result.denominator);//进行约分
            result.FunC(ref result.numerator, ref result.denominator);//分子分母异号时分子为负
            return result;
        }


        public Fraction Div(object a, object b)//除
        {
            Fraction x = new Fraction();
            Fraction y = new Fraction();
            Fraction.IntToFraction(a, b, out x, out y);
            Fraction result = new Fraction();
            result.Numerator = x.Numerator * y.Denominator;
            result.Denominator = x.Denominator * y.Numerator;
            result.YueFen(ref result.numerator, ref result.denominator);//进行约分
            result.FunC(ref result.numerator, ref result.denominator);//分子分母异号时分子为负
            return result;
        }
        
    }
}

----------以下为测试代码---------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace 分数类
{
    class Program
    {
        static void Main(string[] args)
        {
            Fraction num1 = new Fraction(2,4);
            Fraction num2 = new Fraction(3,4);
            Fraction num3 = new Fraction();
            num3 = num3.Add(num1, 7);
            num3.OutPut();
            num3 = num3.Sub(num1, num2);
            num3.OutPut();
            num3 = num3.Mul(num1, num2);
            num3.OutPut();
            num3 = num3.Div(num1, num2);
            num3.OutPut();
            num3.Compare(num1, 3);
            double a;
            a=num2.ToDecimal(num2);
            Console.WriteLine(a);



        }
    }
}


  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值