Treceți la conținutul principal

algebra cod

equations.cpp


#include "Equation.h"

Bblue::Math::Equation::Linear::Linear(const Expression &expression){
 /*
  * ax + b = 0
  * x = -b / a
  */

 if (expression.GetLowOrder() == 0 && expression.GetHighOrder() == 1 && expression.IsHomogenous()){
  auto a = expression.GetFirstTermWithHighOrder(1), b = expression.GetFirstTermWithHighOrder(0);
  result_ = { a.PrintVariableCharacters().at(0), -b.GetCoefficient() / a.GetCoefficient() };
 }
 else
  result_ = { '\0', 0.0 };
}

Bblue::Math::Equation::Quadratic::Quadratic(const Expression &expression){
 /*
  * ax^2 + bx + c = 0
  * x = (-b +/- sqrt(b^2 - 4ac)) / 2a
  */

 if (expression.GetLowOrder() == 0 && expression.GetHighOrder() == 2 && expression.IsHomogenous()){
  auto a = expression.GetFirstTermWithHighOrder(2), b = expression.GetFirstTermWithHighOrder(1), c = expression.GetFirstTermWithHighOrder(0);
  auto bac = (b.GetCoefficient() * b.GetCoefficient()) - (4 * a.GetCoefficient() * c.GetCoefficient());
  auto sqrtBac = std::sqrt(bac);
  auto vcs = a.PrintVariableCharacters();

  result_.first = { vcs + "1", (-b.GetCoefficient() + sqrtBac) / (2 * a.GetCoefficient()) };
  result_.second = { vcs + "2", (-b.GetCoefficient() - sqrtBac) / (2 * a.GetCoefficient()) };
 }
 else
  result_ = { { "", 0.0 }, { "", 0.0 } };
}

Bblue::Math::Equation::Simultaneous::Simultaneous(const Expression &first, const Expression &second){
 /*
  * ax + by + c = 0
  * dx + ey + f = 0
  *
  *     bf - ce
  * x = -------
  *     ae - bd
  *
  *     cd - af
  * y = -------
  *     ae - bd
  */

 if (first.GetLowOrder() == 0 && first.GetHighOrder() == 1 && first.IsSingleVariableTerms() && 
  second.GetLowOrder() == 0 && second.GetHighOrder() == 1 && second.IsSingleVariableTerms()){
  auto vcs = first.PrintVariableCharacters();
  if (vcs.size() < 2u)
   vcs = second.PrintVariableCharacters();

  if (vcs.size() == 2u){//2 variables required
   auto a = first.GetFirstTermWithVariableCharacters(vcs.substr(1, 1)), b = first.GetFirstTermWithVariableCharacters(vcs.substr(0, 1)), 
    c = first.GetFirstTermWithHighOrder(0);

   auto d = second.GetFirstTermWithVariableCharacters(vcs.substr(1, 1)), e = second.GetFirstTermWithVariableCharacters(vcs.substr(0, 1)), 
    f = second.GetFirstTermWithHighOrder(0);

   auto ce = c.GetCoefficient() * e.GetCoefficient();
   auto bf = b.GetCoefficient() * f.GetCoefficient();

   auto cd = c.GetCoefficient() * d.GetCoefficient();
   auto af = a.GetCoefficient() * f.GetCoefficient();

   auto ae = a.GetCoefficient() * e.GetCoefficient();
   auto bd = b.GetCoefficient() * d.GetCoefficient();

   result_.first = { vcs.substr(1, 1), (bf - ce) / (ae - bd) };
   result_.second = { vcs.substr(0, 1), (cd - af) / (ae - bd) };
  }
  else
   result_ = { { "", 0.0 }, { "", 0.0 } };
 }
 else
  result_ = { { "", 0.0 }, { "", 0.0 } };
}




equation.h

#pragma once

#ifndef BBLUE_MATH_EQUATION_H
#define BBLUE_MATH_EQUATION_H

#include "Expression.h"

namespace Bblue{
 namespace Math{
  namespace Equation{
   class Linear{
   public:
    Linear(const Expression &);

    std::pair<char, double> GetResult() const{
     return result_;
    }

   protected:
    std::pair<char, double> result_;
   };

   class Quadratic{
   public:
    struct Result{
     std::string key;
     double value;
    };

    Quadratic(const Expression &);

    std::pair<Result, Result> GetResult() const{
     return result_;
    }

   protected:
    std::pair<Result, Result> result_;
   };

   class Simultaneous{
   public:
    struct Result{
     std::string key;
     double value;
    };

    Simultaneous(const Expression &, const Expression &);

    std::pair<Result, Result> GetResult() const{
     return result_;
    }

   protected:
    std::pair<Result, Result> result_;
   };
  }
 }
}

#endif /* !BBLUE_MATH_EQUATION_H */


expression.cpp

#include "Expression.h"

std::string Bblue::Math::DoubleToString(double value){
 auto stringValue = std::to_string(value);
 if (stringValue.find('.') >= stringValue.size())
  return stringValue;

 while (!stringValue.empty() && *stringValue.rbegin() == '0')
  stringValue.erase(stringValue.size() - 1);

 if (!stringValue.empty() && *stringValue.rbegin() == '.')
  stringValue.erase(stringValue.size() - 1);

 return stringValue;
}

void Bblue::Math::Term::Insert(const Variable &variable){
 if (variable.GetPower() == 0)//Inerting 1 does not affect the term
  return;

 for (auto iter = variables_.begin(); iter != variables_.end(); ++iter){
  if (iter->GetCharacter() == variable.GetCharacter()){//Variable character found: Apply power
   Variable raised(iter->GetCharacter(), iter->GetPower() + variable.GetPower());
   variables_.erase(iter);
   if (raised.GetPower() != 0)
    Insert_(raised);
   return;
  }
 }

 Insert_(variable);
}

void Bblue::Math::Term::Insert_(const Variable &variable){
 for (auto iter = variables_.begin(); iter != variables_.end(); ++iter){
  if (variable < *iter){//Insert sorted
   variables_.insert(iter, variable);
   return;
  }
 }

 variables_.push_back(variable);
}

bool Bblue::Math::Term::IsDivisibleBy(const Term &term) const{
 if (GetHighOrder() < term.GetHighOrder() || variables_.size() < term.variables_.size())
  return false;

 for (const auto &termVariable : term.variables_){
  auto matched = false;
  for (const auto &variable : variables_){
   if (variable.GetCharacter() == termVariable.GetCharacter()){
    if (variable.GetPower() >= termVariable.GetPower())
     matched = true;
    break;
   }
  }

  if (!matched)
   return false;
 }

 return true;
}

bool Bblue::Math::Term::Contains(const Term &term) const{
 if (variables_.size() < term.variables_.size())
  return false;

 for (const auto &termVariable : term.variables_){
  auto matched = false;
  for (const auto &variable : variables_){
   if (variable.GetCharacter() == termVariable.GetCharacter()){
    matched = true;
    break;
   }
  }

  if (!matched)
   return false;
 }

 return true;
}

short Bblue::Math::Term::CompareVariables(const Term &term) const{
 if (GetHighOrder() != term.GetHighOrder())
  return (GetHighOrder() < term.GetHighOrder()) ? -1 : 1;

 for (auto i = variables_.begin(), j = term.variables_.begin(); i != variables_.end() && j != term.variables_.end(); ++i, ++j){
  if (*i != *j)
   return (*i < *j) ? -1 : 1;
 }

 if (variables_.size() != term.variables_.size())
  return (variables_.size() < term.variables_.size()) ? -1 : 1;

 return 0;
}

short Bblue::Math::Term::Compare(const Term &term) const{
 auto result = CompareVariables(term);
 if (result != 0)
  return result;

 if (coefficient_ != term.coefficient_)
  return (coefficient_ < term.coefficient_) ? -1 : 1;

 return 0;
}

