Tutorial 11: Problem B: score type conversion, 11. problem

Source: Internet
Author: User

Tutorial 11: Problem B: score type conversion, 11. problem

Home Web Board ProblemSet Standing Status Statistics
  Problem B: score type conversion Problem B: score class type conversion Time Limit: 3 Sec Memory Limit: 128 MB
Submit: 579 Solved: 431
[Submit] [Status] [Web Board] Description encapsulates a score class Fract to process score functions and operations. The following operations are supported: 1. constructor: input two parameters n and m, indicating n/m. The score is converted to the simplest score immediately during the constructor. 2. show () function: The score output is in the form of "a/B" or "-a/B". a and B are all unsigned integers. If a is 0 or B is 1, only the symbols and molecules are output, and "/" and denominator are not output. 3. double type conversion function: returns the decimal number by dividing the numerator by the denominator. Note: When the numerator is 0, do not output "-0" ----------------------------------------------------------------------you design a Fract class so that the main () function can be run and output properly. For the call format, see append. cc.

 

Input Multiple rows. Each row has two integers, the numerator and the denominator, respectively, until the end of EOF. The input denominator is not 0;

 

Each row of Output outputs a real number and a score, consistent with the input order. The real number is obtained by dividing the numerator by the denominator. The score is output in the simplest form, and the negative sign only appears at the beginning. If the denominator is 1 or the numerator is 0, only one integer is output, that is, the numerator part, there is no "/" or denominator.

 

Sample Input1 320-1580 150-9 16 612 16-33-486 110-10 Sample Output0.333333 1/3-1.33333-4/30. 533333 8/15-9-91 10.75 3/40. 6875 11/160. 545455 6/110 0 HINT Append Codeappend. c, append. cc, [Submit] [Status] [Web Board]

Please refer to the following link for more information:
All Copyright Reserved 2010-2011 SDUSTOJ TEAM
GPL2.0 2003-2011 HUSTOJ Project TEAM
Anything about the Problems, Please Contact Admin: admin

#include<iostream>#include<cstdio>using namespace std;int cccc(int a,int b){    if(!b)        return a;    return cccc(b,a%b);}class Fract{public:    int n,m;    Fract(int a,int b):n(a),m(b)    {        int Max,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;        }    }    void show()    {        if(n==0||m==1)            cout<<n<<endl;        else        {            cout<<n<<"/"<<m<<endl;        }    }    operator double()    {        return (double)n/m;    }};int main(){    int n, m;    while(cin >> n >> m)    {        Fract fr(n, m);        cout << (double)fr << " ";        fr.show();    }}

 

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.