在下列程序中,insert、creat、inorder函数的作用分别是什么,程序如下:#include<iostream> using namespace std; typedef int KeyType; typedef struct { KeyType key; } ElemType; typedef struct BitNode { ElemType data; struct BitNode* lchild, * rchild; }BitNode, * BiTree; BiTree insert(BiTree b, BiTree s) { if (b == NULL) b = s; else if (s->data.key > b->data.key) b->rchild = insert(b->rchild, s); else if (s->data.key < b->data.key) b->lchild = insert(b->lchild, s); return b; } BiTree creat() { int k; BiTree t, s; t = NULL; scanf_s("%d", &k); while (k != -1) { s = (BiTree)malloc(sizeof(BitNode)); s->data.key = k; s->lchild = NULL; s->rchild = NULL; t = insert(t, s); scanf_s("%d", &k); } return t; } void inorder(BiTree t) { if (t) { inorder(t->lchild); printf_s("%3d", t->data); inorder(t->rchild); } } void main() { BiTree t; printf_s("输入数据,以-1结尾:"); t = creat(); printf_s("顺序为:"); inorder(t); }
时间: 2024-04-26 14:21:55 浏览: 14
这段程序是实现二叉排序树的建立和中序遍历。具体来说,insert函数是用来将新节点插入到二叉排序树中的,它的参数b代表二叉排序树的根节点,参数s代表要插入的新节点;creat函数是用来创建二叉排序树的,它会调用insert函数将每个输入的数值插入到二叉排序树中,并返回建立好的二叉排序树的根节点;inorder函数是用来实现中序遍历的,它的参数t代表要遍历的二叉排序树的根节点。整个程序的主函数main只是调用了creat函数和inorder函数,用来测试建立和遍历二叉排序树的结果。
相关问题
异常原因#include <iostream> #include<memory> #include<string> using namespace std; class Product { public: virtual void Creat() = 0; }; class ProductA : public Product { public: void Creat() { cout << "创建产品A"; }; }; class ProductB : public Product { public: void Creat() { cout << "创建产品B"; } }; class Factory { public: virtual void makeProduct() = 0; void setProduct(char type_of_product) { if (type_of_product == 'a'){ unique_ptr<Product> productA = make_unique<ProductA>(); this->product = productA.get(); } }; protected: Product* product; }; class Factory1 : public Factory { public: void makeProduct() { cout << "工厂1:"; this->product->Creat(); } }; class Factory2 : public Factory { public: void makeProduct() { cout << "工厂2:"; this->product->Creat(); } }; int main() { unique_ptr<Product> productA = make_unique<ProductA>(); unique_ptr<Factory> factory1 = make_unique<Factory1>(); factory1->setProduct('A'); factory1->makeProduct(); return 0; }
这份代码中存在一些问题:
1. `Factory::setProduct()` 中只有对产品A的处理,没有对产品B的处理。
2. `Factory::product` 指针没有进行初始化,可能会导致程序崩溃。
3. `unique_ptr<Factory> factory1 = make_unique<Factory1>();` 中的 `Factory1` 类没有定义默认构造函数,因此需要手动提供一个构造函数。
以下是修改后的代码:
```
#include <iostream>
#include <memory>
#include <string>
using namespace std;
class Product {
public:
virtual void Creat() = 0;
};
class ProductA : public Product {
public:
void Creat() {
cout << "创建产品A";
};
};
class ProductB : public Product {
public:
void Creat() {
cout << "创建产品B";
}
};
class Factory {
public:
virtual void makeProduct() = 0;
virtual ~Factory() {}
void setProduct(char type_of_product) {
if (type_of_product == 'a') {
unique_ptr<Product> productA = make_unique<ProductA>();
this->product = productA.get();
}
else if (type_of_product == 'b') {
unique_ptr<Product> productB = make_unique<ProductB>();
this->product = productB.get();
}
};
protected:
Product* product = nullptr;
};
class Factory1 : public Factory {
public:
Factory1() {}
void makeProduct() {
cout << "工厂1:";
if (product) {
product->Creat();
}
else {
cout << "未设置产品" << endl;
}
}
};
class Factory2 : public Factory {
public:
Factory2() {}
void makeProduct() {
cout << "工厂2:";
if (product) {
product->Creat();
}
else {
cout << "未设置产品" << endl;
}
}
};
int main() {
unique_ptr<Factory> factory1 = make_unique<Factory1>();
factory1->setProduct('a');
factory1->makeProduct();
return 0;
}
```
修改后的代码中,`Factory::setProduct()` 中加入了对产品B的处理;`Factory::product` 指针进行了初始化;`Factory1` 和 `Factory2` 中分别添加了默认构造函数并且在 `makeProduct()` 中判断是否设置了产品。
#include "fstream" #include <iomanip> #include <io.h> #include <fcntl.h>
#include "fstream" 是C++中用于文件操作的头文件之一,可以用于读写文件。其中包括了 ifstream 和 ofstream 两个类,分别用于文件的输入和输出。
<iomanip> 是一个输入输出流操作符头文件,可以用于设置流格式,如设置输出宽度、精度等。
<io.h> 和 <fcntl.h> 是用于 Windows 平台下的文件操作的头文件。其中 <io.h> 包含了一些与文件操作相关的函数和宏定义,如 _open、_close、_read、_write 等;<fcntl.h> 中包含了一些文件操作的常量和函数,如 O_RDONLY、O_WRONLY、O_CREAT、_setmode 等。
如果您需要在 Windows 平台下进行文件操作,则可以使用这两个头文件。但是在其他平台上,可能会使用不同的头文件或者不需要引入这些头文件。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)