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

📄 cell.cpp

📁 building parser with C++ functions
💻 CPP
字号:
/**
 * \file Cell.cpp
 *
 * A partial dummy implementation of the Cell.hpp interface for
 * example purposes, to support the cons list ADT interface specified
 * in cons.hpp.  Just barely enough is implemented to allow the parser
 * to run.  None of the accessor member functions are implemented.
 *
 * You need to replace this with a real implementation that actually
 * builds a tree data structure and then lets the user access the tree.
 *
 * You don't necessarily need to understand the ugly details of this
 * dummy implementation, which you'll throw away anyhow.
 */

#include "Cell.hpp"
#include "math.h"
// Reminder: cons.hpp expects nil to be defined somewhere.  For this
// implementation, this is the logical place to define it.
Cell* const nil = 0;

IntCell::IntCell (const int i) {
	int_m = i;
};

DoubleCell::DoubleCell (const double d) {
	double_m = d;
};

SymbolCell::SymbolCell (const char* const s) {
	symbol_m = new char[strlen(s) + 1];
	strcpy(symbol_m, s);
};

ConsCell::ConsCell (Cell* const my_car, Cell* const my_cdr) {
	car = my_car;
	cdr = my_cdr;
};

bool Cell::is_int() const {
	return false;
};

bool Cell::is_double() const{
	return false;
};

bool Cell::is_symbol() const{
	return false;
};

bool Cell::is_cons() const{
	return false;
};

bool IntCell::is_int() const {
	return true;
};

bool DoubleCell::is_double() const {
	return true;
};

bool SymbolCell::is_symbol() const {
	return true;
};

bool ConsCell::is_cons() const {
	return true;
};

int Cell::get_int () const {
	cerr << "ERROR"<< endl;
	exit(1);
};

double Cell::get_double () const {
	cerr<< "ERROR"<< endl;
	exit(1);
};

string Cell::get_symbol  () const {
	cerr << "ERROR"<< endl;
	exit(1);
};

Cell* Cell::get_car () const {
	cerr << "ERROR"<< endl;
	exit(1);
};

Cell* Cell::get_cdr () const {
	cerr << "ERROR"<< endl;
	exit(1);
};

int IntCell::get_int () const {
	return int_m;
};

double DoubleCell::get_double () const {
	return double_m;
};

string SymbolCell::get_symbol () const {
	return symbol_m;
};

Cell* ConsCell::get_car () const {
	return car;
};

Cell* ConsCell::get_cdr () const {
	return cdr;
};

void IntCell::print(ostream& os) const {
	os<< int_m;
	return;
};

void DoubleCell::print(ostream& os) const {
        os<< showpoint<< double_m;
	return;
};

void SymbolCell::print(ostream& os) const {
	os<< symbol_m;
	return;
};

void ConsCell::print(ostream& os) const  {
	const Cell* cur = this;
	os << "(";

	while (cur != nil)
	{
		if (cur->get_car() == nil)
			os << "()";
		else
		{
			cur->get_car()->print(os);

			if (cur->get_cdr() != nil)
			os << ' ';
		}
		cur = cur->get_cdr();
	}

	os <<")";
}

Cell* Cell::cell_ceil() const {
	cerr<< "ERROR: wrong type of operand"<< endl;
	exit(1);
};

Cell* DoubleCell::cell_ceil() const {
	Cell *result = new IntCell(int(ceil(get_double())));
	return result;
};

Cell* Cell::cell_floor() const {
	cerr<< "ERROR: wrong type of operand"<< endl;
	exit(1);
};

Cell* DoubleCell::cell_floor() const {
	Cell *result = new IntCell(int(floor(get_double())));
	return result;
};

void Cell::cell_sum(int &i, double &result) const {
	cerr<< "ERROR: wrong type of operand"<< endl;
	exit(1);
};

void IntCell::cell_sum(int &i, double &result) const {
	result += get_int();
};

void DoubleCell::cell_sum(int &i, double &result) const {
	i = 1;
	result += get_double();
};


void Cell::cell_times(int &i, double &result) const {
	cerr<< "ERROR: wrong type of operand"<< endl;
	exit(1);
};

void IntCell::cell_times(int &i, double &result) const {
	result *= get_int();
};

void DoubleCell::cell_times(int &i, double &result) const {
	i = 1;
	result *= get_double();
};


void Cell::cell_sub(int &i, double &result) const {
	cerr<< "ERROR: wrong type of operand"<< endl;
	exit(1);
};

void IntCell::cell_sub(int &i, double &result) const {
	result -= get_int();
};

void DoubleCell::cell_sub(int &i, double &result) const {
	i = 1;
	result -= get_double();
};

void Cell::cell_div(int &i, double &result) const {
	cerr<< "ERROR: wrong type of operand"<< endl;
	exit(1);
};

void IntCell::cell_div(int &i, double &result) const {
	result /= get_int();
	if (i==0)
		result = int(result);
};

void DoubleCell::cell_div(int &i, double &result) const {
	i = 1;
	result /= get_double();
};

bool Cell::is_zero() const {
	return false;
}

bool IntCell::is_zero() const {
	if (get_int() == 0)
		return true;
	else return false;
}

bool DoubleCell::is_zero() const {
	if (get_double() == 0.0)
		return true;
	else return false;
}

bool Cell::is_list() const {
	return false;
}

bool ConsCell::is_list() const {
	return true;
}

⌨️ 快捷键说明

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