Trabalhando com Matrizes

Publicado por Denilson 08/05/2008

[ Hits: 7.124 ]

Download 3329.4901




Esse script faz soma, subtração, multiplicação e a transposta de uma matriz.

  



Esconder código-fonte

#include<iostream.h>
#include<stdio.h>
#include<conio2.h> 
#include<math.h> 
#define UP 72 
#define DOWN 80
#define ENTER 13

void Transposta (int A[10][10],int m,int n){
     int cont=1;
     textcolor(10);
     
printf("\n%c",218);
gotoxy(wherex()+(m*5),wherey());
printf("%c",191);
     
     for(int i=0; i<n; i++){
     cout<<"\n";
     printf("%c",179);
     
     for(int j=0; j<m; j++){
     textcolor(7);
     printf("%3d  ",A[j][i]);}
     textcolor(10);
     printf("%c",179);
     
     if(cont<n){          
     printf("\n%c",179);
     gotoxy(wherex()+(m*5),wherey());
     printf("%c",179);
     cont++;}}
     
printf("\n%c",192);
gotoxy(wherex()+(m*5),wherey());
printf("%c",217);}

void MATRIZ (int A[10][10],int m,int n){int cont=1;
     textcolor(10);
     
printf("\n%c",218);
gotoxy(wherex()+(n*5),wherey());
printf("%c",191);

for(int i=0;i<m;i++){
     cout<<"\n";
     printf("%c",179);
     for(int j=0;j<n;j++){
     textcolor(7);
     printf(" %3d ",A[i][j]);}
     textcolor(10);
     printf("%c",179);
     
     if(cont<m){
     printf("\n%c",179);
     gotoxy(wherex()+(n*5),wherey());
     printf("%c",179);
     cont++;}}
     
printf("\n%c",192);
gotoxy(wherex()+(n*5),wherey());
printf("%c",217);}

void Soma(int A[10][10],int B[10][10],int C[10][10],int m, int n)
{int i,j;
for(i=0;i<m;i++)for(j=0;j<n;j++)C[i][j]=A[i][j]+B[i][j];}

void Subtracao(int A[10][10],int B[10][10],int C[10][10],int m, int n)
{int i,j;
for(i=0;i<m;i++)for(j=0;j<n;j++)C[i][j]=A[i][j]-B[i][j];}

void Multiplicacao(int A[10][10],int B[10][10],
int C[10][10],int m, int n,int p){int i,j,k;
for(i=0;i<m;i++)
for(j=0;j<p;j++){C[i][j]=0;
for(k=0;k<n;k++)C[i][j]=C[i][j]+A[i][k]*B[k][j];}}

char cmenu[5][28]={" Soma de Matrizes          ",
" Subtra‡Æo de Matrizes     "," Multiplica‡Æo de Matrizes ",
" Matriz Transposta         "," Sair                      "};

int linha,tecla; 

void menu(){ 
clrscr();
gotoxy(0,0);

textcolor(11);
cout<<"\n @Autor: Denilson Edinaldo Pinto\n\n\n\n";

textcolor(12);
cout<<"\n\n PROJETO DE MATRIZES\n\n ";
textcolor(11);
cout<<"              MENU:";

textcolor(14);
textbackground(4);

for(linha=1;linha<=5;linha++){ 
gotoxy(22,linha+10); cprintf("%s", cmenu[linha]);}

linha=0; 
textbackground(1); 
gotoxy(22,linha+10); cprintf("%s", cmenu[linha]); 
textbackground(0);} 

