python语言程序设计_梁勇—第三章练习题答案及相关知识点总结

1、几何学:一个五边形的面积 编写一个程序,提示用户输入五边形顶点到中心的距离r,然后算出五边形的面积

import math
def calculate_of_pentagonal_area():
    r = eval(input("Enter the length from the center to a vertex:"))
    s = 2 * r * math.sin(math.pi/5)
    area = 5 * s * s/(4 * math.tan(math.pi/5))
    print("The area of the pentagon is %.2f"%area)

2、几何学:大圆的距离 大圆距离指的是球面上两点的距离,编写一个程序,提示用户输入地球表面两点维度,地球的平均半径为6371.01km

def calculate_of_spherical_distance():
    x1,y1 = eval(input("Enter point 1 (latitude and longtitude)in degrees:"))
    x2,y2 = eval(input("Enter point 2 (latitude and longtitude)in degrees:"))
    radius = 6371.01
    d = radius * math.acos(math.sin(math.radians(x1)) * math.sin(math.radians(x2)) +
                           math.cos(math.radians(x1)) * math.cos(math.radians(x2)) * math.cos(math.radians(y1-y2)))
    print("The distance between the two points is %f"%d)

3、找出ASCII字符,接收一个ASCII码值(1-127),然后显示它对应的字符,

def find_real():
    asc = eval(input("Enter an ASCII code: "))
    ch = str(input("Enter the char number:"))
    real = chr(asc)
    char = ord(ch)
    print("The character is %s"%real)
    print("The ASC_num is %d"%char)

4、金融应用程序,工资表,读取下面信息,然后打印一个工资报表,雇员姓名,一周工作时间,每小时报酬,联邦预扣税,州预扣税率。

def print_salary():
    name = str(input("Enter enployee's name:"))
    work_wime = eval(input("Enter number of hours worked in a week :"))
    pay_rate = eval(input("Enter hourly pay rate: "))
    federal_tax = eval(input("Enter federal tax withholding rate:"))
    tax_rate = eval(input("Enter state tax withholding rate:"))
    gross_pay = work_wime * pay_rate
    federal_without_holding = federal_tax * gross_pay
    state_withoutholding = gross_pay * tax_rate
    total_deduction = federal_without_holding + state_withoutholding
    net_pay = gross_pay - total_deduction
    print("Emplyee Name:%s" % name, "Hours Worked:%.3f" % work_wime, "Pay Rate:%.3f" % pay_rate,
          "Gross Pay:%.3f" % gross_pay, sep="\n")
    print("Deductions:")
    print("   Federal Withoutholding (20.0):%.3f" % federal_without_holding,
          "    State withoutholding(9.0):%.3f" % state_withoutholding, "Total Deduction:%.3f" % total_deduction,
          "Net Pay:%.3f" % net_pay, sep="\n")

if __name__ == '__main__':
    print_salary()

常见的Python函数

abs(x) 返回x的绝对值
max(x1,x2,…) 返回最大值
min(x1,x2,…) 返回最小值
pow(a,b) 返回a^b的值
round(x) 返回与x最接近的整数,如果x与两个整数接近程度相同,则返回偶数值
round(x,n) 保留小数点n位小数点的浮点值

##数学函数

fabs(x) 将x看作一个浮点数,返回它的绝对值
ceil(x) x向上取最近的整数,然后返回这个整数
floor(x) x向下取最近的整数,然后返回这个整数
exp(x) 返回幂函数e^x 的值
log(x,base) 返回以某个特殊值为底的x的特殊值

你可能感兴趣的:(python)