线段类


A.线段类
Time Limit: 1000 MS Memory Limit: 32768 K
Total Submit: 138 (63 users) Total Accepted: 68 (60 users) Special Judge: No
Description
设计一个二维线段类,线段用两个端点的坐标来表示(访问权限为私有),另外除了需要定义构造函数和赋值的方法外,还要定义计算线段长度的方法、对线段进行平移的方法、以原点为参照求对称的方法、以X轴为参照求对称的方法、以Y轴为参照求对称的方法。
然后实现对一条线段,经过不定数量的变换后,输出其端点的坐标和线段长度(长度保留两位小数)。
在main 函数读入数据,定义线段类对象,调用相应的方法进行操作,进而输出正确信息。 

Input
首先输入二维线段的2个端点,4个整数值:x1 y1 x2 y2
然后输入多组数据,每组数据中的第一个数据是字符,'T'表示平移,'S'表示对称。如果是平移,后面的数据为两个整数,分别表示X方向和Y方向的平移量;如果是对称,后面是一个整数,0表示以原点为参照求对称点,1 表示以X轴为参照求对称点,2表示以Y轴为参照求对称点。数据中间用空格分隔。
Output
输出经过多次操作后线段的两个端点的坐标及线段长度,格式等参见样例。
Sample Input
1 2 4 6
T 2 3
S 1
S 2
S 0
T 4 5
Sample Output

(7,10)(10,14) 5.00







import java.util.*;
import java.math.*;


class xd
{
int x1,y1,x2,y2;


public xd(int x1,int y1,int x2,int y2)
{
this.x1 = x1;
this.y1 = y1;
this.x2 = x2;
this.y2 = y2;
}

void changdu()
{
double q = (x1 - x2)*(x1 - x2);
double w = (y1 - y2)*(y1 - y2);
double he = q + w;
double jieguo = Math.sqrt(he);
System.out.printf("%.2f\n",jieguo);

}

    void pingyi(int x,int y)
{
x1 =x1 + x;
y1 = y1 + y;
x2 = x2 + x;
y2 = y2 + y;
}

void duichen0()
{
int tmp1,tmp2,tmp3,tmp4;
tmp1 = x1;
tmp2 = y1;
tmp3 = x2;
tmp4 = y2;
x1 = (-1)*tmp1;
y1 = (-1)*tmp2;
x2 = (-1)*tmp3;
y2 = (-1)*tmp4;
}

void duichenx()
{
y1 = y1*(-1);
y2 = y2*(-1);
}

void duicheny()
{
x1 = x1*(-1);
x2 = x2*(-1);
}

public void output()
{
System.out.printf("(%d,%d)(%d,%d) ",x1,y1,x2,y2);
}
}




public class Main {


public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);

int x1 = input.nextInt();
int y1 = input.nextInt();
int x2 = input.nextInt();
int y2 = input.nextInt();
xd X = new xd(x1,y1,x2,y2);

while(input.hasNext())
{
String s = input.next();
   char c = s.charAt(0);
   if(c == 'T')
   {
    int heng = input.nextInt();
       int zong = input.nextInt();
    X.pingyi(heng,zong);
   }
   if(c == 'S')
   {
    int q = input.nextInt();
    if(q == 0)
    {
    X.duichen0();
    }
    if(q == 1)
    {
    X.duichenx();
    }
    if(q == 2)
    {
    X.duicheny();
    }
   }
}

X.output();X.changdu();
//System.out.printf("%.2lf\n",ymd);

}
}

/*1 2 4 6
T 2 3
S 1
S 2
S 0
T 4 5*/




猜你喜欢

转载自blog.csdn.net/wonder__/article/details/79929082