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

📄 cell.cpp

📁 parser in C++~~~~~~~~~~~~
💻 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"
// Reminder: cons.hpp expects nil to be defined somewhere.  For this
// implementation, this is the logical place to define it.
Cell* const nil = 0;

Cell::Cell(const int i)  
{ 
    int_m = i; 
    tag_m = type_int; 
} 
 
Cell::Cell(const double d) 
{ 
    double_m = d; 
    tag_m = type_double; 
} 
 
Cell::Cell(const char* const s) 
{ 
    symbol_m = new char[strlen(s) + 1]; 
    strcpy(symbol_m, s); 
    tag_m = type_symbol;   
} 
 
Cell::Cell(Cell* const my_car, Cell* const my_cdr) 
{ 
    conspair_m.car = my_car; 
    conspair_m.cdr = my_cdr; 
    tag_m = type_conspair; 
} 
 
bool Cell::is_int() const 
{ 
    if (tag_m == type_int) 
        return true; 
    else return false; 
} 
 
bool Cell::is_double() const 
{ 
    if (tag_m == type_double) 
        return true; 
    else return false; 
} 
 
bool Cell::is_symbol() const 
{ 
    if (tag_m == type_symbol) 
        return true; 
    else return false; 
} 
 
bool Cell::is_cons() const 
{ 
    if (tag_m == type_conspair) 
        return true; 
    else return false; 
} 
 
int Cell::get_int() const 
{ 
    if (is_int()) 
        return int_m; 
    else cerr<< "ERROR"; 
} 
 
double Cell::get_double() const 
{ 
    if (is_double()) 
        return double_m; 
    else cerr<< "ERROR"; 
} 
 
string Cell::get_symbol() const 
{ 
    if (is_symbol()) 
        return symbol_m; 
    else cerr<< "ERROR"; 
} 
 
Cell* Cell::get_car() const 
{ 
    if (is_cons()) 
        return conspair_m.car; 
    else cerr<< "ERROR"; 
} 
 
Cell* Cell::get_cdr() const 
{ 
    if (is_cons()) 
        return conspair_m.cdr; 
    else cerr<< "ERROR"; 
} 
 
 
void Cell::print(ostream& os) const 
{ 
    if (is_int()) 
        os<< int_m;   
     
    if (is_double()) 
        os<<showpoint << double_m; 
     
    if (is_symbol()) 
        os<< symbol_m; 
     
    if (is_cons()) 
    {  
        os<< "("; 
     
        const Cell *cur = this; 
        if (cur->conspair_m.car == nil) 
            os<< "()"; 
        else  
        {        
            while (cur != nil) 
            { 
                if (cur->conspair_m.car == nil) 
                    os<< "()";  
                else 
                { 
                    cur->conspair_m.car->print(os); 
                    os<< " "; 
                } 
                cur = cur->conspair_m.cdr; 
            }  
        } 
        os<< "\b)"; 
    } 
}

⌨️ 快捷键说明

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