我正在做一个用反波兰符号模拟科学计算器的项目。我的垃圾,如"1“、"1.23”、"2/3“等,都存储在一个堆栈中,并带有它们的类型。例如,"1“将作为Integer存储,"1.23”将存储为Double,等等,使用我的类型(由于各种原因,我定义了Double、Integer等)。
class Litteral {
public:
virtual QString toString () const = 0;
};我想这样做:当用户输入运算符(如+ )时,我需要在堆栈上弹出两次(),执行操作,并推()。我已经确认了操作员的身份。当用户输入+时,我执行给定的算法。我的问题是,当识别+和完成两个pop()时,我需要真正执行+,并且需要创建一个正确类型的对象。例如,如果我有一个Integer和一个Double,我必须:
Litteral* l = new Double(pop1+pop2);但如果我有两个整数,我就必须:
Litteral* l = new Integer(pop1 + pop2);等等..。我可以使用大量的'if‘来执行正确的"new ()“,但我觉得这不是一个好的解决方案。我还考虑过使用模板方法来执行相应的算法,但是我必须尽可能多地创建这个函数的版本。这仍然是人的可能,但同样,它感觉不干净。有什么干净的方法吗?
谢谢
编辑:例如,当我弹出2个整数时,我想执行一个给定的操作。此操作的结果必须是整数。但结果并不总是一个整数,它取决于我弹出的东西。如果是一个整数和一个双,结果将是一个双倍。所以我要做的‘新’的类型取决于我弹出的东西,我不想用开关/倍数来实现它。我不想那样做:
if ((typeid(*q1) == typeid(Integer)) && (typeid(*q2) == typeid(Double))) { //...}编辑2:
Literal& pop1 = stack.top();
stack.pop();
Literal& pop2 = stack.top();
stack.pop();
Literal& toAdd = (*pop1.clone() + *pop2.clone()); 错误:不匹配“operator+”(操作数类型为“文字”和“文字”)
课程:
class Literal {
public: virtual QString toString () const = 0;
virtual int getValue() const = 0;
virtual Litteral * clone() const = 0;
virtual Litteral& operator+(const Integer& l) = 0;
};
class Integer: public Literal {
friend class LitteralManager;
int value;
public:
Integer(int v) :value(v) {}
Integer(const Entier& e) { value = e.getValue(); };
virtual QString toString () const;
int getValue() const { return value; }
virtual Entier& operator+(const Entier& e);
Entier * clone() const;
};发布于 2016-05-24 14:33:02
#include <cassert>
#include <typeinfo>
using namespace std;
struct Double;
struct Integer;
struct Literal {
virtual Literal* operator + (const Literal&) const = 0;
virtual Literal* operator + (const Double&) const = 0;
virtual Literal* operator + (const Integer&) const = 0;};
struct Double : Literal {
Literal* operator + (const Literal& rhs) const {
return rhs + *this;}
Literal* operator + (const Double& rhs) const {
return new Double;}
Literal* operator + (const Integer& rhs) const {
return new Double;}};
struct Integer : Literal {
Literal* operator + (const Literal& rhs) const {
return rhs + *this;}
Literal* operator + (const Double& rhs) const {
return new Double;}
Literal* operator + (const Integer& rhs) const {
return new Integer;}};
int main () {
{
Literal* p = new Double;
Literal* q = new Double;
Literal* r = *p + *q;
assert(typeid(*r) == typeid(Double));
}
{
Literal* p = new Double;
Literal* q = new Integer;
Literal* r = *p + *q;
assert(typeid(*r) == typeid(Double));
}
{
Literal* p = new Integer;
Literal* q = new Double;
Literal* r = *p + *q;
assert(typeid(*r) == typeid(Double));
}
{
Literal* p = new Integer;
Literal* q = new Integer;
Literal* r = *p + *q;
assert(typeid(*r) == typeid(Integer));
}
return 0;}发布于 2016-05-21 20:37:58
#include <cassert>
#include <stack>
using namespace std;
struct Literal {
virtual Literal* clone () const = 0;};
struct Double : Literal {
Double* clone () const {
return new Double(*this);}};
struct Integer : Literal {
Integer* clone () const {
return new Integer(*this);}};
int main () {
stack<Literal*> x;
x.push(new Double());
x.push(new Integer());
Literal* p = x.top(); // the Integer
x.pop();
Literal* q = p->clone(); // create a new Integer
assert(typeid(*q) == typeid(Integer));
return 0;}发布于 2016-05-24 15:44:40
以下是一个可能的解决方案:
#include <exception>
#include <functional>
#include <iostream>
#include <cassert>
#include <memory>
class Literal {
public:
virtual ~Literal() = default;
};
class Double : public Literal {
public:
Double(double val) : m_val(val) { }
Double(const Double&) = default;
Double(Double&&) = default;
~Double() = default;
double value() const { return m_val; }
private:
double m_val;
};
class Integer : public Literal {
public:
Integer(int val) : m_val(val) { }
Integer(const Integer&) = default;
Integer(Integer&&) = default;
~Integer() = default;
int value() const { return m_val; }
private:
int m_val;
};
template <typename T> struct make_literal_result;
template <> struct make_literal_result<int> {
using type = Integer;
};
template<> struct make_literal_result<double> {
using type = Double;
};
template <typename T>
using make_literal_result_t = typename make_literal_result<T>::type;
template <typename T> std::unique_ptr<make_literal_result_t<T>> make_literal(T x) {
return std::make_unique<make_literal_result_t<T>>(x);
}
template <typename Res, typename Fn>
Res dynamic_apply(Fn f, const Literal* val) {
auto vald = dynamic_cast<const Double*>(val);
if (vald != nullptr)
return f(vald->value());
auto vali = dynamic_cast<const Integer*>(val);
if (vali != nullptr)
return f(vali->value());
throw std::invalid_argument("Invalid type");
}
std::unique_ptr<Literal> dynamic_plus(const Literal* x, const Literal* y) {
auto curried_plus = [](auto val1) -> std::function<std::unique_ptr<Literal>(const Literal*)> {
return [val1](const Literal* y) -> std::unique_ptr<Literal> {
return dynamic_apply<std::unique_ptr<Literal>>([val1](auto val2) {
return make_literal(val1 + val2);
}, y);
};
};
auto plus_x = dynamic_apply<std::function<std::unique_ptr<Literal>(const Literal*)>>(curried_plus, x);
return plus_x(y);
}
int main() {
Double x { 2.5 };
Integer y { 3 };
auto sum = dynamic_plus(&x, &y);
auto sum_Double = dynamic_cast<Double*>(sum.get());
assert(sum_Double != nullptr);
std::cout << "sum is: " << sum_Double->value() << '\n';
return 0;
}dynamic_plus的一般思想是:存储在plus_x中的结果是一个接受const Literal*并返回std::unique_ptr<Literal>的函数,它的身体在一个通用的lambda上使用dynamic_apply来完成它的工作。反过来,构造plus_x是make_literal在高级函数中的另一个应用程序.(然后,我们还需要使用std::function进行一些类型擦除,以便使dynamic_apply返回一些公共类型。)
https://stackoverflow.com/questions/37367303
复制相似问题