static int bst_insert(bst *pbst, int key[], int nTotal) { int iRet=1; int i=0; bst_node *pNewNode = NULL; bst_node *pCurNode = NULL; if(pbst==NULL || key==NULL) { return -1; } for(i=0; i<nTotal; i++) { //1. construct node pNewNode = (bst_node *)malloc(sizeof(bst_node)); pNewNode->nKey=key[i]; pNewNode->nIsVisited=0; pNewNode->pL=NULL; pNewNode->pR=NULL; //2. if 1st time, construct stack and root; otherwise compare with root recursivelly if(pbst->pBstRoot == NULL) { stack_construct(&pbst->stack, nTotal); pbst->pBstRoot = pNewNode; } else { pCurNode = pbst->pBstRoot; do { if(pNewNode->nKey < pCurNode->nKey) { if(pCurNode->pL != NULL) { pCurNode = pCurNode->pL; } else { pCurNode->pL = pNewNode; break; } } else { if(pCurNode->pR != NULL) { pCurNode = pCurNode->pR; } else { pCurNode->pR = pNewNode; break; } } }while(1); } pbst->nBstSize++; } return iRet; }
void chartab_construct() { if ( regcomp ( &chartab.d_regex, "[[:space:]]*" "'((\\\\)?.)'" "[[:space:]]*" "=" "[[:space:]]*" "\"(((\\\\.)|[^\"])*)\"" , REG_EXTENDED | REG_NEWLINE ) || regcomp ( &chartab.d_regex_oct, "[[:space:]]*" "'\\\\([0-7]{3})'" "[[:space:]]*" "=" "[[:space:]]*" "\"(((\\\\.)|[^\"])*)\"" , REG_EXTENDED | REG_NEWLINE ) || regcomp ( &chartab.d_regex_hex, "[[:space:]]*" "'0x([0-9a-fA-F]{2})'" "[[:space:]]*" "=" "[[:space:]]*" "\"(((\\\\.)|[^\"])*)\"" , REG_EXTENDED | REG_NEWLINE ) ) if (message_show(MSG_EMERG)) message("Chartab_construct(): regcomp() failed"); stack_construct(&chartab.d_chartab_st, NULL); }
/* * maze function defination * */ static int maze_findAllway(int **arry_maze, int nRow, int nColm) { int iRet=0; stack stackM; stack_elem selemCur; printf_dbg("[F:%s, L:%d]\n", __FUNCTION__, __LINE__); if(arry_maze==NULL || nRow<1 || nColm<1) { return -1; } //1. construct stack iRet = stack_construct(&stackM, nRow*nColm); //2. init current stack element selemCur.x = 0; selemCur.y = 0; selemCur.nDir = -1; //3. Deepth-First-Search(DFS) search path do { iRet = maze_goNextStep(&stackM, &selemCur); if(iRet == 1) { #ifdef DEBUG printf_dbg("[F:%s, L:%d] goNextStep success\n", __FUNCTION__, __LINE__); stack_print(&stackM); #endif if(selemCur.x==g_nRow-1 && selemCur.y==g_nColm-1 && g_arry_maze[selemCur.x][selemCur.y]==0) { stack_print(&stackM); iRet = maze_goLastStep(&stackM, &selemCur); } } else if(iRet == 0) { iRet = maze_goLastStep(&stackM, &selemCur); } }while(iRet==1); //4. search all complete; destruct stack stack_destruct(&stackM); return iRet; }
int main() { int sizestr = 1, size = 0, comdat = 0, j = 0, i = 0; mystack_type buf1 = 0, buf2 = 0, buf3 = 0; int* strcommand = (int*)calloc(j + 1,sizeof(int)); assert(strcommand); FILE *product = 0; assembler(); //Перевод команд из текста в цифры product = fopen("Product.txt","r"); /* Открываем файл, считываем из него команды и числа, помещаем всё это в массив. Распределяем введенные данные по массивам меток, команд, чисел. Выполняем программу. */ while(!feof(product)) //Считывание команд { fscanf(product, "%d", &strcommand[j]); j++; strcommand = (int*)realloc(strcommand, sizeof(int)*(j + 1)); } fclose(product); mystack stk = stack_construct(1); //Рабочий стек mystack call_ret = stack_construct(1); //Стек вызова for(i = 0; strcommand[i] && i <= j; i++) { if(strcommand[i] < 0) continue; switch (strcommand[i]) { case 0: return 0;break; //End case 1: //Push i++; stack_push(stk, strcommand[i]); break; case 2: //Pop stack_pop(stk); break; case 3: //Add stack_push(stk, stack_pop(stk) + stack_pop(stk)); break; case 4: //Jump i++; i = strcommand[i] - 1; break; case 5: //Mul stack_push(stk, stack_pop(stk) * stack_pop(stk)); break; case 6: //Sub stack_push(stk, -stack_pop(stk) + stack_pop(stk)); break; case 7: //Div buf1 = stack_pop(stk); buf2 = stack_pop(stk); stack_push(stk, buf2 / buf1); break; case 8: //Push_Ax Ax = stack_top(stk); break; case 9: //Push_Bx Bx = stack_top(stk); break; case 10: //Push_Cx Cx = stack_top(stk); break; case 11: //Push_Dx Dx = stack_top(stk); break; case 12: //Call i++; stack_push(call_ret, i); i = strcommand[i] - 1; break; case 13: //Ret if(stack_check(stk) != 0) i = stack_pop(call_ret); break; case 14: //Jnz i++; if(stack_top(stk) != 0) i = strcommand[i] - 1; break; case 15: //Jz i++; if(stack_top(stk) == 0) i = strcommand[i] - 1; break; case 16: //Cmp if (last > penultimate) := 1 if (last < penultimate) := 0 buf1 = stack_pop(stk); if(buf1 > stack_top(stk)) buf3 = 1; if(buf1 > stack_top(stk)) buf3 = 0; stack_push(stk, buf1); stack_push(stk, buf3); buf3 = 0; break; case 17: //Je JumpIF== i++; buf1 = stack_pop(stk); if(buf1 == stack_top(stk)) buf3 = 0; else buf3 = -1; stack_push(stk, buf1); if(!buf3) i = strcommand[i] - 1; buf3 = 0; break; case 18: //Jg last > i++; buf1 = stack_pop(stk); if(buf1 > stack_top(stk)) buf3 = 0; else buf3 = -1; stack_push(stk, buf1); if(!buf3) i = strcommand[i] - 1; buf3 = 0; break; case 19: //Jl last < i++; buf1 = stack_pop(stk); if(buf1 < stack_top(stk)) buf3 = 0; else buf3 = -1; stack_push(stk, buf1); if(!buf3) i = strcommand[i] - 1; buf3 = 0; break; case 20: //Jng last >= i++; buf1 = stack_pop(stk); if(buf1 >= stack_top(stk)) buf3 = 0; else buf3 = -1; stack_push(stk, buf1); if(!buf3) i = strcommand[i] - 1; buf3 = 0; break; case 21: //Jnl jast <= i++; buf1 = stack_pop(stk); if(buf1 <= stack_top(stk)) buf3 = 0; else buf3 = -1; stack_push(stk, buf1); if(!buf3) i = strcommand[i] - 1; buf3 = 0; break; case 22: //Sqr buf1 = stack_pop(stk); stack_push(stk, buf1 * buf1); break; case 23: //Sqrt buf1 = stack_pop(stk); buf1 = (mystack_type)sqrt(buf1); stack_push(stk, buf1); break; } } proc_dump(stk); stack_destruct(stk); stack_destruct(call_ret); free(strcommand); return 0; }