zoukankan      html  css  js  c++  java
  • 课程设计__分数的计算

    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
    ");
        printf("Please input your wants Fraction's operation
    ");
        char o;
        scanf("%c",&o);
        system("cls");
        switch (o)
        {
        case '+':
            printf("Please input two Fraction's molecule and imag
    ");
            a.setdata();
            b.setdata();
            c=a+b;
            c.display();
            break;
        case '-':
            printf("Please input two Fraction's molecule and imag
    ");
            a.setdata();
            b.setdata();
            c=a-b;
            c.display();
            break;
        case '*':
            printf("Please input two Fraction's molecule and imag
    ");
            a.setdata();
            b.setdata();
            c=a*b;
            c.display();
            break;
        case '/':
            printf("Please input two Fraction's molecule and imag
    ");
            a.setdata();
            b.setdata();
            c=a/b;
            c.display();
            break;
        }
        system("pause");
        return 0;
    }
  • 相关阅读:
    我的安全测试面试_自问自答,不亦乐乎
    Linux Shell 网络层监控脚本(监控包括:连接数、句柄数及根据监控反馈结果分析)
    netstat监控大量ESTABLISHED连接与Time_Wait连接问题
    详解 Spotlight on MySQL监控MySQL服务器
    详解 Spotlight on Unix 监控Linux服务器
    某听书网站系统漏洞,利用抓包拼接方式获取网站资源
    不懂得使用工具的测试不是好测试
    【好书摘要】性能优化中CPU、内存、磁盘IO、网络性能的依赖
    性能调优从哪里入手
    报文解析,从请求报文详细讲到响应码
  • 原文地址:https://www.cnblogs.com/TreeDream/p/5247486.html
Copyright © 2011-2022 走看看