| 237 | } |
| 238 | |
| 239 | std::string Unit::getString() const |
| 240 | { |
| 241 | auto buildSubStr = [&](auto index) { |
| 242 | const std::string unitStrString {unitSymbols.at(index)}; |
| 243 | const auto absol {abs(_exps.at(index))}; |
| 244 | |
| 245 | return absol <= 1 ? unitStrString |
| 246 | : fmt::format("{}^{}", unitStrString, std::to_string(absol)); |
| 247 | }; |
| 248 | |
| 249 | auto buildStr = [&](auto indexes) { |
| 250 | std::vector<std::string> subStrings {}; |
| 251 | std::transform(indexes.begin(), indexes.end(), std::back_inserter(subStrings), buildSubStr); |
| 252 | |
| 253 | return fmt::format("{}", fmt::join(subStrings, "*")); |
| 254 | }; |
| 255 | |
| 256 | //------------------------------------------------------------------------------ |
| 257 | |
| 258 | auto [posValIndexes, negValIndexes] = nonZeroValsIndexes(); |
| 259 | auto numeratorStr = buildStr(posValIndexes); |
| 260 | if (negValIndexes.empty()) { |
| 261 | return numeratorStr; |
| 262 | } |
| 263 | |
| 264 | auto denominatorStr = buildStr(negValIndexes); |
| 265 | |
| 266 | return fmt::format( |
| 267 | "{}/{}", |
| 268 | numeratorStr.empty() ? "1" : numeratorStr, |
| 269 | negValIndexes.size() > 1 ? fmt::format("({})", denominatorStr) : denominatorStr |
| 270 | ); |
| 271 | } |
| 272 | |
| 273 | std::string Unit::representation() const |
| 274 | { |