当前位置: 首页 > news >正文

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

在这里插入图片描述

在这里插入图片描述

类代码部分

class QuadraticEquation:def __init__(self, a, b, c):self.a = aself.b = bself.c = cdef set_a(self, a):self.a = adef get_a(self):return self.adef set_b(self, b):self.b = bdef get_b(self):return self.bdef set_c(self, c):self.c = cdef get_c(self):return self.cdef 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_num1return root_num2 / (2 * self.a)def get_root2(self):root_num1 = pow(QuadraticEquation.get_discrimination(self), 0.5)root_num2 = -self.b - root_num1return 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 codedef 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)

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • Verilog刷题笔记54
  • PowerShell 一键配置IP
  • TMS核心架构与功能模块解析
  • Java-一个接口查询没有值,需要多次调用直到有值,怎么解决?
  • python——数据分析
  • 短视频SDK解决方案,Flutter赋能,跨平台无缝体验
  • Docker私人学习笔记
  • python——concurrent.futures
  • SAP凭证分割【Document Splitting】
  • wsl2 airsim wairing for connect (Windows11 UE4.27)问题解决
  • 惠海H4312 dcdc同步整流降压恒压IC 30V 40V转3.3V/5V/12V小体积大电流单片机供电
  • Zimg:轻量级图片服务器的高性能解决方案
  • C# Stack
  • Spring的核心启动流程
  • yas源码解析 之 hexdump打印二进制
  • 《Java编程思想》读书笔记-对象导论
  • CSS中外联样式表代表的含义
  • JAVA SE 6 GC调优笔记
  • Java程序员幽默爆笑锦集
  • JS创建对象模式及其对象原型链探究(一):Object模式
  • linux安装openssl、swoole等扩展的具体步骤
  • markdown编辑器简评
  • quasar-framework cnodejs社区
  • ReactNative开发常用的三方模块
  • Redis中的lru算法实现
  • TypeScript迭代器
  • 从零搭建Koa2 Server
  • 工程优化暨babel升级小记
  • 码农张的Bug人生 - 见面之礼
  • 前端技术周刊 2019-01-14:客户端存储
  • 悄悄地说一个bug
  • 区块链将重新定义世界
  • 如何借助 NoSQL 提高 JPA 应用性能
  • 软件开发学习的5大技巧,你知道吗?
  • 用简单代码看卷积组块发展
  • 正则学习笔记
  • NLPIR智能语义技术让大数据挖掘更简单
  • 摩拜创始人胡玮炜也彻底离开了,共享单车行业还有未来吗? ...
  • 如何用纯 CSS 创作一个菱形 loader 动画
  • 资深实践篇 | 基于Kubernetes 1.61的Kubernetes Scheduler 调度详解 ...
  • ​Linux Ubuntu环境下使用docker构建spark运行环境(超级详细)
  • ​软考-高级-信息系统项目管理师教程 第四版【第19章-配置与变更管理-思维导图】​
  • #pragma once与条件编译
  • (考研湖科大教书匠计算机网络)第一章概述-第五节1:计算机网络体系结构之分层思想和举例
  • (七)Flink Watermark
  • (十八)Flink CEP 详解
  • (数位dp) 算法竞赛入门到进阶 书本题集
  • (转)Linux NTP配置详解 (Network Time Protocol)
  • (转)淘淘商城系列——使用Spring来管理Redis单机版和集群版
  • *算法训练(leetcode)第四十天 | 647. 回文子串、516. 最长回文子序列
  • .[hudsonL@cock.li].mkp勒索病毒数据怎么处理|数据解密恢复
  • .NET 4 并行(多核)“.NET研究”编程系列之二 从Task开始
  • .net dataexcel 脚本公式 函数源码
  • .net Stream篇(六)
  • .NET 中什么样的类是可使用 await 异步等待的?