⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 oskechengshengji.c

📁 TC3.0编译通过
💻 C
字号:
#include   <stdio.h>   
  #include   <conio.h>   
  #include   <stdlib.h>   
  #include   <ctype.h>   
  #include   <graphics.h>   
    
    
  #define N_OF_TABLES 100   
  #define MENU_POSX 100   
  #define MENU_POSY 350   
    
    
  struct   MemoryTable   
  {   
  int   iId;   
  int   iStartPos;   
  int   iSize;   
  int   iFree;   
  }g_stcMT[N_OF_TABLES];   
  int   g_iMTLength=0;   
    
    
  const   int g_ciMenuAmount   =   3;   
  char   *g_pszMenu[g_ciMenuAmount]   ={"1.   Allot   Memory",   
      "2.   Free   Memory",   
      "3.   Exit"};   
    
    
  int   InitGraph();   
  void   CloseGraph();   
  void   InitMemTable();   
  int   AllotMem(int   iSize);   
  int   FreeMem(int   iId);   
  void   FreeWithLast(int   iId);   
  void   FreeWithNext(int   iId);   
  void   DrawMemory();   
  void   DrawMenu(int   iIndex);   
  void   DrawLine(int   iLeft,   int   iTop,   int   iRight,   int   iBottom,   int   iColor);   
  void   DrawEditBox(int   iLeft,   int   iTop,   int   iRight,   int   iBottom,   int   iColor);   
  void   DrawText(char   *pszText,   int   x,   int   y,   int   iColor);   
  void   Proceed();   
  void   OnKeyDownUp(int   &iMenuIndex);   
  void   OnKeyDownDown(int   &iMenuIndex);   
  void   OnKeyDownEnter(int   iMenuIndex);   
  void   OnMenuAllot();   
  void   OnMenuFree();   
    
    
  int   main()   
  {   
  if(   InitGraph()   )   exit(1);   
    
  InitMemTable();   
    
  DrawLine(10,   MENU_POSY-50,   630,   MENU_POSY-50,   YELLOW);   
  DrawLine(10,   MENU_POSY+80,   630,   MENU_POSY+80,   YELLOW);   
    
  DrawMemory();   
  Proceed();   
    
  CloseGraph();   
    
  return   0;   
  }   
    
    
  int   InitGraph()   
  {   
  int   gdriver   =   DETECT,   gmode,   errorcode;   
  initgraph(&gdriver,   &gmode,   "");   
  errorcode   =   graphresult();   
    
  return   errorcode;   
  }   
    
  void   CloseGraph()   
  {   
  closegraph();   
  }   
    
    
  void   InitMemTable()   
  {   
  g_iMTLength=3;   
    
  for(int   i=0;   i<g_iMTLength;   i++)   
  {   
  g_stcMT[i].iId=i+1;   
  g_stcMT[i].iStartPos=   
  i==0   ?   0   :   g_stcMT[i-1].iStartPos+g_stcMT[i-1].iSize;   
  g_stcMT[i].iSize=(i+1)*100;   
  g_stcMT[i].iFree=0;   
  }   
  }   
    
    
  int   AllotMem(int   iSize)   
  {   
  if(   iSize   <   1   ||   iSize   >   600   )   return   1;   
    
  for(int   i=0;   i<g_iMTLength;   i++)   
  {   
  if(   g_stcMT[i].iFree   ==   0   &&   g_stcMT[i].iSize   >=   iSize   )   
  {   
  for(int   j=g_iMTLength;   j>i+1;   j--)   
  {   
  g_stcMT[j].iId=g_stcMT[j-1].iId+1;   
  g_stcMT[j].iStartPos=g_stcMT[j-1].iStartPos;   
  g_stcMT[j].iSize=g_stcMT[j-1].iSize;   
  g_stcMT[j].iFree=g_stcMT[j-1].iFree;   
  }   
    
  g_stcMT[i+1].iId=g_stcMT[i].iId+1;   
  g_stcMT[i+1].iStartPos=g_stcMT[i].iStartPos+iSize;   
  g_stcMT[i+1].iSize=g_stcMT[i].iSize-iSize;   
  g_stcMT[i+1].iFree=0;   
    
  g_stcMT[i].iSize=iSize;   
  g_stcMT[i].iFree=1;   
    
  g_iMTLength++;   
    
  break;   
  }   
  }   
    
  return   0;   
  }   
    
    
  int   FreeMem(int   iId)   
  {   
  if(   iId   <   1   ||   iId   >   g_iMTLength   )   return   1;   
    
  if(   iId   ==   1   )   FreeWithNext(iId);   
  else   
  {   
  if(   iId   ==   g_iMTLength   )   FreeWithLast(iId);   
  else   
  {   
  FreeWithNext(iId);   
  FreeWithLast(iId);   
  }   
  }   
    
  return   0;   
  }   
    
  void   FreeWithLast(int   iId)   
  {   
  if(   iId   <=   1   )   return;   
    
  if(   g_stcMT[iId-2].iFree   ==   1   )   
  {   
  g_stcMT[iId-1].iFree=0;   
  return;   
  }   
    
  char   szStartPos[5];   
  sprintf(szStartPos,   "%d",   g_stcMT[iId-1].iStartPos);   
  DrawText(szStartPos,   g_stcMT[iId-1].iStartPos+20,   90,   BLACK);   
    
  g_stcMT[iId-2].iSize+=g_stcMT[iId-1].iSize;   
    
  for(int   i=iId-1;   i<g_iMTLength-1;   i++)   
  {   
  g_stcMT[i].iId=g_stcMT[i+1].iId-1;   
  g_stcMT[i].iStartPos=g_stcMT[i+1].iStartPos;   
  g_stcMT[i].iSize=g_stcMT[i+1].iSize;   
  g_stcMT[i].iFree=g_stcMT[i+1].iFree;   
  }   
    
  g_iMTLength--;   
  }   
    
    
  void   FreeWithNext(int   iId)   
  {   
  if(   iId   >=   g_iMTLength   )   return;   
    
  if(   g_stcMT[iId].iFree   ==   1   )   
  {   
  g_stcMT[iId-1].iFree=0;   
  return;   
  }   
    
  char   szStartPos[5];   
  sprintf(szStartPos,   "%d",   g_stcMT[iId].iStartPos);   
  DrawText(szStartPos,   g_stcMT[iId].iStartPos+20,   90,   BLACK);   
    
  g_stcMT[iId-1].iSize+=g_stcMT[iId].iSize;   
  g_stcMT[iId-1].iFree=0;   
    
  for(int   i=iId;   i<g_iMTLength-1;   i++)   
  {   
  g_stcMT[i].iId=g_stcMT[i+1].iId-1;   
  g_stcMT[i].iStartPos=g_stcMT[i+1].iStartPos;   
  g_stcMT[i].iSize=g_stcMT[i+1].iSize;   
  g_stcMT[i].iFree=g_stcMT[i+1].iFree;   
  }   
    
  g_iMTLength--;   
  }   
    
    
  void   DrawMemory()   
  {   
  int   iColorOld=getcolor();   
    
  for(int   i=0;   i<g_iMTLength;   i++)   
  {   
  if(   g_stcMT[i].iFree   ==   0   )   setfillstyle(1,   YELLOW);   
  else   setfillstyle(1,   RED);   
    
  bar(g_stcMT[i].iStartPos+20,   100,   
    g_stcMT[i].iStartPos+g_stcMT[i].iSize+20,   150);   
    
  if(   g_stcMT[i].iFree   ==   0   )   
  DrawLine(g_stcMT[i].iStartPos+20,   100,   
  g_stcMT[i].iStartPos+20,   150,   BLACK);   
  else   
  DrawLine(g_stcMT[i].iStartPos+20,   100,   
  g_stcMT[i].iStartPos+20,   150,   WHITE);   
    
  char   szStartPos[5];   
  sprintf(szStartPos,   "%d",   g_stcMT[i].iStartPos);   
  DrawText(szStartPos,   g_stcMT[i].iStartPos+20,   90,   GREEN);   
  char   szId[5];   
  sprintf(szId,   "%d",   g_stcMT[i].iId);   
  DrawText(szId,   g_stcMT[i].iStartPos+25,   120,   BLACK);   
  }   
    
  setcolor(iColorOld);   
  }   
  }
 void   DrawMenu(int   iIndex)   
  {   
  int   iBD=15;   
  int   iFD=20;   
  int   iColor=getcolor();   
  setcolor(YELLOW);   
    
  for(int   i=0;   i<g_ciMenuAmount;   i++)   
  {   
  if(   i==iIndex   )   setcolor(GREEN);   
  else   setcolor(YELLOW);   
  outtextxy(MENU_POSX,   MENU_POSY+i*iBD,   g_pszMenu[i]);   
    
  if(   i==iIndex   )   setcolor(GREEN);   
  else   setcolor(BLACK);   
  outtextxy(MENU_POSX-iFD,   MENU_POSY+i*iBD,   "->");   
  }   
    
  DrawText("Enter:",   MENU_POSX+200,   MENU_POSY,   8);   
  DrawEditBox(MENU_POSX+200,   MENU_POSY+10,   MENU_POSX+400,   MENU_POSY+25,   8);   
    
  setcolor(iColor);   
  }   
    
  void   DrawLine(int   iLeft,   int   iTop,   int   iRight,   int   iBottom,   int   iColor)   
  {   
  int   iColorOld=getcolor();   
    
  setcolor(iColor);   
  line(iLeft,   iTop,   iRight,   iBottom);   
    
  setcolor(iColorOld);   
  }   
    
  void   DrawEditBox(int   iLeft,   int   iTop,   int   iRight,   int   iBottom,   int   iColor)   
  {   
  int   iColorOld=getcolor();   
    
  setcolor(iColor);   
  rectangle(iLeft,   iTop,   iRight,   iBottom);   
    
  setcolor(iColorOld);   
  }   
    
    
  void   DrawText(char   *pszText,   int   x,   int   y,   int   iColor)   
  {   
  int   iColorOld=getcolor();   
    
  setcolor(iColor);   
  outtextxy(x,   y,   pszText);   
    
  setcolor(iColorOld);   
    
  }   
    
    
  void   Proceed()   
  {   
  int   iMenuIndex=0;   
    
  while(1)   
  {   
  if(   kbhit()   )   
  {   
  int   iKey=getch();   
  if(   iKey   ==   0   )   iKey=getch();   
    
  //if(   iKey   ==   27   )   break;   //ESC   
    
  if(   iKey   ==   13   )   OnKeyDownEnter(iMenuIndex);   
  if(   iKey   ==   72   )   OnKeyDownUp(iMenuIndex);   
  if(   iKey   ==   80   )   OnKeyDownDown(iMenuIndex);   
    
  DrawMemory();   
  }   
    
  DrawMenu(iMenuIndex);   
  }   
  }   
    
    
  void   OnKeyDownUp(int   &iMenuIndex)   
  {   
  iMenuIndex   =   iMenuIndex==0   ?   g_ciMenuAmount-1   :   iMenuIndex-1;   
  }   
    
    
  void   OnKeyDownDown(int   &iMenuIndex)   
  {   
  iMenuIndex   =   iMenuIndex==g_ciMenuAmount-1   ?   0   :   iMenuIndex+1;   
  }   
    
    
  void   OnKeyDownEnter(int   iMenuIndex)   
  {   
  if(   iMenuIndex   ==   g_ciMenuAmount-1   )   
  {   
  CloseGraph();   
  exit(0);   
  }   
    
  if(   iMenuIndex   ==   0   )   
  {   
  OnMenuAllot();   
  }   
    
  if(   iMenuIndex   ==   1   )   
  {   
  OnMenuFree();   
  }   
  }   
    
  void   OnMenuAllot()   
  {   
  DrawText("Enter:",   MENU_POSX+200,   MENU_POSY,   BLUE);   
  DrawEditBox(MENU_POSX+200,   MENU_POSY+10,   MENU_POSX+400,   MENU_POSY+25,   YELLOW);   
    
  char   szNumber[16]={0};   
  int   iNumberIndex=0;   
  while(1)   
  {   
  if(   kbhit()   )   
  {   
  int   iKey=getch();   
  if(   iKey   ==   0   )   iKey=getch();   
    
  if(   iKey   ==   13   )   break;   
  if(   iKey   ==   27   )   
  {   
  DrawText(szNumber,   MENU_POSX+205,   MENU_POSY+14,   BLACK);   
  return;   
  }   
  if(   iKey>47   &&   iKey<58   &&   iNumberIndex<5   )   
  {   
  DrawText(szNumber,   MENU_POSX+205,   MENU_POSY+14,   BLACK);   
    
  szNumber[iNumberIndex++]=iKey;   
  szNumber[iNumberIndex]=0;   
  }   
  if(   iKey==8   &&   iNumberIndex>0   )   
  {   
  DrawText(szNumber,   MENU_POSX+205,   MENU_POSY+14,   BLACK);   
    
  szNumber[--iNumberIndex]=0;   
  }   
  }   
    
  DrawText(szNumber,   MENU_POSX+205,   MENU_POSY+14,   GREEN);   
  }   
    
  DrawText(szNumber,   MENU_POSX+205,   MENU_POSY+14,   BLACK);   
  AllotMem(   atoi(szNumber)   );   
  }   
    
    
  void   OnMenuFree()   
  {   
  DrawText("Enter:",   MENU_POSX+200,   MENU_POSY,   BLUE);   
  DrawEditBox(MENU_POSX+200,   MENU_POSY+10,   MENU_POSX+400,   MENU_POSY+25,   YELLOW);   
    
  char   szNumber[16]={0};   
  int   iNumberIndex=0;   
  while(1)   
  {   
  if(   kbhit()   )   
  {   
  int   iKey=getch();   
  if(   iKey   ==   0   )   iKey=getch();   
    
  if(   iKey   ==   13   )   break;   
  if(   iKey   ==   27   )   
  {   
  DrawText(szNumber,   MENU_POSX+205,   MENU_POSY+14,   BLACK);   
  return;   
  }   
  if(   iKey>47   &&   iKey<58   &&   iNumberIndex<5   )   
  {   
  DrawText(szNumber,   MENU_POSX+205,   MENU_POSY+14,   BLACK);   
    
  szNumber[iNumberIndex++]=iKey;   
  szNumber[iNumberIndex]=0;   
  }   
  if(   iKey==8   &&   iNumberIndex>0   )   
  {   
  DrawText(szNumber,   MENU_POSX+205,   MENU_POSY+14,   BLACK);   
    
  szNumber[--iNumberIndex]=0;   
  }   
  }   
    
  DrawText(szNumber,   MENU_POSX+205,   MENU_POSY+14,   GREEN);   
  }   
    
  DrawText(szNumber,   MENU_POSX+205,   MENU_POSY+14,   BLACK);   
  FreeMem(   atoi(szNumber)   )

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -