Item four-Week 2

Source: Internet
Author: User
Tags greatest common divisor

#include <iostream>
using namespace Std;
Class CFraction
{
Private
int nume; Molecular
int Deno; Denominator
void simplify (); Simplification (so that numerator denominator has no common factor)
Public
cfraction (int nu = 0, int de = 1); constructor, initialized with
void set (int nu, int de); Change the value
void input (); Enter as "Nu/de" in the form "5/2"
void Amplify (int n); Enlarge n times, such as 2/3 magnification 5 times times 10/3
void output (int style);
Output: Take 8/6 For example, style is 0 o'clock, output is 8/6;
Style is 1 o'clock, the output of the post-simplification form 4/3;
Style is 2 o'clock, Output 1 (1/3) Form, indicating one and one-third;
Style is 3 o'clock, output in decimal form, such as 1.3333;
Default mode 0
};
cfraction::cfraction (int nu, int de)
{
Nume = Nu;
Deno = de;
}
void Cfraction::set (int nu = 0, int de = 1)
{
Nume = Nu;
Deno = de;
}
void Cfraction::input ()
{
cout << "Input in the form of numerator/denominator, as 5/2." << endl << "Please enter:";
Char A;
CIN >> Nume >> a >> deno;
while (A! = '/' | | Deno = = 0)
{
cout << "Input in the form of numerator/denominator, as 5/2." << Endl << "Please re-enter:";
CIN >> Nume >> a >> deno;
}
}
void Cfraction::simplify ()
{
if (Nume = = 0)
cout << "No need to simplify" << Endl;
else if (Nume = = Deno)
{
Nume = = Deno = = 1;
}
Else
{
int r, x = nume, y = Deno;
if (x > Y)
{
R = x;
x = y;
y = r;
}
R = 1;
while (r! = 0)
{
R = y%x;
y = x;
x = r;
}//greatest common divisor for Y
Nume = nume/y;
Deno = deno/y;
}
}
void cfraction::amplify (int n)
{
Nume = Nume*n;
Deno = Deno*n;
}
void cfraction::output (int style=0)
{
if (Nume = = 0)
cout<< "Results for:" << 0;
else if (Nume = = Deno)
{
cout << "results are:" << 1;
}
Else
{
Switch (style)
{
Case 0:cout << "Result:" << nume << '/' << Deno; Break
Case 1:{simplify (); output (); break;}
Case 2:
{
if (Nume <= Deno)
Output (1);
Else
{
int a=0;
while (Nume > Deno)
{
Nume = Nume-deno;
++a;
}
cout << "Result:" << a << "(" << nume << '/' << deno << ")" <<endl;
}
Break
}
Case 3:cout << "Result:" << (Double) Nume/(double) Deno << Endl;
}
}
}
int main ()
{
CFraction A;
A.input ();
int n;
cout << "Please enter magnification:";
CIN >> N;
while (n = = 0)
{
cout << "disallowed operation. Please re-enter the magnification:";
CIN >> N;
}
A.amplify (n);
A.output ();
cout << Endl << "Please enter the form of output you need (default is 0), take 8/6 for example:" << Endl << Endl;
cout << "0. Output 8/6" << Endl;
cout << "1. Post-Export form 4/3" << Endl;
cout << "2. Output 1 (1/3) Form, indicating one and One-third" << Endl;
cout << "3. Output in decimal form, e.g. 1.3333" << Endl;
CIN >> N;
while (n! = 0 && N! = 1 && n! = 2 && n! = 3)
{
cout << "Please select in 0--3:";
CIN >> N;
}
A.output (n);
while (1) {}
}

Item four-Week 2

Related Article

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.