翻译一段vc++代码时迷惑错误!(附正确执行的vc++代码,一道操作系统模拟作业调度上机实验题目,有详细注释) (100分)

  • 主题发起人 主题发起人 net_donkey
  • 开始时间 开始时间
N

net_donkey

Unregistered / Unconfirmed
GUEST, unregistred user!
编译没有问题
也没有警告
运行时堆栈益处(问题第6次运行到 While not IsFinished do 时程序停止,异常退出,
报告是是堆栈益处)
困惑是找不到原因
翻译的delphi代码:
program Project1;

{$APPTYPE CONSOLE}

uses
SysUtils;

type
TPCB = class(Tobject)
public
CourseName: String;
Priority: Integer;
ComeTime: Integer;
ServiceTime: Integer;
State: Integer;
Next: TPCB
//引用
constructor Create(CCourseName: String
CPriority, CComeTime, CServiceTime: Integer);
destructor Destroy
Override;
end;
type
TPCBQueue = class(Tobject)
public
constructor Create;
destructor Destroy
Override;
function Push(course: TPCB): TPCBQueue;
function Front: TPCB;
function IsEmpty: Boolean;
function GetLength: Integer;
function Pop: TPCB;
private
Head: TPCB;
Tail: TPCB;
Length: Integer;
end;

type
TComputer = class(TObject)
private
ArriveQueue: TPCBQueue;
WaitQueue: array [0..2] of TPCBQueue;
Time: Integer;
function IsFinished: Boolean;
procedure Print;
public
procedure AddCourse(Course: TPCB);
procedure Scheduler;
constructor Create;
destructor Destroy
Override;
end;


{构造函数}

{ TPCB }

constructor TPCB.Create(CCourseName: String
CPriority, CComeTime,
CServiceTime: Integer);
begin
CourseName := CCourseName;
Priority := CPriority;
ComeTime := CComeTime;
ServiceTime := CServiceTime;
Next := nil;
State := 0;
end;

destructor TPCB.Destroy;
begin
free;
inherited;
end;

{ TPCBQueue }

constructor TPCBQueue.Create;
begin
head := nil;
tail := nil;
Length := 0;
end;

destructor TPCBQueue.Destroy;
begin
Free;
inherited;
end;

function TPCBQueue.Front: TPCB;
begin
try
if not IsEmpty then
Result := Head
else
begin
raise Exception.Create('Error!')
//上升错误
end;
except
raise Exception.Create(' Ambiguity Error!');
end;
end;

function TPCBQueue.GetLength: Integer;
begin
Result := Length;
end;

function TPCBQueue.IsEmpty: Boolean;
begin
try
Result := Length = 0;
except
Raise Exception.Create('IsEmpty!');
end;
end;

function TPCBQueue.Push(course: TPCB): TPCBQueue;
begin
course.Next := nil;
if Length = 0 then // if not Boolean(GetLength) then
Head := course
else
Tail.Next := course;
Tail := course;
inc(Length);
Result := Self;
// 可隐含地返回这句 Result :=Self
可以实现Push().Push().Push...效果
end;

function TPCBQueue.Pop: TPCB;
var
Temp: TPCB;
begin
Result := nil;
if not IsEmpty then
begin
Temp := Head;
Head := Head.Next;
Dec(Length);
Result := Temp;
end
else
Exception.Create('Error!');
end;

{ TComputer }

procedure TComputer.AddCourse(Course: TPCB);
begin
ArriveQueue.Push(Course);
end;

constructor TComputer.Create;
begin
Time := 0;
ArriveQueue := TPCBQueue.Create;
WaitQueue[0] := TPCBQueue.Create;
WaitQueue[1] := TPCBQueue.Create;
WaitQueue[2] := TPCBQueue.Create;
end;

destructor TComputer.Destroy;
begin
Free;
inherited;
end;

function TComputer.IsFinished: Boolean;
begin
try
Result := ArriveQueue.IsEmpty and WaitQueue[0].IsEmpty and WaitQueue[1].IsEmpty
and WaitQueue[2].IsEmpty;
except
Raise Exception.Create('IsFinished Error!');
end;
end;