Bblue::Math::Term Bblue::Math::Term::operator ~() const{
 Term inverse((1 / coefficient_), {});
 for (auto variable = variables_.rbegin(); variable != variables_.rend(); ++variable)//Invert powers
  inverse.variables_.push_back(Variable(variable->GetCharacter(), -variable->GetPower()));

 return inverse;
}

Bblue::Math::Term Bblue::Math::Term::operator +(const Term &right) const{
 if (CompareVariables(right) != 0)//Variables must match
  return Term();

 return Term(coefficient_ + right.coefficient_, variables_);
}

Bblue::Math::Term Bblue::Math::Term::operator -(const Term &right) const{
 if (CompareVariables(right) != 0)//Variables must match
  return Term();

 return Term(coefficient_ - right.coefficient_, variables_);
}

Bblue::Math::Term Bblue::Math::Term::operator *(const Term &right) const{
 Term product(coefficient_ * right.coefficient_, variables_);
 for (const auto &variable : right.variables_)//Apply variables
  product.Insert(variable);

 return product;
}

Bblue::Math::Term Bblue::Math::Term::operator /(const Term &right) const{
 return (*this * ~right);//Invert right and multiply
}

Bblue::Math::Term Bblue::Math::Term::operator ^(short power) const{
 if (power == 0)//Result is 1
  return Term(1.0, {});

 Term raised(std::pow(coefficient_, power), variables_);
 for (auto &variable : raised.variables_)//Apply power
  variable.SetPower(variable.GetPower() * power, true);

 return raised;
}

std::string Bblue::Math::Term::ToString(bool showSign) const{
 if (coefficient_ == 0.0)
  return showSign ? " + 0" : "0";

 if (IsConstant())
  return GetCoefficientString_(showSign);

 std::string term;
 for (const auto &variable : variables_){
  if (variable.GetPower() != 0)
   term += variable.ToString();
 }

 if (coefficient_ == -1.0){
  if (term.empty())
   term = showSign ? " - 1" : "-1";
  else if (showSign)
   term.insert(0, " - ");
  else
   term.insert(0, "-");
 }
 else if (coefficient_ == 1.0){
  if (term.empty())
   term = showSign ? " + 1" : "+1";
  else if (showSign)
   term.insert(0, " + ");
 }
 else
  term.insert(0, GetCoefficientString_(showSign));

 return term;
}

std::string Bblue::Math::Term::PrintVariableCharacters() const{
 std::string value;
 for (const auto &variable : variables_)
  value += variable.GetCharacter();

 return value;
}

std::string Bblue::Math::Term::GetCoefficientString_(bool showSign) const{
 auto coefficient = std::to_string(coefficient_);
 if (showSign){
  if (coefficient[0] == '-'){
   coefficient.insert(1, " ");
   coefficient.insert(0, " ");
  }
  else
   coefficient.insert(0, " + ");
 }

 auto decimalPoint = coefficient.find('.');
 if (decimalPoint < coefficient.size()){
  while (!coefficient.empty() && *coefficient.rbegin() == '0')//Truncate trailing 0's
   coefficient.pop_back();

  if (!coefficient.empty() && *coefficient.rbegin() == '.')
   coefficient.pop_back();
 }

 return coefficient;
}

void Bblue::Math::Expression::Insert(const Term &term){
 for (auto iter = terms_.begin(); iter != terms_.end(); ++iter){
  if (iter->CompareVariables(term) == 0){//Similar term found: Add terms
   auto added = *iter + term;
   terms_.erase(iter);

   if (added.GetCoefficient() == 0.0)
    ComputeOrder_();
   else
    Insert_(added);

   return;
  }
 }

 Insert_(term);
}

void Bblue::Math::Expression::Insert(const Expression &expression){
 for (const auto &term : expression.terms_)
  Insert(term);
}

void Bblue::Math::Expression::Insert_(const Term &term){
 for (auto iter = terms_.begin(); iter != terms_.end(); ++iter){
  if (term > *iter){//Insert sorted
   terms_.insert(iter, term);
   return;
  }
 }

 terms_.push_back(term);
 if (term.GetLowOrder() < order_.first)
  order_.first = term.GetLowOrder();

 if (term.GetHighOrder() > order_.second)
  order_.second = term.GetHighOrder();
}

void Bblue::Math::Expression::ApplyTerm(const Term &term){
 for (auto &item : terms_)
  item *= term;

 ComputeOrder_();
}

void Bblue::Math::Expression::ApplyCoefficient(double coefficient){
 for (auto &term : terms_)
  term.SetCoefficient(term.GetCoefficient() * coefficient);
}

void Bblue::Math::Expression::ComputeOrder_(){
 if (!terms_.empty()){
  auto iter = terms_.begin();
  order_.first = iter->GetLowOrder();
  order_.second = iter->GetHighOrder();

  for (++iter; iter != terms_.end(); ++iter){
   if (iter->GetLowOrder() < order_.first)
    order_.first = iter->GetLowOrder();

   if (iter->GetHighOrder() > order_.second)
    order_.second = iter->GetHighOrder();
  }
 }
 else
  order_.first = order_.second = 0;
}

Bblue::Math::Expression Bblue::Math::Expression::operator -() const{
 Expression neg;
 for (const auto &term : neg.terms_)
  neg.Insert(-term);

 return neg;
}

Bblue::Math::Expression Bblue::Math::Expression::operator +(const Expression &right) const{
 Expression sum = *this;
 for (const auto &term : right.terms_)
  sum.Insert(term);

 return sum;
}

Bblue::Math::Expression Bblue::Math::Expression::operator -(const Expression &right) const{
 Expression sum = *this;
 for (const auto &term : right.terms_)
  sum.Insert(-term);

 return sum;
}

Bblue::Math::Expression Bblue::Math::Expression::operator *(const Expression &right) const{
 Expression product;
 for (const auto &term : terms_){
  for (const auto &rightTerm : right.terms_)
   product.Insert(term * rightTerm);
 }

 return product;
}

Bblue::Math::Expression Bblue::Math::Expression::operator ^(short power) const{
 if (power < 0)
  return (terms_.size() == 1u) ? (terms_[0] ^ power) : Expression();

 if (power == 0)//Constant
  Term(1, {});

 Expression raised = *this;
 while (power > 1){
  raised *= *this;
  --power;
 }

 return raised;
}

Bblue::Math::Term Bblue::Math::Expression::GetFirstTermWithLowOrder(short power) const{
 for (const auto &term : terms_){
  if (term.GetLowOrder() == power)
   return term;
 }

 return Term();
}

Bblue::Math::Term Bblue::Math::Expression::GetFirstTermWithHighOrder(short power) const{
 for (const auto &term : terms_){
  if (term.GetHighOrder() == power)
   return term;
 }

 return Term();
}

Bblue::Math::Term Bblue::Math::Expression::GetFirstTermWithVariableCharacters(const std::string &vcs) const{
 for (const auto &term : terms_){
  if (term.PrintVariableCharacters() == vcs)
   return term;
 }

 return Term();
}

bool Bblue::Math::Expression::IsHomogenous() const{
 if (terms_.empty())
  return true;

 if (!terms_[0].IsHomogenous())
  return false;

 auto iter = terms_.begin(), next = terms_.begin() + 1;
 for (; next != terms_.end(); ++next){
  if (!next->IsHomogenous() || (!iter->IsConstant() && !iter->Contains(*next)))
   return false;

  if (iter->IsConstant())
   iter = next;
 }

 return true;
}

bool Bblue::Math::Expression::IsSingleVariableTerms() const{
 for (const auto &term : terms_){
  if (term.GetVariableCount() > 1u)
   return false;
 }

 return true;
}

std::string Bblue::Math::Expression::ToString() const{
 if (terms_.empty())
  return "";

 auto iter = terms_.begin();
 std::string expression = iter->ToString(false);
 for (++iter; iter != terms_.end(); ++iter)
  expression += iter->ToString(true);

 return expression;
}

