2、Triangle.cpp
1. /* 2. * Copyright (c) 2008, 胡添发(hutianfa@163.com) 3. * 4. * 三角形类型判断 5. * 6. */ 7. 8. #include<stdio.h> 9. #include<String.h> 10. 11. /* 12. * 判断一个整数是否在(0, 200)区间内 13. * 返回值:true-否; false-是 14. */ 15. bool isOutOfRange(int i) 16. { 17. if(0<i && i<200) 18. { 19. return false; 20. } 21. else 22. { 23. return true; 24. } 25. }; 26. 27. /* 28. * 判断三条边是否合法(即:判断三条边都在合法的范围内) 29. * 返回值:true-是; false-否 30. */ 31. bool isLegal(int a, int b, int c) 32. { 33. if(isOutOfRange(a) || isOutOfRange(b) || isOutOfRange(c)) 34. { 35. return false; 36. } 37. return true; 38. } 39. 40. /* 41. * 判断两条边之和是否大于第三边 42. * 返回值:true-是; false-否 43. */ 44. bool isSumBiger(int a, int b, int c) 45. { 46. if(a+b > c) 47. { 48. return true; 49. } 50. return false; 51. } 52. 53. /* 54. * 判断三条边是否能够组成三角形 55. * 返回值:true-是; false-否 56. */ 57. bool isTriangle(int a, int b, int c) 58. { 59. if(isSumBiger(a, b, c) && isSumBiger(a, c, b) && isSumBiger(b, c, a)) 60. { 61. return true; 62. } 63. return false; 64. } 65. 66. /* 67. * 判断两条边是否相等 68. * 返回值:true-是; false-否 69. */ 70. bool isEquals(int a, int b) 71. { 72. if(a==b) 73. { 74. return true; 75. } 76. return false; 77. } 78. 79. /* 80. * 求三角形有几条边相等 81. * 返回值:相等边的数量 82. * 1:没有边相等 2:只有两条边相等 3:三条边相等 83. * 84. */ 85. int howManyEquals(int a, int b, int c) 86. { 87. int count = 1; 88. if(isEquals(a,b)) 89. { 90. count++; 91. } 92. if(isEquals(b,c)) 93. { 94. count++; 95. } 96. if(isEquals(a,c)) 97. { 98. count++; 99. } 100. if(count > 3) //如果三条边都相等,则count多加了一次 101. { 102. count = 3; 103. } 104. 105. return count; 106. } 107. 108. /* 109. * 判断是否满足两边平方之和是否等于第三边的平方 110. * 111. */ 112. bool isPowerSumEquals(int a, int b, int c) 113. { 114. if(a*a + b*b == c*c) 115. { 116. return true; 117. } 118. return false; 119. } 120. 121. /* 122. * 判断第一个数是否比第二个数大 123. */ 124. bool isGreaterThan(int a, int b) 125. { 126. if(a>b) 127. { 128. return true; 129. } 130. return false; 131. } 132. 133. /* 134. * 判断是否是直角三角形 135. * 136. */ 137. bool isRightRriangle(int a, int b, int c) 138. { 139. int max = 0; 140. if(isGreaterThan(a, b)) 141. { 142. max = a; 143. a = b; 144. b = max; 145. } 146. if(isGreaterThan(b, c)) 147. { 148. max = b; 149. b = c; 150. c = max; 151. } 152. return isPowerSumEquals(a, b, c); 153. } 154. 155. /* 156. * 判断三角形的类型,返回值: 157. * 1、不能组成三角形 158. * 2、等边三角形 159. * 3、等腰三角形 160. * 4、直角三角形 161. * 5、一般三角形 162. * 6、某些边不满足限制 163. */ 164. int triangleType(int a, int b, int c) 165. { 166. int type=0; 167. if(isLegal(a, b, c)) 168. { 169. if(isTriangle(a, b, c)) 170. { 171. int num = howManyEquals(a, b, c); 172. if(3==num) 173. { 174. type=2; 175. } 176. else if(2==num) 177. { 178. type=3; 179. } 180. else if(isRightRriangle(a, b, c)) 181. { 182. type=4; 183. } 184. else 185. { 186. type=5; 187. } 188. } 189. else 190. { 191. type=1; 192. } 193. } 194. else 195. { 196. type=6; 197. } 198. return type; 199. } |
相关阅读: