向高手请教程序---使用线程实现龟兔赛跑,但跑到终点程序仍不结束.求救!

ouyangdongfang 2005-12-19 01:01:18
编写一个程序生成两个线程(进程)模拟龟兔赛跑。兔子进程优先级高,因而跑的快,但兔子进程随机发生一些睡眠,在睡眠期间不能跑。乌龟进程优先级低,因而跑的慢,但乌龟进程不发生睡眠。程序应能用动画方式显示龟兔赛跑的进程。
源程序如下:

#include "windows.h"
#include "process.h"
#include "iostream.h"
#include "math.h"
#include "stdlib.h"
#include "conio.h"

int hare=0,tortoise=0;
char position[70];

void hareA()
{

while(1)
{//cout<<"in hareB"<<endl;
while((hare<70)&&(tortoise<70))
{

for(int i=0;i<70;i++)
position[i]='_';
position[hare]='H'; position[tortoise]='T';
hare+=3;
if ((hare==tortoise)&&(hare<70))
{
position[hare]='O';
position[hare+1]='U';
position[hare+2]='C';
position[hare+3]='H';
position[hare+4]='!';
position[hare+5]='!';
position[hare+6]='!';
}
if(hare>=70&&tortoise<70)
cout<<"\n\n\nHare wins!\n";
else if(tortoise>=70&&hare<70)
cout<<"\n\n\nTORTOLSE WINS!\n";
else if(tortoise==hare&&hare>=70)
cout<<"\n\n\nIt's a tie!!\n";
else for(i=0;i<70;i++)
cout<<position[i];
cout<<endl;
Sleep(850);
}
getch();
};
};

void tortoiseA()
{
while(1)
{//cout<<"in tortoiseB"<<endl;
while((hare<70)&&(tortoise<70))
{
for(int i=0;i<70;i++)
position[i]='_';
position[hare+1]='H'; position[tortoise+1]='T';
tortoise++;
if ((hare==tortoise)&&(hare<70))
{
position[hare+1]='O';
position[hare+2]='U';
position[hare+3]='C';
position[hare+4]='H';
position[hare+5]='!';
position[hare+6]='!';
position[hare+7]='!';
}
if(hare>=70&&tortoise<70)
cout<<"\n\n\nHare wins!\n";
else if(tortoise>=70&&hare<70)
cout<<"\n\n\nTORTOLSE WINS!\n";
else if(tortoise==hare&&hare>=70)
cout<<"\n\n\nIt's a tie!!\n";
else for(i=1;i<=70;i++)
cout<<position[i];
cout<<endl;
Sleep(80);
}
getch();
};
};

void main(void)
{
HANDLE hareB;
HANDLE tortoiseB;
DWORD threadID1;
DWORD threadID2;

cout<<"BANG!!!\n\nAND THEY'RE OFF!!!!!!\n\n\n";
// getch();

hareB=CreateThread(0,0,(LPTHREAD_START_ROUTINE)hareA,
0, CREATE_SUSPENDED,&threadID1);
if (hareB==0)
cout<<"Cannot create thread: "<<GetLastError()<<endl;

tortoiseB=CreateThread(0,0,(LPTHREAD_START_ROUTINE)tortoiseA,
0, CREATE_SUSPENDED, &threadID2);
if (tortoiseB==0)
cout<<"Cannot create thread2: "<<GetLastError()<<endl;

SetThreadPriority(hareB, THREAD_PRIORITY_NORMAL);
SetThreadPriority(tortoiseB, THREAD_PRIORITY_LOWEST);
ResumeThread(hareB);
ResumeThread(tortoiseB);
while(1);
}

在兔子到达终点时, 程序不会结束,乌龟仍会前进,直到中断乌龟进各,重新调用兔子线程时,才会结束. 但是我在乌龟的线程里面也有判断是否到达终点啊?
怎么会出现这种情况呢?该如何修改啊?
向高手请教!
求救!!!
...全文
259 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
zengkun100 2005-12-20
  • 打赏
  • 举报
回复
有意思哟!运行起来象动画一样。
睡在床板下_ 2005-12-20
  • 打赏
  • 举报
回复
lz 这个 程序 果然很有意思啊,呵呵, 我也回去搞下~~
佩服 哦/~~
ouyangdongfang 2005-12-20
  • 打赏
  • 举报
回复
哇~~~~~~~~~就是我想要的效果,
虽然我修改了好几次,但都是不行.
非常感谢楼上的程序.
只想着如何使用清屏函数,都没有想到最方便的'\r' 哎!

我算不算抛砖引玉呢?
哈哈
多谢多谢!
improgrammer 2005-12-19
  • 打赏
  • 举报
回复
这个程序有意思。改了一下,供参考。
/*
编写一个程序生成两个线程(进程)模拟龟兔赛跑。
1、兔子进程优先级高,因而跑的快,但兔子进程随机发生一些睡眠。
2、乌龟进程优先级低,因而跑的慢,但乌龟进程不发生睡眠。
3、程序应能用动画方式显示龟兔赛跑的进程。
源程序如下:
*/

