首页 > 编程语言 >《python语言程序设计》2018版第7章第06题代数:平方根 设计一个名为QuadraticEquation类

《python语言程序设计》2018版第7章第06题代数:平方根 设计一个名为QuadraticEquation类

时间:2024-08-17 18:23:18浏览次数:10  
标签:06 get python self 2018 root QuadraticEquation def con

在这里插入图片描述

在这里插入图片描述

类代码部分

class QuadraticEquation:
    def __init__(self, a, b, c):
        self.a = a
        self.b = b
        self.c = c

    def set_a(self, a):
        self.a = a

    def get_a(self):
        return self.a

    def set_b(self, b):
        self.b = b

    def get_b(self):
        return self.b

    def set_c(self, c):
        self.c = c

    def get_c(self):
        return self.c

    def get_discrimination(self):
        return pow(self.b, 2) - (4 * self.a * self.c)

    def get_root1(self):
        root_num1 = pow(QuadraticEquation.get_discrimination(self), 0.5)
        root_num2 = -self.b + root_num1
        return root_num2 / (2 * self.a)

    def get_root2(self):
        root_num1 = pow(QuadraticEquation.get_discrimination(self), 0.5)
        root_num2 = -self.b - root_num1
        return root_num2 / (2 * self.a)

    def get_judge_num(self):
        a_num = QuadraticEquation.get_discrimination(self)
        b_num1 = QuadraticEquation.get_root1(self)
        b_num2 = QuadraticEquation.get_root2(self)

        if a_num == 0:
            return f"The root is {b_num1 or b_num2}"
        elif a_num > 0:
            return "The roots are {:.5f} and {:.5f}".format(b_num1, b_num2)
        else:
            return "The equation has no real roots"

main部分

def main():
    con_method1 = exCode07.QuadraticEquation(1, 3, 1)
    con_method2 = exCode07.QuadraticEquation(1, 2, 1)
    con_method3 = exCode07.QuadraticEquation(1, 2, 3)
    print(f"#1 method result is {con_method1.get_judge_num()}")
    print(f"#2 method result is {con_method2.get_judge_num()}")
    print(f"#3 method result is {con_method3.get_judge_num()}")


main()

顺便这个是4.1题的代码,只不过经过我多次学习用的

04.01 code


def roots(a, b, c):
    con_method = pow(b, 2) - (4 * a * c)

    r1 = (-b + pow(con_method, 0.5)) / (2 * a)
    r2 = (-b - pow(con_method, 0.5)) / (2 * a)
    if con_method == 0:
        print(f"The root is {r1 or r2}")
    elif con_method > 0:
        print("The roots are {:.5f} and {:.5f}".format(r1, r2))
    else:
        print("The equation has no real roots")

roots(1,3,1)
roots(1,2,1)
roots(1,2,3)

标签:06,get,python,self,2018,root,QuadraticEquation,def,con
From: https://blog.csdn.net/m0_37228426/article/details/141284705

相关文章

  • 《python语言程序设计》2018版第7章第05题几何:正n边形,一个正n边形的边都有同样的长度
    结果和代码这里只涉及一个办法方法部分defmain():rX,rY=eval(input("Enterregularpolygonxandyaxis:"))regular_num=eval(input("Enterregularnumber:"))side_long=eval(input("Entersidenumber:"))a=exCode07.Reg......
  • 利用Python实现供应链管理中的线性规划与资源优化——手机生产计划1
    目录写在开头1.Python与线性规划的基础2.供应链管理中的资源优化3.利用Python进行供应链资源优化3.1简单的优化实例3.2考虑多种原材料3.3多种原材料、交付时间与物流融合的情况4.规范性分析在供应链管理中的应用价值写在最后写在开头在全球供应链日益复杂的背景......
  • 使用 Python和 SQLite 打造一个简单的数据库浏览器
    在日常开发中,我们常常需要快速查看和操作SQLite数据库中的数据。虽然有许多现成的工具可以完成这一任务,但有时你可能想要一个更为简单、可定制的解决方案。在这篇博客中,我将带你一步步构建一个简单的SQLite数据库浏览器,它可以用来列出数据库中的表名、查看表的字段名、编写S......
  • 【Python】距离
    写了一个计算距离的脚本,常见距离基本都有。其中测地距离需要依赖曲面,Hausdorff距离之前有实现,而Wasserstei距离可以用sinkhorn方法求解。代码如下:importnumpyasnpdefEuclidean(a,b):returnnp.sqrt(np.sum((a-b)*(a-b)))defManhattan(a,b):returnnp.sum(n......
  • Python系列(5)- 命令行应用 (Command Line Application)
     使用Windows、iOS、Android、HarmonyOS等操作系统的设备,用户与这些设备主要通过图形用户界面(GUI)来交互,比如:鼠标、触屏等。一般用户很少使用这些系统的命令行界面(CLI),在Windows下是通过命令提示符(Cmd)窗口来实现CLI交互,其它系统通过终端(Terminal)窗口。 命令行界面(CLI......
  • Python导入包时提示“attempted relative import beyond top-level package”的解决办
    一篇很好的文章:python跨目录导包失败python不同路径导入包错误 在涉及到相对导入时,package所对应的文件夹必须正确的被python解释器视作package,而不是普通文件夹。否则由于不被视作package,无法利用package之间的嵌套关系实现python中包的相对导入。文件夹被python解释器视作......
  • 代码随想录day3 | LeetCode203. 移除链表元素、LeetCode707. 设计链表、LeetCode206.
    代码随想录day3|LeetCode203.移除链表元素、LeetCode707.设计链表、LeetCode206.反转链表为了防止早上写博客上传图片失败,今天试试下午写,发现图片上传正常链表基础文章链接:链表基础C/C++的定义链表节点方式,如下所示://单链表structListNode{intval;/......
  • Python入门之Lesson1:出发!
    目录前言一、Python简介二、环境搭建1.Python安装2.Pycharm安装三.运行总结前言本章会带领同学们了解和入门python。一、Python简介Python是一种高级编程语言,具有简洁明了的语法和丰富的库,非常适合初学者学习。Python的设计注重代码的可读性和简洁性,其语法类似......
  • 【Python系列】命令 • 合集
    文件传输Python2//将http:///FileName写入Path中python2-c"importurllib2;u=urllib2.urlopen('http:///FileName');f=open('Path','w');f.write(u.read());f.close()"//Python3//将http:///FileName写入Path中,这里要注意decode()中......
  • python安装
    下载安装python到官网下载最新版这里是3.12.5版本https://www.python.org/downloads/在安装Python时,系统环境变量的长度超出了操作系统所允许的限制,如下所示。环境变量通常用于存储系统或应用程序的配置信息,如路径等。如果这些变量的总长度超出了操作系统或文件系统所......