std::string Bblue::Math::Expression::PrintVariableCharacters() const{
 std::string value;
 for (const auto &term : terms_){
  if (!value.empty()){
   auto termValue = term.PrintVariableCharacters();
   for (auto c : termValue){//Add variables not already printed
    if (value.find(c) >= value.size())
     value += c;
   }
  }
  else
   value = term.PrintVariableCharacters();
 }

 return value;
}

Bblue::Math::Expression Bblue::Math::ExpressionParser::Parse(const std::string &expression){
 auto operatorRead = true;
 std::string::size_type offset = 0;
 std::vector<Token> operatorStack, expressionStack;

 while (offset < expression.size()){//Stack expression
  auto next = GetNext(expression, offset, operatorRead);
  if (next.type == TOKEN_TYPE_BINARY_OPERATOR || next.type == TOKEN_TYPE_UNARY_OPERATOR){
   if (next.value[0] == ')'){//Pop operators inside parenthesis
    while (!operatorStack.empty() && operatorStack.rbegin()->value[0] != '('){
     expressionStack.push_back(*operatorStack.rbegin());
     operatorStack.pop_back();
    }

    if (!operatorStack.empty())//Pop '('
     operatorStack.pop_back();
    else
     return Expression();

    operatorRead = false;
   }
   else if (next.value[0] != '('){//Pop operators with higher precedence
    auto precedence = GetPrecedence(next.value[0], next.type == TOKEN_TYPE_BINARY_OPERATOR);
    while (!operatorStack.empty() && (precedence <=
     GetPrecedence(operatorStack.rbegin()->value[0], operatorStack.rbegin()->type == TOKEN_TYPE_BINARY_OPERATOR))){
     expressionStack.push_back(*operatorStack.rbegin());
     operatorStack.pop_back();
    }

    operatorStack.push_back(next);
    operatorRead = true;
   }
   else{
    operatorStack.push_back(next);
    operatorRead = true;
   }
  }
  else{
   expressionStack.push_back(next);
   operatorRead = false;
  }
 }

 while (!operatorStack.empty() && operatorStack.rbegin()->value[0] != '('){
  expressionStack.push_back(*operatorStack.rbegin());
  operatorStack.pop_back();
 }

 if (!operatorStack.empty())//Incomplete parenthesis in expression
  return Expression();

 std::vector<Expression> operandStack;
 for (auto &token : expressionStack){//Evaluate stack
  if (token.type == TOKEN_TYPE_BINARY_OPERATOR){
   switch (token.value[0]){
   case '+':
    if (operandStack.size() < 2u)
     return Expression();

    *(operandStack.rbegin() + 1) += *operandStack.rbegin();
    operandStack.pop_back();
    break;
   case '-':
   case '=':
    if (operandStack.size() < 2u)
     return Expression();

    *(operandStack.rbegin() + 1) -= *operandStack.rbegin();
    operandStack.pop_back();
    break;
   case '*':
    if (operandStack.size() < 2u)
     return Expression();

    *(operandStack.rbegin() + 1) *= *operandStack.rbegin();
    operandStack.pop_back();
    break;
   case '^':
    if (operandStack.size() < 2u || !operandStack.rbegin()->IsConstant())
     return Expression();

    *(operandStack.rbegin() + 1) ^= static_cast<short>(operandStack.rbegin()->GetConstantValue());
    operandStack.pop_back();
    break;
   default:
    return Expression();
   }
  }
  else if (token.type == TOKEN_TYPE_UNARY_OPERATOR){
   switch (token.value[0]){
   case '+':
    break;
   case '-':
    if (operandStack.empty())
     return Expression();

    operandStack.rbegin()->ApplyCoefficient(-1);
    break;
   default:
    return Expression();
   }
  }
  else if (token.type == TOKEN_TYPE_VARIABLE)
   operandStack.push_back(Term(1.0, { Variable(token.value[0], 1) }));
  else//Constant
   operandStack.push_back(Term(std::stod(token.value), {}));
 }

 return (operandStack.size() == 1u) ? operandStack[0] : Expression();
}

