Skip to content

Commit

Permalink
Place * between digits
Browse files Browse the repository at this point in the history
  • Loading branch information
fintarin committed Sep 20, 2023
1 parent 2974004 commit 628eb93
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
10 changes: 9 additions & 1 deletion src/fintamath/expressions/interfaces/IPolynomExpression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "fintamath/expressions/binary/CompExpression.hpp"
#include "fintamath/functions/FunctionArguments.hpp"
#include "fintamath/functions/IOperator.hpp"
#include "fintamath/functions/arithmetic/Mul.hpp"
#include "fintamath/literals/Variable.hpp"
#include "fintamath/literals/constants/IConstant.hpp"
#include "fintamath/literals/constants/Undefined.hpp"
Expand All @@ -30,7 +31,14 @@ std::string IPolynomExpression::toString() const {
result += childToString(*oper, children.front(), {});

for (size_t i = 1; i < children.size(); i++) {
result += childToString(*oper, children[i], children[i - 1]);
const std::string childStr = childToString(*oper, children[i], children[i - 1]);

if (childStr.length() > 2 && childStr[0] == ' ' && std::isdigit(childStr[1]) && std::isdigit(result.back())) {
result += Mul().toString() + childStr.substr(1);
}
else {
result += childStr;
}
}

return result;
Expand Down
14 changes: 12 additions & 2 deletions tests/src/expressions/ExpressionTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,16 @@ TEST(ExpressionTests, stringConstructorTest) {
EXPECT_EQ(Expression("(a+b)^3").toString(), "a^3 + 3 a^2 b + 3 a b^2 + b^3");
EXPECT_EQ(Expression("1*(a+b)^3").toString(), "a^3 + 3 a^2 b + 3 a b^2 + b^3");
EXPECT_EQ(Expression("(a+b)^4").toString(), "a^4 + 4 a^3 b + 6 a^2 b^2 + 4 a b^3 + b^4");
EXPECT_EQ(Expression("5^x 9").toString(), "9*5^x");
EXPECT_EQ(Expression("9 5^x").toString(), "9*5^x");
EXPECT_EQ(Expression("9 5^x 9").toString(), "81*5^x");
EXPECT_EQ(Expression("5^x 99").toString(), "99*5^x");
EXPECT_EQ(Expression("99 5^x").toString(), "99*5^x");
EXPECT_EQ(Expression("99 5^x 99").toString(), "9801*5^x");
EXPECT_EQ(Expression("5^x a").toString(), "a 5^x");
EXPECT_EQ(Expression("a 5^x").toString(), "a 5^x");
EXPECT_EQ(Expression("a 5^x a").toString(), "a^2*5^x");
EXPECT_EQ(Expression("2 5^x 6^y 7^z x^2 y^3").toString(), "2 x^2 y^3*5^x 6^y 7^z");
EXPECT_EQ(Expression("1*(a+b)*1").toString(), "a + b");
EXPECT_EQ(Expression("-1*(a+b)*1").toString(), "-a - b");
EXPECT_EQ(Expression("1*(a+b)*-1").toString(), "-a - b");
Expand Down Expand Up @@ -674,7 +684,7 @@ TEST(ExpressionTests, stringConstructorTest) {
EXPECT_EQ(Expression("ln((10 E)^(2 ln(2))) - 2 ln(2) ln(10 E)").toString(), "0");
EXPECT_EQ(Expression("log(2.3,(E)/(20000.1EE)) + log(2.3,20000.1E)").toString(), "0");
EXPECT_EQ(Expression("log(2, 3) + log(3, 4)").toString(), "log(3, 4) + log(2, 3)");
EXPECT_EQ(Expression("x log(2, 3) + log(2, 5)").toString(), "log(2, 5 3^x)");
EXPECT_EQ(Expression("x log(2, 3) + log(2, 5)").toString(), "log(2, 5*3^x)");
EXPECT_EQ(Expression("x log(2, 3) + log(2, 5a)").toString(), "log(2, 5 a 3^x)");
EXPECT_EQ(Expression("log(2, 3) + 3log(3, 4)").toString(), "3 log(3, 4) + log(2, 3)");
EXPECT_EQ(Expression("3log(2x, 3) + log(3, 4)").toString(), "3 log(2 x, 3) + log(3, 4)");
Expand Down Expand Up @@ -1786,7 +1796,7 @@ TEST(ExpressionTests, parseExprTest) {
EXPECT_EQ(parseExpr("2.35%")->toString(), "(47/20)/100");
EXPECT_EQ(parseExpr("1100*4.76%")->toString(), "1100 (119/25)/100");
EXPECT_EQ(parseExpr("2.35%%%%")->toString(), "((((47/20)/100)/100)/100)/100");
EXPECT_EQ(parseExpr("1100*4.76%1100*4.76%")->toString(), "1100 (119/25)/100 1100 (119/25)/100");
EXPECT_EQ(parseExpr("1100*4.76%1100*4.76%")->toString(), "1100 (119/25)/100*1100 (119/25)/100");
EXPECT_EQ(parseExpr("((((((5)/(8)))/(1)))/(((((((9)/(4)))/(0)))/(5))))")->toString(), "((5/8)/1)/(((9/4)/0)/5)");

EXPECT_EQ(parseExpr("(1 = 1) / 2")->toString(), "(1 = 1)/2");
Expand Down

0 comments on commit 628eb93

Please sign in to comment.