鱼C论坛

 找回密码
 立即注册
查看: 1519|回复: 14

求解答大炮方程,题目在附件

[复制链接]
发表于 2018-10-3 00:30:17 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
import math
g = 9.81
pi = math.pi

def get_distance(velocity:float, angle:float)-> float:
    """
    Calculates the distance a projectile travels on a flat surface
    given its intial velocity, as well as the angle of fire relative
    to the x-axis. The angle is given in radian. This function assumes
    perfect physics, i.e., constant gravity, no air resistance, etc.

    >>> get_distance(0, 1)
    0.0
    >>> get_distance(1, 0)
    0.0
    >>> get_distance(10, 0.25*pi)
    10.19367991845056
    """
    #Your code goes here

def degrees_to_radians(d:float)-> float:
    """
    Takes in an angle in degrees, d, and returns an equivalent
    angle in radians

    >>> degrees_to_radians(0)
    0.0
    >>> degrees_to_radians(180)
    3.141592653589793
    """
    #Your code goes here

def get_radian_of(angle_string: str)-> float:
    """
    Takes in a valid input angle_str and returns the numerical value of the
    angle in radians.

    Examples:
    >>> get_radian_of("1.2r")
    1.2
    >>> get_radian_of("45d")
    0.7853981633974483
    """
    #Your code goes here

def is_a_number(s:str)-> bool:
    """
    Returns True if and only if s is a string representing a positive number.

    Examples:
    >>> is_a_number("1")
    True
    >>> is_a_number("One")
    False
    >>> is_a_number("-3")
    False
    >>> is_a_number("3.")
    True
    >>> is_a_number("3.1.2")
    False
    """
    #Your code goes here

def is_valid_angle(s:str)-> bool:
    """
    Returns True if and only if s is a valid angle. See the assignment
    description and examples for more information regarding what's valid

    Examples:
    >>> is_valid_angle("85.3d")
    True
    >>> is_valid_angle("85.3.7D")
    False
    >>> is_valid_angle("90d")
    False
    >>> is_valid_angle("0.001r")
    True
    >>> is_valid_angle("1.5R")
    True
    """
    #Your code goes here

def approx_equal(x, y, tol):
    """
    Returns True if and only if x and y are with tol of each other.

    Examples:
    >>> approx_equal(1,2,1)
    True
    >>> approx_equal(4,3,1)
    True
    >>> approx_equal(4,3,0.99)
    False
    >>> approx_equal(-1.5,1.5,3)
    True
    """
    #Your code goes here
   


"""
DO NOT MODIFY THE CODE BELOW.

You are not required/expected to understand the following code.
If you're interested though, take a look.
"""
if __name__ == "__main__":
    while True:
        target = float(input("Enter a target distance: "))
        tol = float(input("Enter how close you need to be to your target: "))
        target_hit = False
        while not target_hit:
            valid_velocity = False
            while not valid_velocity:
                v = input("Enter a valid velocity: ")
                valid_velocity = is_a_number(v)   
            valid_angle = False
            v = float(v)
            while not valid_angle:
                theta = input("Enter a valid angle: ")
                valid_angle = is_valid_angle(theta)
            theta = get_radian_of(theta)
            d = get_distance(float(v), theta)
            target_hit = approx_equal(target, d, tol)
            if target_hit:
                print("Congratulations! You hit the target.")
            elif target > d:
                print("The shot hit short of the target, try again.")
            else:
                print("The shot hit past the target, try again.")
            
               

要求

要求

要求

