class Graph: def getArea(self): pass def getPerimeter(self): pass def info(self): pass class Rectangle(Graph): def __init__(self, width, height): self.width = width self.height = height def getArea(self): return self.width * self.height def getPerimeter(self): return 2 * (self.width + self.height) def info(self): print("矩形长为:", self.width) print("矩形宽为:", self.height) print("矩形周长为:", self.getPerimeter()) print("矩形面积为:", self.getArea()) class Circle(Graph): def __init__(self, radius): self.radius = radius def getArea(self): return 3.14 * self.radius ** 2 def getPerimeter(self): return 2 * 3.14 * self.radius def info(self): print("矩形长为:", self.width) print("矩形宽为:", self.height) print("矩形周长为:", self.getPerimeter()) print("矩形面积为:", self.getArea()) def countandPrint(inst): inst.info() R1 = Rectangle(90, 80) C1 = Circle(120) countandPrint(R1) countandPrint(C1)这段编程有什么问题
时间: 2024-02-28 18:53:22 浏览: 83
这段代码有一个问题,因为在 Circle 类的 info 方法中,代码打印了矩形的长和宽,而不是圆的半径和直径。正确的代码应该是:
```
class Circle(Graph):
def __init__(self, radius):
self.radius = radius
def getArea(self):
return 3.14 * self.radius ** 2
def getPerimeter(self):
return 2 * 3.14 * self.radius
def info(self):
print("圆的半径为:", self.radius)
print("圆的直径为:", self.radius * 2)
print("圆的周长为:", self.getPerimeter())
print("圆的面积为:", self.getArea())
```
相关问题
在图的邻接表存储结构下(基于顶点列表和单链表实现),本题要求图类里实现2个方法函数 def addVertex(self, vex_val): def addEdge(self, f, t, cost=0): 函数接口定义: 在这里描述函数接口。例如: def addVertex(self, vex_val): def addEdge(self, f, t, cost=0): 在这里解释接口参数。例如:其中 f和t分别是构成边的顶点在列表中的序号。 裁判测试程序样例: 在这里给出函数被调用进行测试的例子。例如: class arcnode: def __init__(self,adjvex,weight,link=None): self.adjvex = adjvex self.weight = weight self.link=link class vexnode: def __init__(self,data,first_arc=None): self.data = data self.first_arc = first_arc class Graph: def __init__(self): self.vex_list=[] self.vex_num=0 self.edge_num=0 # 请在这里填写答案 # 请在这里填写答案 def print_graph(self): for i in range(self.vex_num): print(self.vex_list[i].data,end="->") cur = self.vex_list[i].first_arc while cur: print("adj:{},weight:{}".format(cur.adjvex,cur.weight),end="->") cur = cur.link print('None') if __name__ =="__main__": g = Graph() s =input() for vertex in s: g.addVertex(vertex) g.addEdge(0,1,11) g.addEdge(0,2,55) g.addEdge(2,3,88) g.addEdge(0,3,33) g.addEdge(1,2,44) g.print_graph()
class arcnode:
def __init__(self, adjvex, weight, link=None):
self.adjvex = adjvex
self.weight = weight
self.link = link
class vexnode:
def __init__(self, data, first_arc=None):
self.data = data
self.first_arc = first_arc
class Graph:
def __init__(self):
self.vex_list = []
self.vex_num = 0
self.edge_num = 0
def addVertex(self, vex_val):
self.vex_list.append(vexnode(vex_val))
self.vex_num += 1
def addEdge(self, f, t, cost=0):
node = arcnode(t, cost)
node.link = self.vex_list[f].first_arc
self.vex_list[f].first_arc = node
self.edge_num += 1
def print_graph(self):
for i in range(self.vex_num):
print(self.vex_list[i].data, end="->")
cur = self.vex_list[i].first_arc
while cur:
print("adj:{},weight:{}".format(cur.adjvex, cur.weight), end="->")
cur = cur.link
print('None')
if __name__ == "__main__":
g = Graph()
s = input()
for vertex in s:
g.addVertex(vertex)
g.addEdge(0, 1, 11)
g.addEdge(0, 2, 55)
g.addEdge(2, 3, 88)
g.addEdge(0, 3, 33)
g.addEdge(1, 2, 44)
g.print_graph()
按照如下定义给G=Graph()添加边举例子class Graph: def __init__(self): self.vertices = {} self.numVertices = 0 # 初始图 顶点0 def addVertex(self, key): # add顶点 self.numVertices = self.numVertices + 1 newVertex = Vertex(key) self.vertices[key] = newVertex # 顶点对应属性 return newVertex def getVertex(self, key): if key in self.vertices: return self.vertices[key] else: return None def __contains__(self, key): # check判断是否存在 return key in self.vertices def addEdge(self, f, t, cost): # 添加边 if f not in self.vertices: nv = self.addVertex(f) # 顶点未在图的顶点集先加入 if t not in self.vertices: nv = self.addVertex(t) self.vertices[f].addNeighbor(self.vertices[t], cost) # 添加邻接点
好的,假设我们要给这个图添加一条从顶点0到顶点1的边,边权为2,代码如下:
```
G = Graph()
G.addVertex(0)
G.addVertex(1)
G.addEdge(0, 1, 2)
```
这样就在图中添加了一条从顶点0到顶点1的边,边权为2。
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![gz](https://img-home.csdnimg.cn/images/20210720083447.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)