c++ 使用结构体实现有理数库
程序员文章站
2022-07-10 18:30:35
...
问题描述
使用结构体设计实现有理数库。使用整数表示有理数的分子与分母,完成有理数的加减乘除与简化运算。
代码实现
刚学习C++,对知识掌握不全面,有问题欢迎各位纠正。
- 头文件 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);
- 库内容实现: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;
}
- 主调函数: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
执行结果:
推荐阅读
-
Python中使用装饰器和元编程实现结构体类实例
-
Python使用win32com模块实现数据库表结构自动生成word表格的方法
-
C语言开发函数库时对外接口隐藏库内结构体实现细节的方法
-
C++ 使用openssl库实现 DES 加密——CBC模式 && RSA加密——公加私解——私加公解
-
使用Python向C语言的链接库传递数组、结构体、指针类型的数据
-
Python中使用装饰器和元编程实现结构体类实例
-
c++ 使用结构体实现有理数库
-
c++使用librdkafka库实现kafka的消费实例
-
C++线性表通过结构体实现操作和结构体字符串快速排序和shell排序结合
-
c++使用Easyx图形库实现飞机大战