⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cell.hpp

📁 building parser with C++ functions
💻 HPP
字号:
/**
 * \mainpage COMP151 Programming Assignment 2, Spring 2007
 *
 * \author Fok On Man
 * \author 06035253
 * \author cs_fom
 * \author L2A
 *
 * \date 28/03/2007
 *
 * Instructor: <a href="http://www.cs.ust.hk/~dekai/">Dekai Wu</a>
 * Due: 2007.03.28 at 23:00 by CASS
 */

/**
 * \file Cell.hpp
 *
 * Encapsulates the abstract interface for a concrete class-based
 * implementation of cells for a cons list data structure.
 */

#ifndef CELL_HPP
#define CELL_HPP

#include <cstddef>
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <stack>

using namespace std;

/**
 * \class Cell
 * \brief Class Cell
 */
class Cell {
public:
	/**
	 * \brief Check if this is an int cell.
	 * \return True iff this is an int cell.
	 */
	virtual bool is_int() const;

	/**
	 * \brief Check if this is a double cell.
	 * \return True iff this is a double cell.
	 */
	virtual bool is_double() const;

	/**
	 * \brief Check if this is a symbol cell.
	 * \return True iff this is a symbol cell.
	 */
	virtual bool is_symbol() const;

	/**
	 * \brief Check if this is a cons cell.
	 * \return True iff this is a cons cell.
	 */
	virtual bool is_cons() const;

	/**
	 * \brief Accessor (error if this is not an int cell).
	 * \return The value in this int cell.
	 */
	virtual int get_int() const;

	/**
	 * \brief Accessor (error if this is not a double cell).
	 * \return The value in this double cell.
	 */
	virtual double get_double() const;

	/**
	 * \brief Accessor (error if this is not a symbol cell).
	 * \return The symbol name in this symbol cell.
	 */
	virtual string get_symbol() const;

	/**
	 * \brief Accessor (error if this is not a cons cell).
	 * \return First child cell.
	 */
	virtual Cell* get_car() const;

	/**
	 * \brief Accessor (error if this is not a cons cell).
	 * \return Rest child cell.
	 */
	virtual Cell* get_cdr() const;

	/**
	 * \brief Print the subtree rooted at this cell, in s-expression notation.
	 * \param os The output stream to print to.
	 */
	virtual void print(ostream& os = cout) const = 0;

	/**
	 * \brief To do the operator "ceiling".
	 */
	virtual Cell* cell_ceil() const;

	/**
	 * \brief To do the operator "floor".
	 */
	virtual Cell* cell_floor() const;

	/**
	 * \brief To do the operator "+".
	 */
	virtual void cell_sum(int &i, double &result) const;

	/**
	 * \brief To do the operator "*".
	 */
	virtual void cell_times(int &i, double &result) const;

	/**
	 * \brief To do the operator "-".
	 */
	virtual void cell_sub(int &i, double &result) const;

	/**
	 * \brief To do the operator "/".
	 */
	virtual void cell_div(int &i, double &result) const;

	/**
	 * \brief To check if it is zero.
	 */
	virtual bool is_zero() const;

	virtual bool is_list() const;
};

class IntCell:public Cell {
private:
	int int_m;
public:
	/**
	 * \brief Constructor of IntCell.
	 */
	IntCell (const int i);

	/**
	 * \brief Check if this is a double cell.
	 * \return True iff this is a double cell.
	 */
	virtual bool is_int() const;

	/**
	 * \brief Accessor (error if this is not an int cell).
	 * \return The value in this int cell.
	 */
	virtual int get_int() const;

	/**
	 * \brief Print the subtree rooted at this cell, in s-expression notation.
	 * \param os The output stream to print to.
	 */
	virtual void print(ostream& os = cout) const;

	/**
	 * \brief To do the operator "+".
	 */
	virtual void cell_sum(int &i, double &result) const;

	/**
	 * \brief To do the operator "*".
	 */
	virtual void cell_times(int &i, double &result) const;

	/**
	 * \brief To do the operator "-".
	 */
	virtual void cell_sub(int &i, double &result) const;

	/**
	 * \brief To do the operator "/".
	 */
	virtual void cell_div(int &i, double &result) const;

	/**
	 * \brief To check if it is zero.
	 */
	virtual bool is_zero() const;
};

class DoubleCell:public Cell {
private:
	double double_m;
public:
	/**
	* \brief Constructor of DoubleCell.
	*/
	DoubleCell (const double d);

	/**
	 * \brief Check if this is a double cell.
	 * \return True iff this is a double cell.
	 */
	virtual bool is_double() const;

	/**
	 * \brief Accessor (error if this is not a double cell).
	 * \return The value in this double cell.
	 */
	virtual double get_double() const;

	/**
	 * \brief Print the subtree rooted at this cell, in s-expression notation.
	 * \param os The output stream to print to.
	 */
	virtual void print(ostream& os = cout) const ;

	/**
	 * \brief To do the operator "ceiling".
	 */
	virtual Cell* cell_ceil() const;

	/**
	 * \brief To do the operator "floor".
	 */
	virtual Cell* cell_floor() const;

	/**
	 * \brief To do the operator "+".
	 */
	virtual void cell_sum(int &i, double &result) const;

	/**
	 * \brief To do the operator "*".
	 */
	virtual void cell_times(int &i, double &result) const;

	/**
	 * \brief To do the operator "-".
	 */
	virtual void cell_sub(int &i, double &result) const;

	/**
	 * \brief To do the operator "/".
	 */
	virtual void cell_div(int &i, double &result) const;

	/**
	 * \brief To check if it is zero.
	 */
	virtual bool is_zero() const;
};

class SymbolCell:public Cell {
private:
	char* symbol_m;
public:
	/**
	* \brief Constructor of SymbolCell.
	*/
	SymbolCell (const char* const s);

	/**
	 * \brief Check if this is a symbol cell.
	 * \return True iff this is a symbol cell.
	 */
	virtual bool is_symbol() const;

	/**
	 * \brief Accessor (error if this is not a symbol cell).
	 * \return The value in this sumbol cell.
	 */
	virtual string get_symbol() const;

	/**
	 * \brief Print the subtree rooted at this cell, in s-expression notation.
	 * \param os The output stream to print to.
	 */
	virtual void print(ostream& os = cout) const;
};

class ConsCell:public Cell {
private:
	Cell *car;
	Cell *cdr;
public:
	/**
	* \brief Constructor of ConsCell.
	*/
	ConsCell (Cell* const my_car, Cell* const my_cdr);

	/**
	 * \brief Check if this is a cons cell.
	 * \return True iff this is a cons cell.
	 */
	virtual bool is_cons() const;

	/**
	 * \brief Accessor (error if this is not a cons cell).
	 * \return First child cell.
	 */
	virtual Cell* get_car() const;

	/**
	 * \brief Accessor (error if this is not a cons cell).
	 * \return Rest child cell.
	 */
	virtual Cell* get_cdr() const;

	/**
	 * \brief Print the subtree rooted at this cell, in s-expression notation.
	 * \param os The output stream to print to.
	 */
	virtual void print(ostream& os = cout) const;

	virtual bool is_list() const;
};
// Reminder: cons.hpp expects nil to be defined somewhere (for this
// implementation, Cell.cpp is the logical place to define it).
// Here we promise this again, just to be safe.
extern Cell* const nil;

#endif // CELL_HPP



⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -