[C++] c++磁盘存储空间的管理模拟(UNIX存储管理的成组链接法的设计与实现) →→→→→进入此内容的聊天室

来自 , 2019-09-27, 写在 C++, 查看 102 次.
URL http://www.code666.cn/view/07a4e20a
  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<iostream.h>
  4.  
  5. const int MAXGROUP=10;//定义组的大小
  6. const int MAXJOB=100;//定义一个作业最大能申请的块数
  7.  
  8. //结构体定义
  9. typedef struct node
  10. {
  11.         int quantity;
  12.         int cell[MAXGROUP];
  13.         struct node *next;
  14. } group;
  15.  
  16. typedef struct node1
  17. {
  18.         char name[20];
  19.         int quantity;
  20.         int cell[MAXJOB];
  21.         struct node1 *next;
  22. } job;
  23.  
  24. group *head;
  25. int total;
  26.  
  27. job *jhead;
  28.  
  29. //初始化组函数
  30. group *initial()
  31. {
  32.         int i;
  33.         group *p;
  34.  
  35.         p=new group;
  36.  
  37.         p->quantity=0;
  38.         p->next=NULL;
  39.  
  40.         for ( i=0; i<MAXGROUP; i++ )
  41.         {
  42.                 p->cell[i]=-1;
  43.         }
  44.  
  45.         return p;
  46. }
  47.  
  48. //初始化作业函数
  49. job *initial_job()
  50. {
  51.         int i;
  52.         job *p;
  53.  
  54.         p=new job;
  55.  
  56.         strcpy ( p->name,"" );
  57.         p->quantity=0;
  58.         p->next=NULL;
  59.  
  60.         for ( i=0; i<MAXGROUP; i++ )
  61.         {
  62.                 p->cell[i]=-1;
  63.         }
  64.  
  65.         return p;
  66. }
  67.  
  68. //读入空闲块流文件
  69. void readData()
  70. {
  71.         FILE *fp;
  72.         char fname[20];
  73.         int temp;
  74.         group *p;
  75.  
  76.         cout<<"请输入初始空闲块数据文件名:";
  77.  
  78.         cin>>fname;
  79.  
  80.         if ( ( fp=fopen ( "5unix.txt","r" ) ) ==NULL )
  81.         {
  82.                 cout<<"错误,文件打不开,请检查文件名:)"<<endl;
  83.         }
  84.         else
  85.         {
  86.                 cout<<"=================================================="<<endl;
  87.                 cout<<"读入的初始空闲块号:";
  88.                 while ( !feof ( fp ) )
  89.                 {
  90.                         fscanf ( fp,"%d ",&temp );
  91.                         if ( head->quantity<MAXGROUP )
  92.                         {
  93.                                 head->cell[head->quantity]=temp;
  94.                                 head->quantity++;
  95.                         }
  96.                         else
  97.                         {
  98.                                 p=initial();
  99.                                 p->next=head;
  100.                                 head=p;
  101.                                 p->cell[p->quantity]=temp;
  102.                                 p->quantity++;
  103.                         }
  104.                         total++;
  105. //输出初始数据
  106.                         cout<<temp<<" ";
  107.                 }
  108.  
  109.                 cout<<endl<<"总空闲块数:"<<total<<endl;
  110.         }
  111. }
  112.  
  113. //查看专用块函数
  114. void view()
  115. {
  116.         int i;
  117.  
  118.         cout<<endl<<"专用块数据如下:"<<endl;
  119.         cout<<"-------------------------------"<<endl;
  120.         cout<<"所存储的空闲块号:";
  121.         for ( i=0; i<head->quantity; i++ )
  122.         {
  123.                 cout<<head->cell[i]<<" ";
  124.         }
  125.         cout<<endl<<"专用块空闲块数为:"<<head->quantity;
  126.         cout<<endl<<"总空闲块数:"<<total<<endl;
  127. }
  128.  
  129.  
  130. //新申请函数
  131. void bid()
  132. {
  133.         char jobname[20];
  134.         int number;
  135.         int i;
  136.         job *p;
  137.  
  138.         cout<<"----------------------------------"<<endl;
  139.         cout<<"请输入新专业名:";
  140.         cin>>jobname;
  141.         cout<<"所需内存块数:";
  142.         cin>>number;
  143.  
  144.         if ( number>total )
  145.         {
  146.                 cout<<"所需内存块数大于当前空闲块数,请稍候再试:)"<<endl;
  147.         }
  148.         else
  149.         {
  150.                 p=initial_job();
  151.                 strcpy ( p->name,jobname );
  152.                 p->next=jhead->next;
  153.                 jhead->next=p;
  154.                 p->quantity=number;
  155.                 cout<<"所申请到的空闲块号流:";
  156.                 for ( i=0; i<number; i++ )
  157.                 {
  158.                         if ( head->quantity>1 )
  159.                         {
  160.                                 cout<<head->cell[head->quantity-1]<<" ";
  161.                                 head->quantity--;
  162.                                 p->cell[i]=head->cell[head->quantity-1];
  163.                         }
  164.                         else
  165.                         {
  166.                                 cout<<head->cell[0]<<" ";
  167.                                 p->cell[i]=head->cell[head->quantity-1];
  168.                                 head->quantity--;
  169.                                 if ( head->next!=NULL )
  170.                                 {
  171.                                         head=head->next;
  172.                                 }
  173.                         }
  174.                         total--;
  175.                 }
  176.         }
  177.         cout<<endl;
  178. }
  179.  
  180. //撤消作业
  181. void finish()
  182. {
  183.         char jobname[20];
  184.         int i;
  185.         job *p,*q;
  186.         group *r;
  187.  
  188.         cout<<"请输入要撤消的作业名:";
  189.         cin>>jobname;
  190.  
  191.         q=jhead;
  192.         p=jhead->next;
  193.         while ( ( p!=NULL ) && ( strcmp ( p->name,jobname ) ) )
  194.         {
  195.                 q=q->next;
  196.                 p=p->next;
  197.         }
  198.  
  199.         if ( p==NULL )
  200.         {
  201.                 cout<<"Sorry,没有该作业"<<endl;
  202.         }
  203.         else
  204.         {
  205.                 for ( i=0; i<p->quantity; i++ )
  206.                 {
  207.                         if ( head->quantity<MAXGROUP )
  208.                         {
  209.                                 head->cell[head->quantity]=p->cell[i];
  210.                                 head->quantity++;
  211.                         }
  212.                         else
  213.                         {
  214.                                 r=initial();
  215.                                 r->next=head;
  216.                                 head=r;
  217.                                 r->cell[r->quantity]=p->cell[i];
  218.                                 r->quantity++;
  219.                         }
  220.                 }
  221.                 total+=p->quantity;
  222.  
  223.                 q->next=p->next;
  224.                 delete p;
  225.         }
  226. }
  227.  
  228. //显示版权信息函数
  229. void version()
  230. {
  231.         cout<<endl<<endl;
  232.  
  233.         cout<<" ┏━━━━━━━━━━━━━━━━━━━━━━━┓"<<endl;
  234.         cout<<" ┃     模拟UNIX的成组链接法        ┃"<<endl;
  235.         cout<<" ┠───────────────────────┨"<<endl;
  236.         cout<<" ┃   (c)All Right Reserved Neo       ┃"<<endl;
  237.         cout<<" ┃      sony006@163.com          ┃"<<endl;
  238.         cout<<" ┃     version 2004 build 1122      ┃"<<endl;
  239.         cout<<" ┗━━━━━━━━━━━━━━━━━━━━━━━┛"<<endl;
  240.  
  241.         cout<<endl<<endl;
  242. }
  243.  
  244.  
  245. void main()
  246. {
  247.         int f=1;
  248.         int chioce;
  249.  
  250.         version();
  251.         head=initial();
  252.         total=0;
  253.         jhead=initial_job();
  254.  
  255.         readData();
  256.  
  257.         while ( f==1 )
  258.         {
  259.                 cout<<"===================================================="<<endl;
  260.                 cout<<" 模拟UNIX的成组链接法 "<<endl;
  261.                 cout<<"===================================================="<<endl;
  262.                 cout<<"1.申请空闲块 2.撤消作业 3.查看专用块 0.退出"<<endl;
  263.                 cout<<"请选择:";
  264.                 cin>>chioce;
  265.  
  266.                 switch ( chioce )
  267.                 {
  268.                 case 1:
  269.                         bid();
  270.                         break;
  271.                 case 2:
  272.                         finish();
  273.                         break;
  274.                 case 3:
  275.                         view();
  276.                         break;
  277.                 case 0:
  278.                         f=0;
  279.                         break;
  280.                 default:
  281.                         cout<<"选择错误!"<<endl;
  282.                 }
  283.         }
  284.  
  285. }
  286.  

回复 "c++磁盘存储空间的管理模拟(UNIX存储管理的成组链接法的设计与实现)"

这儿你可以回复上面这条便签

captcha