| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 852 人关注过本帖
标题:大家帮忙看下 输出有问题
只看楼主 加入收藏
世界模型
Rank: 4
等 级:业余侠客
威 望:1
帖 子:240
专家分:226
注 册:2010-9-12
结帖率:97.44%
收藏
已结贴  问题点数:20 回复次数:4 
大家帮忙看下 输出有问题
程序代码:
/*
*单链表插入排序
*/
#include <stdio.h>
#include <stdlib.h>


 struct PCB
{
    char name[10];
    int  rtime;
    int  priority;
    char state;
    struct PCB *next;
};

/*
*初始化PCB
*/
struct PCB *initPCB()
{
  struct PCB *head;

 
  head=(struct PCB *)malloc(sizeof(struct PCB));
  if(NULL==head)
  {
      exit(-1);
  }
  head->next=NULL;

  return head;
}

struct PCB *inputValue(struct PCB *head)
{
  struct PCB *temp;

  temp=head;
  //for(int i=0;i<5;i++)
  //{
    printf("please input the information of process\n");
    printf("name:");
    scanf("%s",temp->name);
    printf("run time:");
    scanf("%d",&temp->rtime);
    printf("priority:");
    scanf("%d",&temp->priority);
    temp->state='R';
    printf("\n");

 // }
  return temp;
}

/*
*找到优先级最高的进程
*/
void  MaxPriorityProcess(struct PCB * head,struct PCB * pCurrent)
{
  struct PCB *temp;
  struct PCB *p;//p为新节点分配内存,temp用来遍历链表

  temp=head->next;
  p=(struct PCB*)malloc(sizeof(struct PCB));
  p->priority=pCurrent->priority;

  while(temp)
  {
      if(pCurrent->priority>temp->priority) //如果大于某个已经存在的节点
      {                                         //则把新节点插入到该节点前面
          p->next=temp->next;
          temp->next=p;
      }
      temp=temp->next;
  }

  p->next=NULL;//如果小于所有已经存在的节点,则执行下面的代码
  head->next=p;//把新节点插入到链表的最后


}

/*
*打印PCB
*/
void printPCB(struct PCB * head)
{
    struct PCB *temp;

    temp=head->next;
    while(temp!=NULL)
    {
        printf("\n process name: %s\n run time: %d\n priority num: %d\n process state:%c\n",
            temp->name,temp->rtime,temp->priority,temp->state);
        temp=temp->next;
    }
    printf("\n");
}

int main()
{
    struct PCB *head=NULL;
    struct PCB *temp=NULL;

    //head=initPCB();
     /*head=(struct PCB *)malloc(sizeof(struct PCB));
     if(NULL==head)
     {
         exit(-1);
     }
     head->next=NULL;
    for(int i=0;i<5;i++)         
    {
      temp=initPCB();  
      //temp->next = NULL;
      MaxPriorityProcess(head,temp);          
    }*/
    head=initPCB();
    for(int i=0;i<5;i++)
    {
      temp=inputValue(head);
      MaxPriorityProcess(head,temp);    
    }
    printPCB(head);             
   
    return 0;
}



2011-11-24 11:46
肆虐Raging
Rank: 2
等 级:论坛游民
帖 子:4
专家分:10
注 册:2011-11-22
收藏
得分:0 
看不懂 嘿嘿
2011-11-25 13:59
无名可用
Rank: 4
等 级:业余侠客
帖 子:79
专家分:259
注 册:2010-7-27
收藏
得分:20 
程序代码:
// Note:Your choice is C++ IDE
/*
*单链表插入排序
*/
#include <stdio.h>
#include <stdlib.h>

typedef struct PCB
{
    char name[10];
    int  rtime;
    int  priority;
    char state;
    struct PCB *next;
}PCB;

/*
*初始化PCB
*/
PCB *initPCB()
{
  PCB *head;

  head=(PCB *)malloc(sizeof(PCB));
  if(NULL==head)
  {
      exit(-1);
  }
  head->next=NULL;

  return head;
}

/*
*  新建一个节点,并返回
*/
PCB *inputValue()
{
    PCB *temp = NULL;
    
    temp = initPCB();

    printf("please input the information of process\n");
    printf("name:");
    scanf("%s",temp->name);
    printf("run time:");
    scanf("%d",&temp->rtime);
    printf("priority:");
    scanf("%d",&temp->priority);
    temp->state='R';
    printf("\n");

      return temp;
}

