?? rlrun.cpp
字號:
/*《模擬龜兔程序》用隨機數產生器模擬龜兔賽跑程序。
選手從70個方格的“第一格”起跑,每格表示跑道上
的一個位置,終點線在第70格處。程序如下調整動物
位置:
烏龜 fast plod(快走) 50% 右移 3 格
slip(滑倒) 20% 左移 6 格
slow plod(慢走) 30% 右移 1 格
兔子 sleep(睡覺) 20% 不動
big hop(大步跳) 20% 右移 9 格
big slip(大步滑倒) 10% 左移 12格
small hop(小步跳) 30% 右移 1 格
small slip(小步滑倒) 20% 左移 2 格
用變量顯示動物的位置(位置1 到 70)。每個動物從
位置1開始。如果動物跌回1格外則移回1格。
如果兩者占用一格,則烏龜會咬兔子,程序從該位置
打印OUCH!!!*/
#include <iostream>
#include <stdlib.h>
#include <iomanip>
#include <time.h>
using namespace std;
class tortoise { //烏龜類
private:
void randmove(); //隨機產生數字,決定調用那些函數
void fastplod();
void slip();
void slowplod();
public:
tortoise();
~tortoise();
bool finish;
int location; //記錄烏龜所在位置
void go(); //烏龜出發函數
};
tortoise::tortoise()
{
location=0;
finish=false;
srand(time(NULL));
}
tortoise::~tortoise()
{
if(finish==false)
cout <<"可惜,烏龜掛了!位置:"<<location<<endl;
else
cout <<"烏龜說:我是老大!!!"<<endl;
}
void tortoise::randmove()
{
int step;
step=rand()%10+1; //產生從1到10的隨機整數
if(step>=1&&step<=5)
fastplod(); //烏龜快走
if(step>=6&&step<=7)
slip(); //烏龜滑倒
if(step>=8&&step<=10)
slowplod(); //烏龜慢走
if(location<0)
location=0; //如果烏龜跌到起點以后則將位置回零
if(location>=70)
{
finish=true; //判斷烏龜是否到達終點
}
cout <<"烏龜:"<<setfill('-')<<setw(location)<<"龜"<<endl;
}
void tortoise::fastplod()
{
location=location+3;
cout<<"烏龜快走至位置"<<location<<endl;
}
void tortoise::slip()
{
location=location-6;
cout<<"烏龜跌倒!位置:"<<location<<endl;
}
void tortoise::slowplod()
{
cout<<"靠,烏龜才走 1 格,到了:"<<location<<endl;
}
void tortoise::go()
{
randmove();
}
class rabbit { //兔子類
private:
void randmove(); //隨機產生數字,決定調用那些函數
void sleep();
void bighop();
void bigslip();
void smallhop();
void smallslip();
public:
rabbit();
~rabbit();
bool finish;
int location; //記錄兔子所在位置
void go(); //兔子出發函數
};
rabbit::rabbit()
{
location=0;
finish=false;
srand(time(NULL));
}
rabbit::~rabbit()
{
if(finish==false)
cout <<"兔子輸掉了比賽!它哭了!!!位置:"<<location<<endl;
else
cout <<"兔子到達終點,仰天大笑!"<<endl;
}
void rabbit::randmove()
{
int step;
step=rand()%10+1; //產生從1到10的隨機整數
if(step>=1&&step<=2)
sleep(); //兔子睡著了
if(step>=3&&step<=4)
bighop(); //兔子大步跳
if(step==5)
bigslip(); //兔子大步滑倒
if(step>=6&&step<=8)
smallhop(); //兔子小步跳
if(step>=9&&step<=10)
smallslip(); //兔子小步滑倒
if(location<0)
location=0; //如果兔子跌到起點以后則將位置回零
if(location>=70)
{
finish=true; //判斷兔子是否到達終點
}
cout <<"兔子:"<<setfill('-')<<setw(location)<<"兔"<<endl;
}
void rabbit::bighop()
{
location=location+9;
cout<<"兔子大步跳到位置"<<location<<endl;
}
void rabbit::bigslip()
{
location=location-12;
cout<<"兔子摔了個兔啃屎!位置:"<<location<<endl;
}
void rabbit::sleep()
{
cout<<"兔子暈點睡著,位置:"<<location<<endl;
}
void rabbit::smallhop()
{
location=location+1;
cout<<"兔子小跳步到位置:"<<location<<endl;
}
void rabbit::smallslip()
{
location=location-2;
cout<<"兔子打了個趔趄,位置:"<<location<<endl;
}
void rabbit::go()
{
randmove();
}
void main()
{
rabbit r;
tortoise t;
while(t.location<70 && r.location<70)
{
system("cls");
t.go();
r.go();
if(t.location==r.location)
{
cout<<"\n\n兔子:靠你咬我屁股!!!"<<endl;
cout<<"烏龜:咬你你咋!"<<endl;
}
for(int t=0;t<=59999999;t=t+2)
t--;
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -