[C++] c++ 磁盘调度算法模拟 →→→→→进入此内容的聊天室

来自 , 2020-09-24, 写在 C++, 查看 100 次.
URL http://www.code666.cn/view/0609154f
  1. for(i = 0; i < MAXQUEUE; i++) {
  2.     queue[i].go = -1;
  3.     queue[i].visited = 0;
  4. }
  5.  
  6. start = 53; //磁头的初始位置
  7. }
  8.  
  9. //读入磁道号流
  10. void readData() {
  11.     FILE *fp;
  12.     char fname[20];
  13.     int temp, i;
  14.  
  15.     cout << "请输入磁道号流文件名:";
  16.     strcpy(fname, "7hard.txt");
  17.     cin >> fname;
  18.  
  19.     if((fp = fopen(fname, "r")) == NULL) {
  20.         cout << "错误,文件打不开,请检查文件名:)" << endl;
  21.     } else {
  22.         while(!feof(fp)) {
  23.             fscanf(fp, "%d ", &temp);
  24.             queue[quantity].go = temp;
  25.             quantity++;
  26.         }
  27.         cout << endl << "---------------------------------------------" << endl;
  28.         cout << "所读入的磁道号流:";
  29.         for(i = 0; i < quantity; i++) {
  30.             cout << queue[i].go << " ";
  31.         }
  32.         cout << endl << "请求数为:" << quantity << endl;
  33.     }
  34. }
  35.  
  36. //FIFO算法
  37. void FIFO() {
  38.     int i;
  39.     int total = 0;
  40.     int current;
  41.  
  42.     cout << endl << "---------------------------------------------" << endl;
  43.     cout << "FIFO算法的访问磁道号顺序流:";
  44.  
  45.     current = start;
  46.     for(i = 0; i < quantity; i++) {
  47.         cout << queue[i].go << " ";
  48.         total += abs(queue[i].go - current);
  49.         current = queue[i].go;
  50.     }
  51.     cout << endl << "磁头移过的柱面数:" << total;
  52. }
  53.  
  54. //最短寻道优先调度算法
  55. void shortest() {
  56.     int i, j, p;
  57.     int total = 0;
  58.     int current;
  59.  
  60.     cout << endl << "---------------------------------------------" << endl;
  61.     cout << "最短寻道优先调度算法的访问磁道号顺序流:";
  62.  
  63.     current = start;
  64.     for(i = 0; i < quantity; i++) {
  65.         p = 0;
  66.         while(queue[p].visited != 0) {
  67.             p++;
  68.         }
  69.         for(j = p; j < quantity; j++) {
  70.             if((queue[j].visited == 0) && (abs(current - queue[p].go) > abs(current - queue[j].go))) {
  71.                 p = j;
  72.             }
  73.         }
  74.         cout << queue[p].go << " ";
  75.         total += abs(queue[p].go - current);
  76.         queue[p].visited = 1;
  77.         current = queue[p].go;
  78.     }
  79.     cout << endl << "磁头移过的柱面数:" << total;
  80. }
  81.  
  82. //电梯算法
  83. void elevator() {
  84.     int i, j, p, flag;
  85.     int total = 0;
  86.     int current;
  87.  
  88.     cout << endl << "---------------------------------------------" << endl;
  89.     cout << "电梯调度算法" << endl;
  90.  
  91.     //磁头初始向里
  92.     cout << "磁头初始向里的访问磁道号顺序流:";
  93.  
  94.     current = start;
  95.     for(i = 0; i < quantity; i++) {
  96.         flag = 1000;
  97.         p = -1;
  98.         for(j = 0; j < quantity; j++) {
  99.             if((queue[j].visited == 0) && (queue[j].go >= current)) {
  100.                 if(abs(queue[j].go - current) < flag) {
  101.                     p = j;
  102.                     flag = abs(queue[j].go - current);
  103.                 }
  104.             }
  105.         }
  106.         if(p != -1) {
  107.             cout << queue[p].go << " ";
  108.             total += abs(queue[p].go - current);
  109.             current = queue[p].go;
  110.             queue[p].visited = 1;
  111.         } else {
  112.             for(j = 0; j < quantity; j++) {
  113.                 if((queue[j].visited == 0) && (queue[j].go < current)) {
  114.                     if(abs(queue[j].go - current) < flag) {
  115.                         p = j;
  116.                         flag = abs(queue[j].go - current);
  117.                     }
  118.                 }
  119.             }
  120.             cout << queue[p].go << " ";
  121.             total += abs(queue[p].go - current);
  122.             current = queue[p].go;
  123.             queue[p].visited = 1;
  124.         }
  125.     }
  126.     cout << endl << "磁头移过的柱面数:" << total << endl;
  127.  
  128.     //磁头初始向外
  129.     for(i = 0; i < quantity; i++) {
  130.         queue[i].visited = 0;
  131.     }
  132.     total = 0;
  133.  
  134.     cout << "磁头初始向外的访问磁道号顺序流:";
  135.  
  136.     current = start;
  137.     for(i = 0; i < quantity; i++) {
  138.         flag = 1000;
  139.         p = -1;
  140.         for(j = 0; j < quantity; j++) {
  141.             if((queue[j].visited == 0) && (queue[j].go <= current)) {
  142.                 if(abs(queue[j].go - current) < flag) {
  143.                     p = j;
  144.                     flag = abs(queue[j].go - current);
  145.                 }
  146.             }
  147.         }
  148.         if(p != -1) {
  149.             cout << queue[p].go << " ";
  150.             total += abs(queue[p].go - current);
  151.             current = queue[p].go;
  152.             queue[p].visited = 1;
  153.         } else {
  154.             for(j = 0; j < quantity; j++) {
  155.                 if((queue[j].visited == 0) && (queue[j].go > current)) {
  156.                     if(abs(queue[j].go - current) < flag) {
  157.                         p = j;
  158.                         flag = abs(queue[j].go - current);
  159.                     }
  160.                 }
  161.             }
  162.             cout << queue[p].go << " ";
  163.             total += abs(queue[p].go - current);
  164.             current = queue[p].go;
  165.             queue[p].visited = 1;
  166.         }
  167.     }
  168.     cout << endl << "磁头移过的柱面数:" << total;
  169.  
  170. }
  171.  
  172. //显示版权信息函数
  173. void version() {
  174.     cout << endl << endl;
  175.  
  176.     cout << " ┏━━━━━━━━━━━━━━━━━━━━━━━┓" << endl;
  177.     cout << " ┃     磁盘调度算法系统        ┃" << endl;
  178.     cout << " ┠───────────────────────┨" << endl;
  179.     cout << " ┃   (c)All Right Reserved Neo       ┃" << endl;
  180.     cout << " ┃      sony006@163.com          ┃" << endl;
  181.     cout << " ┃     version 2004 build 1122      ┃" << endl;
  182.     cout << " ┗━━━━━━━━━━━━━━━━━━━━━━━┛" << endl;
  183.  
  184.     cout << endl << endl;
  185. }
  186.  
  187.  
  188. void main() {
  189.     int i;
  190.  
  191.     version();
  192.     initial();
  193.  
  194.     readData();
  195.  
  196.     FIFO();
  197.  
  198.     shortest();
  199.  
  200.     for(i = 0; i < quantity; i++) {
  201.         queue[i].visited = 0;
  202.     }
  203.  
  204.     elevator();
  205. }
  206.  

回复 "c++ 磁盘调度算法模拟"

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

captcha