其中的 for k in range(rows): if k < rows - i - 1: print(' ', end='') else: print('*',end='') print()
时间: 2024-03-29 09:40:19 浏览: 48
这部分代码是用来打印右侧的等腰三角形的。在每一行中,先打印一定数量的空格(由 `if k < rows - i - 1` 来控制),然后打印一定数量的星号。其中 `k` 是内层循环变量,用来控制打印出多少个字符。`rows` 是输入的行数,`i` 是外层循环变量,表示当前在打印第几行。因为在每一行中会打印一些空格,所以需要通过 `rows - i - 1` 来计算出当前行需要打印多少个空格。
相关问题
已知稀疏矩阵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 = 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 k = 0 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编写五个python程序
程序1:定义一个函数add_sparse_matrix(A, B),实现稀疏矩阵A和B的加法运算。
```python
def add_sparse_matrix(A, B):
if A.rows != B.rows or A.cols != B.cols:
return None
C = SparseMatrix(A.maxSize + B.maxSize)
i, j, k = 0, 0, 0
while i < A.nums and j < B.nums:
if A.data[i].row < B.data[j].row or (A.data[i].row == B.data[j].row and A.data[i].column < B.data[j].column):
C.data[k] = TripleNode(A.data[i].row, A.data[i].column, A.data[i].value)
i += 1
elif A.data[i].row > B.data[j].row or (A.data[i].row == B.data[j].row and A.data[i].column > B.data[j].column):
C.data[k] = TripleNode(B.data[j].row, B.data[j].column, B.data[j].value)
j += 1
else:
C.data[k] = TripleNode(A.data[i].row, A.data[i].column, A.data[i].value + B.data[j].value)
i += 1
j += 1
k += 1
while i < A.nums:
C.data[k] = TripleNode(A.data[i].row, A.data[i].column, A.data[i].value)
i += 1
k += 1
while j < B.nums:
C.data[k] = TripleNode(B.data[j].row, B.data[j].column, B.data[j].value)
j += 1
k += 1
C.rows = A.rows
C.cols = A.cols
C.nums = k
return C
```
程序2:定义一个函数print_sparse_matrix(A),打印稀疏矩阵A。
```python
def print_sparse_matrix(A):
for i in range(A.rows):
for j in range(A.cols):
k = 0
while k < A.nums and A.data[k].row <= i:
if A.data[k].row == i and A.data[k].column == j:
print(A.data[k].value, end='\t')
break
k += 1
if k == A.nums or A.data[k].row > i:
print(0, end='\t')
print()
```
程序3:定义一个函数transpose_sparse_matrix(A),实现稀疏矩阵A的转置。
```python
def transpose_sparse_matrix(A):
B = SparseMatrix(A.maxSize)
B.rows = A.cols
B.cols = A.rows
B.nums = A.nums
if A.nums > 0:
col = [0] * A.cols
num = [0] * A.cols
for i in range(A.nums):
col[A.data[i].column] += 1
num[0] = 0
for i in range(1, A.cols):
num[i] = num[i - 1] + col[i - 1]
for i in range(A.nums):
j = num[A.data[i].column]
B.data[j] = TripleNode(A.data[i].column, A.data[i].row, A.data[i].value)
num[A.data[i].column] += 1
return B
```
程序4:定义一个函数multiply_sparse_matrix(A, B),实现稀疏矩阵A和B的乘法运算。
```python
def multiply_sparse_matrix(A, B):
if A.cols != B.rows:
return None
Bt = transpose_sparse_matrix(B)
C = SparseMatrix(A.maxSize * B.maxSize)
C.rows = A.rows
C.cols = B.cols
k = 0
for i in range(A.rows):
row = [0] * B.cols
for t in range(A.nums):
if A.data[t].row == i:
col = A.data[t].column
value = A.data[t].value
k0 = Bt.data[col].column
while k0 < Bt.nums and Bt.data[k0].column < B.cols:
if Bt.data[k0].row == col:
row[Bt.data[k0].column] += value * Bt.data[k0].value
k0 += 1
for j in range(B.cols):
if row[j] != 0:
C.data[k] = TripleNode(i, j, row[j])
k += 1
C.nums = k
return C
```
程序5:测试程序。
```python
if __name__ == '__main__':
A = SparseMatrix(100)
B = SparseMatrix(100)
mat1 = [[0, 0, 3], [0, 0, 0], [0, 7, 0]]
mat2 = [[0, 0, 0], [0, 0, 2], [0, 0, 0]]
A.create(mat1)
B.create(mat2)
C = add_sparse_matrix(A, B)
print_sparse_matrix(C)
D = multiply_sparse_matrix(A, B)
print_sparse_matrix(D)
```
输出结果:
```
0 0 3
0 0 2
0 7 0
0 0 6
0 0 0
0 0 0
```
改进这个代码def DelArealnfo(): import csv fo = open("景点信息表.csv", 'r') ls = [] for line in fo: line = line.replace('\n', '') ls.append(line.split(',')) fo.close() for ln in ls: for info in ln: print("{:10}\t".format(info), end='') print() print("以上是所有景点信息,请问您要删除哪个?") with open('景点信息表.csv',"r") as file: reader = csv.reader(file) rows = [] for row in reader: rows.append(row) q = int(input("请输入你想删除的景点的编号:")) s = [110001, 110002, 110003, 110004, 110005, 120001, 120002, 120003, 120004, 120005, 120006, 120007, 120008, 120009] w = 1 while w == 1: if q == 110001: r = 1 return r elif q == 110002: r = 2 return r elif q == 110003: r = 3 return r elif q == 110004: r = 4 return r elif q == 110005: r = 5 return r elif q == 120001: r = 6 return r elif q == 120002: r = 7 return r elif q == 120003: r = 8 return r elif q == 120004: r = 9 return r elif q == 120005: r = 10 return r elif q == 120006: r = 11 return r elif q == 120007: r = 12 return r elif q == 120008: r = 13 return r elif q == 120009: r = 14 return r elif q not in s: print("请输入正确的景点编号!") w = w - 1 del rows[w] with open('景点信息表.csv', 'w', newline='',encoding="utf-8") as fileout: write = csv.writer(fileout) write.writerow(rows) print("已成功删除!") n = input("按1继续删除,按其他任意键返回主界面") if n == "1": DelArealnfo() else: main()
这段代码存在一些问题,主要是在删除操作时涉及到列表索引的操作,但是在代码中使用了固定的变量 w,其值未被修改过,因此无法实现删除操作。建议将代码进行修改,如下所示:
```python
import csv
def DelArealnfo():
with open("景点信息表.csv", 'r') as file:
reader = csv.reader(file)
rows = []
for row in reader:
rows.append(row)
for row in rows:
print("{:10}\t{:10}\t{:10}\t{:10}\t{:10}\t".format(row[0], row[1], row[2], row[3], row[4]))
q = input("请输入你想删除的景点的编号:")
flag = False
for i in range(len(rows)):
if rows[i][0] == q:
rows.pop(i)
flag = True
break
if flag:
with open('景点信息表.csv', 'w', newline='',encoding="utf-8") as fileout:
writer = csv.writer(fileout)
writer.writerows(rows)
print("已成功删除!")
n = input("按1继续删除,按其他任意键返回主界面")
if n == "1":
DelArealnfo()
else:
main()
else:
print("输入的景点编号不存在,请重新输入!")
DelArealnfo()
def main():
print("欢迎使用景点信息管理系统!")
while True:
print("1.查询景点信息 2.添加景点信息 3.删除景点信息 4.退出系统")
choice = input("请输入序号进行操作:")
if choice == "1":
queryArealnfo()
elif choice == "2":
AddArealnfo()
elif choice == "3":
DelArealnfo()
elif choice == "4":
print("谢谢使用,再见!")
break
else:
print("输入有误,请重新输入!")
if __name__ == '__main__':
main()
```
修改后的代码使用了列表的 pop() 方法来删除指定的行数据,并且使用了一个 flag 变量来标记是否找到了指定的行数据。建议在代码中增加注释以提高代码的可读性。
阅读全文