示例#1
0
文件: editcor.c 项目: 8l/FUZIX
PROC
docommand(cmdtype cmd)
{
    cmdtype movecmd;	/* movement command for y, d, c */
    char    cmdch;
    int     oldc;	/* old count */
    int     endp;	/* end position before change */
    extern bool s_wrapped;

    resetX();				/* un-derange the cursor */
    oldc = newc = -1;
    endY = yp;
    newend = disp = curr;
    ok = TRUE;				/* so far everything is working */
    cmdch = ch;
    if (cmd != UNDO_C && cmd != YANK_C) {
	if (macro<0)
	    zerostack(&undo);
	if (redoing != TRUE) {
	    rcp = rcb;		/* point at start of redo buffer */
	    if (count > 1) {	/* put in a count? */
		numtoa(rcb,count);
		rcp += strlen(rcb);
	    }
	    *rcp++ = cmdch;	/* the command char goes in... */
	    xerox = TRUE;	/* hoist the magical flags */
	}
    }

    if (cmd <= YANK_C) {
	readchar();
	if (ch >= '0' && ch <= '9') {
	    oldc = count;
	    gcount();				/* get a new count */
	    if (cmd == ADJUST_C)		/* special for >>,<< wierdness */
		swap(&count, &oldc);		/* reverse sw & count */
	    else
		count = count*max(oldc,1);	/* combine them */
	}
	if (ch == cmdch) {		/* diddle lines */
	    yank.lines = TRUE;
	    endp = nextline(TRUE, curr, count);
	    curr = bseekeol(curr);
	    disp = curr;
	}
	else {				/* diddle 'things' */
	    yank.lines = FALSE;
	    movecmd = movemap[ch];

	    if (ok = (findCP(curr,&endp,movecmd) == LEGALMOVE)) {
		if (curr > endp) {
		    swap(&curr,&endp);
		    ok = (cmd != CHANGE_C);
		}
		if (adjcurr[movecmd])
		    curr++;
		if (adjendp[movecmd])
		    endp++;
	    }
	    if (!ok) {
		if (ch != ESC)
		    error();
		goto killredo;
	    }
	}
	
	endY = setY(endp);
	newend = curr;
	disp = curr;
	switch (cmd) {
	    case DELETE_C:
		ok = deletion(curr, endp);
		break;
	    case ADJUST_C:
		adjuster((cmdch == '<'), endp-1, oldc);
		break;
	    case CHANGE_C:
		if (endp <= pend+1) {
		    mvcur(setY(endp-1), setX(endp-1));
		    printch('$');
		    mvcur(yp, xp);
		}
		if (deletion(curr, endp))
		    ok = ((newend = insertion(1, 0, &disp, &endY, TRUE)) >= 0);
		else
		    ok = FALSE;
		break;
	    case YANK_C:
		if (!doyank(curr, endp))
		    error();
		return 0;	/* xerox will not be true, nor will redoing */
	}

    }
    else {
	endp = curr;
	endY = yp;

	switch (cmd) {
	    case I_AT_NONWHITE:
	    case A_AT_END:
	    case APPEND_C:
	    case INSERT_C:		/* variations on insert */
		if (cmd != INSERT_C) {
		    if (cmd == APPEND_C)
			curr = min(curr+1, lend);
		    else if (cmd == A_AT_END)
			curr = lend;
		    else /* if (cmd == I_AT_NONWHITE) */
			curr = skipws(lstart);
		    xp = setX(curr);
		    mvcur(yp,xp);
		}
		newend = insertion(count, 0, &disp, &endY, TRUE);
		ok = (newend >= 0);
		break;
	    case OPEN_C:
	    case OPENUP_C:
		newend = insertion(1,setstep[ (cmd==OPENUP_C)&1 ],
						&disp,&endY,TRUE)-1;
		ok = (newend >= 0);
		break;
	    case REPLACE_C:
	    case TWIDDLE_C:
		if (cmd == REPLACE_C) {
		    if ((cmdch = readchar()) == ESC)
			goto killredo;
		}
		if (findCP(curr, &endp, GO_RIGHT) == LEGALMOVE)
		    squiggle(endp-1, cmdch, (cmd==REPLACE_C));
		break;
	    case PUT_BEFORE:
	    case PUT_AFTER:
		ok = put(cmd==PUT_AFTER);
		break;
	    case BIG_REPL_C:
		bigreplace();
		break;
	    case RESUBST_C:
		ok = FALSE;
		if (dst[0] != 0) {
		    newend = chop(curr, &lend, TRUE, &ok);
		    if (newend >= 0) {
			endY = setY(newend+strlen(dst));
			ok = TRUE;
		    }
		}
		break;
	    case JOIN_C:
		join(count);		/* join lines */
		break;
	    case UNDO_C:			/* undo last modification */
		ok = fixcore(&newend) >= 0;
		disp = newend;
		endY = MAGICNUMBER;
		break;
	}
    }

    if (ok) {
	setpos((newc<0)?newend:newc);
	setend();
	if (curr < ptop || curr > pend) {
	    yp = settop(12);
	    redisplay(TRUE);
	}
	else {
	    yp = setY(curr);
	    if (endY != setY(newend))	/* shuffled lines */
		refresh(setY(disp), setX(disp), disp, pend, TRUE);
	    else			/* refresh to end position */
		refresh(setY(disp), setX(disp), disp, newend, FALSE);
	}
	if (curr >= bufmax && bufmax > 0) {	/* adjust off end of buffer */
	    setpos(bufmax-1);
	    yp = setY(curr);
	}
	if (s_wrapped) {
	    prompt(FALSE, "search wrapped around end of buffer");
	    s_wrapped = 0;
	}
	else
	    clrprompt();
	modified = TRUE;
    }
    else {
	error();
killredo:
	rcb[0] = 0;
    }
    mvcur(yp, xp);
    if (xerox)
	*rcp = 0;	/* terminate the redo */
    redoing = FALSE;
    xerox = FALSE;
    core[bufmax] = EOL;
}
 int main () {
 int i , choice;
 int value;
 tree* newNode;
 printf(" \nEnter your choice :\n");
 printf("1. Create AVL tree.\n");
 printf("2. Inserting a node in AVL tree.\n");
 printf("3. Delete a node from the tree.\n");
 printf("4. Perform binary search.\n");
 printf("5. Find predecessor and successor of a node.\n");
 printf("6. Print AVL Tree\n");
 printf("\n   Press any other key to Exit.\n");
 scanf("%d" , &choice);
 
 
 while (1) {
 switch( choice ) {
 int n ,i , key;
 case 1 : //creation
 printf("How many nodes you want to insert\n");
 scanf("%d",&n);
 for ( i = 0 ; i < n ; i++) {
 printf("Enter the value\n");
 scanf("%d" ,&value);
 newNode = create (value);
 if (root == NULL)  //tree is empty.
 root = newNode;
 else
 root = insert( root , newNode);
 }
 break;
 
 case 2 : //inserte a node.
 printf("Enter the value\n");
 scanf("%d" ,&value);
 newNode = create (value);
 if (root == NULL) { //tree is empty.
 root = newNode;
 return;
 }
 root = insert ( root , newNode );
 //inorder(root);
 break;
 
 case 3 : //delete a node
 if(root == NULL) {
    printf("Tree is empty.\n");
    break;
 }
 printf("Enter the key which you want to delete :\n");
 scanf ("%d" ,&key);
 root = deletion (root , key);  
 //inorder(root);
 break;
 case 4 :
 if(root == NULL) {
    printf("Tree is empty.\n");
    break;
 }
 printf("Enter the key which you want to search :\n");
 scanf ("%d" ,&key);
 tree* loc;
 loc = (tree*)binarySearch ( root , key);
 if (loc==NULL)
   printf("The key %d is not present in AVL tree.\n",key);
 else
   printf("The key %d is present in AVL tree and its occurrence is =%d\n",key,loc->count);
 break;
 case 5 :
 printf("Enter the key for which you want to find predecessor and successor :\n");
 scanf ("%d" ,&key);
 findPredAndSucc ( root , key);
 break;
 
 case 6 :
      printf("\n\n");
      printTree ( root , 0); //printing tree in vertical fashion.
 break;
   
 default :
  exit(0);
 }
 printf(" \nEnter your choice :\n");
 printf("1. Create AVL tree.\n");
 printf("2. Inserting a node in AVL tree.\n");
 printf("3. Delete a node from the tree.\n");
 printf("4. Perform binary search.\n");
 printf("5. Find predecessor and successor of a node.\n");
 printf("6. Print AVL Tree\n");
 printf("\n   Press any other key to Exit.\n");
 scanf("%d" , &choice);
 
 }
 getch();
 return 0;
 }