/*
*找到优先级最高的进程
*/
void MaxPriorityProcess( PCB * head, PCB * pCurrent)
{
    
    if( head->next == NULL )
    {
        head->next = pCurrent;
        return;
    }
    
    /* 从小到大排 */
    PCB* p = head->next;
    PCB* q = head;        
    while( p != NULL )
    {
        if( (pCurrent->priority) < (p->priority) )
        {
            q->next = pCurrent;
            pCurrent->next = p;
            break;    
        }
        q = p;
        p = p->next;
    }
    q->next = pCurrent;
    
    
  /*PCB *temp;
  PCB *p;//p为新节点分配内存,temp用来遍历链表

  temp=head->next;
  p=(PCB*)malloc(sizeof(PCB));
  p->priority=pCurrent->priority;

  while(temp)
  {
      if(pCurrent->priority>temp->priority) //如果大于某个已经存在的节点
      {                                         //则把新节点插入到该节点前面
          p->next=temp->next;
          temp->next=p;
      }
      temp=temp->next;
  }

  p->next=NULL;//如果小于所有已经存在的节点,则执行下面的代码
  head->next=p;//把新节点插入到链表的最后*/


}

/*
*打印PCB
*/
void printPCB( PCB * head )
{
    PCB *temp;

    temp=head->next;
    while(temp!=NULL)
    {
        printf("\n process name: %s\n run time: %d\n priority num: %d\n process state:%c\n",
            temp->name,temp->rtime,temp->priority,temp->state);
        temp=temp->next;
    }
    printf("\n");
}

int main()
{
    PCB *head=NULL;
    PCB *temp=NULL;

    head=initPCB(); // 头结点为空
    for(int i=0;i<3;i++)
    {
      temp=inputValue();
      MaxPriorityProcess(head,temp);
      printf("=================\n");    
    }
    printPCB(head);             
   
    return 0;
}

稍微改了下,没有明白你MaxPriorityProcess()函数的作用。。感觉有点乱
2011-11-26 14:29
世界模型
Rank: 4
等 级:业余侠客
威 望:1
帖 子:240
专家分:226
注 册:2010-9-12
收藏
得分:0 
程序代码:
/*
*模拟简单的进程调度
*/
#include <stdio.h>
#include <stdlib.h>

typedef struct PCB
{
    char name[10];
    int  rtime;
    int  priorityNum;
    char state;
    struct PCB *next;
}PCB;

/*
*初始化PCB
*/
PCB *initPCB()
{
  PCB *head;

  head=(PCB *)malloc(sizeof(PCB));
  if(NULL==head)
  {
      exit(-1);
  }
  head->next=NULL;

  return head;
}

/*
*  新建一个节点,并返回
*/
PCB *inputValue()
{
    PCB *temp = NULL;
   
    temp = initPCB();

    printf("please input the information of process\n");
    printf("name:");
    scanf("%s",temp->name);
    printf("run time:");
    scanf("%d",&temp->rtime);
    printf("priorityNum:");
    scanf("%d",&temp->priorityNum);
    temp->state = 'R';
    printf("\n");

    return temp;
}

/*
*找到优先级最高的进程
*/
void findProcess( PCB * head, PCB * pCurrent )
{
   
    if( head->next == NULL )
    {
        head->next = pCurrent;
        return ;
    }
   
    /* 从大到小排 */
    PCB* p = head->next;
    PCB* q = head;       
    while( p != NULL )
    {
        if( (pCurrent->priorityNum) > (p->priorityNum) )
        {
            q->next = pCurrent;
            pCurrent->next = p;
            break;   
        }
        q = p;
        p = p->next;
    }
    q->next = pCurrent;
}

/*
*打印PCB
*/
void printPCB( PCB * head )
{
    PCB *temp;

    temp = head->next;
    while( temp!=NULL )
    {
        printf("\n process name: %s\n run time: %d\n priority num: %d\n process state:%c\n",
            temp->name,temp->rtime,temp->priorityNum,temp->state);
        temp = temp->next;
    }
    printf("\n\n");
}

/*
*运行进程
*/
void runProcess( PCB * head,PCB * selected )//选中一进程 优先数减一 运行时间减一 如果运行的时间为0 就将装太改为1 如果不为0 加入队列 重新比较 获得优先数最大进程 依次下去直到
{                                            //所有的进程状态都为E  求实现
    PCB *p,*q;

    while( selected->next != NULL )
    {
         selected->priorityNum--;
         selected->rtime--;

         if( selected->rtime == 0 )
        {
            selected->state = 'E';
            break;
        }
           
        /*将运行时间不为0的进程加入队列*/
         p = head->next;
        q = head;
        if( q == NULL )//空队列
        {
            q = selected; 
        }
          if( (selected->priorityNum) > (p->priorityNum) )
        {
             q->next = selected;
             selected->next = p;
        }
    }

}

int main()
{
    PCB *head = NULL;
    PCB *temp = NULL;

    head = initPCB(); // 头结点为空

    for(int i=0;i<3;i++)
    {
      temp = inputValue();
      findProcess(head,temp);
      printf("=================\n");       
    }
  
    runProcess(head,temp);
    printPCB(head);   
   
    return 0;
}


2011-11-26 23:25
世界模型
Rank: 4
等 级:业余侠客
威 望:1
帖 子:240
专家分:226
注 册:2010-9-12
收藏
得分:0 
回复 3楼 无名可用
在帮咱看看啊 感激不尽
2011-11-26 23:26
快速回复:大家帮忙看下 输出有问题
数据加载中...
 
   



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

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