Change code to meet the need of the tools which not support C99.

This commit is contained in:
Saturneric 2018-07-18 11:53:09 +08:00
parent 880fa14d10
commit 79b529a841
6 changed files with 111 additions and 72 deletions

Binary file not shown.

Binary file not shown.

View File

@ -1,25 +1,28 @@
#include "list_expand.h"
Node *nodeWithInt(int m_int) {
Node *p_node;
int *p_int = (int *)malloc(sizeof(int));
*p_int = m_int;
Node *p_node = initNode();
p_node = initNode();
initMallocValueForNode(p_node, "int", (void *)p_int);
return p_node;
}
Node *nodeWithDouble(double m_double) {
Node *p_node;
double *p_double = (double *)malloc(sizeof(double));
*p_double = m_double;
Node *p_node = initNode();
p_node = initNode();
initMallocValueForNode(p_node, "double", (void *)p_double);
return p_node;
}
Node *nodeWithString(const char *m_string) {
Node *p_node;
char *p_string = (char *)malloc(sizeof(char)*(strlen(m_string) + 1));
strcpy_s(p_string, sizeof(p_string), m_string);
Node *p_node = initNode();
p_node = initNode();
initMallocValueForNode(p_node, "string", (void *)p_string);
return p_node;
}
@ -40,7 +43,8 @@ Node *nodeWithComplex(void) {
Node *findByIndexForNode(List *p_list, unsigned long long m_index) {
Node *p_node = p_list->head;
for (unsigned long long i = 0; i < m_index; i++) {
unsigned long long i;
for (i = 0; i < m_index; i++) {
p_node = p_node->next;
}
return p_node;
@ -86,28 +90,29 @@ void *getByPointerForNode(Node *p_node) {
}
void printListInfo(List *p_list, int priority) {
for (int i = 0; i < priority; i++) printf(" ");
printf("###LIST(location:%p, id:%llu){\n", p_list, p_list->id);
for (int i = 0; i < priority + 1; i++) printf(" ");
printf("HEAD->%p / Tail->%p / Length:%llu\n", p_list->head, p_list->tail, p_list->length);
Node *p_node = p_list->head;
int i = 0;
Node *p_node;
for (i = 0; i < priority; i++) printf(" ");
printf("###LIST(location:%p, id:%llu){\n", p_list, p_list->id);
for (i = 0; i < priority + 1; i++) printf(" ");
printf("HEAD->%p / Tail->%p / Length:%llu\n", p_list->head, p_list->tail, p_list->length);
p_node = p_list->head;
while (p_node != NULL) {
for (int i = 0; i < priority + 1; i++) printf(" ");
for (i = 0; i < priority + 1; i++) printf(" ");
printf("%d.... \n", i);
printNodeInfo(p_node, priority + 1);
p_node = p_node->next;
i++;
}
for (int i = 0; i < priority; i++) printf(" ");
for (i = 0; i < priority; i++) printf(" ");
printf("}\n");
}
void printList(List *p_list) {
int if_nearLast = 0;
Node *p_node = p_list->head;
printf("[");
int if_nearLast = 0;
while (p_node != NULL) {
if (!if_nearLast && p_node->next == NULL) if_nearLast = 1;
if (!strcmp(p_node->type, "int")) {
@ -134,44 +139,46 @@ void printList(List *p_list) {
}
void printNodeInfo(Node *p_node, int priority) {
for (int i = 0; i < priority; i++) printf(" ");
int i;
for (i = 0; i < priority; i++) printf(" ");
printf("#NODE(location:%p, id:%llu){\n", p_node, p_node->id);
for (int i = 0; i < priority + 1; i++) printf(" ");
for (i = 0; i < priority + 1; i++) printf(" ");
printf("NEXT->%p / LAST->%p / MALLOC:%d\n", p_node->next, p_node->last, p_node->if_malloc);
if (!strcmp(p_node->type, "int")) {
for (int i = 0; i < priority + 1; i++) printf(" ");
for (i = 0; i < priority + 1; i++) printf(" ");
printf("VALUE(int):%d\n", *(int *)(p_node->value));
}
else if (!strcmp(p_node->type, "double")) {
for (int i = 0; i < priority + 1; i++) printf(" ");
for (i = 0; i < priority + 1; i++) printf(" ");
printf("VALUE(double):%a\n", *(double *)(p_node->value));
}
else if (!strcmp(p_node->type, "string")) {
for (int i = 0; i < priority + 1; i++) printf(" ");
for (i = 0; i < priority + 1; i++) printf(" ");
printf("VALUE(string):%s\n", (char *)(p_node->value));
}
else if (!strcmp(p_node->type, "pointer")) {
for (int i = 0; i < priority + 1; i++) printf(" ");
for (i = 0; i < priority + 1; i++) printf(" ");
printf("VALUE(pointer):%s\n", (char *)(p_node->value));
}
else if (!strcmp(p_node->type, "list")) {
for (int i = 0; i < priority + 1; i++) printf(" ");
for (i = 0; i < priority + 1; i++) printf(" ");
printf("VALUE(List):\n");
printListInfo((List *)p_node->value, priority + 2);
}
for (int i = 0; i < priority; i++) printf(" ");
for (i = 0; i < priority; i++) printf(" ");
printf("}\n");
}
void printNode(Node *p_node) {
int i;
printf("#NODE(location:%p, id:%llu){\n", p_node, p_node->id);
printf(" ");
printf("NEXT->%p / LAST->%p\n", p_node->next, p_node->last, p_node->if_malloc);
for (int i = 0; i < 1; i++) printf(" ");
for (i = 0; i < 1; i++) printf(" ");
printf("ifMalloc: ");
if (p_node->if_malloc) {
printf("YES\n");
for (int i = 0; i < 1; i++) printf(" ");
for (i = 0; i < 1; i++) printf(" ");
printf("Value(type: %s): ", p_node->type);
if (!strcmp(p_node->type, "int")) {
printf("%d", *(int *)(p_node->value));
@ -196,25 +203,28 @@ void printNode(Node *p_node) {
Node *findByIntForNode(List *p_list, int target) {
Node *t_node;
int *p_target = (int *)malloc(sizeof(int));
*p_target = target;
Node *t_node = findByValue(p_list, "int", p_target);
t_node = findByValue(p_list, "int", p_target);
free(p_target);
return t_node;
}
Node *findByDoubleForNode(List *p_list, double target) {
Node *t_node;
double *p_target = (double *)malloc(sizeof(double));
*p_target = target;
Node *t_node = findByValue(p_list, "double", p_target);
t_node = findByValue(p_list, "double", p_target);
free(p_target);
return t_node;
}
Node *findByStringForNode(List *p_list, char *target) {
Node *t_node;
char *p_temp = (char *)malloc(sizeof(char)*(strlen(target) + 1));
strcpy_s(p_temp, sizeof(p_temp), target);
Node *t_node = findByValue(p_list, "string", p_temp);
t_node = findByValue(p_list, "string", p_temp);
free(p_temp);
return t_node;
}
@ -225,9 +235,11 @@ Node *findByPointerForNode(List *p_list, void *target) {
}
int addValueForComplex(Node * p_node, char *type, void *value) {
List *c_list;
Node *c_node;
if (!strcmp(p_node->type, "list")) {
List *c_list = (List *)p_node->value;
Node *c_node = initNode();
c_list = (List *)p_node->value;
c_node = initNode();
initMallocValueForNode(c_node, type, value);
insertInTail(c_list, c_node);
return 0;
@ -275,24 +287,27 @@ int addPointerForComplex(Node *p_node, void *temp) {
List *m_findByInt(List* p_list, int temp) {
int *p_temp = (int *)malloc(sizeof(int));
List *t_list;
*p_temp = temp;
List *t_list = mply_findByValue(p_list, "int", (void *)p_temp);
t_list = mply_findByValue(p_list, "int", (void *)p_temp);
free(p_temp);
return t_list;
}
List *m_findByDouble(List* p_list, double temp) {
List *t_list;
double *p_temp = (double *)malloc(sizeof(double));
*p_temp = temp;
List *t_list = mply_findByValue(p_list, "double", (void *)p_temp);
t_list = mply_findByValue(p_list, "double", (void *)p_temp);
free(p_temp);
return t_list;
}
List *m_findByString(List* p_list, char *temp) {
List *t_list;
char *p_temp = (char *)malloc(sizeof(char)*(strlen(temp) + 1));
strcpy_s(p_temp, sizeof(p_temp), temp);
List *t_list = mply_findByValue(p_list, "string", (void *)p_temp);
t_list = mply_findByValue(p_list, "string", (void *)p_temp);
free(p_temp);
return t_list;
}

24
test.c
View File

@ -2,9 +2,11 @@
int list(void) {
int i;
List *t_list, *m_list;;
init_rand();
safeModeForNode(1);
List *t_list = initList();
t_list = initList();
/*for(int i = 0; i < 9; i++){
Node *t_node = initNode();
@ -18,7 +20,7 @@ int list(void) {
insertInTail(t_list,t_node);
initMalllocValue(t_node,(void *)"there");*/
for (int i = 0; i < 12; i++) {
for (i = 0; i < 12; i++) {
insertInHead(t_list, nodeWithInt(i));
insertInTail(t_list, nodeWithInt(i));
}
@ -27,7 +29,7 @@ int list(void) {
printListInfo(t_list, 0);
printList(t_list);
List *m_list;
m_list = m_findByIntForNode(t_list, 5);
printList(m_list);
printf("\n");
@ -44,13 +46,18 @@ int _useTreeThroughDown(TNode *p_tnode, unsigned long long height) {
int tree(void) {
TNode *t_tnode, *cr_tnode, *cl_tnode;
Tree *t_tree;
TNode *gs_tnode;
safeModeForTree(1);
Tree *t_tree = initTree();
TNode *t_tnode = tnodeWithInt(1), *cr_tnode = tnodeWithInt(3),*cl_tnode = tnodeWithInt(2);
t_tree = initTree();
t_tnode = tnodeWithInt(1);
cr_tnode = tnodeWithInt(3);
cl_tnode = tnodeWithInt(2);
addChildInRight(t_tnode, cl_tnode);
addChildInRight(t_tnode, cr_tnode);
addChildInRight(cl_tnode, tnodeWithInt(4));
TNode *gs_tnode = tnodeWithInt(5);
gs_tnode = tnodeWithInt(5);
addChildInRight(cl_tnode,gs_tnode);
addChildInRight(cr_tnode, tnodeWithInt(6));
addChildInRight(cr_tnode, tnodeWithInt(7));
@ -63,11 +70,12 @@ int tree(void) {
}
int stack(void) {
int i;
Stack *t_stack = initStack();
for (int i = 0; i < 10; i++) {
for (i = 0; i < 10; i++) {
pushStack(t_stack, snodeWithInt(i));
}
for (int i = 0; i < 10; i++) {
for (i = 0; i < 10; i++) {
printf("%d", getValueByIntForSNode(popStack(t_stack)));
}
releaseStack(t_stack);

View File

@ -29,6 +29,7 @@ int safeModeForTree(int ifon) {
}
TNode *initTNode(void) {
Node *s_node;
TNode *p_tnode = (TNode *)malloc(sizeof(TNode));
p_tnode->id = getId();
p_tnode->child_num = 0;
@ -41,14 +42,14 @@ TNode *initTNode(void) {
if (if_safeModeForTree) {
if (if_safeModeForNode) {
if_safeModeForNode = 0;
Node *s_node = initNode();
s_node = initNode();
initMallocValueForNode(s_node, "pointer", (void *)p_tnode);
insertInTail(tnode_list, s_node);
if_safeModeForNode = 1;
}
else
{
Node *s_node = initNode();
s_node = initNode();
initMallocValueForNode(s_node, "pointer", (void *)p_tnode);
insertInTail(tnode_list, s_node);
}
@ -57,20 +58,21 @@ TNode *initTNode(void) {
}
Tree *initTree(void) {
Node *s_node;
Tree *p_tree = (Tree *)malloc(sizeof(Tree));
p_tree->id = getId();
p_tree->root = NULL;
if (if_safeModeForTree) {
if (if_safeModeForNode) {
if_safeModeForNode = 0;
Node *s_node = initNode();
s_node = initNode();
initMallocValueForNode(s_node, "pointer", (void *)p_tree);
if_safeModeForNode = 1;
insertInTail(tree_list, s_node);
}
else
{
Node *s_node = initNode();
s_node = initNode();
initMallocValueForNode(s_node, "pointer", (void *)p_tree);
insertInTail(tree_list, s_node);
}
@ -184,14 +186,14 @@ TNode *getChildByValue(TNode *p_tnode, char *type, void *value) {
int _dogetChildByValue(const char *type, void *value) {
if (!strcmp(type, target_type)) {
TNode *p_tode = (TNode *)value;
if (!strcmp(target_value, "int")) {
if (!strcmp((char *)target_value, "int")) {
if (*(int *)p_tode->value == *(int *)target_value)
{
target_value_value = p_tode;
return -1;
}
}
else if (!strcmp(target_value, "double"))
else if (!strcmp((char *)target_value, "double"))
{
if (*(double *)p_tode->value == *(double *)target_value)
{
@ -199,7 +201,7 @@ int _dogetChildByValue(const char *type, void *value) {
return -1;
}
}
else if (!strcmp(target_value, "string"))
else if (!strcmp((char *)target_value, "string"))
{
if (!strcmp((char *)p_tode->value, (char *)target_value))
{
@ -207,7 +209,7 @@ int _dogetChildByValue(const char *type, void *value) {
return -1;
}
}
else if (!strcmp(target_value, "pointer"))
else if (!strcmp((char *)target_value, "pointer"))
{
if (p_tode->value == target_value)
{
@ -291,10 +293,11 @@ int removeChildByIndex(TNode *p_tnode, unsigned long long index) {
int TreeThroughUp(Tree *p_tree, int(*func)(TNode *, unsigned long long height)) {
int i;
TNode *p_tnode = p_tree->root;
if (p_tnode != NULL) {
if (p_tnode->child_num > 0) {
for (int i = 0; i < p_tnode->child_num; i++) {
for (i = 0; i < p_tnode->child_num; i++) {
if (_doTreeThroughUp(getChildByIndex(p_tnode, i), 1, func) == -1) {
break;
}
@ -306,23 +309,26 @@ int TreeThroughUp(Tree *p_tree, int(*func)(TNode *, unsigned long long height))
}
int _doTreeThroughUp(TNode *p_tnode, int height, int(*func)(TNode *, unsigned long long height)) {
int i, func_back;
if (p_tnode->child_num > 0) {
for (int i = 0; i < p_tnode->child_num; i++) {
for (i = 0; i < p_tnode->child_num; i++) {
if (_doTreeThroughUp(getChildByIndex(p_tnode, i), height + 1, func)) return -1;
}
}
int func_back = func(p_tnode, height);
func_back = func(p_tnode, height);
if (func_back == -1)return -1;
return 0;
}
int TreeThroughDown(Tree *p_tree, int(*func)(TNode *, unsigned long long height)) {
int i;
TNode *p_tnode = p_tree->root;
if (p_tree->root != NULL) {
func(p_tnode, 0);
if (p_tree->root->child_num > 0) {
for (int i = 0; i < p_tnode->child_num; i++) {
for (i = 0; i < p_tnode->child_num; i++) {
if (_doTreeThroughDown(getChildByIndex(p_tnode, i), 1, func) == -1) {
break;
}
@ -333,9 +339,10 @@ int TreeThroughDown(Tree *p_tree, int(*func)(TNode *, unsigned long long height)
}
int _doTreeThroughDown(TNode *p_tnode, int height, int(*func)(TNode *, unsigned long long height)) {
int i;
int func_back = func(p_tnode, height);
if (p_tnode->child_num > 0) {
for (int i = 0; i < p_tnode->child_num; i++) {
for (i = 0; i < p_tnode->child_num; i++) {
if (_doTreeThroughDown(getChildByIndex(p_tnode, i), height + 1, func)) return -1;
}
}
@ -427,9 +434,11 @@ int releaseOnlyTNode(TNode *p_tnode) {
}
int releaseAllForTree(void) {
Node *p_node;
Tree *p_tree;
if (if_safeModeForTree) {
if_safeModeForTree = 0;
Node *p_node = tnode_list->head;
p_node = tnode_list->head;
while (p_node != NULL) {
TNode *p_tnode = (TNode *)p_node->value;
releaseOnlyTNode(p_tnode);
@ -437,7 +446,7 @@ int releaseAllForTree(void) {
}
p_node = tree_list->head;
while (p_node != NULL) {
Tree *p_tree = (Tree *)p_node->value;
p_tree = (Tree *)p_node->value;
releaseOnlyTree(p_tree);
p_node = p_node->next;
}

View File

@ -58,17 +58,18 @@ void *getValueByPointerForTree(TNode *p_tnode) {
}
int printTNode(TNode *p_tnode, int priority) {
int i;
if (p_tnode != NULL) {
for (int i = 0; i < priority; i++) printf(" ");
for (i = 0; i < priority; i++) printf(" ");
if (priority == 0) printf("###");
else printf("#");
printf("TNode(id: %llu)\n", p_tnode->id);
for (int i = 0; i < priority + 1; i++) printf(" ");
for (i = 0; i < priority + 1; i++) printf(" ");
printf("ifMalloc: ");
if (p_tnode->if_malloc) {
printf("YES\n");
for (int i = 0; i < priority + 1; i++) printf(" ");
for (i = 0; i < priority + 1; i++) printf(" ");
printf("Value(type: %s): ", p_tnode->type);
if (!strcmp(p_tnode->type, "int")) {
printf("%d\n", *(int *)(p_tnode->value));
@ -86,7 +87,7 @@ int printTNode(TNode *p_tnode, int priority) {
else printf("NO\n");
if (p_tnode->child_num > 0) {
for (int i = 0; i < priority + 1; i++) printf(" ");
for (i = 0; i < priority + 1; i++) printf(" ");
printf("Child number: %llu\n", p_tnode->child_num);
}
return 0;
@ -95,15 +96,18 @@ int printTNode(TNode *p_tnode, int priority) {
}
int printTNodeWithHome(TNode *p_tnode,int priority) {
int i;
List *p_home;
Node *p_node;
if (p_tnode != NULL) {
if (priority == 0) printf("###");
else printf("#");
printf("TNode(id: %llu)\n", p_tnode->id);
for (int i = 0; i < priority + 1; i++) printf(" ");
for (i = 0; i < priority + 1; i++) printf(" ");
printf("ifMalloc: ");
if (p_tnode->if_malloc) {
printf("YES\n");
for (int i = 0; i < priority + 1; i++) printf(" ");
for (i = 0; i < priority + 1; i++) printf(" ");
printf("Value(type: %s): ", p_tnode->type);
if (!strcmp(p_tnode->type, "int")) {
printf("%d\n", *(int *)(p_tnode->value));
@ -121,22 +125,22 @@ int printTNodeWithHome(TNode *p_tnode,int priority) {
else printf("NO\n");
if (p_tnode->father != NULL) {
for (int i = 0; i < priority + 1; i++) printf(" ");
for (i = 0; i < priority + 1; i++) printf(" ");
printf("Father id: %llu\n", p_tnode->father->id);
}
else
{
for (int i = 0; i < priority + 1; i++) printf(" ");
for (i = 0; i < priority + 1; i++) printf(" ");
printf("Father: NO\n");
}
if (p_tnode->child_num > 0) {
for (int i = 0; i < priority + 1; i++) printf(" ");
for (i = 0; i < priority + 1; i++) printf(" ");
printf("Child(number: %llu):\n", p_tnode->child_num);
}
List *p_home = p_tnode->home;
Node *p_node = p_home->head;
p_home = p_tnode->home;
p_node = p_home->head;
while (p_node != NULL) {
printTNode((TNode *)p_node->value, priority + 2);
p_node = p_node->next;
@ -147,16 +151,19 @@ int printTNodeWithHome(TNode *p_tnode,int priority) {
}
int printTNodeWithFamily(TNode *p_tnode, int priority) {
int i;
List *p_home;
Node *p_node;
if (p_tnode != NULL) {
for (int i = 0; i < priority; i++) printf(" ");
for (i = 0; i < priority; i++) printf(" ");
if (priority == 0) printf("###");
else printf("#");
printf("TNode(id: %llu)\n", p_tnode->id);
for (int i = 0; i < priority + 1; i++) printf(" ");
for (i = 0; i < priority + 1; i++) printf(" ");
printf("ifMalloc: ");
if (p_tnode->if_malloc) {
printf("YES\n");
for (int i = 0; i < priority + 1; i++) printf(" ");
for (i = 0; i < priority + 1; i++) printf(" ");
printf("Value(type: %s): ", p_tnode->type);
if (!strcmp(p_tnode->type, "int")) {
printf("%d\n", *(int *)(p_tnode->value));
@ -174,22 +181,22 @@ int printTNodeWithFamily(TNode *p_tnode, int priority) {
else printf("NO\n");
if (p_tnode->father != NULL) {
for (int i = 0; i < priority + 1; i++) printf(" ");
for (i = 0; i < priority + 1; i++) printf(" ");
printf("Father id: %llu\n", p_tnode->father->id);
}
else
{
for (int i = 0; i < priority + 1; i++) printf(" ");
for (i = 0; i < priority + 1; i++) printf(" ");
printf("Father: NO\n");
}
if (p_tnode->child_num > 0) {
for (int i = 0; i < priority + 1; i++) printf(" ");
for (i = 0; i < priority + 1; i++) printf(" ");
printf("Child(number: %llu):\n", p_tnode->child_num);
}
List *p_home = p_tnode->home;
Node *p_node = p_home->head;
p_home = p_tnode->home;
p_node = p_home->head;
while (p_node != NULL) {
printTNodeWithFamily((TNode *)p_node->value, priority + 2);
p_node = p_node->next;