模拟乌龟和野兔的程序中的错误 [英] Error in a program that simulates the Tortoise and the Hare

查看:68
本文介绍了模拟乌龟和野兔的程序中的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当乌龟和野兔在同一块上(在特定阶段)时,我的程序应显示"Ouch".但是,它显示不正确.另外,我在显示比赛的最后阶段和结果时遇到问题.感谢任何人的帮助.代码如下:

My program should display "Ouch" when the tortoise and hare are on the same block (during a certain phase). However it displays it incorrectly. Plus I have a problem with displaying the last phase of the race and the result. Thanks for anyone''s help. Here''s the code:

#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

int END=70;

int tortoise = 1;
int hare = 1;


int y;
int randNum1= static_cast<int>((1+rand()+time(0))%10);
int moveTortoise=0;

int x;
int randNum2= static_cast<int>((1+rand()+time(0))%10);
int moveHare= 0;




void moveTortoise1(int *T)
{
    if (*T !=END)
  {
      randNum1=static_cast<int>((1+rand()+time(0))%10);
      *T++;

   switch (randNum1)
       {
           case 1: case 2: case 3: case 4: case 5:
               moveTortoise=3;
               break;
           case 6: case 7:
               moveTortoise=-6;
               break;
           case 8: case 9: case 10:
               moveTortoise=1;
               break;

       }
  *T+=moveTortoise;
    if ( *T<1 )
      *T=1;

   else if (*T>END )
   {
       *T=END;

   }

    }

    for (y=0;y<=*T;y++)
    {
        if (y== *T)
        {
            cout<<"T";
        }
        else cout<<"-";
    }
    cout<<endl;

}

void moveHare1(int *H)
{


     if (*H !=END)
  {
      randNum2=static_cast<int>((1+rand()+time(0))%10);
      *H++;




   switch (randNum2)
       {
           case 1: case 2:
               moveHare=0;
               break;
           case 3: case 4:
               moveHare=9;
               break;
           case 5:
               moveHare=-12;
               break;
           case 6: case 7: case 8:
               moveHare=1;
               break;
           case 9: case 10:
               moveHare=-2;
               break;
        }
     *H+=moveHare;
   if ( *H<1 )
      *H=1;

   else if (*H>END )
   {
       *H=END;

   }

   for (x=0;x<=*H;x++)
   {
       if (x==*H)
       {
        cout<<"H";
       }
       else cout<<"-";
   }

   cout<<endl<<endl;
  }


}

int main()
{

   cout<<" BANG!!!\n AND THEY'RE OFF !!!"<<endl<<endl;
   while (tortoise != END && hare !=END)
   {
       moveTortoise1(&tortoise);
       moveHare1(&hare);
       if (tortoise==hare)
           cout<<"OUCH!!!"<<endl;
   }

   if (tortoise<hare)

       cout<< "Hare wins. Yuch.";
   else if (tortoise==hare)
       cout<< "It's a tie";
   else
       cout<< "TORTOISE WINS!!! YAY!!!";

   cout<<endl;
}

推荐答案

一个很好的细微问题,使我难过了一段时间.您有两个语句的结果与预期不符,这是因为后缀增加发生在之前间接.在moveXXX函数中,您需要进行以下更改:
A nice subtle problem which had me stumped for a while. You have two statements whose results are not what you expect, due to the fact that postfix increment occurs before indirection. In your moveXXX functions you need to make the following changes:
// in moveTortoise1
(*T)++;        // was *T++

// in moveHare1
(*H)++;        // was *H++


请参见此页面 [


See this page[^] for details of operator precedence and associativity.

I think you may need to look at your randomisation as the Tortoise seems to be winning all the time.


这篇关于模拟乌龟和野兔的程序中的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