| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 547 人关注过本帖
标题:请大家帮我看看写的程序,关于时间片的
只看楼主 加入收藏
张起灵
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2012-10-20
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:8 
请大家帮我看看写的程序,关于时间片的

#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
 
struct PCBNode
 {
         string name;
    float runTime;      //总的需要运行时间
    float remainTime;   //剩下需要运行时间
    float arriveTime;   //进入就绪队列时间
    float startTime;    //开始运行时间
    float finishTime;   //结束运行时间
    float totalTime;    //周转时间
    float weightTotalTime;  //带权周转时间
         PCBNode * next;
 };
 
struct LinkQueue  //队列定义
 {
         PCBNode * front; //队头指针
         PCBNode * rear;  //队尾指针
 };
 
//队列初始化
 void InitialQueue(LinkQueue& q);
 //输入PCBNode
 void Input(PCBNode* p);
 //队列构造
 void CreateQueue(LinkQueue& q, int n);


 //时间片轮转算法
 void RoundRobin(LinkQueue& q, int n);
//打印队列
 void PrintQueue(LinkQueue& q);
 
int main()
 {
    int i = 1;
         while (i)
         {
                 
                         LinkQueue queue;
                         InitialQueue(queue);
                        
                        int num = 0;
                         cout << "输入进程个数" << endl;
                         cin >>num;
                         CreateQueue(queue, num);
                         RoundRobin(queue, num);
                         PrintQueue(queue);
               
               
                 cout << endl;
                 cout << "继续请输入1,退出请输入0" <<endl;
                 cin >>i;
         }
         



        return 0;
         
 }
 
//队列初始化
 void InitialQueue(LinkQueue& q)
 {
         q.front = NULL;
         q.rear = NULL;
 }
 //输入PCB进程控制块的信息
 void Input(PCBNode*p)
 {
         //需要用户输入的信息
         cout << "输入进程名称" <<endl;
         cin >> p->name;
         cout << "输入到达时间" << endl;
         cin >> p->arriveTime;
     cout << "输入运行时间" << endl;
         cin >> p->runTime;
         //自动初始化的信息
         p->startTime = 0;  //开始时间
         p->finishTime = 0;  //完成时间
         p->remainTime = p->runTime;  //还需要时间
         p->totalTime = 0;   //周转时间
         p->weightTotalTime = 0;   //带权周转时间
         p->next = NULL;
 
}
 //创建队列
 void CreateQueue(LinkQueue& q, int n)
 {
         for (int i=0; i<n; i++)
         {
                 PCBNode *pcb=new PCBNode;
                 Input(pcb);
                 if (q.front == NULL)
                 {
                         PCBNode * temp = new PCBNode;
                         temp = pcb;
                         q.front = q.rear = temp;
                    
                 }
                 else
                 {
                         //此处必须新建一个PCBNode,否则连不上队列
                         PCBNode * temp = new PCBNode;
                         temp = pcb;
                         q.rear->next = temp;
                         q.rear = temp;
                        
                 }
         }
         q.rear->next = NULL;
 }

 //时间片轮转法,时间片为1
 void RoundRobin(LinkQueue& q, int n)
 {      
         
         LinkQueue p;
         PCBNode*temp1=q.front;
        
         int flag=1;
         int time=q.front->arriveTime;
         p.front=p.rear=temp1;
         PCBNode*temp2=p.front;
         p.rear->next=NULL;
        while(temp2){
         cout<<temp2->name;
           temp2=temp2->next;
        }
        temp1=q.front;
        while(temp1){
             cout<<temp1->name;
             temp1=temp1->next;
        }
         /* while (flag)
         {
                 PCBNode * current = p.front;
                 //如果当前进程还需要时间运行
                 if (current->arriveTime <= time && current->remainTime != 0)
                 {
                         //第一次占有CPU
                         if (current->remainTime == current->runTime)
                         {
                                 //执行1次修改进程相应信息
                                 current->startTime = time;
                                 if (current->remainTime <= 1)
                                 {
                                         time += current->remainTime;
                                         current->remainTime = 0;
                                         current->finishTime = time;
                                 }
                                 else
                                 {
                                         time++;
                                         current->remainTime--;
                                 }
                                 //将执行过的进程插到队尾
                                 PCBNode * temp = p.front;

                           
                                 if(temp->next==NULL){
                                     cout << current->name<<"************"<<endl;
                                     cout<<temp1->name<<"&&&&"<<endl;
                                       p.front=temp1;  
                                       current=p.front;
                                       current->next=temp;
                                       temp->next=NULL;PrintQueue(p);
                                       p.rear=temp;

                                      
                                       temp1=temp1->next;
                                      
                                       
                                 }
                                 
                                 else{
                                     while (temp->next != NULL)
                                     {
                                         temp = temp->next;
                                     }
 
                                cout << current->name;
                                cout<<"****************";
                                p.front = current->next;
                                 temp->next = current;
                                 current->next = NULL;
                                 p.rear = current;
                                 PrintQueue(p);
                                 
                           
                                
                                 temp->next=temp1;
                                 temp1->next=current;
                                 temp1=temp1->next;
                                 }
                           
                                 
                         }
                         //第N次占有CPU
                         else
                         {
                                 if (current->remainTime <= 1)
                                 {
                                         time += current->remainTime;
                                         current->remainTime = 0;
                                         current->finishTime = time;
                                 }
                                 else
                                 {
                                         time++;
                                         current->remainTime--;
                                 }
                                 //将执行过的进程插到队尾
                                 
                                 PCBNode * temp = p.front;
                                 
                                 while (temp->next != NULL)
                                 {
                                         temp = temp->next;
                                 }
 
                                cout << current->name;
                                p.front = current->next;
                                 temp->next = current;
                                 current->next = NULL;
                                p.rear=current;
                                 
                         }
                 }
            
                else
                 {
                         //将执行过的进程插到队尾
                         PCBNode * temp = p.front;
                         while (temp->next != NULL)
                         {
                                 temp = temp->next;
                         }
 
                        p.front = current->next;
                         temp->next = current;
                         current->next = NULL;
                         p.rear = current;
 
                }
 
            PCBNode * m = p.front;
            cout<<"%%"<<m->next->name<<endl;;
                 while (m != NULL)
                 {      
                         if(m->remainTime != 0)
                         {
                                 flag = 1;
                                 break;
                         }
                         else
                         {
                                 flag = 0;
                         }
                         m = m->next;
                 }
         //while循环结束
                 
         }
 
        //周转时间,带权周转时间
        /* q=p;
         PCBNode * current = q.front;
         while (current != NULL)
         {
                 current->totalTime = current->finishTime - current->arriveTime;
                 current->weightTotalTime = (current->finishTime - current->arriveTime) / current->runTime;
                 current = current->next;
         }*/
      
 }
         
         
         
      

         
