- 在线时间
- 428 小时
- 最后登录
- 2017-2-22
- 注册时间
- 2011-9-18
- 听众数
- 8
- 收听数
- 0
- 能力
- 20 分
- 体力
- 6079 点
- 威望
- 110 点
- 阅读权限
- 200
- 积分
- 3684
- 相册
- 1
- 日志
- 0
- 记录
- 0
- 帖子
- 759
- 主题
- 60
- 精华
- 0
- 分享
- 0
- 好友
- 40
TA的每日心情 | 开心 2017-2-22 14:21 |
|---|
签到天数: 271 天 [LV.8]以坛为家I
群组: 2014年美赛冲刺培训 群组: 物联网工程师考试 群组: 2013年电工杯B题讨论群 群组: 物联网工程师培训 群组: 2013电工杯A题讨论群组 |
定义一个Shape抽象类,建立一个point类,表示平面中的一个点;建立一个line类,表示平面中的一条线段,内含两个point类的对象;建立triangle类,表示一个三角形,内含三个line类的对象构成一个三角形,确定派生关系,编制相应程序输出相关信息,设计triangle类的成员函数完成三条边是否能构成三角形的检验和三角型面积计算,输出相关信息。
. u7 w- _; T- F) \! E% |) u' Y# H3 m' V
. _$ N. I9 o5 o& g) L1 b
#include<iostream>
7 [8 _) L* \3 U9 u# c1 t* g3 l, [#include<string>
7 H& y: w$ h0 I: f9 q( v* Q7 y#include<math.h>
7 a6 O8 p! m: B$ `+ cusing namespace std;
! ?4 a/ I% H/ F6 j7 F$ s* {: v% Jclass Shape
# ?. o) ~ X0 y) M! J{public:
4 y7 d5 r# y6 O) l2 B2 i% ?};4 c- t7 Y, t- j$ I+ o6 E
class Point:public Shape
+ I: V0 V9 X; f6 u{public:
2 D7 ]& U& w: c+ d4 y4 ePoint(float a,float b)
- {7 s( |7 U0 m+ z9 C( o{x=a;y=b;}. V) v- ~% p j- A$ T5 G( w+ x
6 b6 r. \6 z( y- v5 T/ {void setPofloat(float a,float b)8 [. k) ^' N* [1 _3 L5 i0 V& B
{x=a;y=b;}) A' [( h0 S9 A/ y
float x,y;+ A2 l4 v4 J' Z1 u
};
/ k/ z3 X& c# \5 f6 X* q" pclass Line
( j% v, o3 b8 ]& r0 H3 B9 }; f% u% y{public:
" C8 A' n# Z5 a% A. R! G# LLine(float a1,float b1,float a2,float b2):
, A/ ?, k0 b+ o1 B9 f/ @9 ~p1(a1,b1),p2(a2,b2){};
5 q: x0 U8 e& v) N Y9 ~2 C8 [$ z8 y& V: i& ^: |7 i
double length()9 L! F3 c1 `) \( h/ _
{ return (sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y))); }) D9 \2 f1 K. \; T
void setline(){, b2 Y; y0 M( t# ]7 o
cin>>p1.x>>p1.y>>p2.x>>p2.y;
( h: @. x, @) |0 l8 t5 n7 n}1 p7 l _. m C$ G( ?# @
protected:
8 z5 _. L. X5 h Point p1,p2;
# B, N+ g& x, v& ^. V};
* ^) x n7 o* R3 {$ h/ zclass Triangle
7 v) M# S- r! M% n) E& X{
* i. }' ^, ]' ^& ^( Q" Cpublic:) E# b/ j$ h' [4 Y# y+ Z2 i
Triangle(float a1,float b1,float a2,float b2, float a3,float b3,float a4,float b4 ,float a5,float b5,float a6,float b6):9 |0 k- ]" z! [$ R
l1(a1,b1,a2,b2), l2(a3,b3,a4,b4) , l3(a5,b5,a6,b6){};
$ B* ]& M2 j) X
* m9 F! Q: h* K6 R% Ifloat delta(){
0 q2 _& V8 I. N! L0 d; N. ` d1=l1.length();
8 f) D; k: h' s n" x! o' o d2=l2.length();
( l2 h' p9 ]. X$ r; @ d3=l3.length();
1 v! K+ Y; U( O( G if((d1+d2>d3)&&(d2+d3>d1)&&(d3+d1>d2))
2 ~: `! }# P: M: N3 } return 1;
% @, R! {0 K \* w ]( D+ m6 c0 g else
: ^' {! ^" x0 |# e: i: w return 0;, `& N; \/ `5 j7 Z
}
# \& M' Z& ]% Q& c; n! m2 D, y2 Gvoid sett(){0 J# p$ [5 z t7 Y! y
cout<<"输入3条线的6个点的坐标(12个数字)"<<endl;
# S+ U: m9 v7 o) C/ c3 i- g l1.setline();) ?0 K* A9 @9 m8 C$ L. N/ a
l2.setline();
$ b8 v6 ?9 d7 M5 ? l3.setline();, w2 m- R% u/ I9 V) b3 M6 m2 i
cout<<"3条线的长度分别为:"<<endl;; B7 K( T4 z& I5 R, S
cout<<l1.length()<<", "<<l2.length()<<", "<<l3.length()<<"."<<endl;
7 q2 ^& X( ]6 C, a! P% Z1 E3 Z try
9 M8 c) ^: g4 a1 M& |; z8 R {
4 m; q, @7 E' M double p=(l1.length()+l2.length()+l3.length())/2;
) [. L- G* J' o( n* ^1 ?9 r if ( (l1.length()+l2.length()<=l3.length()) || (l2.length()+l3.length()<=l1.length()) || (l3.length()+l1.length()<=l2.length()) ) throw l1.length();
$ F; t* G, _6 h, O& | , X5 x. Z4 m( p/ h- l
double s=sqrt( p*(p-l1.length()) * (p-l2.length()) *(p-l3.length()) );. r2 X7 K. w2 Q$ b5 ^/ i5 ?) s
cout<<"三角形的面积:"<<s<<endl;
" D, B* D3 I2 y) H* ^9 E }
% h; x7 k% d7 D4 E/ D, L. M9 C
% T6 J1 t7 k7 O. T7 s! E$ k catch (double); T! [& G# U1 s! v
{cout<<"经过异常处理,发现您所输入的坐标构成的3条线段长度不满足三角形";4 ~1 k2 W, S8 \: X+ j$ S+ c
}6 D4 I/ J( H, J7 ~8 O0 F
}//sett()& x6 D2 S, P' D4 j0 q! Q% f
protected:
7 m+ t; t0 B- v, \) o" e, C Line l1,l2,l3;
6 {$ o J: y _$ p# B float d1,d2,d3;
8 Z7 d! c& \" k+ D6 W1 b0 H};0 H& ~$ `- ?, \" ?
int main()
+ m' s( ?+ z) a/ r) M2 |( ]! ~! \{9 U( g3 \+ k# _' Y" a6 j
Triangle t1(0,0,1,1, 1,2,3,4 , 4,5,6,7);
4 h S$ z4 l( p try {/ p# C2 U" _& T5 F4 P
float whether=t1.delta();: f J) O: C8 V/ i
if(!whether) throw whether;3 ~6 q+ @, Y) W# r8 I6 C j
t1.sett();. F3 J, C) E) x4 g$ T
}, }3 j3 i9 W! g6 j+ x6 r& k
catch(float)2 p- F0 z/ K) [7 f' J: e$ b+ t3 k/ @: q! _
{cout<<"(0,0)(1,1),(1,2),(3,4),(4,5),(6,7)这几个点不可以构成三角形"<<endl;
9 }; a1 P* b( E: d }
; d4 R! d3 o& U$ J; G/ p. d return 0;
$ i( V, r% E5 V [: j}
/ _+ S" `0 K5 v5 p# K5 E |
|