📄 workdos.cpp
字号:
// WorkDos.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <afxwin.h>
#include <stdlib.h>
#include <iostream.h>
#include <fstream.h>
#include <string.h>
#include <ctype.h>
#include <stdio.h>
#include <dos.h>
//##ModelId=467AA51C007F
enum DIRECTION{usable,unusable,visit,obstacle};//坐标点的方向属性:此方向是否可走,是否被访问过用来记录路径
//##ModelId=467AA51C00B0
enum SIGNATURE{borderline,inner_point};//坐标点的基本属性:边界,障碍,棋盘内的点
//##ModelId=467AA51C00CE
typedef struct POINT_CHESS
{
int x,y;//坐标值x横坐标,y纵坐标
char value;//点的值
DIRECTION east, west, south, north;//4个方向
SIGNATURE sign;
//POINT_CHESS * next;
} POINT_CHESS;
/////////////////////////////////////////////////////////////////////////////////////////
//##ModelId=467AA51C011E
class Chess
{
private:
//##ModelId=467AA51C013C
int NumOfMatrix;//使用此变量可以便于棋盘数的更改
//##ModelId=467AA51C01B4
POINT_CHESS Chess_Point[14][14];//按行存储
//##ModelId=467AA51C01D3
void convert(int n);//附加函数转换数字为字符
public:
//##ModelId=467AA51C01FB
chess();
//##ModelId=467AA51C020F
~Chess();
//##ModelId=467AA51C0219
void init_Chess( char filename[20] );
//##ModelId=467AA51C0237
char get_point_value(int x,int y);//获得坐标x,y的值
//##ModelId=467AA51C025F
POINT_CHESS Get_point(int x,int y);//获得坐标x,y的点,返回坐标点
//##ModelId=467AA51C0287
void set_char(int x,int y,char ch,char filename[20]);//设置(x,y)的值并写回文件
};
//##ModelId=467AA51C01D3
void Chess::convert(int n)
{
int i;
char c;
if((i=n/10)!=0)
convert(i);
c=n%10+'0';
}
//##ModelId=467AA51C0237
char Chess::get_point_value(int x,int y)
{
char temp;
temp=Chess_Point[x][y].value;
return temp;
}
//##ModelId=467AA51C025F
POINT_CHESS Chess::Get_point(int x,int y)
{
POINT_CHESS temp;
temp=Chess_Point[x][y];
return temp;
}
//##ModelId=467AA51C0287
void Chess::set_char(int x,int y,char ch,char filename[20])
{
bool wrighted=FALSE;//判断是否存在(x,y)=若不存在则写入文件尾
Chess_Point[x][y].value=ch;
fstream chessfile; //写入文本文件
chessfile.open(filename,ios::nocreate | ios::in | ios::out | ios::app);
//////////////用于输出改变结果
int i,x1,y1,j;//j用来定位写入文件的行
char temp[10];
char buf[50];
j=0;
while (!chessfile.eof())
{
chessfile.getline(buf,sizeof(buf));
//cout<<buf<<endl;
i=1;
temp[0]=buf[i];
i++;
if(buf[i]==',')
x1=atoi(&temp[0]);
else
{
temp[1]=buf[2];
x1=atoi(temp);
temp[1]='\n';
i++;
}
//cout<<"x1="<<x1<<endl;
i++;
temp[0]=buf[i];
i++;
if(buf[i]==')')
y1=atoi(&temp[0]);
else
{
temp[1]=buf[i];
y1=atoi(temp);
temp[1]='\n';
i++;
}
//cout<<"y1="<<y1<<endl;
//判断是否为值即是否有这一项
i++;j++;
if(buf[i] == '=' && x1==x && y1==y)//重写此项
{
//待写
wrighted=TRUE;
break;//中止循环
}
}
if(!chessfile.eof())//未到文件尾,即存在这一项
{
chessfile.close();
chessfile.open(filename,ios::nocreate | ios::in | ios::out);
while(!chessfile.eof() && wrighted)
{
chessfile.getline(buf,sizeof(buf));
}
}
if( !wrighted )//写入文件尾
{
chessfile.seekg(ios::end);
chessfile.put('(');
//////将整数转换成字符
int temp_i;
char c1,c2;
c2=' ';
if((temp_i=x1/10)!=0)
{
c2=temp_i%10+'0';
}
c1=x1%10+'0';
chessfile.put(c1);
if(c2!=' ')
{
chessfile.put(c2);
c2=' ';
}
chessfile.put(',');
if((temp_i=y1/10)!=0)
{
c2=temp_i%10+'0';
}
c1=y1%10+'0';
chessfile.put(c1);
if(c2!=' ')
{
chessfile.put(c2);
}
///////
chessfile.put(')');
chessfile.put('=');
chessfile.put(ch);
chessfile.write("the end of file! \n",sizeof("the end of file! \n"));
//fprintf(fptr,"this is a output a!\n");
}
chessfile.close();
}
//##ModelId=467AA51C01FB
Chess::chess()
{
NumOfMatrix=14;
int i,j;
for( i=0;i<NumOfMatrix;i++)
{
for( j=0;j<NumOfMatrix;j++)
{
Chess_Point[i][j].x=i;
Chess_Point[i][j].y=j;
Chess_Point[i][j].value=' ';//坐标点值初始化为空格
if(i==0 || j==0 || i==13 || j==13)
{
Chess_Point[i][j].east=usable;
Chess_Point[i][j].west=usable;
Chess_Point[i][j].south=usable;
Chess_Point[i][j].north=usable;
Chess_Point[i][j].sign=borderline;
if(i==0)
Chess_Point[i][j].north=unusable;
if(j==0)
Chess_Point[i][j].west=unusable;
if(i==13)
Chess_Point[i][j].south=unusable;
if(j==13)
Chess_Point[i][j].east=unusable;
}
else
{
Chess_Point[i][j].east=usable;
Chess_Point[i][j].west=usable;
Chess_Point[i][j].south=usable;
Chess_Point[i][j].north=usable;
Chess_Point[i][j].sign=inner_point;
}
}
}
}
//##ModelId=467AA51C0219
void Chess::init_Chess( char filename[20] )//初始化棋盘 按行存储
{
NumOfMatrix=14;
int i,j;
for( i=0;i<NumOfMatrix;i++)
{
for( j=0;j<NumOfMatrix;j++)
{
Chess_Point[i][j].x=i;
Chess_Point[i][j].y=j;
Chess_Point[i][j].value=' ';//坐标点值初始化为空格
if(i==0 || j==0 || i==13 || j==13)
{
Chess_Point[i][j].east=usable;
Chess_Point[i][j].west=usable;
Chess_Point[i][j].south=usable;
Chess_Point[i][j].north=usable;
Chess_Point[i][j].sign=borderline;
if(i==0)
Chess_Point[i][j].north=unusable;
if(j==0)
Chess_Point[i][j].west=unusable;
if(i==13)
Chess_Point[i][j].south=unusable;
if(j==13)
Chess_Point[i][j].east=unusable;
}
else
{
Chess_Point[i][j].east=usable;
Chess_Point[i][j].west=usable;
Chess_Point[i][j].south=usable;
Chess_Point[i][j].north=usable;
Chess_Point[i][j].sign=inner_point;
}
}
}
fstream chessfile;
chessfile.open(filename,ios::nocreate | ios::in);
int x1,y1,x2,y2;//临时保存坐标
int xline,yline;//临时保存障碍线
char temp[5];
char buf[50];
for(i=0;i<20;i++)
{
buf[i]='\n';
}
while (!chessfile.eof())
{
chessfile.getline(buf,sizeof(buf));
cout<<buf<<endl;
i=1;
temp[0]=buf[i];
i++;
if(buf[i]==',')
x1=atoi(&temp[0]);
else
{
temp[1]=buf[2];
x1=atoi(temp);
temp[1]='\n';
i++;
}
//cout<<"x1="<<x1<<endl;
i++;
temp[0]=buf[i];
i++;
if(buf[i]==')')
y1=atoi(&temp[0]);
else
{
temp[1]=buf[i];
y1=atoi(temp);
temp[1]='\n';
i++;
}
//cout<<"y1="<<y1<<endl;
//判断是否为值
i++;
if(buf[i] == '=')
{
Chess_Point[x1][y1].value=buf[i+1];
cout<<"点("<<x1<<","<<y1<<")的值为:"<<
Chess_Point[x1][y1].value<<endl;
continue;
}
i=i+2;
temp[0]=buf[i];
i++;
if(buf[i]==',')
x2=atoi(&temp[0]);
else
{
temp[1]=buf[i];
x2=atoi(temp);
temp[1]='\n';
i++;
}
//cout<<"x2="<<x2<<endl;
i++;
temp[0]=buf[i];
i++;
if(buf[i]==')')
y2=atoi(&temp[0]);
else
{
temp[1]=buf[i];
y2=atoi(temp);
temp[1]='\n';
i++;
}
//cout<<"y2="<<y2<<endl;
i=0;
//设置障碍
if(x1==x2)
{
xline=x1;
Chess_Point[xline][y1].south=obstacle;
for(i=y1+1;i<y2;i++)
{
Chess_Point[xline][i].south=obstacle;
Chess_Point[xline][i].north=obstacle;
}
Chess_Point[xline][i].north=obstacle;
}
if(y1==y2)
{
yline=y1;
Chess_Point[x1][yline].east=obstacle;
for(i=x1+1;i<x2;i++)
{
Chess_Point[i][yline].east=obstacle;
Chess_Point[i][yline].west=obstacle;
}
Chess_Point[i][yline].west=obstacle;
}
i=0;
while (buf[i] != '\n')
{
buf[i]='\n';
i++;
}
}
chessfile.close();
}
//##ModelId=467AA51C020F
Chess::~Chess()
{
}
/////////////////////////////////////////////////////////////////////////////////////////////////
//##ModelId=467AA51C0309
class Robot
{
private:
char b1,b2;//机器人的变量
//##ModelId=467AA51C031D
char face_direction;//机器人面对的方向
//##ModelId=467AA51C0332
POINT_CHESS current_place;//机器人当前的坐标点
//##ModelId=467AA51C0345
BOOL READ_USED;//是否已经进行读操作,默认为false
public:
//##ModelId=467AA51C0359
Robot();
void Init_Robot(Robot * Robot_Object);
//##ModelId=467AA51C0363
~Robot();
//##ModelId=467AA51C036D
void evaluate_b1_from_b2();//b2=b1
//##ModelId=467AA51C0377
void evaluate_b2_from_b1();//b1=b2
//##ModelId=467AA51C0381
void Move(Chess Chess_Object);
//##ModelId=467AA51C0395
void Left(char face_direction);
//##ModelId=467AA51C039F
void Write(Chess Chess_Object,int x,int y,char filename[20]);
//##ModelId=467AA51C03BD
void Read(Chess Chess_Object,int x,int y);
//##ModelId=467AA51C03DB
char Get_Robot_direction();//获得当前机器人的方向,对外接口函数
//##ModelId=467AA51C03E5
POINT_CHESS Get_Robot_Current_Place();//获得当前机器人的位置,对外接口函数
};
//##ModelId=467AA51C03E5
POINT_CHESS Robot::Get_Robot_Current_Place()
{
POINT_CHESS temp;
temp=current_place;
return temp;
}
//##ModelId=467AA51C03DB
char Robot::Get_Robot_direction()
{
char temp;
temp=face_direction;
return temp;
}
//##ModelId=467AA51C0359
Robot::Robot()
{
current_place.x=0;
current_place.y=0;
face_direction='N';//E, W,S,N分别代表东西南北4个方向
b1=b2=' ';//初始为空格
current_place.north=unusable;
current_place.west=unusable;
current_place.south=usable;
current_place.east=usable;
current_place.value=' ';
READ_USED=FALSE;
}
void Robot::Init_Robot(Robot * Robot_Object)
{
Robot_Object->current_place.x=0;
Robot_Object->current_place.y=0;
Robot_Object->face_direction='N';//E, W,S,N分别代表东西南北4个方向
Robot_Object->b1=Robot_Object->b2=' ';//初始为空格
Robot_Object->current_place.north=unusable;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -