#if/#endif的用法

话不多说请看代码`

#include<stdio.h>
#include<stdlib.h>

int main()
{
#if 0
	int a = 0;
	int b = 0;
	int min = 0;
	int i = 0;
	int max = 0;

	printf("请输入两个数");
	scanf_s("%d%d", &a,&b);
	for (i = 2; i <=( min = a < b ? a : b);i++)
	{
		if (0 == a % i && 0 == b % i)
			max = i;
	}
	if (0 == max)
		printf("两个数互质");
	else
		printf("两个数的最大公约数是%d\n", max);
#endif
	int a = 0, b = 1;
	int* p = &a;
	int* q = &b;
	printf("a=%d,b=%d\n", *q, *p);
	system("pause");
	return 0;
}

注意到如果#if 后面的是0,那么执行#endif 后面的代码。反之为1的话,相反。

  • 12
    点赞
  • 30
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
在 C++ 中,可以定义一个类来实现 IRBNode 接口,并实现接口中声明的成员方法和属性。具体实现方法如下: ```c++ enum Color { RED, BLACK }; class IRBNode : public IComparable { public: virtual IRBNode* Left() const = 0; virtual void Left(IRBNode* node) = 0; virtual IRBNode* Right() const = 0; virtual void Right(IRBNode* node) = 0; virtual Color Color() const = 0; virtual void Color(Color color) = 0; virtual IRBNode* Parent() const = 0; virtual void Parent(IRBNode* node) = 0; virtual IRBNode* Grandparent() const = 0; virtual IRBNode* Sibling() const = 0; virtual IRBNode* Uncle() const = 0; virtual void AssignValueTo(IRBNode* other) const = 0; }; ``` 在实现类中,可以根据具体的需求进行实现,比如: ```c++ class RBNode : public IRBNode { public: // 实现 IComparable 接口 int CompareTo(const IComparable& other) const override { const RBNode& otherNode = dynamic_cast<const RBNode&>(other); // 按照某种规则比较节点 if (this->value > otherNode.value) { return 1; } else if (this->value < otherNode.value) { return -1; } else { return 0; } } // 实现 IRBNode 接口 IRBNode* Left() const override { return this->left; } void Left(IRBNode* node) override { this->left = node; } IRBNode* Right() const override { return this->right; } void Right(IRBNode* node) override { this->right = node; } Color Color() const override { return this->color; } void Color(Color color) override { this->color = color; } IRBNode* Parent() const override { return this->parent; } void Parent(IRBNode* node) override { this->parent = node; } IRBNode* Grandparent() const override { if (this->parent != nullptr) { return this->parent->Parent(); } else { return nullptr; } } IRBNode* Sibling() const override { if (this->parent != nullptr) { if (this == this->parent->Left()) { return this->parent->Right(); } else { return this->parent->Left(); } } else { return nullptr; } } IRBNode* Uncle() const override { if (this->parent != nullptr) { return this->parent->Sibling(); } else { return nullptr; } } void AssignValueTo(IRBNode* other) const override { RBNode* otherNode = dynamic_cast<RBNode*>(other); otherNode->value = this->value; // 其他属性的赋值 // ... } private: int value; RBNode* left; RBNode* right; Color color; RBNode* parent; }; ``` 在实际使用时,可以通过创建对象来调用实现的方法和属性,如下所示: ```c++ RBNode* node1 = new RBNode(); RBNode* node2 = new RBNode(); // 设置节点的值和属性 // ... if (node1->CompareTo(*node2) > 0) { // 节点 1 大于节点 2 // ... } ``` 注意,在 C++ 中需要手动管理内存,需要在不需要使用对象时手动释放内存,避免内存泄漏。
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值