Bblue::Math::ExpressionParser::Token Bblue::Math::ExpressionParser::GetNext(const std::string &expression, std::string::size_type &offset, bool operatorRead){
 while (offset < expression.size() && expression[offset] <= ' ')
  ++offset;

 if (offset >= expression.size())
  return Token{ "", TOKEN_TYPE_NONE };

 if (expression[offset] == '('){
  if (!operatorRead)
   return Token{ "*", TOKEN_TYPE_BINARY_OPERATOR };

  ++offset;
  return Token{ "(", TOKEN_TYPE_UNARY_OPERATOR };
 }

 if (expression[offset] == ')'){
  ++offset;
  return Token{ ")", TOKEN_TYPE_UNARY_OPERATOR };
 }

 if (expression[offset] == '+' || expression[offset] == '-' || expression[offset] == '^' || expression[offset] == '=' || expression[offset] == '*'){
  ++offset;
  if (operatorRead)
   return Token{ expression.substr(offset - 1, 1), TOKEN_TYPE_UNARY_OPERATOR };

  return Token{ expression.substr(offset - 1, 1), TOKEN_TYPE_BINARY_OPERATOR };
 }

 if (!operatorRead)
  return Token{ "*", TOKEN_TYPE_BINARY_OPERATOR };

 if (isalpha(expression[offset])){
  ++offset;
  return Token{ expression.substr(offset - 1, 1), TOKEN_TYPE_VARIABLE };
 }

 return GetNumber(expression, offset);
}

Bblue::Math::ExpressionParser::Token Bblue::Math::ExpressionParser::GetNumber(const std::string &expression, std::string::size_type &offset){
 while (offset < expression.size() && expression[offset] <= ' ')
  ++offset;

 std::string number;
 bool pointRead = false;

 while (offset < expression.size() && (isdigit(expression[offset]) || (expression[offset] == '.' && !pointRead))){
  number += expression[offset++];
  if (expression[offset] == '.')
   pointRead = true;
 }

 return (number.empty() || (number.size() == 1u && number[0] == '.')) ? Token{ "", TOKEN_TYPE_NONE } : Token{ number, TOKEN_TYPE_NUMBER };
}

short Bblue::Math::ExpressionParser::GetPrecedence(char op, bool isBinary){
 switch (op){
 case '+':
 case '-':
  return isBinary ? 1 : 4;
 case '*':
  return 2;
 case '^':
  return 3;
 case '=':
  return 0;
 default:
  break;
 }

 return -1;
}

expression.h

#pragma once

#ifndef BBLUE_MATH_EXPRESSION_H
#define BBLUE_MATH_EXPRESSION_H

#include <cmath>
#include <string>
#include <vector>
#include <memory>

namespace Bblue{
 namespace Math{
  std::string DoubleToString(double);

  class Variable{
  public:
   Variable(char c, short power)
    : c_(c), power_(power){}

   bool operator <(const Variable &right) const{
    return (power_ == right.power_) ? (c_ < right.c_) : (power_ < right.power_);
   }

   bool operator >(const Variable &right) const{
    return (power_ == right.power_) ? (c_ > right.c_) : (power_ > right.power_);
   }

   bool operator ==(const Variable &right) const{
    return (power_ == right.power_ && c_ == right.c_);
   }

   bool operator !=(const Variable &right) const{
    return (power_ != right.power_ || c_ != right.c_);
   }

   void SetPower(short power, bool replace){
    if (replace)
     power_ = power;
    else
     power_ += power;
   }

   char GetCharacter() const{
    return c_;
   }

   short GetPower() const{
    return power_;
   }

   bool IsDivisibleBy(const Variable &variable) const{
    return (variable.power_ == 0 || (variable.c_ == c_ && variable.power_ <= power_));
   }

   std::string ToString() const{
    if (power_ == 0)
     return "1";

    return (power_ == 1) ? (c_ + std::string()) : (c_ + ("^" + std::to_string(power_)));
   }