procedure TComputer.Print;
var
i, j: Integer;
Temp: TPCB;
begin
Writeln('At time ' + inttostr(time));
for i := 0 to 2 do
begin
if WaitQueue.IsEmpty then
Writeln('WaitQueue[' + inttostr(i) + '] is Empty.')
else
begin
Temp := WaitQueue.Front;
Write('WaiteQueue[' + inttostr(i) + '] is: ');
for j := 0 to WaitQueue.GetLength - 1 do
begin
Write(Temp.CourseName+ ' ');
Temp := Temp.Next;
end;
Writeln;
end;
end;
end;

procedure TComputer.Scheduler;
var
Course: TPCB
//当成一个指针用
CurPriority, CurSlice: Integer;
begin
CurPriority := 2;
While not IsFinished do
begin
{如果到达队列不为空,则把这个队列中的最前面一个取出(指针方式)}
if not ArriveQueue.IsEmpty then
Course := ArriveQueue.Front
else
Course := nil;
{下面这个while循环实现把所有等待队列中的任务加载到相应优先级队列中}
while (not ArriveQueue.IsEmpty) and (Course.ComeTime <= time) do
begin
// if Course.ComeTime <= Time then
begin
Course := ArriveQueue.Pop
//取出
WaitQueue[Course.Priority].Push(Course)
//装载到相应的优先级队列中
Writeln('At time ' + inttostr(Course.ComeTime) + ' Course '
+ Course.CourseName +' Comes.' )
//显示在何时什么任务到来
if Course.Priority < CurPriority then
CurPriority := Course.Priority
//如果是优先级最高设置为当前优先级
end;
if not ArriveQueue.IsEmpty then
Course := ArriveQueue.Front
//把等待队列中最靠前的取出(指针方式)
end
{While end}
Print
//输出优先级队列的当前状态
if not WaitQueue[CurPriority].IsEmpty then //这个队列是优先级最高的队列
begin
Course := WaitQueue[CurPriority].pop
//取出一个执行
if Course.ServiceTime < (1 shl CurPriority) then
{如果需要服务的时间小于当前优先级的时间片 1 shl 0 = 1
1 shl 1 =2
1 shl 2 =4 }
CurSlice := Course.ServiceTime
else
CurSlice := 1 shl CurPriority
//在当前优先级的时间片下还执行不完情况下
Course.ServiceTime := Course.ServiceTime - CurSlice
//剩余服务时间
time := time + CurSlice
//使用时间
if Course.ServiceTime > 0 then //如果还没有执行完毕
begin
{如果优先级是0、1 则优先级减低1,并且放到该优先级队列中}
if CurPriority <= 1 then
WaitQueue[CurPriority + 1].Push(Course)
else
WaitQueue[2].Push(Course)
//放到该队列
end
else //如果已经执行完毕
begin
Writeln('At Time: ' + inttostr(Time) + ' Course ' + Course.CourseName +' leave.');
Course.free
//销毁
end;
if (WaitQueue[CurPriority].IsEmpty) and (CurPriority <= 1) then
{如果当前优先级的队列已经空了就让它去下一个优先级队列去提取执行对象}
Inc(CurPriority);
end
else
inc(Time)
//如果所有的WaitQueue都为空了,而且还有任务没有到来
end;
end;

var
AComputer: TComputer;
begin
AComputer := TComputer.Create;
AComputer . AddCourse (TPCB.Create('A' , 1 , 0 , 6 )) ;
AComputer . AddCourse (TPCB.Create('B' , 1 , 1 , 4 )) ;
AComputer . AddCourse (TPCB.Create('C' , 2 , 2 , 10 )) ;
AComputer . AddCourse (TPCB.Create('D' , 0 , 3 , 6 )) ;
AComputer . AddCourse (TPCB.Create('E' , 0 , 6 , 1 )) ;
AComputer . AddCourse (TPCB.Create('F' , 1 , 8 , 2 )) ;
AComputer . AddCourse (TPCB.Create('G' , 1 , 12 , 5 )) ;
AComputer . AddCourse (TPCB.Create('H' , 2 , 12 , 10 )) ;
AComputer . AddCourse (TPCB.Create('J' , 1 , 12 , 4 )) ;
AComputer . AddCourse (TPCB.Create('K' , 0 , 18 , 3 )) ;
AComputer . AddCourse (TPCB.Create('L' , 2 , 25 , 15 )) ;
AComputer . AddCourse (TPCB.Create('M' , 1 , 25 , 8 )) ;
AComputer . Scheduler ;
AComputer.Destroy;
end.

