码迷,mamicode.com
首页 > 其他好文 > 详细

分数的计算

时间:2016-03-06 15:34:00      阅读:159      评论:0      收藏:0      [点我收藏+]

标签:

1、操作符重载

2、类的封装

#include <stdio.h>
#include <algorithm>
#include <iostream>

using namespace std;

int gcd(int m,int n)///求最大公约数
{
    if(n==0)
        return m;
    else return gcd(n,m%n);
}

class Fraction
{
public:
    Fraction();
    Fraction(int a,int b):molecule(a),denominator(b) {}
    void setdata();
    void display();
    Fraction operator *(Fraction &a)
    {
        Fraction c;
        int temp1;///分子
        int temp2;///分母
        int L;///最大公约数
        temp1=this->molecule*a.molecule;
        temp2=this->denominator*a.denominator;
        L=gcd(temp1,temp2);
        c.molecule=temp1/L;
        c.denominator=temp2/L;
        return c;
    }
    Fraction operator +(Fraction &a)
    {
        Fraction c;
        int temp1;///分子
        int temp2;///分母
        int L;///最大公约数
        temp1=this->molecule*a.denominator+this->denominator*a.molecule;
        temp2=this->denominator*a.denominator;
        L=gcd(temp1,temp2);
        c.molecule=temp1/L;
        c.denominator=temp2/L;
        return c;
    }
    Fraction operator -(Fraction &a)
    {
        Fraction c;
        int temp1;///分子
        int temp2;///分母
        int L;///最大公约数
        temp1=this->molecule*a.denominator-this->denominator*a.molecule;
        temp2=this->denominator*a.denominator;
        L=gcd(temp1,temp2);
        c.molecule=temp1/L;
        c.denominator=temp2/L;
        return c;
    }
    Fraction operator /(Fraction &a)
    {
        Fraction c;
        int temp1;///分子
        int temp2;///分母
        int L;///最大公约数
        temp1=this->molecule*a.denominator;
        temp2=this->denominator*a.molecule;
        L=gcd(temp1,temp2);
        c.molecule=temp1/L;
        c.denominator=temp2/L;
        return c;
    }
private:
    int molecule;///分子
    int denominator;///分母
};

Fraction::Fraction()
{
    molecule=0;
    denominator=1;
}

void Fraction::setdata()
{
    cin>>molecule>>denominator;
}

void Fraction::display()
{
    cout<<molecule<<"/"<<denominator<<endl;
}

int main()
{
    Fraction a,b,c;
    printf("This is Fraction‘s operation\n");
    printf("Please input your wants Fraction‘s operation\n");
    char o;
    scanf("%c",&o);
    system("cls");
    switch (o)
    {
    case +:
        printf("Please input two Fraction‘s molecule and imag\n");
        a.setdata();
        b.setdata();
        c=a+b;
        c.display();
        break;
    case -:
        printf("Please input two Fraction‘s molecule and imag\n");
        a.setdata();
        b.setdata();
        c=a-b;
        c.display();
        break;
    case *:
        printf("Please input two Fraction‘s molecule and imag\n");
        a.setdata();
        b.setdata();
        c=a*b;
        c.display();
        break;
    case /:
        printf("Please input two Fraction‘s molecule and imag\n");
        a.setdata();
        b.setdata();
        c=a/b;
        c.display();
        break;
    }
    system("pause");
    return 0;
}

 

分数的计算

标签:

原文地址:http://www.cnblogs.com/TreeDream/p/5247486.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!