简介
一个计算器程序。
目前支持的功能:
- 最多一元运算
- 加、减、乘、除
- 整数指数幂
以后可能会设计的功能:
- 高精度计算
- 读入函数、字母常数
- 分数指数幂(根式)
- 平面几何计算
目前计算器的功能还比较简陋,今后还会不断优化、编写新功能,
力求为各位和我一样的学生党提供解题的便利 (便于抄答案) 。
大家发现什么问题,或是有什么建议,都可以在评论区提出,
编写程序还是花了不少心思的,各位点个赞支持下吧~
代码
main.cpp
#include <iostream>
#include "Algebratools.h"
Calculator Main = {};
std::string expr = "";
Fraction Result = {};
void Help();
int main() {
puts("使用方法:");
puts("1. 指定未知数名称(只由大小写字母构成,字数任意,不允许空格)");
puts(" 此时输入“*”了解更多关于此程序的信息");
puts(" 输入“~”以退出程序\n");
puts("2. 输入待计算的代数式(允许空格,请注意运算优先级,只允许使用小括号)");
puts(" 目前只支持单元的加、减、乘、除、整数指数幂;");
puts(" 不支持多元运算、分数/未知数指数幂、对数、读入字母常数(如π、e)、读入函数、微积分运算等等操作\n");
puts("3. 查看计算结果\n");
Main.init_priority();
while (true) {
puts("未知数:");
std::cin >> Main.variable;
if (Main.variable == "~") break;
if (Main.variable == "*") Help();
else {
puts("代数式:");
getchar();
getline(std::cin, expr);
Result = Main.Calculate(expr);
Main.Print(Result);
puts("");
}
}
return 0;
}
void Help() {
puts("关于程序的更多信息,以 Q & A 形式呈现\n");
puts("Q1:以后计划做多元运算功能吗?");
puts("A1:并没有……多元整式无法辗转相除求最大公因式(涉及到抽象代数,太高深了我看不懂,但结论就是这样),\n因此一般的多元分式无法约分\n");
puts("Q2:那么分数指数幂呢?");
puts("A2:目前的计划是再声明一个 Radical(根式)结构体,正在学习重因式,应该对化简程序的设计有帮助\n");
puts("Q3:为什么无法实现未知数指数幂、对数、三角函数?");
puts("A3:它们是超越函数,无法用有限次加、减、乘、除、乘方、开方运算表示(貌似涉及无穷级数求和,也正在学习)\n");
puts("Q4:字母常数和函数的读入可以实现吗?");
puts("A4:这个实现起来还是蛮简单的~大家敬请期待吧\n");
}
Algebratools.h
#pragma once
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <stack>
#include <unordered_map>
long long gcd(long long a, long long b);
long long lcm(long long a, long long b);
long long quick_pow(long long a, long long b);
struct Num {
long long numerator;
long long denominator;
bool operator ==(const Num& N)const {return numerator == N.numerator && denominator == N.denominator;}
bool operator <(const Num& N)const {
return (denominator * N.denominator > 0 ?
(numerator * N.denominator < denominator * N.numerator)
: (numerator * N.denominator > denominator * N.numerator));
}
bool operator >(const Num& N)const {
return (denominator * N.denominator > 0 ?
(numerator * N.denominator > denominator * N.numerator)
: (numerator * N.denominator < denominator * N.numerator));
}
bool operator <=(const Num& N)const {
return numerator == N.numerator && denominator == N.denominator
|| (denominator * N.denominator > 0 ?
(numerator * N.denominator < denominator * N.numerator)
: (numerator * N.denominator > denominator * N.numerator));
}
bool operator >=(const Num& N)const {
return numerator == N.numerator && denominator == N.denominator
|| (denominator * N.denominator > 0 ?
(numerator * N.denominator > denominator * N.numerator)
: (numerator * N.denominator < denominator * N.numerator));
}
bool operator !=(const Num& N)const {return numerator != N.numerator || denominator != N.denominator;}
};
bool IsInteger(Num N);
Num operator +(Num A, Num B);
Num AddInvert(Num N);
Num operator -(Num A, Num B);
Num operator *(long long A, Num B);
Num operator *(Num A, Num B);
Num MulInvert(Num N);
Num operator /(Num A, Num B);
Num Simplify(Num N);
const Num Num_AddIdent = { 0, 1 };
const Num Num_MulIdent = { 1, 1 };
struct Monomial {
Num coefficient;
Num exponent;
bool operator ==(const Monomial& M)const {return exponent == M.exponent;}
bool operator <(const Monomial& M)const {return exponent < M.exponent;}
bool operator >(const Monomial& M)const {return exponent > M.exponent;}
bool operator <=(const Monomial& M)const {return exponent <= M.exponent;}
bool operator >=(const Monomial& M)const {return exponent >= M.exponent;}
bool operator !=(const Monomial& M)const {return exponent != M.exponent;}
};
bool Congruent(Monomial A, Monomial B);
bool IsConstant(Monomial M);
bool IsInteger(Monomial M);
Monomial operator +(Monomial A, Monomial B);
Monomial AddInvert(Monomial M);
Monomial operator -(Monomial A, Monomial B);
Monomial operator *(long long A, Monomial B);
Monomial operator *(Num A, Monomial B);
Monomial operator *(Monomial A, Monomial B);
Monomial MulInvert(Monomial M);
Monomial operator /(Monomial A, Monomial B);
Monomial Simplify(Monomial M);
const Monomial Monomial_AddIdent = { Num_AddIdent, Num_AddIdent };
const Monomial Monomial_MulIdent = { Num_MulIdent, Num_AddIdent };
const Monomial Monomial_VarIdent = { Num_MulIdent, Num_MulIdent };
struct Polynomial {
std::vector<Monomial> term;
bool operator ==(const Polynomial& P)const {return term[term.size() - 1] == P.term[P.term.size() - 1];}
bool operator <(const Polynomial& P)const {return term[term.size() - 1] < P.term[P.term.size() - 1];}
bool operator >(const Polynomial& P)const {return term[term.size() - 1] > P.term[P.term.size() - 1];}
bool operator <=(const Polynomial& P)const {return term[term.size() - 1] <= P.term[P.term.size() - 1];}
bool operator >=(const Polynomial& P)const {return term[term.size() - 1] >= P.term[P.term.size() - 1];}
bool operator !=(const Polynomial& P)const {return term[term.size() - 1] != P.term[P.term.size() - 1];}
};
bool Congruent(Polynomial A, Polynomial B);
bool IsConstant(Polynomial P);
bool IsInteger(Polynomial P);
Polynomial operator +(Polynomial A, Polynomial B);
Polynomial AddInvert(Polynomial P);
Polynomial operator -(Polynomial A, Polynomial B);
Polynomial operator *(long long A, Polynomial B);
Polynomial operator *(Num A, Polynomial B);
Polynomial operator *(Monomial A, Polynomial B);
Polynomial operator *(Polynomial A, Polynomial B);
Polynomial Simplify(Polynomial P);
const Polynomial Polynomial_AddIdent = { {Monomial_AddIdent} };
const Polynomial Polynomial_MulIdent = { {Monomial_MulIdent} };
const Polynomial Polynomial_VarIdent = { {Monomial_VarIdent} };
Polynomial operator /(Polynomial A, Polynomial B);
Polynomial operator %(Polynomial A, Polynomial B);
Polynomial Div(Polynomial A, Polynomial B, Polynomial& r);
Polynomial gcd(Polynomial A, Polynomial B);
struct Fraction {
Polynomial Numerator;
Polynomial Denominator;
};
bool Congruent(Fraction A, Fraction B);
bool IsConstant(Fraction F);
bool IsInteger(Fraction F);
Fraction operator +(Fraction A, Fraction B);
Fraction AddInvert(Fraction F);
Fraction operator -(Fraction A, Fraction B);
Fraction operator *(long long A, Fraction B);
Fraction operator *(Num A, Fraction B);
Fraction operator *(Monomial A, Fraction B);
Fraction operator *(Polynomial A, Fraction B);
Fraction operator *(Fraction A, Fraction B);
Fraction MulInvert(Fraction F);
Fraction operator /(Fraction A, Fraction B);
Fraction Simplify(Fraction F);
const Fraction Fraction_AddIdent = { Polynomial_AddIdent, Polynomial_MulIdent };
const Fraction Fraction_MulIdent = { Polynomial_MulIdent, Polynomial_MulIdent };
const Fraction Fraction_VarIdent = { Polynomial_VarIdent, Polynomial_MulIdent };
struct Calculator {
std::string variable;
std::unordered_map<char, int> priority;
long long number; bool decimal; int accuracy;
std::string str;
bool parity; Fraction Abs;
std::stack<Fraction> nums;
std::stack<char> ops;
std::string expr;
int state;
void init_priority();
bool is_constant(char c);
bool is_string(char c);
bool is_operator(char c);
Fraction read_constant(int& i);
std::string read_string(int& i);
void Operate();
Fraction Calculate(std::string expression);
void PrintCoefficient(bool highest, bool constant, Num Coefficient);
void PrintExponent(Num Exponent);
void PrintSubPolynomial(Polynomial P);
void Print(Fraction F);
};
Algebratools.cpp
#include "Algebratools.h"
long long gcd(long long a, long long b) {
return b == 0 ? a : gcd(b, a % b);
}
long long lcm(long long a, long long b) {
return a / gcd(a, b) * b;
}
long long quick_pow(long long a, long long b) {
long long c = 1;
while (b > 0) {
if (b & 1) c *= a;
a *= a;
b >>= 1;
}
return c;
}
bool IsInteger(Num N) {
return N.denominator == 1;
}
Num operator +(Num A, Num B) {
Num C = {};
C.numerator = A.numerator * B.denominator + A.denominator * B.numerator;
C.denominator = A.denominator * B.denominator;
C = Simplify(C);
return C;
}
Num AddInvert(Num N) {
Num res = {};
res.numerator = -N.numerator;
res.denominator = N.denominator;
return res;
}
Num operator -(Num A, Num B) {
return A + AddInvert(B);
}
Num operator *(long long A, Num B) {
Num C = {};
C.numerator = A * B.numerator;
C.denominator = B.denominator;
C = Simplify(C);
return C;
}
Num operator *(Num A, Num B) {
Num C = {};
C.numerator = A.numerator * B.numerator;
C.denominator = A.denominator * B.denominator;
C = Simplify(C);
return C;
}
Num MulInvert(Num N) {
Num res = {};
res.numerator = N.denominator;
res.denominator = N.numerator;
return res;
}
Num operator /(Num A, Num B) {
return A * MulInvert(B);
}
Num Simplify(Num N) {
Num res = {};
long long d = gcd(N.numerator, N.denominator);
res.numerator = N.numerator / d;
res.denominator = N.denominator / d;
if (res.denominator < 0) {
res.numerator = -res.numerator;
res.denominator = -res.denominator;
}
return res;
}
bool Congruent(Monomial A, Monomial B) {
return A.coefficient == B.coefficient && A.exponent == B.exponent;
}
bool IsConstant(Monomial M) {
return M.exponent == Num_AddIdent;
}
bool IsInteger(Monomial M) {
return IsInteger(M.coefficient) && M.exponent== Num_AddIdent;
}
Monomial operator +(Monomial A, Monomial B) {
Monomial C = {};
C.coefficient = A.coefficient + B.coefficient;
C.exponent = A.exponent;
Simplify(C);
return C;
}
Monomial AddInvert(Monomial M) {
Monomial res = {};
res.coefficient = AddInvert(M.coefficient);
res.exponent = M.exponent;
return res;
}
Monomial operator -(Monomial A, Monomial B) {
return A + AddInvert(B);
}
Monomial operator *(long long A, Monomial B) {
Monomial C = {};
C.coefficient = A * B.coefficient;
C.exponent = B.exponent;
Simplify(C);
return C;
}
Monomial operator *(Num A, Monomial B) {
Monomial C = {};
C.coefficient = A * B.coefficient;
C.exponent = B.exponent;
Simplify(C);
return C;
}
Monomial operator *(Monomial A, Monomial B) {
Monomial C = {};
C.coefficient = A.coefficient * B.coefficient;
C.exponent = A.exponent + B.exponent;
Simplify(C);
return C;
}
Monomial MulInvert(Monomial M) {
Monomial res = {};
res.coefficient = MulInvert(M.coefficient);
res.exponent = AddInvert(M.exponent);
return res;
}
Monomial operator /(Monomial A, Monomial B) {
return A * MulInvert(B);
}
Monomial Simplify(Monomial M) {
Monomial res = {};
res.coefficient = M.coefficient;
if (res.coefficient == Num_AddIdent) res.exponent = Num_AddIdent;
else res.exponent = M.exponent;
return res;
}
bool Congruent(Polynomial A, Polynomial B) {
if (A.term.size() != B.term.size()) return false;
for (int i = 0; i < A.term.size(); i++)
if (!Congruent(A.term[i], B.term[i]))
return false;
return true;
}
bool IsConstant(Polynomial P) {
return IsConstant(P.term[P.term.size() - 1]);
}
bool IsInteger(Polynomial P) {
return IsInteger(P.term[P.term.size() - 1]);
}
Polynomial operator +(Polynomial A, Polynomial B) {
Polynomial C = {};
for (int i = 0; i < A.term.size(); i++) C.term.push_back(A.term[i]);
for (int i = 0; i < B.term.size(); i++) C.term.push_back(B.term[i]);
C = Simplify(C);
return C;
}
Polynomial AddInvert(Polynomial P) {
Polynomial res = {};
for (int i = 0; i < P.term.size(); i++) res.term.push_back(AddInvert(P.term[i]));
return res;
}
Polynomial operator -(Polynomial A, Polynomial B) {
return A + AddInvert(B);
}
Polynomial operator *(long long A, Polynomial B) {
Polynomial C = {};
for (int i = 0; i < B.term.size(); i++) C.term.push_back(A * B.term[i]);
Simplify(C);
return C;
}
Polynomial operator *(Num A, Polynomial B) {
Polynomial C = {};
for (int i = 0; i < B.term.size(); i++) C.term.push_back(A * B.term[i]);
Simplify(C);
return C;
}
Polynomial operator *(Monomial A, Polynomial B) {
Polynomial C = {};
for (int i = 0; i < B.term.size(); i++) C.term.push_back(A * B.term[i]);
Simplify(C);
return C;
}
Polynomial operator *(Polynomial A, Polynomial B) {
Polynomial C = {};
for (int i = 0; i < A.term.size(); i++)
for (int j = 0; j < B.term.size(); j++)
C.term.push_back(A.term[i] * B.term[j]);
C = Simplify(C);
return C;
}
Polynomial Simplify(Polynomial P) {
Polynomial res = {};
Monomial M = Monomial_AddIdent;
sort(P.term.begin(), P.term.end());
for (int i = 0; i < P.term.size(); i++)
if (P.term[i] == M) M = M + P.term[i];
else {
M = Simplify(M);
if (!Congruent(M, Monomial_AddIdent)) res.term.push_back(M);
M = P.term[i];
}
M = Simplify(M);
if (!Congruent(M, Monomial_AddIdent) || res.term.empty()) res.term.push_back(M);
return res;
}
Polynomial operator /(Polynomial A, Polynomial B) {
Polynomial C = {};
Monomial Q = {};
if (Congruent(A, Polynomial_AddIdent) || A < B) return Polynomial_AddIdent;
while (!Congruent(A, Polynomial_AddIdent) && A >= B) {
Q = A.term[A.term.size() - 1] / B.term[B.term.size() - 1];
C.term.push_back(Q);
A = A - (Q * B);
}
reverse(C.term.begin(), C.term.end());
return C;
}
Polynomial operator %(Polynomial A, Polynomial B) {
return A - ((A / B) * B);
}
Polynomial Div(Polynomial A, Polynomial B, Polynomial& r) {
Polynomial C = {};
Monomial Q = {};
while (A >= B) {
Q = A.term[0] / B.term[0];
C.term.push_back(Q);
A = A - (Q * B);
}
r = A;
return C;
}
Polynomial gcd(Polynomial A, Polynomial B) {
return Congruent(B, Polynomial_AddIdent) ? A : gcd(B, A % B);
}
bool Congruent(Fraction A, Fraction B) {
return Congruent(A.Numerator, B.Numerator) && Congruent(A.Denominator, B.Denominator);
}
bool IsConstant(Fraction F) {
return IsConstant(F.Numerator) && IsConstant(F.Denominator);
}
bool IsInteger(Fraction F) {
return IsInteger(F.Numerator) && Congruent(F.Denominator, Polynomial_MulIdent);
}
Fraction operator +(Fraction A, Fraction B) {
Fraction C = {};
C.Numerator = (A.Numerator * B.Denominator) + (A.Denominator * B.Numerator);
C.Denominator = A.Denominator * B.Denominator;
C = Simplify(C);
return C;
}
Fraction AddInvert(Fraction F) {
Fraction res = {};
res.Numerator = AddInvert(F.Numerator);
res.Denominator = F.Denominator;
return res;
}
Fraction operator -(Fraction A, Fraction B) {
return A + AddInvert(B);
}
Fraction operator *(long long A, Fraction B) {
Fraction C = {};
C.Numerator = A * B.Numerator;
C.Denominator = B.Denominator;
C = Simplify(C);
return C;
}
Fraction operator *(Num A, Fraction B) {
Fraction C = {};
C.Numerator = A.numerator * B.Numerator;
C.Denominator = A.denominator * B.Denominator;
C = Simplify(C);
return C;
}
Fraction operator *(Monomial A, Fraction B) {
Fraction C = {};
C.Numerator = A * B.Numerator;
C.Denominator = B.Denominator;
C = Simplify(C);
return C;
}
Fraction operator *(Polynomial A, Fraction B) {
Fraction C = {};
C.Numerator = A * B.Numerator;
C.Denominator = B.Denominator;
C = Simplify(C);
return C;
}
Fraction operator *(Fraction A, Fraction B) {
Fraction C = {};
C.Numerator = A.Numerator * B.Numerator;
C.Denominator = A.Denominator * B.Denominator;
C = Simplify(C);
return C;
}
Fraction MulInvert(Fraction F) {
Fraction res = {};
res.Numerator = F.Denominator;
res.Denominator = F.Numerator;
return res;
}
Fraction operator /(Fraction A, Fraction B) {
return A * MulInvert(B);
}
Fraction operator ^(Fraction A, Fraction B) {
if (Congruent(A, Fraction_AddIdent) && Congruent(B, Fraction_AddIdent)) {
puts("ERR: 0 ^ 0");
return Fraction_AddIdent;
}
if (!IsInteger(B)) {
puts("ERR: does not support fractional exponent");
return Fraction_AddIdent;
}
if (Congruent(A, Fraction_AddIdent) && B.Numerator.term[0].coefficient < Num_AddIdent) {
puts("ERR: 0 ^ (-x)");
return Fraction_AddIdent;
}
Fraction C = Fraction_MulIdent;
if (B.Numerator.term[0].coefficient > Num_AddIdent) {
while (!Congruent(B, Fraction_AddIdent)) {
C = C * A;
B = B - Fraction_MulIdent;
}
}
else {
while (!Congruent(B, Fraction_AddIdent)) {
C = C / A;
B = B + Fraction_MulIdent;
}
}
return C;
}
Fraction Simplify(Fraction F) {
Fraction res = {};
Polynomial d = gcd(F.Numerator, F.Denominator);
res.Numerator = F.Numerator / d;
res.Denominator = F.Denominator / d;
if (res.Denominator.term[res.Denominator.term.size() - 1].coefficient < Num_AddIdent) {
res.Numerator = AddInvert(res.Numerator);
res.Denominator = AddInvert(res.Denominator);
}
long long u = 1;
for (int i = 0; i < res.Numerator.term.size(); i++)
u = lcm(u, res.Numerator.term[i].coefficient.denominator);
for (int i = 0; i < res.Denominator.term.size(); i++)
u = lcm(u, res.Denominator.term[i].coefficient.denominator);
res.Numerator = u * res.Numerator;
res.Denominator = u * res.Denominator;
return res;
}
void Calculator::init_priority() {
priority['('] = 0;
priority['+'] = priority['-'] = 1;
priority['*'] = priority['/'] = 2;
priority['^'] = 3;
}
bool Calculator::is_constant(char c) {return c >= '0' && c <= '9';}
bool Calculator::is_string(char c) {return c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z';}
bool Calculator::is_operator(char c) {return c == '+' || c == '-' || c == '*' || c == '/' || c == '^'; }
Fraction Calculator::read_constant(int& i) {
Fraction res = {};
number = 0;
decimal = false;
accuracy = 1;
while (is_constant(expr[i]) || expr[i] == '.') {
if (decimal) accuracy *= 10;
if (is_constant(expr[i])) number = number * 10 + expr[i] - '0';
else if (!decimal) decimal = true;
else if (decimal) {
puts("ERR: too many decimal points");
return Fraction_AddIdent;
}
i++;
}
i--;
res = { number * Polynomial_MulIdent, accuracy * Polynomial_MulIdent };
res = Simplify(res);
return res;
}
std::string Calculator::read_string(int& i) {
std::string res = "";
while (is_string(expr[i])) {
res.push_back(expr[i]);
i++;
}
i--;
return res;
}
void Calculator::Operate() {
Fraction B = nums.top(); nums.pop();
Fraction A = nums.top(); nums.pop();
char op = ops.top(); ops.pop();
Fraction C = {};
switch (op) {
case '+': C = A + B; break;
case '-': C = A - B; break;
case '*': C = A * B; break;
case '/': C = A / B; break;
case '^': C = A ^ B; break;
default: break;
}
nums.push(C);
}
Fraction Calculator::Calculate(std::string expression) {
while (!nums.empty()) nums.pop();
while (!ops.empty()) ops.pop();
expr = "(" + expression + ")";
state = 0;
for (int i = 0; i < expr.size(); i++)
if (expr[i] == ' ') continue;
else if (state == 0) {
if (is_constant(expr[i])) {
nums.push(read_constant(i));
state = 1;
}
else if (is_string(expr[i])) {
if (read_string(i) == variable) {
nums.push(Fraction_VarIdent);
state = 1;
}
else {
puts("ERR: variable not found");
return Fraction_AddIdent;
}
}
else if (expr[i] == '+' || expr[i] == '-') {
if (expr[i] == '+') parity = true;
else parity = false;
i++;
if (is_constant(expr[i])) {
Abs = read_constant(i);
if (!parity) Abs = AddInvert(Abs);
nums.push(Abs);
state = 1;
}
else {
puts("ERR: no number after an operator");
return Fraction_AddIdent;
}
}
else if (expr[i] == '(') ops.push(expr[i]);
else {
puts("ERR: illegal for state0");
return Fraction_AddIdent;
}
}
else if (state == 1) {
if (is_string(expr[i])) {
if (read_string(i) == variable) {
while (priority[ops.top()] >= priority['*']) Operate();
ops.push('*');
nums.push(Fraction_VarIdent);
}
else {
puts("variable not found");
return Fraction_AddIdent;
}
}
else if (is_operator(expr[i])) {
while (priority[ops.top()] >= priority[expr[i]]) Operate();
ops.push(expr[i]);
state = 2;
}
else if (expr[i] == '(') {
while (priority[ops.top()] >= priority['*']) Operate();
ops.push('*');
ops.push('(');
state = 0;
}
else if (expr[i] == ')') {
while (ops.top() != '(') Operate();
ops.pop();
}
else {
puts("ERR: illegal for state1");
return Fraction_AddIdent;
}
}
else {
if (is_constant(expr[i])) {
nums.push(read_constant(i));
state = 1;
}
else if (is_string(expr[i])) {
if (read_string(i) == variable) {
nums.push(Fraction_VarIdent);
state = 1;
}
else {
puts("ERR: variable not found");
return Fraction_AddIdent;
}
}
else if (expr[i] == '(') {
ops.push(expr[i]);
state = 0;
}
else {
puts("ERR: illegal for state2");
return Fraction_AddIdent;
}
}
return nums.top();
}
void Calculator::PrintSubPolynomial(Polynomial P) {
bool monomial = (P.term.size() == 1);
if (!monomial) putchar('(');
for (int i = P.term.size() - 1; i >= 0; i--) {
PrintCoefficient(i == P.term.size() - 1, IsConstant(P.term[i]), P.term[i].coefficient);
if (P.term[i].exponent > Num_AddIdent) {
std::cout << variable;
if (P.term[i].exponent != Num_MulIdent) {
putchar('^');
PrintExponent(P.term[i].exponent);
}
}
}
if (!monomial) putchar(')');
}
void Calculator::PrintCoefficient(bool highest, bool constant, Num Coefficient) {
if (!highest || Coefficient < Num_AddIdent) putchar(Coefficient > Num_AddIdent ? '+' : '-');
if (constant || Coefficient != Num_MulIdent && Coefficient != AddInvert(Num_MulIdent)) {
if (IsInteger(Coefficient)) printf("%lld", abs(Coefficient.numerator));
else printf("(%lld/%lld)", abs(Coefficient.numerator), Coefficient.denominator);
}
}
void Calculator::PrintExponent(Num Exponent) {
if (IsInteger(Exponent)) printf("%lld", Exponent.numerator);
else printf("(%lld/%lld)", Exponent.numerator, Exponent.denominator);
}
void Calculator::Print(Fraction F) {
if (Congruent(F, Fraction_AddIdent)) putchar('0');
else if (IsConstant(F.Denominator)) {
for (int i = F.Numerator.term.size() - 1; i >= 0; i--) {
PrintCoefficient(i == F.Numerator.term.size() - 1, IsConstant(F.Numerator.term[i]),
F.Numerator.term[i].coefficient / F.Denominator.term[0].coefficient);
if (F.Numerator.term[i].exponent > Num_AddIdent) {
std::cout << variable;
if (F.Numerator.term[i].exponent != Num_MulIdent) {
putchar('^');
PrintExponent(F.Numerator.term[i].exponent);
}
}
}
}
else {
PrintSubPolynomial(F.Numerator);
putchar('/');
PrintSubPolynomial(F.Denominator);
}
puts("");
}
大佬,实力啊
太有实力了
谢谢~~O(∩_∩)O