int FullSegmentInfo(ANT_DESK info, int segment, float mas, float heigh, float *pMas, int *pNum, ANT_INFO *antrop) { int i = 0; float persent, norm; *antrop = SegmentInfo(mas, heigh, segment, info); if (fabs(antrop->mass) < 0.000001) return 0; if (segment >= UNI_SEGMENTS_NUM) segment -= TOTAL_SEGMENTS_NUM - UNI_SEGMENTS_NUM; rewind(info.model); ShiftDown(info.model, segment, i); while (fgetc(info.model) != ';'); if (!fscanf(info.model, "%d", pNum)) return 0; for (i = 0; i < *pNum; i++) { if (fscanf(info.model,";%f;%f;%f;%f", pMas + 3*i, pMas + 3*i + 1, pMas + 3*i + 2, &persent) != 4) return 0; norm = sqrt(pMas[3*i]*pMas[3*i] + pMas[3*i + 1]*pMas[3*i + 1] + pMas[3*i + 2]*pMas[3*i + 2]); norm = (persent * antrop->length) / (norm * 100); // norm = (antrop->length) / (norm * 100); pMas[3*i] *= norm; pMas[3*i + 1] *= norm; pMas[3*i + 2] *= norm; } return 1; }
// 堆排序,时间复杂度O(nlogn) void HeapSort(int a[], int n) { MakeHeap(a, n); for(int i=n; i>1; --i) { swap(a[i], a[1]); ShiftDown(a, i-1, 1); } }
void Isam::Insert(Person * newPerson) { bool done = false; int col = 0; node * nn = m_col[0]->CreateNode(newPerson); while(!done) { if(m_col[col +1]->GetSize() == 0 || _CASECMP(m_col[col+1]->GetFirst(), nn->_data->GetLast()) > 0) { m_col[col]->Insert(nn); if(m_col[col]->GetSize() > MAX_ROW_SIZE) { nn = m_col[col]->GetLast(); ShiftDown(col); col++; } else done = true; } else { col++; } } }
vi_key GetKeyboard( void ) { unsigned short key; int scan; bool shift; key = BIOSGetKeyboard( &scan ); shift = ShiftDown(); key &= 0xff; if( key == 0xE0 && scan != 0 ) { key = 0; } return( GetVIKey( key, scan, shift ) ); }
// Shifts Gear void DriveTrain::ShiftSpeed() { if(mPosition == kLow) { ShiftUp(); //mPosition = kHigh; } else { ShiftDown(); //mPosition = kLow; } }
void AnimateVertical(uint8_t prev, uint8_t gear) { //convert to ASCII codes prev += SYMBOL_GEAR_NUMBER; gear += SYMBOL_GEAR_NUMBER; for(uint8_t i=0;i<8;i++) { if( gear < prev ) ShiftUp(i, (PGM_P)FONTTAB+gear*8, (PGM_P)FONTTAB+prev*8 ); else ShiftDown(8-i, (PGM_P)FONTTAB+prev*8, (PGM_P)FONTTAB+gear*8 ); _delay_ms(GEAR_ANIM_DELAY); } ledPutc(gear); }
/* * startSelectedRegion - start selection region area from keyboard */ int startSelectedRegion( bool line_based ) { if( ShiftDown() && SelRgn.selected ) { EditFlags.Dragging = TRUE; UpdateCursorDrag(); } else { if( SelRgn.selected ) { UnselectRegion(); return( ERR_NO_ERR ); } EditFlags.Dragging = TRUE; InitSelectedRegion(); SelRgn.lines = line_based; SelRgn.selected = TRUE; updateRegion(); } return( ERR_NO_ERR ); } /* startSelectedRegion */
int ScrollDown( const char* szText, int Len, int* pOffset ) { int c = *pOffset / 8; int bit = 8-*pOffset % 8; if( c >= Len ) { *pOffset = 0; return *pOffset; } char c1 = szText[c++]; char c2 = c <= Len-1 ? szText[c] : ' ' /*extra space at the end*/; ShiftDown( bit, pCurrentFont+c1*8, pCurrentFont+c2*8 ); (*pOffset)++; return *pOffset; }
void PlayGame() { char userChoice = 'k'; printf("Going to start game. Use AD for left, right. WS for up, down.\n"); printf("k to kill game.\n"); InitTiles(); NextStep(); do{ if(!NextStep()){ break; } PrintTiles(); refresh(); // printing on screen - via ncurses. userChoice = getch(); // if(userChoice=='\n') userChoice = getchar(); switch(userChoice){ case 'a': case 'A': MergeLeft(); ShiftLeft(); break; case 'd': case 'D': MergeRight(); ShiftRight(); break; case 'w': case 'W': MergeUp(); ShiftUp(); break; case 's': case 'S': MergeDown(); ShiftDown(); break; } clear(); } while(SpaceAvailable() && userChoice != 'k'); printf("\nGame over.\n"); }
/* * DoSelectSelection - selected region was selected, invoke aprops script */ vi_rc DoSelectSelection( bool doMenu ) { linenum sl, el, tl; int sc, ec, tc; vi_rc rc = ERR_NO_ERR; if( SelRgn.selected && doMenu ) { sl = SelRgn.start.line; el = SelRgn.end.line; if( sl > el ) { tl = sl; sl = el; el = tl; } if( CurrentPos.line >= sl && CurrentPos.line <= el ) { if( sl == el && !SelRgn.lines ) { sc = SelRgn.start.column; ec = SelRgn.end.column; if( sc > ec ) { sc--; tc = sc; sc = ec; ec = tc; } else if( sc < ec ) { ec--; } if( CurrentPos.column >= sc && CurrentPos.column <= (ec + 1) ) { rc = InvokeColSelHook( sc, ec ); } } else { rc = InvokeLineSelHook( SelRgn.start.line, SelRgn.end.line ); } UnselectRegion(); return( rc ); } UnselectRegion(); } if( ShiftDown() && LastEvent != '_' ) { sc = 1; ec = CurrentLine->len; SetSelRegionCols( CurrentPos.line, sc, ec ); if( doMenu ) { rc = InvokeLineSelHook( CurrentPos.line, CurrentPos.line ); } } else { rc = GimmeCurrentEntireWordDim( &sc, &ec, FALSE ); if( rc != ERR_NO_ERR ) { return( ERR_NO_ERR ); } SetSelRegionCols( CurrentPos.line, sc, ec ); if( doMenu ) { rc = InvokeColSelHook( sc, ec ); } } if( doMenu ) { UnselectRegion(); } return( rc ); } /* DoSelectSelection */
/* * HandleMouseEvent - handle main editor mouse events */ vi_rc HandleMouseEvent( void ) { windim win_x, win_y; window_id wid; info *cinfo; window *w; int i; bool diff_word; vi_rc rc; wid = GetMousePosInfo( &win_x, &win_y ); if( BAD_ID( wid ) ) { return( ERR_NO_ERR ); } w = WINDOW_FROM_ID( wid ); if( !w->has_border ) { win_x += 1; win_y += 1; } if( dragThumb ) { if( LastMouseEvent == MOUSE_RELEASE ) { dragThumb = false; } if( wid != current_window_id ) { return( ERR_NO_ERR ); } if( win_x == w->width - 1 ) { return( PositionToNewThumbPosition( w, win_y ) ); } return( ERR_NO_ERR ); } if( EditFlags.Dragging ) { if( LastMouseEvent == MOUSE_DRAG || LastMouseEvent == MOUSE_REPEAT ) { UpdateDrag( wid, win_x, win_y ); } else { if( LastMouseEvent == MOUSE_PRESS_R || LastMouseEvent == MOUSE_PRESS ) { EditFlags.Dragging = false; if( LastMouseEvent == MOUSE_PRESS_R ) { LastMouseEvent = MOUSE_RELEASE_R; } } } } if( LastMouseEvent == MOUSE_RELEASE_R || LastMouseEvent == MOUSE_DCLICK ) { if( wid == current_window_id && InsideWindow( wid, win_x, win_y ) ) { diff_word = (LastMouseEvent == MOUSE_DCLICK); if( GoToLineRelCurs( LeftTopPos.line + win_y - 1 ) ) { return( ERR_NO_ERR ); } win_x += LeftTopPos.column; win_x = RealColumnOnCurrentLine( win_x ); GoToColumnOnCurrentLine( win_x ); if( diff_word ) { InitWordSearch( EditVars.WordAltDefn ); } rc = DoSelectSelection( true ); if( diff_word ) { InitWordSearch( EditVars.WordDefn ); } return( rc ); } } /* * all kinds of stuff to do if the button was pressed */ if( LastMouseEvent == MOUSE_PRESS || LastMouseEvent == MOUSE_PRESS_R ) { if( wid != current_window_id ) { /* * swap to another window */ for( cinfo = InfoHead; cinfo != NULL; cinfo = cinfo->next ) { if( wid == cinfo->current_window_id ) { BringUpFile( cinfo, true ); break; } } } if( wid == current_window_id ) { if( !ShiftDown() ) { UnselectRegion(); } if( w->has_border && LastMouseEvent == MOUSE_PRESS ) { /* * clicked on menu for window */ if( win_x == 0 && win_y == 0 ) { return( DoWindowGadgetMenu() ); } /* * check for resize request */ if( win_x == w->width - 1 && win_y == w->height - 1 ) { return( ResizeCurrentWindowWithMouse() ); } /* * check for move request */ if( win_y == 0 ) { return( MoveCurrentWindowWithMouse() ); } } /* * check for locate cursor */ if( InsideWindow( wid, win_x, win_y ) ) { if( ShiftDown() ) { EditFlags.Dragging = true; } if( GoToLineRelCurs( LeftTopPos.line + win_y - 1 ) ) { return( ERR_NO_ERR ); } win_x += LeftTopPos.column; win_x = RealColumnOnCurrentLine( win_x ); GoToColumnOnCurrentLine( win_x ); if( ShiftDown() ) { EditFlags.Dragging = false; } else { InitSelectedRegion(); } return( ERR_NO_ERR ); } } if( EditFlags.Menus && wid == menu_window_id ) { i = GetMenuIdFromCoord( win_x - 1 ); if( i >= 0 ) { return( SetToMenuId( i ) ); } } } /* * allow double click to close window */ if( wid == current_window_id && LastMouseEvent == MOUSE_DCLICK ) { if( win_y == 0 && win_x == 0 ) { return( NextFile() ); } } /* * try to scroll screen */ if( (LastMouseEvent == MOUSE_REPEAT || LastMouseEvent == MOUSE_DCLICK || LastMouseEvent == MOUSE_PRESS) && w->has_border && wid == current_window_id && win_x == w->width - 1 ) { if( win_y == w->height - 2 ) { return( MoveScreenDown() ); } if( win_y == 1 ) { return( MoveScreenUp() ); } /* * if we have gadgets, then scroll based on position of scroll * thumb. furthermore, if the thumb is selected, then begin * thumb dragging mode */ if( w->has_gadgets ) { if( win_y == w->vert_scroll_pos ) { dragThumb = true; return( ERR_NO_ERR ); } else if( win_y < w->vert_scroll_pos ) { return( MovePageUp() ); } else { return( MovePageDown() ); } } else { if( win_y < w->height / 2 ) { return( MovePageUp() ); } else { return( MovePageDown() ); } } } /* * start dragging */ if( wid == current_window_id && (LastMouseEvent == MOUSE_DRAG || LastMouseEvent == MOUSE_DRAG_R ) && InsideWindow( wid, win_x, win_y ) ) { EditFlags.Dragging = true; UpdateDrag( wid, win_x, win_y ); } return( ERR_NO_ERR ); } /* HandleMouseEvent */
// 删除堆中第i个元素 void Del(int a[], int &n, int i) { a[i] = a[n--]; if(i>1 && a[i]>a[i/2]) ShiftUp(a, i); else ShiftDown(a, n, i); }
// 把数组a变成具备最大堆性质的数组 void MakeHeap(int a[], int n) { for(int i=n/2; i>0; --i) ShiftDown(a, n, i); }
ANT_INFO SegmentInfo(const float mas, const float hgh, int segment, ANT_DESK info) { ANT_INFO a; int i = 0; float b0, b1, b2, height = hgh * 100; if (info.seg == NULL || info.cm == NULL || segment < 0 || segment >= TOTAL_SEGMENTS_NUM || mas < 0 || height < 0) *ReturnReadError(&a); if (segment >= UNI_SEGMENTS_NUM) segment -= TOTAL_SEGMENTS_NUM - UNI_SEGMENTS_NUM; rewind(info.seg); rewind(info.cm); ShiftDown(info.seg, 1, i); // Check of the bounds fscanf(info.seg, ";%f;%f", &b0, &b1); if (mas > b0 || mas < b1) return *ReturnReadError(&a); fscanf(info.seg, ";%f;%f\n", &b0, &b1); if (height > b0 || height < b1) return *ReturnReadError(&a); ShiftDown(info.seg, segment, i); // Mass calculation if (ReadInfo(info.seg, &b0, &b1, &b2) != 0) a.mass = RegressionResult(mas, height, b0, b1, b2); else return *ReturnReadError(&a); ShiftDown(info.seg, UNI_SEGMENTS_NUM + 1, i); // Length calculation if (ReadInfo(info.seg, &b0, &b1, &b2) != 0) a.length = RegressionResult(mas, height, b0, b1, b2) / 100; else return *ReturnReadError(&a); ShiftDown(info.seg, UNI_SEGMENTS_NUM + 1, i); // Ix calculation if (ReadInfo(info.seg, &b0, &b1, &b2) != 0) a.ix = RegressionResult(mas, height, b0, b1, b2) / (100 * 100); else return *ReturnReadError(&a); ShiftDown(info.seg, UNI_SEGMENTS_NUM + 1, i); // Iy calculation if (ReadInfo(info.seg, &b0, &b1, &b2) != 0) a.iy = RegressionResult(mas, height, b0, b1, b2) / (100 * 100); else return *ReturnReadError(&a); ShiftDown(info.seg, UNI_SEGMENTS_NUM + 1, i); // Iz calculation if (ReadInfo(info.seg, &b0, &b1, &b2) != 0) a.iz = RegressionResult(mas, height, b0, b1, b2) / (100 * 100); else return *ReturnReadError(&a); // relative position reading ShiftDown(info.cm, segment + 2, i); while (fgetc(info.cm) != ';'); if (fscanf(info.cm,"%f;%f;%f", &b0, &b1, &b2) != 3) return *ReturnReadError(&a); switch (info.id) { case MALE * CHI: a.rcm = a.length * b0 / 100; break; case FEMALE * CHI: a.rcm = a.length * b1 / 100; break; case MALE * GER: a.rcm = a.length * b2 / 100; break; case FEMALE * GER: if (fscanf(info.cm, ";%f", &b0) == 0) return *ReturnReadError(&a); a.rcm = a.length * b0 / 100; break; } return a; } // End of SegmentInfo()