Problem S: Template array class for fractional classes

Source: Internet
Author: User

Problem S: Template array class for fractional classes time limit:3 Sec Memory limit:128 MB
submit:2155 solved:1624
[Submit] [Status] [Web Board] Description encapsulates a template array class array, supporting the operation: 1. constructor Array (int n) to initialize the array to n storage space; 2. function input (int n), reading up to n elements, but not exceeding the upper limit of the array storage space; 3. Overloads the subscript operator, which returns the elements of the array. Encapsulates a fractional class fract, used to handle fractional functions and operations, to support your array class use. 1. Construction: Pass in two parameters N and M, represent n/m, and fractions are converted to the simplest fraction at the time of construction. 2. Show () function: The fractional output is in the form of "A/b" or "-a/b", and A and B are unsigned integers. If A is 0 or B is 1, only symbols and molecules are output, not output "/" and denominator. 3. Reload the + = operator on the fractional class to perform the addition of fractions. -----------------------------------------------------------------------------you to design two classes: The array class and the Fract class, so that main () function to run and get the correct output. Call format See append.cc

Input inputs are two parts, namely a set of real-number test samples and a set of complex test samples. Both sets of sample samples are positive integer n, and N is less than 1000,n to indicate the need to enter n real numbers (or fractions). The second line of the test sample starts with n real numbers (or fractions). Each of these scores is entered as two integers n, m, indicating the fractional n/m.

Output the first part outputs a real number, which is the sum of the first set of sample samples; the second part outputs a fraction and is the sum of the second set of sample samples. When the fractional output is in the simplest form, the minus sign appears only at the front, and if the denominator is 1 or the numerator is 0, only one integer, the numerator part, without the "/" and the denominator portion is printed.

Sample Input
46 8 7 591 320-1580 150-9 16 612 16-33-486 110-10

  

Sample Output --17117/2640HINT Append codeappend.c, append.cc,
#include <iostream> #include <cmath> #include <vector>using namespace std;int yinshu (int x, int y) {int    mm    if (x>y) mm=y;    else mm=x;    int temp;    for (int i=1; i<=mm; i++) if (x%i==0&&y%i==0) temp=i; return temp;}    Template <typename t>class array{public:vector<t> num;    int Len; Array (int n): Len (n) {} void input (int n) {for (int i=0; i<n&&i<len; i++) {T T            Emp            cin>>temp;        Num.push_back (temp); }} T &operator[] (int n) {return num[n];}};    Class Fract{public:int N, M;    int t;        Fract (int x=0, int y=1) {int min_; if (x*y<0) t=-1;        else t=1;        int xx=x, yy=y;        if (xx<0) xx=-xx;        if (yy<0) yy=-yy; if (xx>yy) min_=yy;        else min_=xx;        for (int i=min_; i>1; i--) if (xx%i==0&&yy%i==0) {xx/=i; yy/=i; } n=xx*t;M=yy;        } Fract &operator+= (Fract &p) {Fract temp (n (p.m.) + (P.N) *m,m* (p.m.));    Return *this=temp;//must be returned itself.        } void Show () {if (n==0) cout<<0<<endl;        else if (m==1) cout<<n<<endl;    else cout<<n<< "/" <<m<<endl;        } Friend IStream &operator>> (IStream &is, Fract &p) {int x, y;        is>>x>>y;        Fract temp (x, y); P.N=TEMP.N;        p.m=temp.m;    return is;    }};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 ();}

  

Problem S: Template array class for fractional classes

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.