
求Python大神帮助解答 貌似要写程序 关于三角函数面积
PartC-AreaofaTriangleWriteaprogramthatpromptstheuserforthreesidedimensions,asfloats,w...
Part C - Area of a Triangle
Write a program that prompts the user for three side dimensions, as
floats, which then outputs the area of a triangle made up from these
three sides. The side dimensions, a, b and c, must not only be greater
than zero, but they must satisfy what is called the "Triangle
Inequality
Theorem" which is a fancy name for the rule that states: "The sum of two
sides must add up to be greater than the length of the remaining third
side." In terms of the side lengths, these statements must be
satisfied:
a + b > c
b + c > a
a + c > b
The area of any triangle
can be calculated using what is commonly called "Heron's Rule" after Heron
of Alexandria, even though the theorem was first developed by Archimedes.
Given the three sides, a, b and c:
s = (a + b + c)/2
area = sqrt(s(s - a)(s - b)(s - c))
Use the math.sqrt() function to calculate the square root. To use this function in the math module, you will need to place the statment import math at the top of your program before your def main() : line. Limit the output decimal places to two digits after the decimal point. 展开
Write a program that prompts the user for three side dimensions, as
floats, which then outputs the area of a triangle made up from these
three sides. The side dimensions, a, b and c, must not only be greater
than zero, but they must satisfy what is called the "Triangle
Inequality
Theorem" which is a fancy name for the rule that states: "The sum of two
sides must add up to be greater than the length of the remaining third
side." In terms of the side lengths, these statements must be
satisfied:
a + b > c
b + c > a
a + c > b
The area of any triangle
can be calculated using what is commonly called "Heron's Rule" after Heron
of Alexandria, even though the theorem was first developed by Archimedes.
Given the three sides, a, b and c:
s = (a + b + c)/2
area = sqrt(s(s - a)(s - b)(s - c))
Use the math.sqrt() function to calculate the square root. To use this function in the math module, you will need to place the statment import math at the top of your program before your def main() : line. Limit the output decimal places to two digits after the decimal point. 展开
2个回答
展开全部
程序已经帮您写好,下面是按照要求的程序,因为没有说明如果构不成三角形应该输出什么,所以暂时先输出This is an invalid triangle这样的字样,如果有明确的要求,可以更改。
import math
a,b,c=raw_input("Please enter length of three sides: ").split()
a=float(a)
b=float(b)
c=float(c)
if a<=0 or b<=0 or c<=0:
print "This is an invalid triangle."
elif a+b<=c or a+c<=b or b+c<=a:
print "This is an invalid triangle."
else:
s=(a+b+c)/2
area=math.sqrt(s*(s-a)*(s-b)*(s-c))
print "%.2f" % area
经过一系列测试,这个程序是完全可行的,用的是Python 2.7.3
若不懂,请追问,望采纳!
import math
a,b,c=raw_input("Please enter length of three sides: ").split()
a=float(a)
b=float(b)
c=float(c)
if a<=0 or b<=0 or c<=0:
print "This is an invalid triangle."
elif a+b<=c or a+c<=b or b+c<=a:
print "This is an invalid triangle."
else:
s=(a+b+c)/2
area=math.sqrt(s*(s-a)*(s-b)*(s-c))
print "%.2f" % area
经过一系列测试,这个程序是完全可行的,用的是Python 2.7.3
若不懂,请追问,望采纳!
展开全部
import sys
import math
if len(sys.argv)<4:
print "you must input three side of a triangle"
sys.exit(-1)
try:
a,b,c=float(sys.argv[1]),float(sys.argv[2]),float(sys.argv[3])
if a+b>c and b+c>a and c+a>b and a>0 and b>0 and c>0:
s=(a+b+c)/2
area=math.sqrt(s*(s-a)*(s-b)*(s-c))
print "area is:",area
else:
print "the side can't match the rule"
except Exception as e:
print "we encount error:",e
import math
if len(sys.argv)<4:
print "you must input three side of a triangle"
sys.exit(-1)
try:
a,b,c=float(sys.argv[1]),float(sys.argv[2]),float(sys.argv[3])
if a+b>c and b+c>a and c+a>b and a>0 and b>0 and c>0:
s=(a+b+c)/2
area=math.sqrt(s*(s-a)*(s-b)*(s-c))
print "area is:",area
else:
print "the side can't match the rule"
except Exception as e:
print "we encount error:",e
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |