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

📄 ctree.c

📁 《c++ 实践之路》光盘中的源代码
💻 C
字号:
/* (c) Bartosz Milewski 2000 */
#include "ctree.h"
#include <stdio.h>
#include <malloc.h>

double Calc (struct Node * pNode)
{
	double x = 0.0;
	switch (pNode->type)
	{
		case NUM_NODE:
			x =((struct NumNode *)pNode)->val;
			break;
		case ADD_NODE:
		{
			struct BinNode * pN = (struct BinNode *) pNode;
			x = Calc (pN->pLeft) + Calc (pN->pRight);
			break;
		}
		case MULT_NODE:
		{
			struct BinNode * pN = (struct BinNode *) pNode;
			x = Calc (pN->pLeft) * Calc (pN->pRight);
			break;
		}
		default:
			printf ("Bad node type\n");
	}
	return x;
}

struct Node * CreateNumNode (double value)
{
	struct NumNode * pNode = 
			malloc (sizeof (struct NumNode));
	pNode->type = NUM_NODE;
	pNode->val = value;
	return (struct Node *) pNode;
}

struct Node * CreateBinNode (int type, struct Node * pLeft, struct Node * pRight)
{
	struct BinNode * pNode = 
			malloc (sizeof (struct BinNode));
	pNode->type = type;
	pNode->pLeft = pLeft;
	pNode->pRight = pRight;
	return (struct Node *) pNode;
}

int main ()
{
	struct Node *pNode1, *pNode2, *pNode3, *pNode4, *pNode5;
	double x;
	/* ( 20.0 + (-10.0) ) * 0.1 */
	pNode1 = CreateNumNode (20.0);
	pNode2 = CreateNumNode (-10.0);
	pNode3 = CreateBinNode (ADD_NODE, pNode1, pNode2);
	pNode4 = CreateNumNode (0.1);
	pNode5 = CreateBinNode (MULT_NODE, pNode3, pNode4);
	
	printf ("Calculating the tree\n");
	x = Calc (pNode5);
	printf ("Result: %lf\n", x);
	
	free (pNode1);
	free (pNode2);
	free (pNode3);
	free (pNode4);
	free (pNode5);
	return 0;
}

⌨️ 快捷键说明

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