Advertisement
Guest User

matrices.cpp

a guest
Feb 27th, 2011
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.86 KB | None | 0 0
  1. // matrices.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <iostream>
  6. using namespace std;
  7.  
  8. #include "matrices.h"
  9.  
  10. ostream& operator<<(ostream&os, matrice&b) {
  11.     os << "Contenu de la matrice:" << endl;
  12.     for(unsigned i=0; i<b.lignes;i++) {
  13.         for(unsigned j=0;j<b.colonnes;j++)
  14.             int a = b.mat[i][j];
  15.             //os << b.mat[i][j] << " \t";
  16.         os << endl;
  17.     }
  18.     return os;
  19. }
  20.  
  21. matrice operator*(int x, matrice&b) {
  22.     return b*x;
  23. }
  24.  
  25. matrice::matrice(unsigned l, unsigned c) {
  26.     lignes=(l>=0)?l:1;
  27.     colonnes=(c>=0)?c:1;
  28.     mat = new int * [l];
  29.     for(unsigned i=0;i<l;i++) {
  30.         mat[i] = new int[c];
  31.         for(unsigned j=0;j<c;j++)
  32.             mat[i][j] = 0;
  33.     }
  34. }
  35.  
  36. matrice::matrice(int ** m, unsigned l, unsigned c) {
  37.     lignes=(l>=0)?l:1;
  38.     colonnes=(c>=0)?c:1;
  39.     mat = new int * [l];
  40.     for(unsigned i=0;i<l;i++) {
  41.         mat[ i ] = new int [c];
  42.         for(unsigned j=0;j<c;j++)
  43.                 mat[i][j] = m[i][j];
  44.     }
  45. }
  46.  
  47. matrice::matrice(const matrice & copy) {
  48.     lignes=copy.lignes;
  49.     colonnes=copy.colonnes;
  50.     mat = new int * [lignes];
  51.     for(unsigned i=0;i<lignes;i++) {
  52.         mat[i] = new int[colonnes];
  53.         for(unsigned j=0;j<colonnes;j++)
  54.                 mat[i][j] = copy.mat[i][j];
  55.     }
  56. }
  57. matrice::~matrice(){
  58.     for(unsigned i=0;i<lignes;i++) delete [] mat[i];
  59.     delete [] mat;
  60. }
  61. matrice & matrice::operator+(matrice & b) {
  62.     if(lignes == b.lignes && colonnes == b.colonnes) {
  63.         matrice add(lignes, colonnes);
  64.         add.lignes=lignes;
  65.         add.colonnes=colonnes;
  66.         for(unsigned i=0;i<lignes;i++)
  67.             for(unsigned j=0;j<colonnes;j++)
  68.                 add.mat[i][j] = mat[i][j] + b.mat[i][j];
  69.         return add;
  70.     }
  71. }
  72. matrice & matrice::operator-() {
  73.     return *this*(-1);
  74. }
  75. matrice & matrice::operator-(matrice&b) {
  76.     if(lignes == b.lignes && colonnes == b.colonnes) {
  77.         matrice sous2(lignes, colonnes);
  78.         sous2.lignes=lignes;
  79.         sous2.colonnes=colonnes;
  80.         for(unsigned i=0;i<lignes;i++)
  81.             for(unsigned j=0;j<colonnes;j++)
  82.                 sous2.mat[i][j] = mat[i][j] - b.mat[i][j];
  83.         return sous2;
  84.     }
  85. }
  86. matrice & matrice::operator*(int b) {
  87.     matrice mult(lignes, colonnes);
  88.     mult.lignes=lignes;
  89.     mult.colonnes=colonnes;
  90.     for(unsigned i=0;i<lignes;i++)
  91.         for(unsigned j=0;j<colonnes;j++)
  92.             mult.mat[i][j] = mat[i][j]*b;
  93.     return mult;
  94. }
  95. matrice & matrice::operator*(matrice&b) {
  96.     if(colonnes == b.lignes) {
  97.         matrice mult2(lignes, colonnes);
  98.         for(unsigned i=0;i<lignes;i++)
  99.             for(unsigned j=0;j<colonnes;j++) //colonnes étant égal à b.lignes
  100.                 for(unsigned k=0;k<b.lignes;k++)
  101.                     mult2.mat[i][j]= mat[i][k]*b.mat[k][j];
  102.         return mult2;
  103.     }
  104. }
  105. matrice & matrice::revenus(matrice&n) {
  106.     matrice newr(1, 3);
  107.     for(int i=0; i<3; i++)
  108.         for(int j=0; j<3; j++)
  109.             newr.mat[1][j] += mat[i][j]*n.mat[i][j];
  110.     return newr;
  111. }
  112.  
  113.  
  114.  
  115. int _tmain(int argc, _TCHAR* argv[])
  116. {
  117.     int tn[3][4] = { { 200, 300, 200, 250 },{ 40, 50, 45, 60 },{ 100, 100, 80, 80 }};
  118.     int **tabn;
  119.     tabn = new int * [3];
  120.     for(int i=0; i<3; i++) { tabn[i] = new int [4]; for(int j=0; j<4; j++) tabn[i][j] = tn[i][j]; }
  121.     matrice n(tabn, 3, 4);
  122.  
  123.     int tu[2][3] = {{3, 5, 8},{4, 0, 5}};
  124.     int **tabu;
  125.     tabu = new int * [2];
  126.     for(int i=0; i<2; i++) { tabu[i] = new int [3]; for(int j=0; j<3; j++)  tabu[i][j] = tu[i][j]; }
  127.     matrice u(tabu, 2, 3);
  128.    
  129.     int tp[1][2] = { {5, 8} };
  130.     int **tabp;
  131.     tabp = new int * [1];
  132.     tabp[0] = new int [2];
  133.     for(int j=0; j<2; j++) tabp[0][j] = tp[0][j];
  134.     matrice p(tabp, 1, 2);
  135.  
  136.     int tc[1][3] = { {10, 1, 7} };
  137.     int **tabc;
  138.     tabc = new int * [1];
  139.     tabc[0] = new int [3];
  140.     for(int j=0; j<3; j++) tabc[0][j] = tc[0][j];
  141.     matrice c(tabc, 1, 3);
  142.  
  143.     int tv[3][4] = {{30, 31, 29, 30},{35, 36, 37, 36},{26, 22, 24, 25}};
  144.     int **tabv;
  145.     tabv = new int * [3];
  146.     for(int i=0; i<3; i++) { tabv[i] = new int [4]; for(int j=0; j<4; j++) tabv[i][j] = tv[i][j]; }
  147.     matrice v(tabv, 3, 4);
  148.  
  149.     int **tabb;
  150.     tabb = new int * [2];
  151.     for(int i=0; i<2; i++) tabb[i] = new int [4];
  152.     matrice b(tabb, 2, 4);
  153.     b=u;
  154.     //b=u*n;
  155.     cout << "Voici les besoins en matieres par rapport a chaque usines \n";
  156.     //cout << b;
  157.  
  158.     int **tabk;
  159.     tabk = new int * [1];
  160.     tabk[0] = new int [4];
  161.     matrice k(tabk, 1, 4);
  162.     k=p*b;
  163.     cout << "Voici les cout des matieres premieres a chaque usines \n";
  164.     cout << k;
  165.  
  166.     int **tabw;
  167.     tabw = new int * [1];
  168.     for(int i=0; i<1; i++) tabw[i] = new int [4];
  169.     matrice w(tabw, 1, 4);
  170.     w=c*n;
  171.     cout << "Voici les cout de production pour chaque usine \n";
  172.     cout << w;
  173.  
  174.     int **tabt;
  175.     tabt = new int * [1];
  176.     for(int i=0; i<1; i++) tabt[i] = new int [4];
  177.     matrice t(tabt, 1, 4);
  178.     t=w+k;
  179.     cout << "Voici les cout des matieres premieres a chaque usines \n";
  180.     cout << t;
  181.  
  182.     matrice r = v.revenus(n);
  183.     cout << "Voici les revenus total pour chaque usines \n";
  184.     cout << r;
  185.  
  186.     int **taba;
  187.     taba = new int * [1];
  188.     for(int i=0; i<1; i++) taba[i] = new int [4];
  189.     matrice a(taba, 1, 4);
  190.     a=r-t;
  191.     cout << "Voici les cout des matieres premieres a chaque usines \n" << a;
  192.  
  193.     system("pause");
  194.     return 0;
  195. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement