In the default constructor, the default value of the denominator cannot be 0!!
| Home |
Web Board |
Problemset |
Standing |
Status |
Statistics |
Problem D: Template array class for fractional classes Problem D: Template array class for fractional classes time limit: 3 Sec Memory Limit: MB
Submit: 509 Solved: 350
[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 Input8 7 591 320-1580 150-9 612 16-33-486 110-10Sample Output26-17117/2640HINT Append codeappend.c, append.cc, [Submit] [Status] [Web Board]
??? < Chinese????? 中文版???
All Copyright Reserved 2010-2011 sdustoj TEAM
GPL2.0 2003-2011 hustoj Project TEAM
Anything about the problems admin:admin
#include <iostream>#include<vector>#include<typeinfo>using namespacestd;template<typename t>classarray{ Public: Vector<T>A; Array (intN) {}voidInputintN) { for(intI=0; i<n; i++) {T tmp; CIN>>tmp; A.push_back (TMP); }} T&operator[](intN) {returnA[n]; }};intCcccintAintb) { if(!b)returnA; returnCCCC (b,a%b);}classfract{ Public: intn,m; Fract (intA=0,intb=1): N (a), M (b) {intMax,min,c,x,y; if(a<0) a*=-1; if(b<0) b*=-1; C=CCCC (Max (A, b), Min (A, b)); if(m<0) {n*=-1; M*=-1; } if(c!=1) {n/=C; M/=C; } } voidShow () {if(n==0|| m==1) cout<<n<<Endl; Else{cout<<n<<"/"<<m<<Endl; }} Fract&operator+ = (Fract &x) {Fract y (X.N*m+x.m*n,x.m*m); return* This=y; } operator Double() { return(Double) n/m*1.0; } Friend IStream&operator>> (IStream & is, Fract &f) { is>>f.n>>f.m; return is; }}; Fractoperator*(Fract f1,fract f2) {Fract f (f1.n*f2.n,f1.m*f2.m); returnF;}intMain () {intN; CIN>>N; Array<Double> db ( +); Db.input (n); DoubleDbsum (0.0); for(inti =0; I < n; i++) Dbsum+=Db[i]; cout<< Dbsum <<Endl; CIN>>N; Array<Fract> FR ( +); Fr.input (n); Fract Frsum (0,1); for(inti =0; I < n; i++) Frsum+=Fr[i]; Frsum.show ();}
Experiment 11:problem D: Template array class for fractional classes