void PrintQueue(LinkQueue& q)
 {
         PCBNode * current = q.front;
         double total = 0;
         double weightTotal = 0;
         int i = 0;
         cout << endl;
         cout << "运行结果如下:" <<endl;
         while (current != NULL)
         {
                 cout << "进程名称  " << "到达时间  " << "开始时间  " << "运行时间  "
                        <<"结束时间  " << "周转时间  " << "带权周转时间" <<endl;
                 cout << setiosflags(ios::left);
                 cout << setw(13) << current->name << setw(10) << current->arriveTime << setw(10)
                        << current->startTime << setw(10) << current->runTime << setw(10) << current->finishTime
                         << setw(10) << current->totalTime << setw(14) << current->weightTotalTime << endl;
                 total += current->totalTime;
                 weightTotal += current->weightTotalTime;
                 i++;
                 current = current->next;
         }
         cout << "平均周转时间:" << total/i <<endl;
         cout << "平均带权周转时间:" << weightTotal/i <<endl;
 }
 
搜索更多相关主题的帖子: include 
2012-10-21 10:03
张起灵
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2012-10-20
收藏
得分:0 
我预期想得到的效果是

进程名 a b c d e
到达时间 0 1 2 4 5
服务时间 4 3 5 2 4
完成时间 12 10 18 11 17
周转时间 12 9 16 8 13
带权周转 3 3 3.2 4 3.25






2012-10-21 10:10
张起灵
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2012-10-20
收藏
得分:0 
而不是这样的

http://imgsrc.baidu.com/forum/pic/item/4b5e4bfbfbedab649e941131f736afc378311e2a.jpg
2012-10-21 10:12
张起灵
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2012-10-20
收藏
得分:0 
就比如说有5个作业等待完成a,b,c,d,e,按先来先服务的顺序。首先将a,b,c,d,e依次加入到队伍,因此队列变化为
1. a
2、b,a
3、a,c,b
4、c,b,d,a
5、b,d,a,e,c
之后按照5的顺序循环作业,直至完成

2012-10-21 10:14
张起灵
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2012-10-20
收藏
得分:0 

我现在遇到的困难是将一个已经建立好的队列q,怎样才能将它font一个个的出列,复制到另一个队列p的队尾,而不影响队列q出队后的队列
2012-10-21 10:16
张起灵
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2012-10-20
收藏
得分:0 
我现在遇到的困难是将一个已经建立好的队列q,怎样才能将它font一个个的出列,复制到另一个队列p的队尾,而不影响队列q出队后的队列
2012-10-21 10:20
JYIT
Rank: 3Rank: 3
等 级:论坛游侠
威 望:1
帖 子:30
专家分:101
注 册:2012-10-17
收藏
得分:7 
你的数据结构学得相当过硬
2012-10-21 21:11
寒风中的细雨
Rank: 17Rank: 17Rank: 17Rank: 17Rank: 17
等 级:贵宾
威 望:66
帖 子:1710
专家分:8645
注 册:2009-9-15
收藏
得分:7 
Ding~
2012-10-22 12:22
张起灵
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2012-10-20
收藏
得分:0 
我已经自己解决了
收到的鲜花
2012-10-24 08:36
快速回复:请大家帮我看看写的程序,关于时间片的
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.043743 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved