Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <stdio.h>
- using namespace std;
- class MACIERZ{
- friend ostream & operator<< (ostream & s1, MACIERZ & o1);
- friend istream & operator>>(istream & s1, MACIERZ & o1);
- double *ws;
- int rozm;
- public:
- MACIERZ(){
- ws=NULL;
- }
- MACIERZ(int rozm, int wart){
- rozm = rozm;
- ws = new double[rozm*rozm];
- for ( int i = 0; i < rozm*rozm; i++){
- ws[i] = wart;
- }
- }
- MACIERZ(const MACIERZ &o1){
- rozm = o1.rozm;
- ws = new double[o1.rozm*o1.rozm];
- for ( int i = 0; i < rozm*rozm; i++){
- ws[i] = o1.ws[i];
- }
- }
- ~MACIERZ(){
- if (ws!=NULL){
- delete [] ws;
- ws=NULL;
- }
- }
- MACIERZ & operator=(const MACIERZ & o1){
- if (this == &o1) return *this;
- this->rozm = o1.rozm;
- if (this->ws!=NULL){
- delete [] this->ws;
- }
- if (o1.rozm>0){
- this->ws = new double[o1.rozm*o1.rozm];
- for (int i=0;i<(o1.rozm*o1.rozm);i++){
- this->ws[i] = o1.ws[i];
- }
- }
- return *this;
- }
- MACIERZ operator+ (const MACIERZ &o1){
- MACIERZ wynik(*this);
- if (this->rozm != o1.rozm){
- cout <<"Nie można dodać macierzy o różnych wymiarach!";
- return wynik;
- }
- else{
- for (int i=0;i< o1.rozm*o1.rozm; i++){
- wynik.ws[i] += o1.ws[i];
- }
- return wynik;
- }
- }
- MACIERZ operator- (const MACIERZ &o1){
- MACIERZ wynik= MACIERZ(*this);
- if (this->rozm != o1.rozm){
- cout <<"Nie można wykonać" <<endl;
- return wynik;
- }
- else{
- for (int i=0;i< o1.rozm*o1.rozm; i++){
- wynik.ws[i] -= o1.ws[i];
- }
- return wynik;
- }
- }
- MACIERZ operator*(const MACIERZ &o1){
- MACIERZ wynik= MACIERZ(*this);
- for (int k=0; k<o1.rozm*o1.rozm;k++){
- wynik.ws[k]=0;
- }
- if (this->rozm != o1.rozm){
- cout <<"Nie można mnozyc macierzy roznych rozmiarow!" <<endl;
- return wynik;
- }
- else{
- for (int i=0; i<o1.rozm*o1.rozm;i+=o1.rozm){
- for (int j=0; j<o1.rozm;j++){
- for (int k=0; k<o1.rozm;k++){
- wynik.ws[i+j]+=this->ws[k+i]*o1.ws[k*o1.rozm+j];
- }
- }
- }
- return wynik;
- }
- }
- int operator==(const MACIERZ &o1){
- if (this->rozm != o1.rozm){
- cout <<"Macierze różnych rozmów!" <<endl;
- return 0;
- }
- else{
- int flaga=0;
- for(int i=0;i< o1.rozm*o1.rozm; i++){
- if(this->ws[i] != o1.ws[i]) flaga+=1;
- }
- if(flaga==0) return 1;
- else{
- return 0;
- }
- }
- }
- int operator>=(const MACIERZ &o1){
- //operatory >= i <= porownuja sume elemntow macierzy
- if (this->rozm != o1.rozm){
- cout <<"Macierze różnych rozmiarów!" <<endl;
- return 0;
- }
- else{
- int s1=0;
- int s2=0;
- for(int i=0;i< this->rozm*this->rozm; i++){
- s1+=this->ws[i];
- }
- for(int i=0;i< o1.rozm*o1.rozm; i++){
- s2+=o1.ws[i];
- }
- if(s1>=s2) return 1;
- else{
- return 0;
- }
- }
- }
- int operator<=(const MACIERZ &o1){
- if (this->rozm != o1.rozm){
- cout <<"Macierze sa różnych rozmiarów!" <<endl;
- return 0;
- }
- else{
- int s1=0;
- int s2=0;
- for(int i=0;i< this->rozm*this->rozm; i++){
- s1+=this->ws[i];
- }
- for(int i=0;i< o1.rozm*o1.rozm; i++){
- s2+=o1.ws[i];
- }
- if(s1<=s2) return 1;
- else{
- return 0;
- }
- }
- }
- int operator!=(const MACIERZ &o1){
- if (this->rozm != o1.rozm){
- cout <<"Macierze sa różnych rozmiarów!" <<endl;
- return 0;
- }
- else{
- int flaga=0;
- for(int i=0;i< this->rozm*this->rozm; i++){
- if (this->ws[i] != o1.ws[i])flaga+=1;
- }
- if(flaga!=0) return 1;
- else{
- return 0;
- }
- }
- }
- //koniec klasy MACIERZ
- };
- ostream & operator<< (ostream & s1, MACIERZ & o1){
- if (o1.ws!=NULL){
- for(int i=0, j=1;i<(o1.rozm*o1.rozm);i++, j++){
- cout << o1.ws[i] <<" " ;
- if (j==o1.rozm){
- s1<<endl;
- j=0;
- }
- }
- }
- return s1;
- }
- istream & operator>>(istream & s1, MACIERZ & o1){
- s1>>o1.rozm;
- if (o1.ws!=NULL){
- delete [] o1.ws;
- o1.ws=NULL;
- }
- if (o1.rozm>0){
- o1.ws = new double[o1.rozm*o1.rozm];
- for (int i=0;i<(o1.rozm*o1.rozm);i++){
- cin >> o1.ws[i];
- }
- }
- return s1;
- }
- int main(){
- MACIERZ zm, zm2, zm3, zm4, zm5;
- cout << "Podaj macierz z1."<< endl;
- cin >> zm;
- cout<<endl;
- cout << "Macierz 1" <<endl;
- cout<<zm<<endl;
- zm2 = zm;
- cout << "Macierz 2 po dzialaniu ="<<endl;
- cout<<zm2<<endl;
- zm3=zm*zm2;
- cout<<zm3<<endl;
- zm3 = zm+zm2;
- cout <<"Macierz 3 po dzialaniu zm1+zm2"<<endl;
- cout<<zm <<endl;
- cout <<zm3 <<endl;
- cout<<zm4<<endl;
- zm4 = zm-zm2;
- cout <<"Macierz 4 po odejmowaniu zm-zm2" <<endl;
- cout<<zm4 <<endl;
- if (zm>=zm2) cout<<"Tak, zm>=zm2" <<endl;
- if (zm<=zm2) cout<<"Tak, zm<=zm3" <<endl;
- if (zm>=zm3) cout<<"Tak, zm>=zm3"<<endl;
- if (zm!=zm3) cout<<"Tak,zm!=zm3" <<endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement