欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

c++ 使用结构体实现有理数库

程序员文章站 2022-07-10 18:30:35
...

c++ 使用结构体实现有理数库

问题描述

使用结构体设计实现有理数库。使用整数表示有理数的分子与分母,完成有理数的加减乘除与简化运算。

代码实现

刚学习C++,对知识掌握不全面,有问题欢迎各位纠正。

  1. 头文件 rational_number.h
struct rational_number
{
    int numerator;   //分子
    int denominator; //分母
};
rational_number create();
void show_rational_number(rational_number x);
rational_number add(rational_number x, rational_number y);
rational_number sub(rational_number x, rational_number y);
rational_number multi(rational_number x, rational_number y);
rational_number div(rational_number x, rational_number y);
rational_number simplify(rational_number x);
  1. 库内容实现:rational_number.cpp
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include "rational_number.h"

using namespace std;

rational_number create();
void show_rational_number(rational_number x);
int get_gcd(int x, int y);
rational_number simplify(rational_number x);
rational_number add(rational_number x, rational_number y);
rational_number sub(rational_number x, rational_number y);
rational_number multi(rational_number x, rational_number y);
rational_number div(rational_number x, rational_number y);

/*输入有理数*/
rational_number create()
{
    rational_number x;
    cout << "Input number for numerator and denominator, like '1 2' means 1/2: ";
    cin >> x.numerator >> x.denominator;
    if (x.denominator == 0)
    {
        cout << "denominator value error, it cannot be 0!" << endl;
        exit(1);
    }
    //如果分母为负数,统一将符号给分子,分母设置为正数
    if (x.denominator < 0)
    {
        x.numerator *= (-1);
        x.denominator *= (-1);
    }
    show_rational_number(x);
    cout << endl;
    return x;
}

void show_rational_number(rational_number x)
{
    cout << x.numerator << "/" << x.denominator;
}

/*欧式算法求最大公约数*/
int get_gcd(int x, int y)
{
    x = x < 0 ? (-1 * x) : x;
    y = y < 0 ? (-1 * y) : y;
    unsigned int r = x % y;
    while (r != 0)
    {
        x = y;
        y = r;
        r = x % y;
    }
    return y;
}

/*简化有理数*/
rational_number simplify(rational_number x)
{
    rational_number r;
    int t = get_gcd(x.numerator, x.denominator);
    if (t)
    {
        r.numerator = x.numerator / t;
        r.denominator = x.denominator / t;
    }
    return r;
}

rational_number add(rational_number x, rational_number y)
{
    rational_number result;
    x = simplify(x);
    y = simplify(y);
    result.numerator = x.numerator * y.denominator + y.numerator * x.denominator;
    result.denominator = x.denominator * y.denominator;
    result = simplify(result);
    return result;
}
rational_number sub(rational_number x, rational_number y)
{
    rational_number result;
    x = simplify(x);
    y = simplify(y);
    result.numerator = x.numerator * y.denominator - y.numerator * x.denominator;
    result.denominator = x.denominator * y.denominator;
    result = simplify(result);
    return result;
}
rational_number multi(rational_number x, rational_number y)
{
    rational_number result;
    x = simplify(x);
    y = simplify(y);
    result.numerator = x.numerator * y.numerator;
    result.denominator = x.denominator * y.denominator;
    result = simplify(result);
    return result;
}
rational_number div(rational_number x, rational_number y)
{
    rational_number result;
    x = simplify(x);
    y = simplify(y);
    result.numerator = x.numerator * y.denominator;
    result.denominator = x.denominator * y.numerator;
    result = simplify(result);
    return result;
}
  1. 主调函数:main.cpp
#include <iostream>
#include "rational_number.h"

using namespace std;

int main()
{
    rational_number x, y;
    x = create();
    y = create();
    cout << "x: ";
    show_rational_number(x);
    cout << " after simplify: ";
    show_rational_number(simplify(x));
    cout << endl;

    cout << "y: ";
    show_rational_number(y);
    cout << " after simplify: ";
    show_rational_number(simplify(y));
    cout << endl;

    cout << "x + y = ";
    show_rational_number(add(x, y));
    cout << endl;

    cout << "x - y = ";
    show_rational_number(sub(x, y));
    cout << endl;

    cout << "x * y = ";
    show_rational_number(multi(x, y));
    cout << endl;

    cout << "x / y = ";
    show_rational_number(div(x, y));
    cout << endl;

    return 0;
}

使用方法

使用g++ 命令编译并调用:

g++ main.cpp rational_number.cpp -o main.exe
./main.exe

执行结果:
c++ 使用结构体实现有理数库

相关标签: c++ c++ linux