C++:实现一个分数(fraction)类

本文介绍了如何在C++中创建一个能处理至少108位数字的超长整数类(Extralongint),并实现了加减乘除及比较操作。同时,设计了一个分数类(Fraction),支持基本算术运算,且能以简化形式打印输出。示例中展示了Extralongint的运算和Fraction的使用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

exercise 3


Tasks marked with a ∗ are assessed coursework. Hand in your solutions to these via email to rn@ic.ac.uk. (Resit students do not need to submit coursework.) Use the subject line “ C++ CW: surname firstname CW3”, where surname firstname CW3.cpp is the attached file that contains your solution. The course will be assessed based on 5 pieces of coursework (25%) and an end of term driving test (75%). Your submission must be your own work (submissions will be checked for plagiarism), and it should compile (and run) with the GNU C++ compiler g++. The deadline for submitting the coursework is 10pm on 24/02/2019.

  1. Extra long int
    Create a class extralongint that can handle very long integer numbers, say with at least up to 108 digits. Code the mathematical operations +, -, * and /, for this class and provide ways to input and output its objects. Furthermore, overload the relational operators <, > and ==. Let a = 1234567890987654321, b = 9876543210123456789. Compute the following values
    (1) a∗b
    (2) a∗b/(b−a)
    (3) b2 − a2 (d) 51!
    (4) Is 2122 > 2221 ?

  2. Fractions
    Design a class fraction that allows you to store numbers in fractional form and do basic arithmetics with them. Your class declaration should include at least the following methods.

      class fraction {
      friend ostream &operator<< (ostream &os, const fraction &f);
      private:
        int numerator, denominator;
        void reduce();
      public:
        fraction(int n = 0, int d = 1) : numerator(n), denominator(d) { reduce(); }
        fraction o
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值