小小的模拟一下。
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <algorithm>
using namespace std;
char str[];
double a[];
double b[];
void CL()
{
a[] = a[] = 0.0;
a[] = a[] = sqrt(2.0)/2.0;
a[] = 1.0;
a[] = -1.0;
a[] = a[] = -sqrt(2.0)/2.0;
b[] = 1.0;
b[] = -1.0;
b[] = b[] = sqrt(2.0)/2.0;
b[] = b[] = 0.0;
b[] = b[] = -sqrt(2.0)/2.0;
}
int judge(int x)
{
if(str[x+] == ','||str[x+] == '.')
{
if(str[x] == 'N')
return ;
else if(str[x] == 'E')
return ;
else if(str[x] == 'S')
return ;
else if(str[x] == 'W')
return ;
}
else
{
if(str[x] == 'N'&&str[x+] == 'E')
return ;
else if(str[x] == 'S'&&str[x+] == 'E')
return ;
else if(str[x] == 'S'&&str[x+] == 'W')
return ;
else if(str[x] == 'N'&&str[x+] == 'W')
return ;
}
return ;
}
int main()
{
int len,i,cas = ,pre,j,temp;
double x,y;
CL();
while(scanf("%s",str)!=EOF)
{
if(strcmp(str,"END") == ) break;
len = strlen(str);
pre = ;
x = y = ;
for(i = ; i < len; i ++)
{
if(str[i] == ','||str[i] == '.')
{
temp = ;
for(j = pre; j < i; j ++)
{
if(str[j] <= ''&&str[j] >= '')
temp = temp* + str[j] - '';
else
break;
}
x += temp*a[judge(j)];
y += temp*b[judge(j)];
pre = i+;
}
}
printf("Map #%d\n",cas++);
printf("The treasure is located at (%.3f,%.3f).\n",x,y);
printf("The distance to the treasure is %.3f.\n\n",sqrt(x*x+y*y));
}
return ;
}