创建一个Fraction类(分数)实现分数的加减乘除,比较大小、约分等方法.要求:为类添加属性

<span style="font-family:KaiTi_GB2312;font-size:32px;">#import <Foundation/Foundation.h>
@interface Fraction : NSObject
@property (nonatomic) NSInteger molecular;       //分子
@property (nonatomic) NSInteger denominator;   //分母
//初始化
- (id)initWithMolecular:(NSInteger)molecular denominator:(NSInteger)denominator;
//分数相加
- (Fraction *)sumFraction:(Fraction *)fraction;
//分数相减
- (Fraction *)differencingFraction:(Fraction *)fraction;
//分数相乘
- (Fraction *)multiplicaFraction:(Fraction *)fraction;
//分数相除
- (Fraction *)divisionFraction:(Fraction *)fraction;
//约分
- (Fraction *)reduceOfFraction;
//比较大小
- (void)compareFraction:(Fraction *)fraction;
@end
</span>

<span style="font-family:KaiTi_GB2312;font-size:32px;">@implementation Fraction
- (id)initWithMolecular:(NSInteger)molecular denominator:(NSInteger)denominator
{
    self = [super init];
    if (self) {
        _molecular = molecular;
        _denominator = denominator;
    }
    return self;
}
//分数相加
- (Fraction *)sumFraction:(Fraction *)fraction
{
    Fraction *temp = [Fraction alloc];
    temp.molecular = self.molecular * fraction.denominator + self.denominator * fraction.molecular;
    temp.denominator = self.denominator * fraction.denominator;
    return temp;
}
//分数相减
- (Fraction *)differencingFraction:(Fraction *)fraction
{
    Fraction *temp = [Fraction alloc];
    temp.molecular = _molecular * fraction.denominator - _denominator * fraction.molecular;
    temp.denominator = _denominator * fraction.denominator;
    return temp;
}
//分数相乘
- (Fraction *)multiplicaFraction:(Fraction *)fraction
{
    Fraction *temp = [Fraction alloc];
    temp.molecular = _molecular * fraction.molecular;
    temp.denominator = _denominator * fraction.denominator;
    return temp;
}
//分数相除
- (Fraction *)divisionFraction:(Fraction *)fraction
{
    Fraction *temp = [Fraction alloc];
    temp.molecular = self.molecular * fraction.denominator;
    temp.denominator = self.denominator * fraction.molecular;
    return temp;
}
//比较大小
- (void)compareFraction:(Fraction *)fraction
{
    Fraction *temp = [Fraction alloc];
    temp = [self differencingFraction:fraction];
    if (temp.molecular > 0) {
        NSLog(@"%ld/%ld较大",self.molecular,self.denominator);
    }else if(temp.molecular == 0){
        NSLog(@"两个数一样大");
    }else{
        NSLog(@"%ld/%ld较大",fraction.molecular,fraction.denominator);
    }
}
//约分
- (Fraction *)reduceOfFraction
{
    if (self.molecular < self.denominator) {
        for (NSInteger i = self.molecular; i > 0; i--) {
            if (self.molecular % i == 0 && self.denominator % i == 0) {
                self.molecular = self.molecular / i;
                self.denominator = self.denominator / i;
            }
        }
    }else if(self.molecular > self.denominator){
        for (NSInteger i = self.denominator; i > 0; i--) {
            if (self.molecular % i == 0 && self.denominator % i == 0) {
                self.molecular = self.molecular / i;
                self.denominator = self.denominator / i;
            }
        }
    }
    return self;
}

@end</span>

<span style="font-family:KaiTi_GB2312;font-size:32px;"> @autoreleasepool {
        Fraction *fra1 = [[Fraction alloc]initWithMolecular:3 denominator:4];
        Fraction *fra2 = [[Fraction alloc]initWithMolecular:2 denominator:5];
        //分数相加
        Fraction *number1 =[fra1 sumFraction:fra2];
        [number1 reduceOfFraction];
        NSLog(@"%ld/%ld ",(long)number1.molecular,number1.denominator);
        
        //分数相减
        Fraction *number2 =[fra1 differencingFraction:fra2];
        [number2 reduceOfFraction];
        NSLog(@"%ld/%ld",number2.molecular,number2.denominator);
        
        //分数相乘
        Fraction *number3 = [fra1 multiplicaFraction:fra2];
        [number3 reduceOfFraction];
        NSLog(@"%ld/%ld",number3.molecular,number3.denominator);
        
        //分数相除
        Fraction *number4 = [fra1 divisionFraction:fra2];
        [number4 reduceOfFraction];
        NSLog(@"%ld/%ld",number4.molecular,number4.denominator);
        
        //比较大小
        [fra1 compareFraction:fra2];</span>




好的,以下是一个简单的分数定义,包含加减乘除运算符重载和分数显示功能: ```cpp #include <iostream> using namespace std; class Fraction { public: int numerator; // 分子 int denominator; // 分母 Fraction(int n = 0, int d = 1) : numerator(n), denominator(d) {} // 重载加法运算符 Fraction operator+(const Fraction& f) const { int n = numerator * f.denominator + f.numerator * denominator; int d = denominator * f.denominator; return Fraction(n, d); } // 重载减法运算符 Fraction operator-(const Fraction& f) const { int n = numerator * f.denominator - f.numerator * denominator; int d = denominator * f.denominator; return Fraction(n, d); } // 重载乘法运算符 Fraction operator*(const Fraction& f) const { int n = numerator * f.numerator; int d = denominator * f.denominator; return Fraction(n, d); } // 重载除法运算符 Fraction operator/(const Fraction& f) const { int n = numerator * f.denominator; int d = denominator * f.numerator; return Fraction(n, d); } // 分数显示函数 void display() const { cout << numerator << "/" << denominator << endl; } }; int main() { Fraction a(1, 2); Fraction b(3, 4); Fraction c = a + b; c.display(); // 输出 5/4 Fraction d = a - b; d.display(); // 输出 -1/4 Fraction e = a * b; e.display(); // 输出 3/8 Fraction f = a / b; f.display(); // 输出 2/3 return 0; } ``` 在上面的代码中,我们定义了一个 `Fraction`,用来表示分数中包含了两个数据成员 `numerator` 和 `denominator` 分别表示分子和分母。我们重载了加减乘除四个运算符,使得可以直接对两个分数进行加减乘除运算。我们还定义了一个 `display()` 函数,用来显示分数的值。 在主函数中,我们创建了两个分数对象 `a` 和 `b`,并进行了加减乘除四种运算,最后通过 `display()` 函数显示了运算结果。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值