ContinuousExtendedKalmanFilter::~ContinuousExtendedKalmanFilter(void)
{
	deletion();
	return;
}
DiscreteExtendedKalmanFilter::~DiscreteExtendedKalmanFilter(void)
{
	deletion();
	return;
}
示例#5
0
  /* deletion in binary search tree */
  void deletion(struct treeNode **node, struct treeNode **parent, int data) {
        struct treeNode *tmpNode, *tmpParent;
        if (*node == NULL)
                return;
        if ((*node)->data == data) {
                /* deleting the leaf node */
                if (!(*node)->left && !(*node)->right) {
                        if (parent) {
                                /* delete leaf node */
                                if ((*parent)->left == *node)
                                        (*parent)->left = NULL;
                                else
                                        (*parent)->right = NULL;
                                free(*node);
                        } else {
                                /* delete root node with no children */
                                free(*node);
                        }
                /* deleting node with one child */
                } else if (!(*node)->right && (*node)->left) {
                        /* deleting node with left child alone */
                        tmpNode = *node;
                        (*parent)->right = (*node)->left;
                        free(tmpNode);
                        *node = (*parent)->right;
                } else if ((*node)->right && !(*node)->left) {
                        /* deleting node with right child alone */
                        tmpNode = *node;
                        (*parent)->left = (*node)->right;
                        free(tmpNode);
                        (*node) = (*parent)->left;
                } else if (!(*node)->right->left) {
                        /*
                         * deleting a node whose right child
                         * is the smallest node in the right
                         * subtree for the node to be deleted.
                         */

                        tmpNode = *node;

                        (*node)->right->left = (*node)->left;

                        (*parent)->left = (*node)->right;
                        free(tmpNode);
                        *node = (*parent)->left;
                } else {
                        /*
                         * Deleting a node with two children.
                         * First, find the smallest node in
                         * the right subtree.  Replace the
                         * smallest node with the node to be
                         * deleted. Then, do proper connections
                         * for the children of replaced node.
                         */
                        tmpNode = (*node)->right;
                        while (tmpNode->left) {
                                tmpParent = tmpNode;
                                tmpNode = tmpNode->left;
                        }
                        tmpParent->left = tmpNode->right;
                        tmpNode->left = (*node)->left;
                        tmpNode->right =(*node)->right;
                        free(*node);
                        *node = tmpNode;
                }
        } else if (data < (*node)->data) {
                /* traverse towards left subtree */
                deletion(&(*node)->left, node, data);
        } else if (data > (*node)->data) {
                /* traversing towards right subtree */
                deletion(&(*node)->right, node, data);
        }
  }