本题其实很简单,只是题目比较难理解。
简单来说,从(300,420)这点出发,先向右移动10个单位长度,之后每次移动都是10个单位长度。
只有第一行最后是"moveto",之后都是"lineto",最后两行是"stroke"和"showpage"。
输入'A'表示以当前方向顺时针转90°,移动10单位长度;输入'V'表示逆时针转90°,移动10单位长度。
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
char arr[210]; //输入字符数组
while (cin >> arr)
{
int x = 300, y = 420; //起始坐标
char direction = 'e'; //起始方向
cout << x << " " << y << " " << "moveto" << endl;
x += 10;
cout << x << " " << y << " " << "lineto" << endl;
int len = strlen(arr);
for (int i = 0; i < len; i++)
{
switch (direction) //根据当前方向和输入字符选择操作
{
case 'e': //东
if (arr[i] == 'A')
{
direction = 's';
y -= 10;
}
else
{
direction = 'n';
y += 10;
}
break;
case 'n': //北
if (arr[i] == 'A')
{
direction = 'e';
x += 10;
}
else
{
direction = 'w';
x -= 10;
}
break;
case 'w': //西
if (arr[i] == 'A')
{
direction = 'n';
y += 10;
}
else
{
direction = 's';
y -= 10;
}
break;
case 's': //南
if (arr[i] == 'A')
{
direction = 'w';
x -= 10;
}
else
{
direction = 'e';
x += 10;
}
break;
}
cout << x << " " << y << " " << "lineto" << endl;
}
cout << "stroke" << endl << "showpage" << endl;
}
return 0;
}
继续加油。