当前位置:编程学习 > C/C++ >>

C++程序—分数的加减乘除

求完整的程序代码,运用函数重载。尽量简洁一点的。采纳后另有追加分数100.一定要快点啊!

答案://类的头文件fclass.h

class fraction
{
private:
 int nr;                 //分子
 int dn;                 //分母
public:
 fraction input();
 fraction operator +(fraction &);
 fraction operator -(fraction &);
 fraction operator *(fraction &);
    fraction operator /(fraction &);
 void output();  //输出约分后的分数
 void output1(); // 输出普通分数
};

 

//实现文件fclass.cpp

#include<iostream.h>
#include"fclass.h"
fraction fraction::operator+(fraction &a)
{
 fraction g;
 g.dn=dn*a.dn;
 g.nr=nr*a.dn+a.nr*dn;
 return g;
}
fraction fraction::operator-(fraction &a)
{
 fraction g;
 g.dn=dn*a.dn;
 g.nr=nr*a.dn-a.nr*dn;
 return g;
}
fraction fraction::operator*(fraction &a)
{
 fraction g;
 g.dn=dn*a.dn;
 g.nr=nr*a.nr;
 return g;
}
fraction fraction::operator/(fraction &a)
{
 fraction g;
 g.dn=dn*a.nr;
 g.nr=nr*a.dn;
 return g;
}
fraction fraction::input()

 fraction a;
 cin>>a.nr>>a.dn;
 return a;
}
void fraction::output()
{
 int i;
 if(dn==0)
  cout<<"error"<<endl;
 else
  if(nr%dn==0)
   cout<<nr/dn<<endl;
  else
  {
   for(i=2;i<=nr||i<=dn;)
   {
    if(nr%i==0&&dn%i==0)
    {
     nr=nr/i;
     dn=dn/i;
     i=2;
    }
    else
     i++;
   }
   cout<<nr <<"/"<<dn<<endl;
  }
}
void fraction::output1()
{
 cout<<nr<<"/"<<dn;
}
//主程序

#include<iostream.h>
#include"fclass.h"
void menu()
{
 cout<<"=================================================="<<endl;
 cout<<"1-输入2个新的分数,可进行如下运算:"<<endl;
 cout<<"2-加法"<<endl;
 cout<<"3-减法"<<endl;
 cout<<"4-乘法"<<endl;
 cout<<"5-除法"<<endl;
 cout<<"6-退出"<<endl;
 cout<<"=================================================="<<endl;
}

void main()
{
 void menu(); 
 int m; 
 bool flag=true;
 fraction s1,s2,s3;
 while(flag)
 { menu();
  cin>>m;
  switch(m)
  {
  case 1:
   cout<<"请按照如下方式输入2个分数: 分子/分母"<<endl;   
   s1=s1.input();
   s2=s2.input();
   cout<<"请输入你要进行的计算:"<<endl;
   break;
  case 2:
   s3=s1+s2;
   s1.output1();
   cout<<" + ";
   s2.output1();
   cout<<"=";
   s3.output();
   break;
  case 3:
   s3=s1-s2;
   s1.output1();
   cout<<" - ";
   s2.output1();
   cout<<"=";
   s3.output();
   break;
  case 4:
   s3=s1*s2;
   s1.output1();
   cout<<" * ";
   s2.output1();
   cout<<"=";
   s3.output();
   break;
  case 5:
   s3=s1/s2;
   s1.output1();
   cout<<" / ";
   s2.output1();
   cout<<"=";
   s3.output();
   break;
  case 6:
   flag=false;
   break;
  default:
   break;
  }
 }
 

}

 

//这是分数操作类的定义代码:保存为Fraction.h文件
#include<iostream.h>
#include<stdlib.h>
#include<string.h>

class Fraction
{
public:
    Fraction();
    friend const Fraction operator +(const Fraction& lhs,const Fraction& rhs);
    friend const Fraction operator - (const Fraction& lhs,const Fraction& rhs);
    friend const Fraction operator / (const Fraction& lhs,const Fraction& rhs);  
    friend const Fraction operator * (const Fraction& lhs,const Fraction& rhs);  
    long Gcd(long num,long deno);//求最大公约数
    void Print();//输出函数
    void Simplify();//分数化简
    void Read();
    int StrIndex(char s[],int pos,char t);
    char * SubString(char sub[],char s[],unsigned int pos,unsigned int len);

private:
    double numerator;//分子
    double denominator;//分母
    char arr[50];
    char arr1[50];
    char arr2[50];    
};

Fraction::Fraction()
{
    this->numerator = 1;
    this->denominator = 1;
}

const Fraction  operator+(const Fraction& lhs,const Fraction& rhs)
{
    Fraction newFrac;

    if(lhs.denominator == 0 || rhs.denominator == 0)
    {
        cout<<"存在分母为0的分数"<<endl;
        exit(-1);
    }  
    newFrac.numerator = (lhs.numerator * rhs.denominator) + (rhs.numerator * lhs.denominator);    
    newFrac.denominator = lhs.denominator * rhs.denominator;
    
    newFrac.Simplify();
    return newFrac;
}

const Fraction operator -(const Fraction& lhs,const Fraction& rhs)
{
    Fraction newFrac;

    if(lhs.denominator == 0 || rhs.denominator == 0)
    {       
        cout<<"存在分母为0的分数"<<endl;
        exit(-1);
    }  

    newFrac.numerator = rhs.denominator * lhs.numerator - rhs.numerator * lhs.denominator;
    newFrac.denominator = lhs.denominator * rhs.denominator;

    newFrac.Simplify();
    return newFrac;
}

const Fraction operator *(const Fraction &lhs,const Fraction &rhs)
{
    if((lhs.denominator * rhs.denominator) == 0)
    {
        cout<<"存在分母为零的分数"<<endl;
        exit(-1);
    }
    else
    {
        Fraction newFrac;
        newFrac.denominator = lhs.denominat

上一个:关于斐波那契数列的C++课程设计
下一个:C++函数数组做参数调用问题

CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,