要求
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2018-10-3 18:14:49 | 显示全部楼层

  1. import math
  2. g = 9.81
  3. pi = math.pi

  4. def get_distance(velocity:float, angle:float)-> float:
  5.     """
  6.     Calculates the distance a projectile travels on a flat surface
  7.     given its intial velocity, as well as the angle of fire relative
  8.     to the x-axis. The angle is given in radian. This function assumes
  9.     perfect physics, i.e., constant gravity, no air resistance, etc.

  10.     >>> get_distance(0, 1)
  11.     0.0
  12.     >>> get_distance(1, 0)
  13.     0.0
  14.     >>> get_distance(10, 0.25*pi)
  15.     10.19367991845056
  16.     """
  17.     #Your code goes here
  18.     return velocity**2 * math.sin(2*angle) / g

  19. def degrees_to_radians(d:float)-> float:
  20.     """
  21.     Takes in an angle in degrees, d, and returns an equivalent
  22.     angle in radians

  23.     >>> degrees_to_radians(0)
  24.     0.0
  25.     >>> degrees_to_radians(180)
  26.     3.141592653589793
  27.     """
  28.     #Your code goes here
  29.     return d/180*pi

  30. def get_radian_of(angle_string: str)-> float:
  31.     """
  32.     Takes in a valid input angle_str and returns the numerical value of the
  33.     angle in radians.

  34.     Examples:
  35.     >>> get_radian_of("1.2r")
  36.     1.2
  37.     >>> get_radian_of("45d")
  38.     0.7853981633974483
  39.     """
  40.     #Your code goes here
  41.     if angle_string[-1] in ['r','R']:
  42.         return angle_string[:-1]
  43.     else:
  44.         a = float(angle_string[:-1])
  45.         return degrees_to_radians(a)

  46. def is_a_number(s:str)-> bool:
  47.     """
  48.     Returns True if and only if s is a string representing a positive number.

  49.     Examples:
  50.     >>> is_a_number("1")
  51.     True
  52.     >>> is_a_number("One")
  53.     False
  54.     >>> is_a_number("-3")
  55.     False
  56.     >>> is_a_number("3.")
  57.     True
  58.     >>> is_a_number("3.1.2")
  59.     False
  60.     """
  61.     #Your code goes here
  62.     if s.isnumeric():
  63.         if float(s) >= 0:
  64.             return True
  65.         else:
  66.             return False
  67.     else:
  68.         return False

  69. def is_valid_angle(s:str)-> bool:
  70.     """
  71.     Returns True if and only if s is a valid angle. See the assignment
  72.     description and examples for more information regarding what's valid

  73.     Examples:
  74.     >>> is_valid_angle("85.3d")
  75.     True
  76.     >>> is_valid_angle("85.3.7D")
  77.     False
  78.     >>> is_valid_angle("90d")
  79.     False
  80.     >>> is_valid_angle("0.001r")
  81.     True
  82.     >>> is_valid_angle("1.5R")
  83.     True
  84.     """
  85.     #Your code goes here
  86.     if s[-1] not in ['d','D','r','R']:
  87.         return False
  88.     if s[-1] in ['d','D']:
  89.         try:
  90.             if float(s[:-1]) >= 0 and float(s[:-1]) <= 90:
  91.                 return True
  92.             else:
  93.                 return False
  94.         except:
  95.             return False
  96.     else:
  97.         try:
  98.             if float(s[:-1]) >= 0 and float(s[:-1]) <= pi/2:
  99.                 return True
  100.             else:
  101.                 return False
  102.         except:
  103.             return False

  104. def approx_equal(x, y, tol):
  105.     """
  106.     Returns True if and only if x and y are with tol of each other.

  107.     Examples:
  108.     >>> approx_equal(1,2,1)
  109.     True
  110.     >>> approx_equal(4,3,1)
  111.     True
  112.     >>> approx_equal(4,3,0.99)
  113.     False
  114.     >>> approx_equal(-1.5,1.5,3)
  115.     True
  116.     """
  117.     #Your code goes here
  118.     if math.fabs(x-y) < tol:
  119.         return True
  120.     else:
  121.         return False
  122.    


  123. """
  124. DO NOT MODIFY THE CODE BELOW.

  125. You are not required/expected to understand the following code.
  126. If you're interested though, take a look.
  127. """
  128. if __name__ == "__main__":
  129.     while True:
  130.         target = float(input("Enter a target distance: "))
  131.         tol = float(input("Enter how close you need to be to your target: "))
  132.         target_hit = False
  133.         while not target_hit:
  134.             valid_velocity = False
  135.             while not valid_velocity:
  136.                 v = input("Enter a valid velocity: ")
  137.                 valid_velocity = is_a_number(v)   
  138.             valid_angle = False
  139.             v = float(v)
  140.             while not valid_angle:
  141.                 theta = input("Enter a valid angle: ")
  142.                 valid_angle = is_valid_angle(theta)
  143.             theta = get_radian_of(theta)
  144.             d = get_distance(float(v), theta)
  145.             target_hit = approx_equal(target, d, tol)
  146.             if target_hit:
  147.                 print("Congratulations! You hit the target.")
  148.             elif target > d:
  149.                 print("The shot hit short of the target, try again.")
  150.             else:
  151.                 print("The shot hit past the target, try again.")
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-10-3 20:44:49 | 显示全部楼层

为什么你这么优秀!我看到英文就不想看题了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-10-3 21:04:45 | 显示全部楼层
RIXO 发表于 2018-10-3 20:44
为什么你这么优秀!我看到英文就不想看题了

不知道哥哥在说什么,把几行代码填上去而已
我的编程能力还很差,真的 还写不出我要的东西
优秀的人应该是回答那个迷宫题,可是我想应该没有人想回答吧~ 给个悬赏还差不多,那位姐姐还有三个礼拜的时间。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-10-3 21:18:17 | 显示全部楼层
claws0n 发表于 2018-10-3 21:04
不知道哥哥在说什么,把几行代码填上去而已
我的编程能力还很差,真的 还写不出我要的 ...

已赚到然而写的很蠢
最近好多刷外网题的啊。。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-10-3 21:28:23 | 显示全部楼层
塔利班 发表于 2018-10-3 21:18
已赚到然而写的很蠢
最近好多刷外网题的啊。。

200大元,被强悍的恐怖份子扫了
哥哥,没看到答案
跟楼主一样,都是作业吧,不然怎么会有日期?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-10-3 21:37:24 | 显示全部楼层
claws0n 发表于 2018-10-3 21:28
200大元,被强悍的恐怖份子扫了
哥哥,没看到答案
跟楼主一样,都是作业吧,不然怎么会有日 ...

恩,思路按雪冬版主马走日那篇基本就做出来了,代码甲方不方便放
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-10-3 22:22:21 | 显示全部楼层
塔利班 发表于 2018-10-3 21:37
恩,思路按雪冬版主马走日那篇基本就做出来了,代码甲方不方便放

写那个迷宫代码其实不难,但是题目要求 show,把迷宫给画出来,好像不一般
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-10-4 08:44:24 From FishC Mobile | 显示全部楼层
真的很难。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-11-6 22:57:50 | 显示全部楼层
这个帖子已经被教授盯上了 兄dei
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-11-6 22:58:31 | 显示全部楼层
alexorange178 发表于 2018-11-6 22:57
这个帖子已经被教授盯上了 兄dei

big brother is watching you
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-11-7 00:08:00 | 显示全部楼层
老哥你是真的nb,要不我帮你把A2也发上来
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-11-7 09:18:12 | 显示全部楼层
过来顶一下
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-11-8 01:32:06 | 显示全部楼层
Screen Shot 2018-11-07 at 12.29.45.png
UT警告(手动滑稽)
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-11-8 09:19:32 | 显示全部楼层
过来顶一下
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-7-6 04:37

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表