一个有理数(Rational)是由一个分子(numerator)和分母(denominator)组成的a/b形式的数。编写程序实现有理数如下功能: 包括三个文件Rational.h,Rational.cpp(30分),TestRationalClass .cpp(20分)。 Class Rational { Public: Rational(); Rational(long numerator,long denominator); //最简分式 Long getNumerator(); Long getDenominator(); Rational add(Rational &secondRational); Rational divide(Rational &secondRational); Int compareTo(Rational &secondRational); Bool equals(Rational &secondRational); Int intValue(); Double doubleValue(); String toString(); //分为分母=1与a/b的方式;提示:使用itoa()与strcat()函数 Private: Long numerator; Long denominator; Static long gcd(long n, long d); };
时间: 2023-03-08 16:55:51 浏览: 211
private); Rational subtract(Rational private); Rational multiply(Rational); Rational divide(Rational private); void printRational(); Private: Long numerator; Long denominator; };这是一个可以实现有理数功能的C++程序:Rational.h:#ifndef RATIONAL_H
#define RATIONAL_Hclass Rational
{
public:
Rational(); //默认构造函数
Rational(long numerator, long denominator); //最简分式
long getNumerator();
long getDenominator();
Rational add(Rational private);
Rational subtract(Rational private);
Rational multiply(Rational private);
Rational divide(Rational private);
void printRational();
private:
long numerator;
long denominator;
};#endif //RATIONAL_H
相关问题
写一个C++代码:一个有理想(Rational)是由一个分子(numerator)和分母(denominator)组成的a/b形式的数。编写程序实现有理数如下功能: 包括三个文件Rational.h,Rational.cpp(30分),TestRationalClass .cpp(20分)。
#include <iostream>
using namespace std;//Rational.h
class Rational {
private:
int numerator;
int denominator;
public:
Rational(int num, int den);
int getNumerator();
int getDenominator();
void setNumerator(int num);
void setDenominator(int den);
void add(Rational r);
void subtract(Rational r);
void multiply(Rational r);
void divide(Rational r);
void print();
};//Rational.cpp
Rational::Rational(int num, int den){
numerator = num;
denominator = den;
}int Rational::getNumerator(){
return numerator;
}int Rational::getDenominator(){
return denominator;
}void Rational::setNumerator(int num){
numerator = num;
}void Rational::setDenominator(int den){
denominator = den;
}void Rational::add(Rational r){
numerator = numerator*r.denominator + denominator*r.numerator;
denominator = denominator*r.denominator;
}void Rational::subtract(Rational r){
numerator = numerator*r.denominator - denominator*r.numerator;
denominator = denominator*r.denominator;
}void Rational::multiply(Rational r){
numerator = numerator*r.numerator;
denominator = denominator*r.denominator;
}void Rational::divide(Rational r){
numerator = numerator*r.denominator;
denominator = denominator*r.numerator;
}void Rational::print(){
cout<<numerator<<"/"<<denominator<<endl;
}//TestRationalClass.cpp
int main(){
Rational r1 = Rational(1,2);
Rational r2 = Rational(2,3);
r1.add(r2);
r1.print();
r1.subtract(r2);
r1.print();
r1.multiply(r2);
r1.print();
r1.divide(r2);
r1.print();
return 0;
}
Rational有理数(分数)1.分子numerator,分母denominator 2.构造方法2个3.生成getter和setter方法4.求两个分数的和5.ToString方法"1/2"
1. 分子numerator, 分母denominator
```java
public class Rational {
private int numerator;
private int denominator;
// 构造方法和其他方法略
}
```
2. 构造方法2个
```java
public Rational(int numerator, int denominator) {
this.numerator = numerator;
this.denominator = denominator;
}
public Rational(int numerator) {
this.numerator = numerator;
this.denominator = 1;
}
```
3. 生成getter和setter方法
```java
public int getNumerator() {
return numerator;
}
public void setNumerator(int numerator) {
this.numerator = numerator;
}
public int getDenominator() {
return denominator;
}
public void setDenominator(int denominator) {
this.denominator = denominator;
}
```
4. 求两个分数的和
```java
public Rational add(Rational other) {
int newNumerator = this.numerator * other.denominator + other.numerator * this.denominator;
int newDenominator = this.denominator * other.denominator;
return new Rational(newNumerator, newDenominator);
}
```
5. ToString方法"1/2"
```java
@Override
public String toString() {
return numerator + "/" + denominator;
}
```
阅读全文