正确执行的vc++代码:
#include <iostream.h>
#include <iomanip.h>
#include <string.h>
#include <conio.h>

class Empty {
public :
Empty () {};
};

class PCB {
public :
char CourseName ;
int Priority ;
int ComeTime ;
int ServiceTime ;
int State ;
PCB * next;
PCB (char , int , int , int ) ;
};

PCB :: PCB (char name , int priority , int ctime , int stime )
{
CourseName = name ;
Priority = priority ;
ComeTime = ctime ;
ServiceTime = stime ;
State = 0 ;
next = 0 ;
}

class PCBQueue {
public :
PCBQueue ();
~PCBQueue ();
PCBQueue &amp
push (PCB * const &amp
course);
PCB * pop ();
PCB * &amp
Front ()
//指针的引用 (相当与双重指针)
bool IsEmpty ();
int Length ();
private :
PCB * head;
PCB * tail;
int length;
};

inline PCBQueue :: PCBQueue ()
{
head = tail = 0;
length = 0;
}

PCBQueue :: ~PCBQueue ()
{
while (!IsEmpty ())
delete pop ();
head = tail = 0;
length = 0;
}

PCBQueue &amp
PCBQueue :: push (PCB * const &amp
course)
{
course -> next = 0 ;
if (!length)
head = course ;
else
tail -> next = course ;
tail = course ;
length ++;
return * this;
}

PCB * PCBQueue :: pop ()
{
PCB * temp ;
if (!IsEmpty ())
{
temp = head;
head = head -> next;
length --;
return temp;
}
else
throw Empty ();
}

inline PCB * &amp
PCBQueue :: Front ()
{
if (!IsEmpty ())
return head ;
else
throw Empty () ;
}

inline bool PCBQueue :: IsEmpty ()
{
return length == 0;
}

inline int PCBQueue :: Length ()
{
return length;
}

ostream &amp
operator << (ostream &amp
out ,PCBQueue &amp
TPCBQueue)
{
if (!TPCBQueue . IsEmpty ())
{
PCB * temp = TPCBQueue . Front () ;
for (int i = 0
i <= TPCBQueue . Length () - 1
i ++)
{
out << setw (2) << temp -> CourseName ;
temp = temp -> next;
}
out << endl;
}
return out;
}

class Computer {
private :
PCBQueue ArriveQueue ;
PCBQueue WaitQueue [3] ;
int time ;
bool IsFinished (void) ;
void Print (void) ;
public :
Computer () ;
void AddCourse (PCB * const &amp;) ;
void Scheduler (void) ;
};

Computer :: Computer () : time (0) {}

void Computer :: AddCourse (PCB * const &amp
course)
{
ArriveQueue . push (course) ;
}

bool Computer :: IsFinished (void)
{
return ( ArriveQueue . IsEmpty () &amp;&amp
WaitQueue [0] . IsEmpty ()
&amp;&amp
WaitQueue [1] . IsEmpty () &amp;&amp
WaitQueue [2] . IsEmpty ()) ;
}

void Computer :: Print (void)
{
cout << "At time : " << time << endl ;
for ( int i = 0
i <= 2
i ++ )
{
if (WaitQueue . IsEmpty ())
cout << "WaitQueue [" << i << "] is Empty ." << endl ;
else
{
cout << "WaitQueue [" << i << "] is : " ;
cout << WaitQueue
//重载<<
}
}
}