main(){
int A[10][10],B[10][10],C[10][10],m,n,p,i,j,x,y,k,cont=1; 
 
menu();

do{
A[10][10]=0,B[10][10]=0,C[10][10]=0;
m=0,n=0,p=0,i=0,j=0,x=0,y=0,k=0,cont=1;

tecla=getch();

if(tecla==DOWN&&linha<5){ 
textbackground(4); 
gotoxy(22,linha+10); cprintf("%s", cmenu[linha]); 
linha++; 
textbackground(1); 
gotoxy(22,linha+10); cprintf("%s", cmenu[linha]);} 

if(tecla==UP&&linha>-1){ 
textbackground(4); 
gotoxy(22,linha+10); cprintf("%s", cmenu[linha]); 
linha--; 
textbackground(1); 
gotoxy(22,linha+10); cprintf("%s", cmenu[linha]);}

if(tecla==DOWN&&linha==5){ 
textbackground(4); 
gotoxy(22,linha+10); cprintf("%s", cmenu[linha]); 
linha=0; 
textbackground(1); 
gotoxy(22,linha+10); cprintf("%s", cmenu[linha]);} 

if(tecla==UP&&linha==-1){ 
textbackground(4); 
gotoxy(22,linha+11); cprintf("%s", cmenu[linha+1]); 
linha=4; 
textbackground(1); 
gotoxy(22,linha+10); cprintf("%s", cmenu[linha]);}

if(tecla==ENTER&&linha==0){ 
textbackground(0); 
clrscr();

textcolor(11);
cout<<"\n Soma de Matrizes";
textcolor(10);
cout<<"\n\n Digite o n§ de linhas: ";
textcolor(7);
cin>>m;

textcolor(10);
cout<<"\n\n Digite o n§ de colunas: ";
textcolor(7);
cin>>n;

textcolor(10);
cout<<"\n\n Digite a Matriz A: ";
textcolor(7);

for(y=10,i=0;i<m;y++,i++){
for(x=22,j=0;j<n;x=x+5,j++){gotoxy(x,y);
                            cin>>A[i][j];
                            cout<<" ";}}
                            
textcolor(10);
cout<<"\n\n Digite a Matriz B: ";
textcolor(7);

for(y=y+2,i=0;i<m;y++,i++){
for(x=22,j=0;j<n;x=x+5,j++){gotoxy(x,y);
                            cin>>B[i][j];
                            cout<<" ";}}
                                    
Soma(A,B,C,m,n);
textcolor(10);
cout<<"\n\n Matriz C: \n";
MATRIZ (C,m,n);

getch(); 
menu();}
                                    
if(tecla==ENTER&&linha==1){ 
textbackground(0); 
clrscr();

textcolor(11);
cout<<"\n Subtra‡Æo de Matrizes";
textcolor(10);
cout<<"\n\n Digite o n§ de linhas: ";
textcolor(7);
cin>>m;

textcolor(10);
cout<<"\n\n Digite o n§ de colunas: ";
textcolor(7);
cin>>n;

textcolor(10);
cout<<"\n\n Digite a Matriz A: ";
textcolor(7);

for(y=10,i=0;i<m;y++,i++){
for(x=22,j=0;j<n;x=x+5,j++){gotoxy(x,y);
                            cin>>A[i][j];
                            cout<<" ";}}
textcolor(10);

cout<<"\n\n Digite a Matriz B: ";
textcolor(7);

for(y=y+2,i=0;i<m;y++,i++){
for(x=22,j=0;j<n;x=x+5,j++){gotoxy(x,y);
                            cin>>B[i][j];
                            cout<<" ";}}
                                    
Subtracao(A,B,C,m,n);
textcolor(10);
cout<<"\n\n Matriz C: \n";
MATRIZ (C,m,n);

getch(); 
menu();}
                                    
if(tecla==ENTER&&linha==2){ 
textbackground(0); 
clrscr();

textcolor(11);
cout<<"\n Multiplica‡Æo de Matrizes";
textcolor(10);
cout<<"\n\n Matriz A: ";
cout<<"\n\n Digite o n§ de linhas: ";
textcolor(7);
cin>>m;

textcolor(10);
cout<<"\n\n Digite o n§ de colunas: ";
textcolor(7);
cin>>n;

textcolor(10);
cout<<"\n\n Digite a Matriz A: ";
textcolor(7);

for(y=12,i=0;i<m;y++,i++){
for(x=22,j=0;j<n;x=x+5,j++){gotoxy(x,y);
                            cin>>A[i][j];
                            cout<<" ";}}
textcolor(10);
cout<<"\n\n Matriz B: ";
cout<<"\n\n Digite o n§ de colunas: ";
textcolor(7);
cin>>p;

textcolor(10);
cout<<"\n\n Digite a Matriz B: ";
textcolor(7);

for(y=y+7,i=0;i<m;y++,i++){
for(x=22,j=0;j<p;x=x+5,j++){gotoxy(x,y);
                            cin>>B[i][j];
                            cout<<" ";}}
                                    
Multiplicacao(A,B,C,m,n,p);
textcolor(10);
cout<<"\n\n Matriz C: \n";

printf("\n%c",218);
gotoxy(wherex()+(p*5),wherey());
printf("%c",191);

for(int i=0;i<m;i++){
     cout<<"\n";
     printf("%c",179);
     for(int j=0;j<p;j++){
     textcolor(7);
     printf(" %3d ",C[i][j]);}
     textcolor(10);
     printf("%c",179);
     
     if(cont<m){
     printf("\n%c",179);
     gotoxy(wherex()+(p*5),wherey());
     printf("%c",179);
     cont++;}}
     
     printf("\n%c",192);
     gotoxy(wherex()+(p*5),wherey());
     printf("%c",217);
getch(); 
menu();}
                                    
if(tecla==ENTER&&linha==3){ 
textbackground(0); 
clrscr();

textcolor(11);
cout<<"\n Matriz Transposta";
textcolor(10);
cout<<"\n\n Matriz A: ";
cout<<"\n\n Digite o n§ de linhas: ";
textcolor(7);
cin>>m;

textcolor(10);
cout<<"\n\n Digite o n§ de colunas: ";
textcolor(7);
cin>>n;

textcolor(10);
cout<<"\n\n Digite a Matriz A: ";
textcolor(7);

for(y=12,i=0;i<m;y++,i++){
for(x=22,j=0;j<n;x=x+5,j++){gotoxy(x,y);
                            cin>>A[i][j];}}
textcolor(10);
cout<<"\n\n Transposta de A: \n";

Transposta (A,m,n);
getch(); 
menu();}

if(tecla==ENTER&&linha==4){ 
textbackground(0); 
clrscr(); 
exit(0);}

}while(tecla!=27);}

Scripts recomendados

Combustivel

4 EP - Poli USP - LIG4 (LigK)

Fibbonacci com Memoization - O(n)

Usando shared memory

Controle de estoque com listas


  

Comentários

Nenhum comentário foi encontrado.


Contribuir com comentário




Patrocínio

Site hospedado pelo provedor RedeHost.
Linux banner

Destaques

Artigos

Dicas

Tópicos

Top 10 do mês

Scripts