Python語言程序設(shè)計 封面.jpeg
編程題3.1一個五邊形的面積
import turtle
import math
r = eval(input("請輸入五邊形的邊長:"))
s = 2 * r * math.sin(math.pi / 5)
area = 5 * s * s / (4 * math.tan(math.pi / 5))
turtle.circle(r, 360, 5)
print("area=", round(area, 2))
turtle.done()
編程題3.2大圓距離
import math
x1, y1 = eval(input("請輸入地球上A點(diǎn)的經(jīng)緯度 x1,y1:"))
x2, y2 = eval(input("請輸入地球上B點(diǎn)的經(jīng)緯度 x2,y2:"))
radius = 6371.01
# 使用math.radians()將度數(shù)轉(zhuǎn)化為弧度數(shù)
x1 = math.radians(x1)
y1 = math.radians(y1)
x2 = math.radians(x2)
y2 = math.radians(y2)
d = radius * math.acos(math.sin(x1) * math.sin(x2) +
math.cos(x1) * math.cos(x2) * math.cos(y1 - y2))
print("The distance between the two points is {:.2f} km".format(d))
編程題3.3面積估算
'''
地圖網(wǎng)站 www.gps-data-team.com/map
68923,Atlanta NE in US,GPS Position: 99°24'18"W, 40°24'37"N for x1,y1
73073,Orlando OK in US,GPS Position: 97°24'29"W, 35°56'40"N for x2,y2
36033,Georgiana AL in US,GPS Position: 86°38'5"W, 31°37'28"N for x3,y3
78.11,Charlotte TX in US,GPS Position: 98°39'20"W, 28°48'51"N for x4,y4
'''
import math
# 經(jīng)緯度坐標(biāo)值拐辽,轉(zhuǎn)化為數(shù)值形式
x1,y1=99.405,40.410
x2,y2=97.408,35.944
x3,y3=86.634,31.624
x4,y4=98.656,28.814
# 經(jīng)緯度轉(zhuǎn)化為弧度數(shù)
x1,y1= math.radians(x1),math.radians(y1)
x2,y2 = math.radians(x2),math.radians(y2)
x3,y3 = math.radians(x3),math.radians(y3)
x4,y4 = math.radians(x4),math.radians(y4)
radius = 6371.01
# 計算城市間距離
dAO=radius * math.acos(math.sin(x1) * math.sin(x2) +
math.cos(x1) * math.cos(x2) * math.cos(y1 - y2))
dAG=radius * math.acos(math.sin(x1) * math.sin(x3) +
math.cos(x1) * math.cos(x3) * math.cos(y1 - y3))
dAC=radius * math.acos(math.sin(x1) * math.sin(x4) +
math.cos(x1) * math.cos(x4) * math.cos(y1 - y4))
dOG=radius * math.acos(math.sin(x3) * math.sin(x2) +
math.cos(x3) * math.cos(x2) * math.cos(y3 - y2))
dCG=radius * math.acos(math.sin(x3) * math.sin(x4) +
math.cos(x3) * math.cos(x4) * math.cos(y3 - y4))
# 計算三角形的面積器躏,ppt p63 面積公式
sAOG=(dAO+dAG+dOG)/2
sACG=(dAC+dAG+dCG)/2
areaAOG=math.sqrt(sAOG*(sAOG-dAG)*(sAOG-dAO)*(sAOG-dOG))
areaACG=math.sqrt(sACG*(sACG-dAG)*(sACG-dAC)*(sACG-dCG))
areaTotal=areaAOG+areaACG
# 輸出結(jié)果
print("areaTotal = ",areaTotal)
編程題3.4五角形的面積
import math
s = eval(input("請輸入五角形的邊長 s = "))
area = 5 * math.pow(s, 2) / (4 * math.tan(math.pi / 5)) # 注意 / 符號后面 ()艺骂,否則計算次序會錯
print("area = ", area)
編程題3.5一個正多邊形的面積
import math
n, s = eval(input("請分別輸入正多邊形的邊數(shù)n留瞳,邊長s:"))
area = n * math.pow(s, 2) / (4 * math.tan(math.pi / n))
print("邊長{},正{}邊形的面積是 {:.2f}".format(s,n,area))
編程題3.6找出ASCII碼的字符
x = eval(input("請輸入0-127的ASCII碼值:"))
print(chr(x))
編程題3.7大寫的隨機(jī)字符
# 大寫的隨機(jī)字符,對應(yīng)的數(shù)字65~90
import time
a = int(time.time()) % 26 + 65
b = int(time.time()) % 26 + 97
print(chr(a)) # 隨機(jī)大寫肾筐,解題關(guān)鍵是確定 A~Z 的 ASCII 范圍
print(chr(b)) # 隨機(jī)小寫
編程題3.9工資表
name = input("Enter emplyee's name:")
hoursWorked = eval(input("Enter number of hours worked in a week:"))
payRate = eval(input("Enter hourly pay rate:"))
ftRate = eval(input("Enter federal tax withholding rate:"))
stRate = eval(input("Enter state tax withholding rate:"))
grossPay = hoursWorked * payRate
federalWithholding = hoursWorked * payRate * ftRate
stateWithholding = hoursWorked * payRate * stRate
totalDeduction = federalWithholding + stateWithholding
netPay = grossPay * (1 - ftRate - stRate)
print()
print(
"Employee Name: {}\nHours Worked: {:.1f}\nPay Rate:${:.1f}\nGross Pay: ${:.1f}".format(
name,
hoursWorked,
payRate,
grossPay))
print()
print(
"Decutions:\n Federal Withholding (20.0%):${:.1f}\n State Withholding (9.0%):${:.2f}\n Total Deduction:${:.2f}".format(
federalWithholding,
stateWithholding,
totalDeduction))
print()
print("Net Pay:$ {:.2f} ".format(netPay))
編程題3.10顯示統(tǒng)一碼
print("\u03b1 \u03b2 \u03b3 \u03b4 \u03b5 \u03b6 \u03b7 \u03b8")
編程題3.11反向數(shù)字
num = eval(input("請輸入一串整數(shù):"))
print(str(num)[::-1]) # 切片倒序打印
暫未更新3.8慷吊,其他暫未更新