void ChangeCell(long x,long y,int op) { struct y_list **ScanY; struct y_list *NewYNode; struct x_list **ScanX; struct x_list *NewXNode; ScanY=&Field; while(*ScanY!=NULL && (*ScanY)->y<y) ScanY=&((*ScanY)->next); if(*ScanY==NULL || (*ScanY)->y!=y) { if(op==SET || op==REV) { CheckPtr(NewYNode=malloc(sizeof(*NewYNode))); CheckPtr(NewXNode=malloc(sizeof(*NewXNode))); NewYNode->next=*ScanY; NewYNode->y=y; NewYNode->line=NewXNode; NewXNode->next=NULL; NewXNode->x=x; *ScanY=NewYNode; } return; } ScanX=&((*ScanY)->line); while(*ScanX!=NULL && (*ScanX)->x<x) ScanX=&((*ScanX)->next); if(*ScanX==NULL || (*ScanX)->x!=x) { if(op==SET || op==REV) { CheckPtr(NewXNode=malloc(sizeof(*NewXNode))); NewXNode->next=*ScanX; NewXNode->x=x; *ScanX=NewXNode; } return; } if(op==REV || op==CLR) { NewXNode=*ScanX; *ScanX=(*ScanX)->next; free(NewXNode); if((*ScanY)->line==NULL) { NewYNode=*ScanY; *ScanY=(*ScanY)->next; free(NewYNode); } } }
void CList::EnterYear(CCar* car) { CheckPtr(car); CYear y; bool bExit = false; do{ cout<<"\nEnter year of car: "; cin.clear(); cin.ignore(cin.rdbuf()->in_avail()); cin>>y; if(cin.good() && y >= 0 && y <= 2012) { car->vYear = y; bExit = true; } else { cout<<"\nError! Bad year!!!\n"; system("pause"); } system("cls"); }while(!bExit); cout<<"\nEntering of year - Good!\n"; system("pause"); system("cls"); }
void CList::EnterDist(CCar* car) { CheckPtr(car); CDist tmp; bool bExit = false; do{ cout<<"\nEnter kilometrage of car (km): "; cin.clear(); cin.ignore(cin.rdbuf()->in_avail()); cin>>tmp; if(cin.good()) { car->vKm = tmp; bExit = true; } else { cout<<"\nError! Bad kilometrage!!!\n"; system("pause"); } system("cls"); }while(!bExit); cout<<"\nEntering of kilometrage - Good!\n"; system("pause"); system("cls"); }
void CList::EnterBrand(CCar* car) { CheckPtr(car); int ch = 0; //choice car->vBrand = Br_EMPTY; do { for(UINT i = 1; i < 12; i++) { cout<<i<<": "; PrintBrand(i); //print list of brands for choice cout<<"\n"; } cout<<"\nLook at the brands, and enter your choice: "; cin.clear(); cin.ignore(cin.rdbuf()->in_avail()); cin>>ch; if(cin.good() && ch >= Br_OPEL && ch <= Br_KIA) { car->vBrand = (CBrand)ch; } else { cout<<"\nError! Bad brand!!!\n"; system("pause"); } system("cls"); }while(car->vBrand == Br_EMPTY); cout<<"\nEntering of brand - Good!\n"; system("pause"); system("cls"); }
void CList::ShowCarData(CCar* car) { CheckPtr(car); cout<<"\nSurname: \t" <<car->pSurName; //cout<<"\nBrand: \t\t"; //PrintBrand(car->vBrand); //cout<<"\nColor: \t\t(R:"<<(UINT)car->vCol.vR<<", G:"<<(UINT)car->vCol.vG<<", B:"<<(UINT)car->vCol.vB<<")"; //cout<<"\nYear: \t\t" <<car->vYear; //cout<<"\nKilometrage: \t" <<car->vKm; }
bool CList::DelNode(CCar* node) { CheckPtr(node); if(node->pSurName) { delete[] node->pSurName; } delete node; node = nullptr; return true; }
void CList::EnterCol(CCar* car) { CheckPtr(car); short r = 0, b = 0, g = 0; bool bExit = false; do{ system("cls"); cout<<"\nEnter Color (0-255): Red "; cin.clear(); cin.ignore(cin.rdbuf()->in_avail()); cin>>r; if(r < 0 || r > 255 || !cin.good()) { cout<<"\nError! Bad value! Try again!\n"; system("pause"); continue; } cout<<"\nEnter Color (0-255): Green "; cin.clear(); cin.ignore(cin.rdbuf()->in_avail()); cin>>g; if(g < 0 || g > 255 || !cin.good()) { cout<<"\nError! Bad value! Try again!\n"; system("pause"); continue; } cout<<"\nEnter Color (0-255): Blue "; cin.clear(); cin.ignore(cin.rdbuf()->in_avail()); cin>>b; if(b < 0 || b > 255 || !cin.good()) { cout<<"\nError! Bad value! Try again!\n"; system("pause"); continue; } /////if entering is good car->vCol.vR = (BYTE)r; car->vCol.vG = (BYTE)g; car->vCol.vB = (BYTE)b; bExit = true; ////////// system("cls"); }while(!bExit); cout<<"\nEntering of Color - Good!\n"; system("pause"); system("cls"); }
BOOL CMemPool::ReleaseBuffer( MEM_POOL_BUFFERCELL* pBuffer ,int iRole) { CSingleLock singlelock(&m_DataLock,TRUE); CString strLog; if (!CheckPtr(pBuffer)) { strLog.Format(_T("###CheckPtr(pBufferCell) failed. iRole=%d,m_dwAllFreeSize=%d, m_dwTotalSize=%d, EmptyBufferList=%d,pBuffer=%d"), iRole,m_dwAllFreeSize, m_dwTotalSize, m_BufferList.GetCount(),pBuffer); WriteLog(_T("CMemPool"), logLevelWarring, strLog); return FALSE; } if (pBuffer->pExtBuffer == NULL) { DWORD nUnitNum = pBuffer->dwUnitNum; DWORD nStartPos = pBuffer->dwBufferPos; if (nStartPos < 0 || nStartPos >= m_dwUnitSum) { return FALSE; } if ((nStartPos + nUnitNum > m_dwUnitSum) || (nStartPos + nUnitNum < 0)) { return FALSE; } for (DWORD i=0; i<nUnitNum; ++i) { m_pUnitUsed[nStartPos + i] = 0;//复位标志 } pBuffer->pBuffer = NULL; pBuffer->dwBufferPos = 0; pBuffer->dwUnitNum = 0; m_dwAllFreeSize += nUnitNum * m_dwUnitSize; strLog.Format(_T("###iRole=%d,m_dwAllFreeSize=%d, m_dwTotalSize=%d, HaveFreedSize=%d, EmptyBufferList=%d, dwBufferPos=%d, dwUnitNum=%d, pBuffer=%d"), iRole,m_dwAllFreeSize, m_dwTotalSize, nUnitNum * m_dwUnitSize, m_BufferList.GetCount(), nStartPos, nUnitNum, pBuffer); // WriteLog(_T("CMemPool"), _T("ReleaseBuffer"), strLog); } else//如果扩展链表不为空,说明当时取的时候就没取到内存池的,是单独new的 { delete []pBuffer->pExtBuffer; pBuffer->pExtBuffer = NULL; } m_BufferList.AddTail((CObject*)pBuffer); return TRUE; }
////////////////////////////////////////////////// //private methods ////////////////////////////////////////////////// void CList::EnterSurName(CCar* car) { CheckPtr(car); char tmp; cout<<"\nEnter surname: "; cin>>tmp; car->pSurName = new char[(UINT) (cin.rdbuf()->in_avail() + 1)]; car->pSurName[0] = tmp; cin>>(car->pSurName + 1 ); system("cls"); cout<<"\nEnter Surname - Good!\n"; system("pause"); system("cls"); }
void Increment(long x) { struct count **Scan; struct count *NewNode; while(*Base!=NULL && (*Base)->x<x-1) Base=&((*Base)->next); for(Scan=Base; *Scan!=NULL && (*Scan)->x<x; Scan=&((*Scan)->next)); if(*Scan!=NULL && (*Scan)->x==x) (*Scan)->cnt++; else { CheckPtr(NewNode=malloc(sizeof(*NewNode))); NewNode->cnt=1; NewNode->next=*Scan; NewNode->x=x; *Scan=NewNode; } }
void CList::GrByBrand() { UINT n = this->GetCount(); UINT size = (UINT)sizeof(CCar); if(n == 0) { cout<<"\nNo available elements in list!\n"; return; } BYTE* ptr = new BYTE[size * n]; //create a new region of memory CheckPtr(ptr); CCar* cur = pFirst; //current - runs in old list pFirst = (CCar*)ptr; //runs in new list CBrand tBrand = Br_EMPTY; for(UINT i = 0; i < n; i++) { tBrand = cur->vBrand; //save current brand pFirst->pNext = (CCar*)(ptr + i * size); pFirst = pFirst->pNext; memcpy(pFirst, cur, size); cur = cur->pNext; if(tBrand != cur->vBrand) { } } pFirst->pNext = nullptr; pFirst = (CCar*)ptr; bNeat = true; //now it is neat list }
void CList::Neat() { UINT n = this->GetCount(); UINT size = (UINT)sizeof(CCar); if(n == 0) { cout<<"\nNo available elements in list!\n"; return; } BYTE* ptr = new BYTE[size * n]; //create a new region of memory CheckPtr(ptr); CCar* cur = pFirst; //current - runs in old list CCar* prev = nullptr; pFirst = (CCar*)ptr; //runs in new list for(UINT i = 0; i < n; i++) { pFirst->pNext = (CCar*)(ptr + i * size); pFirst = pFirst->pNext; memcpy(pFirst, cur, size); prev = cur; if(i < n - 1) { cur = cur->pNext; } delete prev; } pFirst->pNext = nullptr; pFirst = (CCar*)ptr; bNeat = true; //now it is neat list }
void Step(void) { struct y_list *Input; struct y_list **Output; struct x_list *PrevLine; struct x_list *CurrLine; struct x_list *NextLine; struct y_list *NewYNode; struct x_list *NewXNode; struct x_list *NewLine; struct x_list **Add; void *SavePtr; struct x_list *ScanX; long Y; PrevLine=CurrLine=NextLine=NULL; NewLine=NULL; Count=NULL; Input=Field; Output=&Field; do { /* check if we can jump over empty space */ if(PrevLine==NULL && CurrLine==NULL && NextLine==NULL && NewLine==NULL) { if(Input==NULL) /* if there is no more spots then it is done */ break; NextLine=Input->line; Y=Input->y-1; Input=Input->next; } /* build list of neighbour counts for current line */ Base=&Count; for(ScanX=PrevLine; ScanX!=NULL; ScanX=ScanX->next) { Increment(ScanX->x-1); Increment(ScanX->x); Increment(ScanX->x+1); } Base=&Count; for(ScanX=CurrLine; ScanX!=NULL; ScanX=ScanX->next) { Increment(ScanX->x-1); Increment(ScanX->x+1); } Base=&Count; for(ScanX=NextLine; ScanX!=NULL; ScanX=ScanX->next) { Increment(ScanX->x-1); Increment(ScanX->x); Increment(ScanX->x+1); } while(*Output!=NULL && (*Output)->y<Y-1) Output=&((*Output)->next); /* build into field the new line which was created for previous line */ if(NewLine!=NULL) { if(PrevLine==NULL) { /* insert new record into Y list */ CheckPtr(NewYNode=malloc(sizeof(*NewYNode))); NewYNode->y=Y-1; NewYNode->next=*Output; NewYNode->line=NewLine; *Output=NewYNode; } else { FreeLine((*Output)->line); (*Output)->line=NewLine; } } else { if(PrevLine!=NULL) { /* delete record from Y list */ SavePtr=*Output; FreeLine((*Output)->line); *Output=(*Output)->next; free(SavePtr); } } /* create new line for current line */ NewLine=NULL; Add=
 ScanX=CurrLine; while(Count!=NULL) { while(ScanX!=NULL && ScanX->x<Count->x) ScanX=ScanX->next; /* apply rules of surviving and borning */ if(Count->cnt==3 || (Count->cnt==2 && ScanX!=NULL && Count->x==ScanX->x)) { CheckPtr(NewXNode=malloc(sizeof(*NewXNode))); NewXNode->next=NULL; NewXNode->x=Count->x; *Add=NewXNode; Add=&(NewXNode->next); } SavePtr=Count->next; free(Count); Count=SavePtr; } /* move to the next line */ PrevLine=CurrLine; CurrLine=NextLine; Y++; if(Input!=NULL && Input->y==Y+1) { NextLine=Input->line; Input=Input->next; } else { NextLine=NULL; } } while(1); }
void Load(FILE *file) { struct y_list **AddY; struct x_list **AddX; struct y_list *NewYNode; struct x_list *NewXNode; long X,Y; int ch; FreeField(); AddY=&Field; Y=0; do { /* process a line */ X=0; do /* skip leading spaces */ { ch=getc(file); if(ch=='\t') X=(X|7)+1; else if(ch=='\n') break; else if(isspace(ch)) X++; else break; } while(1); if(ch!='\n' && ch!=EOF) { CheckPtr(NewYNode=malloc(sizeof(*NewYNode))); NewYNode->y=Y; NewYNode->next=NULL; AddX=&(NewYNode->line); *AddY=NewYNode; AddY=&(NewYNode->next); do { if(ch=='\t') X=(X|7)+1; else if(isspace(ch)) X++; else { CheckPtr(NewXNode=malloc(sizeof(*NewXNode))); NewXNode->x=X; NewXNode->next=NULL; *AddX=NewXNode; AddX=&(NewXNode->next); X++; } ch=getc(file); } while(ch!='\n' && ch!=EOF); } Y++; } while(ch!=EOF); }
MEM_POOL_BUFFERCELL* CMemPool::GetEmptyBuffer( DWORD dwBufSize, int iRole ) { CSingleLock singlelock(&m_DataLock,TRUE); CString strLog; MEM_POOL_BUFFERCELL* pBufferCell = NULL; if (!m_BufferList.IsEmpty()) { pBufferCell = (MEM_POOL_BUFFERCELL*)m_BufferList.RemoveHead(); if (pBufferCell != NULL) { if (!CheckPtr(pBufferCell)) { //如果此内存块校验失败,这个链表节点丢掉 strLog.Format(_T("CheckPtr(pBufferCell) failed. iRole=%d,m_dwAllFreeSize=%d, m_dwTotalSize=%d, NeedSize=%d, m_dwFreePos=%d"), iRole,m_dwAllFreeSize, m_dwTotalSize, dwBufSize, m_dwFreePos); WriteLog(_T("CMemPool"), logLevelWarring, strLog); return NULL; } } else { strLog.Format(_T("iRole=%d,pBufferCell==NULL.m_dwAllFreeSize=%d, m_dwTotalSize=%d, NeedSize=%d, m_dwFreePos=%d"), iRole,m_dwAllFreeSize, m_dwTotalSize, dwBufSize, m_dwFreePos); WriteLog(_T("CMemPool"), logLevelWarring, strLog); return NULL; } } else { strLog.Format(_T("iRole=%d,m_BufferList.IsEmpty() .m_dwAllFreeSize=%d, m_dwTotalSize=%d, NeedSize=%d, m_dwFreePos=%d"), iRole,m_dwAllFreeSize, m_dwTotalSize, dwBufSize, m_dwFreePos); WriteLog(_T("CMemPool"), logLevelWarring, strLog); return NULL; } //链表节点取出来成功,开始取空闲数据地址 BOOL bFind = FALSE; DWORD dwUnitNeed = dwBufSize / m_dwUnitSize; if (dwBufSize % m_dwUnitSize != 0) ++dwUnitNeed; DWORD dwStart = m_dwFreePos; DWORD dwFree = 0; for (; dwStart < m_dwUnitSum; ++dwStart) { if (m_pUnitUsed[dwStart] == 0) { if (++dwFree >= dwUnitNeed) { bFind = TRUE; break; } } else { dwFree = 0; } } if (!bFind) { dwFree = 0; //如果从上次使用位置没找到足够内存,就从内存池头部开始找空闲的 for (dwStart = 0; dwStart < m_dwFreePos; ++dwStart) { if (m_pUnitUsed[dwStart] == 0) { if (++dwFree >= dwUnitNeed) { bFind = TRUE; break; } } else { dwFree = 0; } } } if (bFind) { m_dwFreePos = dwStart+1; //更新空闲位置,提高查询效率 pBufferCell->dwBufferPos = dwStart - (dwUnitNeed-1); //返回开始位置,便于回收 pBufferCell->dwUnitNum = dwUnitNeed; //返回内存单元数,便于回收 for (DWORD i=0; i<dwUnitNeed; ++i) { m_pUnitUsed[pBufferCell->dwBufferPos + i] = 1;//置内存标志 } pBufferCell->pBuffer = m_pDataBuffer + pBufferCell->dwBufferPos * m_dwUnitSize; //返回指针 m_dwAllFreeSize -= dwUnitNeed * m_dwUnitSize; } else//找遍整个内存池,也没找到连续的符合要求的内存,则动用扩展内存动态分配 { if (pBufferCell->pExtBuffer != NULL) { delete []pBufferCell->pExtBuffer; } pBufferCell->pExtBuffer = new char[dwBufSize]; } strLog.Format(_T("iRole=%d,m_dwAllFreeSize=%d, m_dwTotalSize=%d, NeedSize=%d, EmptyBufferList=%d, dwBufferPos=%d, dwUnitNum=%d, pBufferCell=%d"), iRole,m_dwAllFreeSize, m_dwTotalSize, dwBufSize, m_BufferList.GetCount(), pBufferCell->dwBufferPos, pBufferCell->dwUnitNum, pBufferCell); //WriteLog(_T("CMemPool"), _T("GetEmptyBuffer"), strLog); return pBufferCell; }