#include <windows.h>
#include "process.h"
#include <iostream>
#include "math.h"
#include "stdlib.h"
#include "conio.h"
#include <time.h>

int hare=0,tortoise=0;
char position[70];

using namespace std;

void printPosition()
{
for(int i=0;i<70;i++)
position[i]='_';
position[hare]='H';
position[tortoise]='T';
if ((hare==tortoise))
{
position[hare]='!';
}
if(hare >= 70 || tortoise >=70)
{
if (hare > tortoise)
{
cout<<"\n\n\nHare wins!\n";
}
else if(hare < tortoise)
{
cout<<"\n\n\nTORTOLSE WINS!\n";
}
else
{
cout<<"\n\n\nIt's a tie!!\n";
}
}
else
{
for(i=1;i<=70;i++)
cout<<position[i];
}
cout<<"\r";
}

void hareA()
{
srand(time(0));
while(1)
{
//cout<<"in hareB"<<endl;
while((hare<70)&&(tortoise<70))
{
hare+=3;
if (0 == (rand() % 7))
{
Sleep(850);
}
Sleep(80);
}
};
};

void tortoiseA()
{
while(1)
{
//cout<<"in tortoiseB"<<endl;
while((hare<70)&&(tortoise<70))
{
tortoise++;
Sleep(80);
}
};
};

void main(void)
{
HANDLE hareB;
HANDLE tortoiseB;
DWORD threadID1;
DWORD threadID2;

cout<<"BANG!!!\n\nAND THEY'RE OFF!!!!!!\n\n\n";
// getch();

hareB=CreateThread(0,0,(LPTHREAD_START_ROUTINE)hareA,
0, CREATE_SUSPENDED,&threadID1);
if (hareB==0)
cout<<"Cannot create thread: "<<GetLastError()<<endl;

tortoiseB=CreateThread(0,0,(LPTHREAD_START_ROUTINE)tortoiseA,
0, CREATE_SUSPENDED, &threadID2);
if (tortoiseB==0)
cout<<"Cannot create thread2: "<<GetLastError()<<endl;

SetThreadPriority(hareB, THREAD_PRIORITY_NORMAL);
SetThreadPriority(tortoiseB, THREAD_PRIORITY_LOWEST);
ResumeThread(hareB);
ResumeThread(tortoiseB);
while((hare<70)&&(tortoise<70))
{
printPosition();
Sleep(10);
}
printPosition();
}
xbyjacky 2005-12-19
  • 打赏
  • 举报
回复
感谢楼上的楼上,我现在已经基本上改好了。但是,由于兔子一下子跳三步,后面到达终点时,始终有问题。乌龟的哪个线程就是正确的。
还有,请教清屏的函数是哪个? 我使用CLS();函数时,一直会报错,难道在VC里面不是这个函数吗?TC2.0里面是clrsc();函数,VC里面是哪个啊?
xbyjacky 2005-12-19
  • 打赏
  • 举报
回复
大概意思理解了,谢谢!
但是该怎么改呢?改来改去都不大对劲
iamcaicainiao 2005-12-19
  • 打赏
  • 举报
回复
楼主的程序很有意思。
---------------------------------T--------------------------H
----------------------------------T-------------------------H
-----------------------------------T------------------------H
------------------------------------T-----------------------H
-------------------------------------T----------------------H
--------------------------------------T---------------------H


Hare wins!
有的时候会出现上述的结果,想来就是楼主所描述的现象,不是我有没有理解错误。
其实,程序是正确的,只是可能搂住对上述结果理解有误。
上面的结果,兔子(H)仍然没有达到70,此时兔子为69,因为搂住让
position[hare+1]='H';
也就是让终点70的地方为兔子,所以,感觉兔子已经到达终点了,实际上还没有,
这是兔子应该是69,所以,此时,如果兔子睡觉了,就出现上面的结果了,即乌龟继续跑步,这应很正常。而当兔子为72时,是绝不会出现乌龟仍然跑路的。

不知道我是否有描述清楚,搂住能否理解,呵呵,当然了,还不知道我的理解是不是正确呢。
longituder 2005-12-19
  • 打赏
  • 举报
回复
要考虑两个线程的同步和互斥
dierdong 2005-12-19
  • 打赏
  • 举报
回复
这俩小东西还挺能跑!
healer_kx 2005-12-19
  • 打赏
  • 举报
回复
其实这个问题用线程未必是一种好的方式,其实一个线程之内就可以搞定了。
你在一个线程内排队,实现上用个定时器就可以了。

如果是两个线程的话。比较麻烦
forthelichking 2005-12-19
  • 打赏
  • 举报
回复
没看出毛病 mark

64,642

社区成员

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

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