?? tree.cpp
字號:
// tree.cpp : Defines the entry point for the console application.
//
//#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "treeOp.h"
/* 測試函數*/
void test_tree();
void write_testcfg();
int main()
{
write_testcfg();
/* 調用測試函數*/
test_tree();
return 0;
}
void test_tree()
{
char path[] = "5:31:34:41"; /* 結點 1 的路徑 */
char path2[]= "1:7:9"; /* 結點 2 的路徑 */
char path3[]= "3:16:20"; /* 結點 3 的路徑 */
char *pathp = path; /* 指向結點路徑的指針 */
char *oldpath = NULL;
char *nextpath = NULL;
char *value = NULL; /* path對應的結點值或第一個類型為 1 的并列結點的路徑*/
int flag = NODE_IS_LEAF; /* 輸入輸出參數。輸入時表示結點內型,取值為 NODE_IS_LEAF(0) 或 NODE_IS_BRANCH(1);若 path 對應的結點為內部結點,并且 path的下一個類型為 flag的結點存在,則 flag 的返回值為 NEXT_NODE_EXIST;flag 在這種情況必須先初始化后再傳遞給get_value函數*/
/* 1、定義樹根結點*/
struct tree_node *root = NULL;
//struct tree_node *init_cfg(char *cfg_filepath, char *task_filepath, char **argv, char *direct)
/* 2、創建樹*/
root = init_cfg( (char*)("test.cfg"), (char*)NULL, (char**)NULL,(char*)("00100"));
if (NULL == root)
{
/* 創建樹失敗*/
return ;
}
/* 在屏幕上打印沒進行任操作之前的整棵樹*/
printf("沒進行任操作之前的整棵樹:\n");
print_tree(root,0);
getchar();
printf("\n\n");
/**************************
* 3、獲取的值 -- start *
**************************/
/* 獲取路徑path 對應的結點的值, */
/* 如果 path 對應的結點是內部結點, */
/* 則得到第一個類型為 flag 的子結點的路徑 */
value = get_value(root, pathp, &flag);
if (NULL != value) /* 如果獲取成功*/
{
if (NEXT_NODE_EXIST == flag) /* 得到是下一個并列結點的路徑,說明 path 對應的結點是內部結點*/
{
printf("路徑 %s 對應的結點是內部結點,其下一個并列結點的路徑為:%s\n", pathp, value);
flag = NODE_IS_BRANCH;
oldpath = (char *)malloc(strlen(value)+1);
if (NULL == oldpath)
{
free(value);
value = NULL;
return;
}
strcpy(oldpath, value);
value = get_value(root, value, &flag);
if (NULL != value)
{
printf("路徑 %s 對應的結點是葉子結點,其值為:%s\n", oldpath, value);
free(value);
value = NULL;
}
/* 得到 oldpath 路徑對應結點的下一個兄弟的路徑 */
nextpath = get_next_path2(root, oldpath, &flag);
if (NULL != nextpath)
{
if (NODE_IS_LEAF == flag)
printf("路徑 %s 的下一并列路徑 (類型是葉子) 為:%s\n", oldpath, nextpath);
else if (NODE_IS_BRANCH == flag)
printf("路徑 %s 的下一并列路徑 (類型是樹枝) 為:%s\n", oldpath, nextpath);
free(nextpath);
nextpath = NULL;
}
free(oldpath);
oldpath = NULL;
}
else
{
printf("路徑 %s 對應的結點是葉子結點,其值為:%s\n", pathp, value);
free(value);
value = NULL;
}
}
/*----------------------*
* 3、獲取的值 -- end *
*--------------------- */
/********************************
* 4、修改指定結點的 -- start *
********************************/
//path2[] = "1:7:9"
pathp = path2;
if (update_node(root, pathp, "9", "node9's new value"))
{
/* 在屏幕上打印進行修改操作之后的整棵樹*/
printf("進行修改操作之后的整棵樹:\n");
print_tree(root,0);
getchar();
printf("\n\n");
}
/*----------------------------*
* 4、修改指定結點的 -- end *
*--------------------------- */
/******************************
* 5、插入一個結點 -- start *
******************************/
pathp = path2;
if (insert_node(root, pathp, "newnode", "newnode value"))
{
/* 在屏幕上打印進行插入操作之后的整棵樹 */
printf("進行插入操作之后的整棵樹:\n");
print_tree(root,0);
getchar();
printf("\n\n");
}
/*--------------------------*
* 5、插入一個結點 -- end *
*------------------------- */
/******************************
* 6、刪除一個結點 -- start *
******************************/
//path3 = "3:16:20"
pathp = path3;
if (delete_node(root, pathp))
{
/* 在屏幕上打印進行刪除操作之后的整棵樹 */
printf("進行刪除操作之后的整棵樹:\n");
print_tree(root,0);
getchar();
printf("\n\n");
}
/*--------------------------*
* 6、刪除一個結點 -- end *
*------------------------- */
uninit(root);
root = NULL;
}
void write_testcfg()
{
FILE *fp;
fp = fopen("test.cfg", "w+");
if (NULL == fp)
return;
fputs("[機組1]\n",fp);
fputs(" 測點=測點1\n",fp);
fputs(" [測點2]\n",fp);
fputs(" 8=8\n",fp);
fputs(" [/測點2]\n",fp);
fputs(" 9=9\n",fp);
fputs("[/機組1]",fp);
fclose(fp);
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -