麻烦高手帮忙!!!

yblue 2006-12-29 01:27:41
一下是我的代码,总是运行到最后的时候提示有问题要关闭?这是一个关于作业调度的程序。请高手指点!!!

#include<iostream>
#include"stdio.h"
#define getjcb(type)(type *)malloc(sizeof(type))
#define NULL 0


using namespace std;

int m=0, n=0;

struct JCB {
char name[10]; //作业名
float arriveTime; //作业到达时间
float startTime; //开始运行时间
float neceTime; //需要的时间
float finishTime; //完成时间
char state; //作业状态(运行(r)或等待(w))
JCB *next;
} *p, *arrStart,*q, *arrStart1;


void Sort()
{
p->next = arrStart->next;
arrStart->next = p;
}

void ReSort()
{
arrStart1 = getjcb(JCB);
arrStart1->next = NULL;
for(q=arrStart->next; q!=NULL; q=q->next)
{
p = getjcb(JCB);
for(int i=0; i<10; ++i)
{
p->name[i] = q->name[i];
}
p->arriveTime = q->arriveTime;
p->state = q->state;
p->neceTime = q->neceTime;
p->next = arrStart1->next;
arrStart1->next = p;
}
}

void Run()
{
cout<<"初始状态:"<<endl;
p = arrStart1->next;
while(p != NULL)
{
cout<<"作业"<<"\t"<<"到达时间"<<"\t"<<"所需时间"<<endl;
cout<<p->name<<"\t"<<p->arriveTime<<"\t\t"<<p->neceTime<<endl;
p = p->next;
}
cout<<endl<<"开始运行:"<<endl<<endl;
p = arrStart1->next;
p->state = 'r';
while(p != NULL)
{
int t = p->neceTime;
while(t >0)
{
t--;
cout<<"作业"<<"\t"<<"到达时间"<<"\t"<<"所需时间"<<"\t"<<"还需时间"<<endl;
cout<<p->name<<"\t"<<p->arriveTime<<"\t\t"<<p->neceTime<<"\t\t"<<t<<endl;
}
cout<<"作业"<<p->name<<"完成"<<endl;
cout<<endl;
q = p;
p = p->next;
p->state = 'r';
arrStart1->next = p;
free(q);
}
}

void main()
{
int k = 1;
arrStart = getjcb(JCB);
arrStart->next = NULL;
cout<<"请输入作业的数目: ";
cin>>n;
cout<<endl;
for(int j=0; j<n; ++j, ++k)
{
cout<<"作业"<<k<<"的名称: ";
p = getjcb(JCB);
cin>>p->name;
cout<<"所需时间: ";
cin>>p->neceTime;
p->arriveTime = k;
p->state = 'w';
Sort();
cout<<endl;
}
ReSort();
Run();
}
...全文
85 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
yblue 2006-12-30
  • 打赏
  • 举报
回复
谢谢大家,我试试看先!
一分之千 2006-12-29
  • 打赏
  • 举报
回复
出错的原因就是因为到了链表的最后了你还在对其操作~
p = p->next;或者在这个地方改一下看你的需要~
一分之千 2006-12-29
  • 打赏
  • 举报
回复
这里该一下就可以了
=============
void Run()
{
.....
while(p->next != NULL)
......
}
0黄瓜0 2006-12-29
  • 打赏
  • 举报
回复
struct JCB
{
//.....
} *p, *arrStart,*q, *arrStart1;
//尽量少用全局变量,调试起来很麻烦,你自已稍微不注意就会出错.

64,647

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