void Computer :: Scheduler (void)
{
PCB * Course ;
time = 0 ;
int CurPriority = 2

int CurSlice = 4

while (!IsFinished ())
{
if (!ArriveQueue . IsEmpty ())
Course = ArriveQueue . Front () ;
while (!ArriveQueue . IsEmpty () &amp;&amp
Course -> ComeTime <= time)
{
// if (Course -> ComeTime <= time)
{
Course = ArriveQueue . pop () ;
WaitQueue [Course -> Priority] . push (Course) ;
cout << "At time : " << Course -> ComeTime << " Course "
<< Course -> CourseName << " come ." << endl ;
if (Course -> Priority < CurPriority)
CurPriority = Course -> Priority ;
}
if (!ArriveQueue . IsEmpty ())
Course = ArriveQueue . Front () ;
}

Print () ;
if (!WaitQueue [CurPriority] . IsEmpty ())
{
Course = WaitQueue [CurPriority] . pop () ;
CurSlice = (Course -> ServiceTime < (1 << CurPriority)) ?
Course -> ServiceTime : (1 << CurPriority) ;
Course -> ServiceTime -= CurSlice ;
time += CurSlice ;
if (Course -> ServiceTime > 0)
{
if (CurPriority <= 1)
WaitQueue [CurPriority + 1] . push (Course) ;
else
WaitQueue [2] . push (Course) ;
}
else
{
cout << "At time : " << time << " Course " << Course -> CourseName << " leave ." << endl ;
delete Course ;
}
if (WaitQueue [CurPriority] . IsEmpty() &amp;&amp
CurPriority <= 1)
CurPriority ++ ;
}
else
time ++ ;
}
}

main ()
{
Computer AComputer ;
AComputer . AddCourse (new PCB ('A' , 1 , 0 , 6 )) ;
AComputer . AddCourse (new PCB ('B' , 1 , 1 , 4 )) ;
AComputer . AddCourse (new PCB ('C' , 2 , 2 , 10 )) ;
AComputer . AddCourse (new PCB ('D' , 0 , 3 , 6 )) ;
AComputer . AddCourse (new PCB ('E' , 0 , 6 , 1 )) ;
AComputer . AddCourse (new PCB ('F' , 1 , 8 , 2 )) ;
AComputer . AddCourse (new PCB ('G' , 1 , 12 , 5 )) ;
AComputer . AddCourse (new PCB ('H' , 2 , 12 , 10 )) ;
AComputer . AddCourse (new PCB ('J' , 1 , 12 , 4 )) ;
AComputer . AddCourse (new PCB ('K' , 0 , 18 , 3 )) ;
AComputer . AddCourse (new PCB ('L' , 2 , 25 , 15 )) ;
AComputer . AddCourse (new PCB ('M' , 1 , 25 , 8 )) ;
AComputer . Scheduler () ;

getch ();
return 0;
}

delphi翻译代码执行结果
At time 0 Course A Comes.
At time 0
WaitQueue[0] is Empty.
WaiteQueue[1] is: A
WaitQueue[2] is Empty.
At time 1 Course B Comes.
At time 2 Course C Comes.
At time 2
WaitQueue[0] is Empty.
WaiteQueue[1] is: B
WaiteQueue[2] is: A C
At time 3 Course D Comes.
At time 4
WaiteQueue[0] is: D
WaitQueue[1] is Empty.
WaiteQueue[2] is: A C B
At time 5
WaitQueue[0] is Empty.
WaiteQueue[1] is: D
WaiteQueue[2] is: A C B
At time 6 Course E Comes.
At time 7
WaiteQueue[0] is: E
WaitQueue[1] is Empty.
WaiteQueue[2] is: A C B D
At Time: 8 Course E leave.
Exception EStackOverflow in module ee.exe at 0000822E.
Stack overflow.

VC++正确结果
At time : 0 Course A come .
At time : 0
WaitQueue [0] is Empty .
WaitQueue [1] is : A
WaitQueue [2] is Empty .
At time : 1 Course B come .
At time : 2 Course C come .
At time : 2
WaitQueue [0] is Empty .
WaitQueue [1] is : B
WaitQueue [2] is : A C
At time : 3 Course D come .
At time : 4
WaitQueue [0] is : D
WaitQueue [1] is Empty .
WaitQueue [2] is : A C B
At time : 5
WaitQueue [0] is Empty .
WaitQueue [1] is : D
WaitQueue [2] is : A C B
At time : 6 Course E come .
At time : 7
WaitQueue [0] is : E
WaitQueue [1] is Empty .
WaitQueue [2] is : A C B D
At time : 8 Course E leave .
At time : 8 Course F come .
At time : 8
WaitQueue [0] is Empty .
WaitQueue [1] is : F
WaitQueue [2] is : A C B D
At time : 10 Course F leave .
At time : 10
WaitQueue [0] is Empty .
WaitQueue [1] is Empty .
WaitQueue [2] is : A C B D
At time : 14 Course A leave .
At time : 12 Course G come .
At time : 12 Course H come .
At time : 12 Course J come .
At time : 14
WaitQueue [0] is Empty .
WaitQueue [1] is : G J
WaitQueue [2] is : C B D H
At time : 16
WaitQueue [0] is Empty .
WaitQueue [1] is : J
WaitQueue [2] is : C B D H G
At time : 18 Course K come .
At time : 18
WaitQueue [0] is : K
WaitQueue [1] is Empty .
WaitQueue [2] is : C B D H G J
At time : 19
WaitQueue [0] is Empty .
WaitQueue [1] is : K
WaitQueue [2] is : C B D H G J
At time : 21 Course K leave .
At time : 21
WaitQueue [0] is Empty .
WaitQueue [1] is Empty .
WaitQueue [2] is : C B D H G J
At time : 25 Course L come .
At time : 25 Course M come .
At time : 25
WaitQueue [0] is Empty .
WaitQueue [1] is : M
WaitQueue [2] is : B D H G J C L
At time : 27
WaitQueue [0] is Empty .
WaitQueue [1] is Empty .
WaitQueue [2] is : B D H G J C L M
At time : 29 Course B leave .
At time : 29
WaitQueue [0] is Empty .
WaitQueue [1] is Empty .
WaitQueue [2] is : D H G J C L M
At time : 32 Course D leave .
At time : 32
WaitQueue [0] is Empty .
WaitQueue [1] is Empty .
WaitQueue [2] is : H G J C L M
At time : 36
WaitQueue [0] is Empty .
WaitQueue [1] is Empty .
WaitQueue [2] is : G J C L M H
At time : 39 Course G leave .
At time : 39
WaitQueue [0] is Empty .
WaitQueue [1] is Empty .
WaitQueue [2] is : J C L M H
At time : 41 Course J leave .
At time : 41
WaitQueue [0] is Empty .
WaitQueue [1] is Empty .
WaitQueue [2] is : C L M H
At time : 45
WaitQueue [0] is Empty .
WaitQueue [1] is Empty .
WaitQueue [2] is : L M H C
At time : 49
WaitQueue [0] is Empty .
WaitQueue [1] is Empty .
WaitQueue [2] is : M H C L
At time : 53
WaitQueue [0] is Empty .
WaitQueue [1] is Empty .
WaitQueue [2] is : H C L M
At time : 57
WaitQueue [0] is Empty .
WaitQueue [1] is Empty .
WaitQueue [2] is : C L M H
At time : 59 Course C leave .
At time : 59
WaitQueue [0] is Empty .
WaitQueue [1] is Empty .
WaitQueue [2] is : L M H
At time : 63
WaitQueue [0] is Empty .
WaitQueue [1] is Empty .
WaitQueue [2] is : M H L
At time : 65 Course M leave .
At time : 65
WaitQueue [0] is Empty .
WaitQueue [1] is Empty .
WaitQueue [2] is : H L
At time : 67 Course H leave .
At time : 67
WaitQueue [0] is Empty .
WaitQueue [1] is Empty .
WaitQueue [2] is : L
At time : 71
WaitQueue [0] is Empty .
WaitQueue [1] is Empty .
WaitQueue [2] is : L
At time : 74 Course L leave .

 
难到没有认对这个问题感兴趣?
逼不得以才问的哦
 
问题太长了,我想有耐心看完的人不太多,我看了半天也知你想做什么。
如果你能把你要问的问题提炼出来,
我想效果就好多了。
 
哪里有错误,列出来吧
(运行错误还是编译错误?)
 
翻译的时候那里迷惑
 
是这样的
编译没有问题
也没有警告
运行时堆栈益处(问题第6次运行到 While not IsFinished do 时程序停止,异常退出,
报告是是堆栈益处)
困惑是找不到原因

看看运行结果就知道了是什么样的困惑了:)
 
多人接受答案了。
 
后退
顶部