bool ReadFileToArray(char fileName[], char strLine[100][3], int &nArrLen) { //fileName[]存放文件名 strLine[][3]存放结点的二维数组 nArrLen返回二叉树结点个数 FILE *pFile; char str[1000]; //存放读取一行文本的字符串 pFile = fopen(fileName, "r"); if (!pFile) { printf ("二叉树文件读取失败\n"); return false; } //读取文件第一行,判断二叉树标识BinaryTree是否正确 if (fgets (str, 1000, pFile) != NULL) { if (strcmp (str, "BinaryTree\n") == 0) { printf ("打开文件格式错误\n"); fclose (pFile); return false; } } nArrLen = 0; //循环读取结点行数据存入数组,结点总数加1 while (fscanf (pFile, "%c %c %c\n", &strLine[nArrLen][0], &strLine[nArrLen][1], &strLine[nArrLen][2]) != EOF) nArrLen++; fclose (pFile); return true; }
时间: 2024-02-14 10:24:43 浏览: 17
这段代码定义了一个ReadFileToArray函数,函数的作用是从文件中读取二叉树的结点数据,并将其存储到一个二维字符数组中。函数的参数包括文件名fileName、存放结点数据的二维字符数组strLine和结点个数nArrLen。
函数首先打开文件并检查是否成功打开,如果文件打开失败则返回false。然后读取文件的第一行,判断二叉树标识是否正确,如果不正确则返回false。接着进入一个循环,每次读取一行结点数据并存储到二维字符数组中,同时结点个数增加1。最后关闭文件并返回true。
需要注意的是,该函数中的文件操作使用了C标准库函数fopen、fgets和fscanf,需要在代码文件中包含头文件stdio.h。另外,该函数中使用了一个引用类型的参数nArrLen,表示函数执行结束后该参数的值会被修改。
相关问题
帮我用c++语言完善下列程序,#include <iostream> using namespace std; #define OK 1 #define ERROR 0 typedef int Status; typedef int ElemType; const int MAXSIZE=100; typedef struct { ElemType *base; int front; int rear; }SeqQueue; void InitQueue( SeqQueue &Q ) {//申请队列Q的数据区,构建空的循环队列 } Status EnQueue( SeqQueue &Q,ElemType x) { // 入队 } Status DeQueue( SeqQueue &Q,ElemType &e) { // 出队 } bool QueueEmpty(SeqQueue &Q) { //判队空 } bool QueueFull(SeqQueue &Q) {//判队满 } void DestroyQueue( SeqQueue &Q) // 销毁 { if(Q.base) { delete []Q.base; Q.base=NULL; Q.front=Q.rear=0; } }
#include <iostream>
using namespace std;
#define OK 1
#define ERROR 0
typedef int Status;
typedef int ElemType;
const int MAXSIZE = 100;
typedef struct {
ElemType *base;
int front;
int rear;
} SeqQueue;
void InitQueue(SeqQueue &Q) {
//申请队列Q的数据区,构建空的循环队列
Q.base = new ElemType[MAXSIZE];
Q.front = Q.rear = 0;
}
Status EnQueue(SeqQueue &Q, ElemType x) {
// 入队
if ((Q.rear + 1) % MAXSIZE == Q.front) {
// 队满
return ERROR;
}
Q.base[Q.rear] = x;
Q.rear = (Q.rear + 1) % MAXSIZE;
return OK;
}
Status DeQueue(SeqQueue &Q, ElemType &e) {
// 出队
if (Q.front == Q.rear) {
// 队空
return ERROR;
}
e = Q.base[Q.front];
Q.front = (Q.front + 1) % MAXSIZE;
return OK;
}
bool QueueEmpty(SeqQueue &Q) {
//判队空
return Q.front == Q.rear;
}
bool QueueFull(SeqQueue &Q) {
//判队满
return (Q.rear + 1) % MAXSIZE == Q.front;
}
void DestroyQueue(SeqQueue &Q) {
// 销毁
if (Q.base) {
delete[] Q.base;
Q.base = NULL;
Q.front = Q.rear = 0;
}
}
int main() {
SeqQueue Q;
InitQueue(Q);
EnQueue(Q, 1);
EnQueue(Q, 2);
EnQueue(Q, 3);
int e;
DeQueue(Q, e);
cout << e << endl;
DestroyQueue(Q);
return 0;
}
class RoomManager { private: // Conference Room 1, the smaller one. // Only require the date information for appointment. set<int> _room1; // Conference Room 2, the bigger one. // Require both date and event information for appointment. map<int, string> _room2; public: // Insert an appointment to the smaller Conference Room (1). bool InsertAppointment(const int& date); // Insert an appointment to the bigger Conference Room (2). bool InsertAppointment(const int& date, const string& event); // Cancel an appointment on designated date. // If room_id == 1, cancel the corresponding appointment of _room1. // If room_id == 2, cancel the corresponding appointment of _room2. bool CancelAppointment(const int& room_id, const int& date); // Print all existing appointments in chronological order. // If room_id == 1, just print the date information. Each piece of information is separated by ' '. // If room_id == 2, print the information in form of "date(event)". Each piece of information is separated by ' '. // If there is no even one existing appointment yet, print "No Appointment". void PrintAppointments(const int& room_id) const; };写完这个代码
,我们可以使用 RoomManager 类来管理两个会议室的预约信息。其中,Conference Room 1 只需要日期信息即可进行预约,而 Conference Room 2 需要日期和事件信息才能进行预约。
下面是完整的 RoomManager 类的代码实现:
```c++
#include <iostream>
#include <set>
#include <map>
using namespace std;
class RoomManager {
private:
// Conference Room 1, the smaller one.
// Only require the date information for appointment.
set<int> _room1;
// Conference Room 2, the bigger one.
// Require both date and event information for appointment.
map<int, string> _room2;
public:
// Insert an appointment to the smaller Conference Room (1).
bool InsertAppointment(const int& date) {
if (_room1.find(date) != _room1.end()) {
// There is already an appointment on this date.
return false;
} else {
_room1.insert(date);
return true;
}
}
// Insert an appointment to the bigger Conference Room (2).
bool InsertAppointment(const int& date, const string& event) {
if (_room2.find(date) != _room2.end()) {
// There is already an appointment on this date.
return false;
} else {
_room2[date] = event;
return true;
}
}
// Cancel an appointment on designated date.
// If room_id == 1, cancel the corresponding appointment of _room1.
// If room_id == 2, cancel the corresponding appointment of _room2.
bool CancelAppointment(const int& room_id, const int& date) {
if (room_id == 1) {
if (_room1.find(date) != _room1.end()) {
_room1.erase(date);
return true;
} else {
// There is no appointment on this date.
return false;
}
} else if (room_id == 2) {
if (_room2.find(date) != _room2.end()) {
_room2.erase(date);
return true;
} else {
// There is no appointment on this date.
return false;
}
} else {
// Invalid room_id.
return false;
}
}
// Print all existing appointments in chronological order.
// If room_id == 1, just print the date information. Each piece of information is separated by ' '.
// If room_id == 2, print the information in form of "date(event)". Each piece of information is separated by ' '.
// If there is no even one existing appointment yet, print "No Appointment".
void PrintAppointments(const int& room_id) const {
if (room_id == 1) {
if (_room1.empty()) {
cout << "No Appointment";
} else {
for (const auto& date : _room1) {
cout << date << " ";
}
}
} else if (room_id == 2) {
if (_room2.empty()) {
cout << "No Appointment";
} else {
for (const auto& appointment : _room2) {
cout << appointment.first << "(" << appointment.second << ") ";
}
}
} else {
// Invalid room_id.
cout << "Invalid room_id";
}
cout << endl;
}
};
int main() {
RoomManager rm;
rm.InsertAppointment(20220101);
rm.InsertAppointment(20220102, "Meeting with John");
rm.InsertAppointment(20220103);
rm.InsertAppointment(20220102, "Meeting with Peter");
rm.PrintAppointments(1);
rm.PrintAppointments(2);
rm.CancelAppointment(1, 20220103);
rm.CancelAppointment(2, 20220102);
rm.PrintAppointments(1);
rm.PrintAppointments(2);
return 0;
}
```
这里的 main 函数演示了 RoomManager 类的使用方法。我们可以看到,通过 RoomManager 类,我们可以方便地进行会议室预约、取消预约和打印预约信息等操作。