#include<iostream>
#include<cmath>
#include<vector>
#include<typeinfo>
using namespace std;
int f(int a, int b)
{
int r;
r = a % b;
while(r)
{
a = b;
b = r;
r = a % b;
}
return b;
}
class Fract
{
public:
Fract(int a, int b) : n(a), m(b)
{
int x = f(n, m);
n = n / x;
m = m / x;
//cout << n << m << endl;
}
void show()
{
if(n == 0)
cout << "0" << endl;
else if(m == 1)
cout << n << endl;
else if(n < 0 && m > 0 )
cout << "-" << -n << "/" << m << endl;
else if(n > 0 && m < 0)
cout << "-" << n << "/" << -m << endl;
else if(n < 0 && m < 0)
cout << -n << "/" << -m << endl;
else
cout << n << "/" << m << endl;
}
operator double()
{
return (double)n/m;
}
Fract operator + (Fract &a)
{
int c, d;
c = n * a.m + m * a.n;
d = m * a.m;
return Fract(c, d);
}
Fract& operator += (Fract a)
{
Fract t = *this + a;
return *this = t;
}
private:
int n, m;
};
template <class T>
class Array
{
public:
Array(int n):l_(n) {}
void input(int a)
{
s.clear();
int mi=l_>a?a:l_;
if(typeid(T)!=typeid(double))
for(int i=0; i<mi; i++)
{
int a,b;
cin>>a>>b;
Fract fra(a,b);
s.push_back(fra);
}
else if(typeid(T)==typeid(double))
for(int i=0; i<mi; i++)
{
double j;
cin>>j;
s.push_back(j);
}
}
T& operator[](int a)
{
return s[a];
}
private:
vector<T> s;
int l_;
};
int main()
{
int n;
cin >> n;
Array<double> db(1000);
db.input(n);
double dbsum(0.0);
for(int i = 0; i < n; i++)
dbsum += db[i];
cout << dbsum << endl;
cin >> n;
Array<Fract> fr(1000);
fr.input(n);
Fract frsum(0, 1);
for(int i = 0; i < n; i++)
frsum += fr[i];
frsum.show();
}
相关分类