Advertisement
Guest User

Untitled

a guest
May 11th, 2023
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.43 KB | Source Code | 0 0
  1. def fill(rz, ko, fill=0):
  2.     tab=[]
  3.     temp = []
  4.     for i in range(ko):
  5.         temp.append(fill)
  6.     for i in range(rz):
  7.         tab.append(temp)
  8.     return tab
  9. def podaj(rz, ko):
  10.     tab=[]
  11.     for i in range(rz):
  12.         temp=list(map(float, input().split()))
  13.         while len(temp)!=ko:
  14.             temp=list(map(float, input("Błędna ilość liczb").split()))
  15.         tab.append(temp)
  16.     return tab
  17.  
  18. class matrix:
  19.     def __init__(self, rzedy, kolumny, wartosci=None):
  20.         self.rzedy = rzedy
  21.         self.kolumny = kolumny
  22.         if wartosci==None:
  23.             self.wartosci=fill(rzedy, kolumny)
  24.         else:
  25.             self.wartosci = wartosci
  26.  
  27.     def __str__(self):
  28.         temp=str()
  29.         for i in self.wartosci:
  30.             for j in i:
  31.                 temp=temp+str(j)+" "
  32.             temp=temp + "\n"
  33.         return temp
  34.     def __add__(self1, self2):
  35.         try:
  36.             temp = matrix(self1.rzedy, self1.kolumny)
  37.             for i in range(self1.rzedy):
  38.                 for j in range(self1.kolumny):
  39.                     temp.wartosci[i][j]=self1.wartosci[i][j]+self2.wartosci[i][j]
  40.             return temp
  41.         except:
  42.             return "macierze nie mogą być dodane"
  43.  
  44. wier=int(input("Wiersze:"))
  45. kol=int(input("Kolumny:"))
  46. mac1=matrix(wier, kol, podaj(wier, kol))
  47. wier=int(input("Wiersze:"))
  48. kol=int(input("Kolumny:"))
  49. mac2=matrix(wier, kol, podaj(wier, kol))
  50. print(mac1+mac2)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement