模板数组类(支持分数和浮点数的求和计算)

问题描述

封装一个模板数组类Array,支持以下操作:

  1. 构造函数Array(int n),将数组初始化为n个存储空间,建议使用vector;

  2. 函数input(int n),使用插入运算符<<读取数据,最多读取n个元素,但不能超过数组存储空间的上限;

  3. 重载下标运算符,返回数组的元素。

封装一个分数类Fract,用来处理分数功能和运算,能支持你的Array类使用。

  1. 构造:传入两个参数n和m,表示n/m;分数在构造时立即转化成最简分数。

  2. show()函数:分数输出为“a/b”或“-a/b”的形式,a、b都是无符号整数。若a为0或b为1,只输出符号和分子,不输出“/”和分母。

  3. 在分数类上重载+=运算符,进行分数的加法运算。

输入形式

输入为两部分,分别是一组实数测试样例和一组分数测试样例。

这两组测试样例都以正整数n,且n小于1000,n表示需要输入n个实数(或分数)。

测试样例的第二行开始为n个实数(或分数)。其中每个分数输入为两个整数n、m,表示分数n/m。

输出形式

第一部分输出一个实数,是第一组测试样例之和;第二部分输出一个分数,是第二组测试样例之和。

分数输出时为最简形式,负号只会出现在最前面,若分母为1或分子为0,则只输出一个整数,即分子部分,而没有“/”和分母部分。

样例输入

4

6 8 7 5

9

1 3 20 -15 80 150 -9 1 6 6 12 16 -33 -48 6 11 0 -10

样例输出

26

-17117/2640

代码

#include <iostream>
#include <vector>
#include <cmath>
using namespace std;

template <typename T>
class Array
{
private:
    vector<T> v;

public:
    Array(int n)
    {
        v.resize(n);
    }
    T &operator[](int i)
    {
        return v[i];
    }
    void input(int n)
    {
        T tmp;
        for (int i = 0; i < n; i++)
        {
            cin >> tmp;
            v[i] = tmp;
        }
    }
};

class Fract
{
private:
    int n;
    int m;

public:
    static int gcd(int a, int b)
    {
        if (!a)
            return b;
        else
            return gcd(b % a, a);
    }

    Fract() {}
    Fract(int n, int m)
    {
        int tmp = gcd(n, m);
        n /= tmp;
        m /= tmp;
        this->n = n;
        this->m = m;
    }
    Fract(int n)
    {
        this->n = n;
        m = 1;
    }
    void show()
    {
        string ch;
        if (n < 0 || m < 0)
        {
            ch = "-";
        }
        if (n < 0 && m < 0)
        {
            ch = "";
        }
        if (n == 0)
        {
            cout << 0 << endl;
            return;
        }
        if (abs(n) == 1 && abs(m) == 1)
        {
            cout << ch << 1 << endl;
            return;
        }
        cout << ch << abs(n) << "/" << abs(m) << endl;
    }
    Fract &operator+=(const Fract &other)
    {
        n = this->n * other.m + this->m * other.n;
        m = this->m * other.m;
        int tmp = gcd(n, m);
        n /= tmp;
        m /= tmp;
        return *this;
    }
    friend istream &operator>>(istream &input, Fract &other)
    {
        input >> other.n >> other.m;
        return input;
    }
};

int main()
{
    unsigned int n;
    cin >> n;
    Array<double> db(n);
    db.input(n);
    double dbsum(0.0);
    for (unsigned int i = 0; i < n; i++)
        dbsum += db[i];
    cout << dbsum << endl;

    cin >> n;
    Array<Fract> fr(n);
    fr.input(n);
    Fract frsum(0, 1);

    for (unsigned int i = 0; i < n; i++)
        frsum += fr[i];
    frsum.show();
}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值