#include <stdafx.h>
#include <iostream>
#include <iomanip>
using namespace std;

#define nofreearea 2
#define noadequacyarea 3
#define allocated 4

#define noprocess 2
#define nosuchprocess 3
#define reclaimed 4

typedef struct TUN
{
 int address;
 int size;
 char name; 
 struct TUN *next;
} usedarea , *usedtable;

typedef struct TFN
{
 int address;
 int size;
 struct TFN *next;
} freearea, *freetable;

usedtable usedTable = NULL;
freetable freeTable = NULL;

int alloc( char processname , int processsize )
{

if( freeTable == NULL )
 return 1;
 freetable p = freeTable;
 freetable q = p;
 
 while( p != NULL && p->size < processsize )
 {
  q = p;
  p = p->next;
 }

 if( p == NULL )
  return 3;

 usedtable x = new usedarea;
 x->address = p->address;
 x->size = processsize;
 x->name = processname;
 x->next = NULL;

if( p->size > processsize )
 {
  p->size -= processsize;
  p->address += processsize;
 }

 else
 {
  if( p == freeTable )
   freeTable = NULL;
  else
   q->next = p->next;
  delete p;
 }

 usedtable r = usedTable;
 usedtable t = r;

 while( r != NULL && r->address < x->address )
 {
  t = r;
  r = r->next;
 }
 
 if( usedTable == NULL )
  usedTable = x;
 else
 {
  x->next = r;
  t->next = x;
 }

 return 4;
}

int Reclaim( char processname )
{
 if( usedTable == NULL )
  return 1;
 usedtable p = usedTable;
 usedtable q = p;
 while( p != NULL && p->name != processname )
 {
  q = p;
  p = p->next;
 }

 if( p == NULL )
  return 3;
 
 freetable r = freeTable;
 freetable t = r;
 freetable x;
 while( r != NULL && r->address < p->address )
 {
  t = r;
  r = r->next;
 }

 x = new freearea;
 x->address = p->address;
 x->size = p->size;
 x->next = NULL;

 if( r == freeTable )
 {
  x->next = r;
  freeTable = x;
  t = freeTable;
 }
 else
 {
  x->next = r;
  t->next = x;
 }

 while( t->next != NULL && t->address + t->size == t->next->address )
 {
  t->size += t->next->size;
  r = t->next;
  t->next = t->next->next;
  delete r;
 }
 
 if( p == usedTable )
 {
  usedTable = usedTable->next;
 }
 else
  q->next = p->next;
 delete p;

 return 4;
}

int Init()
{
 freeTable = new freearea;
 freeTable->address = 0;
 freeTable->size = 128;
 freeTable->next = NULL;
 return 1;
}

void processrequest()
{
 char processname;
 int processsize;
cout<<"...................."<<endl;
 cout<<"作业名: ";
 cin >> processname;
 cout<<"作业长度: ";
 cin >> processsize;
if(processsize<=128)
{
    int i=0;
 if( alloc( processname , processsize) == 4 )
 {
    i=i+processsize; 
    if(i>128)
 {
     cout<<"该作业超出空间"<<endl;
 }
 if(i<=128)
    cout<<"该作业已成功获得所需空间"<<endl;
  i=i+processsize;
 cout<<"........................................"<<endl;

 }
 else
  cout<<"该作业超出空间,没有获得所需空间"<<endl;
 cout<<"........................................"<<endl;
 return;
}
if(processsize>128)
{cout<<"该作业超出空间"<<endl;
 cout<<"........................................"<<endl;
}

}

void processreclaim()
{
 int processname;
 cout<<"...................."<<endl;
 cout<<"作业名: ";
 cin >>processname;
 int result = Reclaim( processname );
 if(  result == 4 )
  cout<<"该作业已成功回收"<<endl;
 else if( result == 2 || result == 1 )
  cout<<"系统没有作业或该作业不存在"<<endl;
 cout<<"...................."<<endl;

}

void freeTablePrint()
{
 cout<<endl<<endl<<endl<<"***********************************"<<endl;
 cout<<setw(10)<<"address"<<setw(10)<<"length"<<setw(10)<<"state"<<endl<<endl;
 freetable p = freeTable;
 usedtable q = usedTable;
 int x , y;
 while( p || q )
 {
  if( p )
   x = p->address;
  else
   x = 0x7fffffff;
  if( q )
   y = q->address;
  else
   y = 0x7fffffff;

  if( x < y )
  {
   cout<<setw(10)<<p->address<<setw(10)<<p->size<<setw(10)<<"空闲"<<endl;
   p = p->next;
  }
  if( x > y )
  {
   cout<<setw(10)<<q->address<<setw(10)<<q->size<<setw(10)<<"已分配"<<setw(10)<<"ID="<<q->name<<endl;
   q = q->next;
  }
 }
 cout<<endl<<endl<<endl<<"************************************"<<endl<<endl<<endl;

}

void main()
{
 Init();
 int choose;
 bool exitFlag = false;
 while( !exitFlag )
 {
  
  cout<<"0 退出"<<endl; 
  cout<<"1 分配主存"<<endl;
  cout<<"2 回收主存"<<endl;
  cout<<"3 显示主存"<<endl<<endl<<endl;
  cout<<"选择所要执行的操作:";
  cin>>choose;
  switch( choose )
  {
  case 0:
   exitFlag = true;
   break;
  case 1:
   processrequest();
   break;
  case 2:
   processreclaim();
   break;
  case 3:
   freeTablePrint();
   break;
  }
 }
}

心得体会:

     大概了解了主存空间分配。