杭电 1033【转向】

Edge

Input
The input contains several test cases, each on a separate line. Each line contains a nonempty string of characters A and V describing the longer edge of the sheet. You may assume that the length of the string is less than 200. The input file terminates immediately after the last test case.
 
Output
For each test case generate a PostScript drawing of the edge with commands placed on separate lines. Start every drawing at the coordinates (300, 420) with the command "300 420 moveto". The first turn occurs at (310, 420) using the command "310 420 lineto". Continue with clockwise or counter-clockwise turns according to the input string, using a sequence of "x y lineto" commands with the appropriate coordinates. The turning points are separated at a distance of 10 units. Do not forget the end point of the edge and finish each test case by the commands stroke and showpage.

You may display such drawings with the gv PostScript interpreter, optionally after a conversion using the ps2ps utility.


 
Sample Input
  
  
V AVV
 
Sample Output
  
  
300 420 moveto 310 420 lineto 310 430 lineto stroke showpage 300 420 moveto 310 420 lineto 310 410 lineto 320 410 lineto 320 420 lineto stroke showpage
 
/*
*转向,A表示向右转,V表示向左转,每次是走长度为10,开始转向之前要向右先走10个长度
*每次转向是相对于上次行走的方向
*/
#include<iostream>
#include<string.h>
using namespace std;
int main()
{
	//按照题目要求a作为横坐标初始化为300,c作为纵坐标初始化为420,在之后的代码里表现
    int a=300,b,c,n,x,y,i;
	//按照题目要求输入的转向命令最长为200
    char d[200];
    while(cin>>d)
    {
		//使用strlen函数在n中获取到转向命令的长度
        n=strlen(d);
		//按照题目要求转向之前有要先向右走一次
        b=310;c=420;
		//按照输出要求输出起始地址然后输出moveto,输出第一次行走后的地址输出lineto开始转向
        cout<<a<<" "<<c<<" "<<"moveto"<<endl;
        cout<<b<<" "<<c<<" "<<"lineto"<<endl;
		//x表示横坐标,y表示纵坐标,起先需要向右行走一次所以x记录为1,y记录为0.
        x=1;y=0;
        for(i=0;i<n;i++)
        {
			//如果遇到右转指令需要通过之前的行走方向确定相对右转后的走向
            if(d[i]=='A')
            {
				//如果x等于1表示之前是向右走的,此时的相对右转需要向下走,而横轴不动所以y记为-1,x记为0;纵轴坐标减10,以下同理
                if(x==1)
                {y=-1;x=0;c-=10;}
                else if(x==-1)
                {y=1;x=0;c+=10;}
                else if(y==1)
                {x=1;y=0;b+=10;}
                else if(y==-1)
                {x=-1;y=0;b-=10;}
            }
			//如果遇到左转指令也需要通过之前的行走方向确定相对左转后的走向,与上方代码同理
            if(d[i]=='V')
            {
                if(x==1)
                {y=1;x=0;c+=10;}
                else if(x==-1)
                {y=-1;x=0;c-=10;}
                else if(y==1)
                {x=-1;y=0;b-=10;}
                else if(y==-1)
                {x=1;y=0;b+=10;}
            }
			//每进行一次转向后输出目前所在坐标,并输出lineto,接着通过n以内的for循环进行下一次的转向
            cout<<b<<" "<<c<<" "<<"lineto"<<endl;
        }
		//转向均进行完成后,按照代码要求输出stroke和showpage两行字符串
        cout<<"stroke"<<endl<<"showpage"<<endl;
    }
    return 0;
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值