龟图程序 c++实现 很久以前的代码

很多年前的一篇博客,网易博客关闭,移到此处

最近学习c++,颇有头悬梁锥刺股之决心,然而自己天生愚笨,常想自己终日而思不如别人须臾之所学也,亦恐慌之极。上午花了接近一个小时把c++的龟图程序谢了出来,好在还算完善。今日始开博客希望跟大家多多交流,共同进取。附程序如下:

// 23.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <iostream>
using namespace std;

int left(int);
int rightt(int);
void trace(int[][20],int &,int &,int,int);
void printArrays(int[][20]);

int _tmain(int argc, _TCHAR* argv[])
{
 const int arrays=20;
 int floor[arrays][arrays]={0};
 int m, mm=0, i=0, j=0;
 //enum direction{ up=0,down,right,left};
 //direction dire;
 int dire=0;
 
 cout<<"Please enter a number from 1 to 9 m:";
 cin>>m;
 while(m!=9){
 
 switch(m)
 {
 case 1: mm=0;break;
 case 2: mm=1;break;
 case 3: dire=rightt(dire); cout<<"dire"<<dire<<endl;break;
 case 4:dire=left(dire);break;
 case 5: trace(floor,i,j,dire,mm);break;
 case 6: printArrays(floor);break;
 default: cout<<"the number was not from 1-9! ";
 }
    cout<<"Please enter a number from 1 to 9 m:"; 
 cin>>m;
 }
 return 0;
}

int rightt(int x)
{
 switch(x)
 {case 0: x=1;break;
 case 1: x=2;break;
 case 2: x=3;break;
 case 3: x=0; break;
 default: cout<<"The system was error!";
 }
 return x;
}

int left(int x)
{
 switch(x)
 {case 0: x=3;break;
 case 1: x=0;break;
 case 2: x=1;break;
 case 3: x=2; break;
 default: cout<<"The system was error!";
 }
 return x;
}

void trace(int f[][20],int &i,int &j,int x,int m)
{
 int n;

 cout<<"输入前进步数 n:";
 cin>>n;

 
 switch(x)
 {
 case 0:
  for(int nn=0;nn<n;nn++)
   {if(m==1)
   f[i][j+nn]=1;
  }
  j=j+n;
  break;
 case 1:
  for(int nn=0;nn<n;nn++)
   {if(m==1)
   f[i+nn][j]=1;}
  i+=n;
  break;
 case 2:
  for(int nn=0;nn<n;nn++)
   {if(m==1)
   f[i][j-nn]=1;}
  j-=n;
  break;
 case 3:
  for(int nn=0;nn<n;nn++)
   {if(m==1)
   f[i-nn][j]=1;}
  i-=n;
  break;
 default:
  cout<<"Error!"<<endl;
 }
 
}

void printArrays(int f[20][20])
{
 for(int i=0;i<20;i++)
  {for(int j=0;j<20;j++)
  {
   //cout<<f[i][j];
   if(f[i][j]==1)
      cout<<"* ";
   else  cout<<"  ";
  }
 cout<<endl;
 }
}

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值