茄子在线看片免费人成视频,午夜福利精品a在线观看,国产高清自产拍在线观看,久久综合久久狠狠综合

    <s id="ddbnn"></s>
  • <sub id="ddbnn"><ol id="ddbnn"></ol></sub>

  • <legend id="ddbnn"></legend><s id="ddbnn"></s>

    C++二叉樹計算表達式
    來源:易賢網(wǎng) 閱讀:2793 次 日期:2015-04-16 15:47:14
    溫馨提示:易賢網(wǎng)小編為您整理了“C++二叉樹計算表達式”,方便廣大網(wǎng)友查閱!

    C++二叉樹計算表達式,具體代碼如下:

    #include <iostream>

    using namespace std;

    class calc

    {

    enum Type {DATA, ADD, SUB, MULTI, DIV, OPAREN, CPAREN, EOL};

    struct node

    {

    Type type;

    int data;

    node *lchild, *rchild;

    node(Type t, int d=0, node *lc=NULL, node *rc=NULL)

    {

    type=t; data=d; lchild=lc; rchild=rc;

    }

    };

    node *root;

    node *create(char * &s);

    Type getToken (char * &s, int &value);

    int result (node *t);

    public:

    calc (char *s)

    {

    root=create(s);

    }

    int result()

    {

    if (root==NULL) return 0;

    return result(root);

    }

    };

    calc::node *calc::create(char * &s)

    {

    node *p, *root=NULL;

    Type returnType,flag=DATA;

    int value;

    while (*s)

    {

    flag=returnType;

    returnType=getToken(s,value);

    switch (returnType)

    {

    case DATA:

    case OPAREN:

    if (returnType == DATA) p=new node(DATA,value);

    else p=create(s);

    if (root==NULL) root=p;

    else if (root->rchild==NULL) root->rchild=p;

    else root->rchild->rchild=p;

    break;

    case CPAREN:

    case EOL: return root;

    case ADD:

    case SUB:

    root=new node(returnType,0,root);

    break;

    case MULTI:

    case DIV:

    if (root->type==DATA || root->type==MULTI || root->type==DIV || flag==OPAREN)

    root=new node(returnType,0,root);

    else

    root->rchild=new node(returnType,0,root->rchild);

    }

    }

    return root;

    }

    calc::Type calc::getToken(char *&s, int &data)

    {

    char type;

    while (*s==' ') ++s;

    if (*s>='0' && *s<='9')

    {

    data=0;

    while (*s>='0' && *s<='9') {data=data*10+ *s-'0'; ++s;}

    return DATA;

    }

    if (*s == '\0') return EOL;

    type =*s; ++s;

    switch(type)

    {

    case '+':return ADD;

    case '-':return SUB;

    case '*':return MULTI;

    case '/':return DIV;

    case '(':return OPAREN;

    case ')':return CPAREN;

    default: return EOL;

    }

    }

    int calc::result(node *t)

    {

    int num1,num2;

    if (t->type == DATA) return t->data;

    num1=result(t->lchild);

    num2=result(t->rchild);

    switch(t->type)

    {

    case ADD:t->data=num1+num2;break;

    case SUB:t->data=num1-num2;break;

    case MULTI: t->data=num1*num2;break;

    case DIV:t->data=num1/num2;break;

    }

    return t->data;

    }

    int main()

    {

    char expression[256];

    cin>>expression;

    calc exp(expression);

    cout<<exp.result()<<endl;

    return 0;

    }

    更多信息請查看IT技術專欄

    更多信息請查看技術文章
    易賢網(wǎng)手機網(wǎng)站地址:C++二叉樹計算表達式

    2026國考·省考課程試聽報名

    • 報班類型
    • 姓名
    • 手機號
    • 驗證碼
    關于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡要咨詢 | 簡要咨詢須知 | 新媒體/短視頻平臺 | 手機站點 | 投訴建議
    工業(yè)和信息化部備案號:滇ICP備2023014141號-1 云南省教育廳備案號:云教ICP備0901021 滇公網(wǎng)安備53010202001879號 人力資源服務許可證:(云)人服證字(2023)第0102001523號
    聯(lián)系電話:0871-65099533/13759567129 獲取招聘考試信息及咨詢關注公眾號:hfpxwx
    咨詢QQ:1093837350(9:00—18:00)版權所有:易賢網(wǎng)