龜圖程式 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;
}
}