  protected:
   char c_;
   short power_;
  };

  class Term{
  public:
   Term()
    : coefficient_(0){}

   Term(double coefficient, const std::vector<Variable> &variables)
    : coefficient_(coefficient){
    for (const auto &variable : variables)
     Insert(variable);
   }

   bool operator <(const Term &right) const{
    return (Compare(right) < 0);
   }

   bool operator >(const Term &right) const{
    return (Compare(right) > 0);
   }

   bool operator ==(const Term &right) const{
    return (Compare(right) == 0);
   }

   bool operator !=(const Term &right) const{
    return (Compare(right) != 0);
   }

   Term operator +() const{
    return *this;
   }

   Term operator -() const{
    return Term(-coefficient_, variables_);
   }

   Term operator ~() const;

   Term operator +(const Term &) const;

   Term operator -(const Term &) const;

   Term operator *(const Term &) const;

   Term operator /(const Term &) const;

   Term operator ^(short) const;

   Term &operator +=(const Term &right){
    return (*this = *this + right);
   }

   Term &operator -=(const Term &right){
    return (*this = *this - right);
   }

   Term &operator *=(const Term &right){
    return (*this = *this * right);
   }

   Term &operator /=(const Term &right){
    return (*this = *this / right);
   }

   Term &operator ^=(short power){
    return (*this = *this ^ power);
   }

   void SetCoefficient(double coefficient){
    coefficient_ = coefficient;
   }

   void Insert(const Variable &);

   double GetCoefficient() const{
    return coefficient_;
   }

   Variable GetVariableAtIndex(std::vector<Variable>::size_type index) const{
    return variables_[index];
   }

   std::vector<Variable>::size_type GetVariableCount() const{
    return variables_.size();
   }

   short GetLowOrder() const{
    return (IsConstant() || variables_.begin()->GetPower() > 0) ? 0 : variables_.begin()->GetPower();
   }

   short GetHighOrder() const{
    return IsConstant() ? 0 : variables_.rbegin()->GetPower();
   }

   bool IsConstant() const{
    return variables_.empty();
   }

   bool IsHomogenous() const{
    return (variables_.size() < 2u);
   }

   bool IsDivisibleBy(const Term &) const;

   bool Contains(const Term &) const;

   short CompareVariables(const Term &) const;

   short Compare(const Term &) const;

   std::string ToString(bool) const;

   std::string ToString() const{
    return ToString(false);
   }

   std::string PrintVariableCharacters() const;

  protected:
   void Insert_(const Variable &);

   std::string GetCoefficientString_(bool) const;

   double coefficient_;
   std::vector<Variable> variables_;
  };

  class Expression{
  public:
   Expression(){}

   Expression(const Term &term){
    terms_.push_back(term);
    order_.first = term.GetLowOrder();
    order_.second = term.GetHighOrder();
   }

   Expression(const std::vector<Term> &terms){
    for (const auto &term : terms)
     Insert(term);
   }

   Expression operator +() const{
    return *this;
   }

   Expression operator -() const;

   Expression operator +(const Expression &) const;

   Expression operator -(const Expression &) const;

   Expression operator *(const Expression &) const;

   Expression operator ^(short) const;

   Expression &operator +=(const Expression &right){
    return (*this = *this + right);
   }

   Expression &operator -=(const Expression &right){
    return (*this = *this - right);
   }

   Expression &operator *=(const Expression &right){
    return (*this = *this * right);
   }

   Expression &operator ^=(short power){
    return (*this = *this ^ power);
   }

   void Insert(const Term &);

   void Insert(const Expression &);

   void ApplyTerm(const Term &);

   void ApplyCoefficient(double);

   Term GetTermAtIndex(std::vector<Term>::size_type index) const{
    return terms_[index];
   }

   Term GetFirstTermWithLowOrder(short) const;

   Term GetFirstTermWithHighOrder(short) const;

   Term GetFirstTermWithVariableCharacters(const std::string &) const;

   std::vector<Term>::size_type GetTermCount() const{
    return terms_.size();
   }

   short GetLowOrder() const{
    return order_.first;
   }

   short GetHighOrder() const{
    return order_.second;
   }

   double GetConstantValue() const{
    return IsConstant() ? terms_[0].GetCoefficient() : 0.0;
   }

   bool IsEmpty() const{
    return terms_.empty();
   }

   bool IsConstant() const{
    return (terms_.size() == 1u && terms_[0].IsConstant());
   }

   bool IsHomogenous() const;

   bool IsSingleVariableTerms() const;

   std::string ToString() const;

   std::string PrintVariableCharacters() const;

  protected:
   void Insert_(const Term &);

   void ComputeOrder_();

   std::vector<Term> terms_;
   std::pair<short, short> order_;
  };

  class ExpressionParser{
  public:
   struct Token{
    std::string value;
    unsigned short type;
   };

   static Expression Parse(const std::string &);

   static Token GetNext(const std::string &, std::string::size_type &, bool);

   static Token GetNumber(const std::string &, std::string::size_type &);

   static short GetPrecedence(char, bool);

   static const unsigned short TOKEN_TYPE_NONE     = 0x0000;
   static const unsigned short TOKEN_TYPE_NUMBER    = 0x0001;
   static const unsigned short TOKEN_TYPE_VARIABLE    = 0x0002;
   static const unsigned short TOKEN_TYPE_BINARY_OPERATOR  = 0x0003;
   static const unsigned short TOKEN_TYPE_UNARY_OPERATOR  = 0x0004;
  };
 }
}

#endif /* !BBLUE_MATH_EXPRESSION_H */


Comentarii

Postări populare de pe acest blog

program principal cpp

#include "clasa.h" #include <stdio.h> #include <conio.h> #include <stdlib.h> #include <string.h> #define DELAY 9000000 void delay() { for(long i=0;i<DELAY;i++); } //constructor cu initializare de la tastatura BigInt::BigInt() {char x; signed char t[400]; int i; printf("\nNumarul cu semn "); do s=getche(); while((s!='+')&&(s!='-')); n=0; do {x=getche(); t[n]=x-'0'; n++; } while((x>='0')&&(x<='9')); n--; for(i=0;i<n;i++) nr[i]=t[n-i-1]; } //constructor cu initializare prin parametri BigInt::BigInt(char semn,signed char numar[],int dim) {int i; s=semn; n=dim; for(i=0;i<n;i++) nr[i]=numar[n-i-1]; } //transform un int negativ in pozitiv int BigInt::Pozitiv(int x) {int a,vb; a=0; vb=0; while(vb==0) if((x+a)==0) vb=1; else a=a+1; x=a; return x; } //constructor dintr-un nr int obisnuit BigInt::BigInt(int x) {int i; if(x>=0) s='+'…

NUMERE PRIME ALGORITM C++

// NUMERE PRIME ALGORITM C++//  reediting from scratch //on this page is just the study for a next algoritm for generating the parime nr series like Fibonnaci or ....if possibile

74111121313417374414124343447 if u know the red part you can generate the orange part
1 0 1 111112222 1 1 23

o aplicatie php localitati romania

//APLICATIA SE REFERA LA BAZA DE DATE SIRUTA

//dragtable.js


/* dragtable v1.0 June 26, 2008 Dan Vanderkam, http://danvk.org/dragtable/ http://code.google.com/p/dragtable/ \Bsortabledraggable\B Instructions: - Download this file - Add <script src="dragtable.js"></script> to your HTML. - Add class="draggable" to any table you might like to reorder. - Drag the headers around to reorder them. This is code was based on: - Stuart Langridge's SortTable (kryogenix.org/code/browser/sorttable) - Mike Hall's draggable class (http://www.brainjar.com/dhtml/drag/) - A discussion of permuting table columns on comp.lang.javascript Licensed under the MIT license. */ // Here's the notice from Mike Hall's draggable script: //***************************************************************************** // Do not remove this notice. // // Copyright 2001 by Mike Hall. // See http…