|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
代码运行不了,给出来的是对象没有属性
要求是:已知两个稀疏矩阵A和B,试基于三元组顺序表或十字链表的存储;链表编程实现A+B的运算
- class TripleNode(object):
- def __init__(self,row=0,column=0,value=0):
- self.row = row
- self.column = column
- self.value = value
- class SparseMatrix(object):
- def __init__(self,maxSize):
- self.maxSize = maxSize
- self.data = [None] * self.maxSize #三元组表
- for i in range(self.maxSize):
- self.data[i] = TripleNode()
- self.rows = 0 #行数
- self.cols = 0 #列数
- self.nums = 0 #非零元素个数
- def create(self,mat):
- count =k = 0
- self.rows = len(mat)
- self.cols = len(mat[0])
- for i in range(self.rows):
- for j in range(self.cols):
- if mat[i][j] !=0:
- count +=1
- self.num = count
- self.data = [None] * self.nums
- for i in range(self.rows):
- for j in range(self.cols):
- if mat[i][j] !=0:
- self.data[k] = TripleNode(i,j,mat[i][j])
- k +=1
- def display(self):
- for t in range(self.nums):
- print('(row:%s,column:%s,value:%s)' % (self.data[t].row,self.data[t].column,self.data[t].value),end=' ')
- print('/n')
- def ms_add(self,mat1,mat2):
- k = count1=count2=0
- self.rows = len(mat1)
- self.cols = len(mat1[0])
- for i in range(self.rows):
- for j in range(self.cols):
- if mat1[i][j] !=0:
- count1 += 1
- if mat2[i][j] !=0:
- count2 +=1
- self.nums = count1+count2
- self.data = [None] * self.nums
- for i in range(self.rows):
- for j in range(self.cols):
- if mat1[i][j] != 0 and mat2[i][j] == 0:
- self.data[k] = TripleNode(i,j,mat1[i][j])
- k+=1
- elif mat1[i][j] == 0 and mat2[i][j] != 0:
- self.data[k] = TripleNode(i,j,mat2[i][j])
- k+=1
- elif mat1[i][j] != 0 and mat2[i][j] != 0:
- self.data[k] = TripleNode(i,j,mat1[i][j]+mat2[i][j])
- k+=1
- self.nums=k
- M1 = [
- [0,1,1],
- [0,0,0],
- [0,1,0]
- ]
- M2 = [
- [0,1,1],
- [0,0,0],
- [0,1,0]
- ]
- # print(len(M1))
- # print(len(M1[0]))
- B1=SparseMatrix(100)
- B1.create(M1)
- B2=SparseMatrix(100)
- B2.create(M2)
- B3=SparseMatrix(100)
- B3.ms_add(M1,M2)
- print("矩阵A的三元组: ")
- B1.dispaly()
- print("矩阵B的三元组: ")
- B2.dispaly()
- print("矩阵A和矩阵B相加后的矩阵C的三元组: ")
- B3.dispaly()
- # B1.dispaly()
- # B2.dispaly()
复制代码
本帖最后由 isdkz 于 2023-4-3 10:44 编辑
三个问题:
1、create方法缩进错误
2、第25行的self.num应改为self.nums
3、第91、93、95行的dispaly 应改为 self.display
修正后的代码:
- class TripleNode(object):
- def __init__(self,row=0,column=0,value=0):
- self.row = row
- self.column = column
- self.value = value
- class SparseMatrix(object):
- def __init__(self,maxSize):
- self.maxSize = maxSize
- self.data = [None] * self.maxSize #三元组表
- for i in range(self.maxSize):
- self.data[i] = TripleNode()
- self.rows = 0 #行数
- self.cols = 0 #列数
- self.nums = 0 #非零元素个数
- def create(self,mat):
- count =k = 0
- self.rows = len(mat)
- self.cols = len(mat[0])
- for i in range(self.rows):
- for j in range(self.cols):
- if mat[i][j] !=0:
- count +=1
- self.nums = count
- self.data = [None] * self.nums
- for i in range(self.rows):
- for j in range(self.cols):
- if mat[i][j] !=0:
- self.data[k] = TripleNode(i,j,mat[i][j])
- k +=1
- def display(self):
- for t in range(self.nums):
- print('(row:%s,column:%s,value:%s)' % (self.data[t].row,self.data[t].column,self.data[t].value),end=' ')
- print('/n')
- def ms_add(self,mat1,mat2):
- k = count1=count2=0
- self.rows = len(mat1)
- self.cols = len(mat1[0])
- for i in range(self.rows):
- for j in range(self.cols):
- if mat1[i][j] !=0:
- count1 += 1
- if mat2[i][j] !=0:
- count2 +=1
- self.nums = count1+count2
- self.data = [None] * self.nums
- for i in range(self.rows):
- for j in range(self.cols):
- if mat1[i][j] != 0 and mat2[i][j] == 0:
- self.data[k] = TripleNode(i,j,mat1[i][j])
- k+=1
- elif mat1[i][j] == 0 and mat2[i][j] != 0:
- self.data[k] = TripleNode(i,j,mat2[i][j])
- k+=1
- elif mat1[i][j] != 0 and mat2[i][j] != 0:
- self.data[k] = TripleNode(i,j,mat1[i][j]+mat2[i][j])
- k+=1
- self.nums=k
- M1 = [
- [0,1,1],
- [0,0,0],
- [0,1,0]
- ]
- M2 = [
- [0,1,1],
- [0,0,0],
- [0,1,0]
- ]
- # print(len(M1))
- # print(len(M1[0]))
- B1=SparseMatrix(100)
- B1.create(M1)
- B2=SparseMatrix(100)
- B2.create(M2)
- B3=SparseMatrix(100)
- B3.ms_add(M1,M2)
- print("矩阵A的三元组: ")
- B1.display()
- print("矩阵B的三元组: ")
- B2.display()
- print("矩阵A和矩阵B相加后的矩阵C的三元组: ")
- B3.display()
- # B1.dispaly()
- # B2.dispaly()
复制代码
|
|