Sunteți pe pagina 1din 11

#include <conio.

h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
# define M 20
# define s 21
void limpiar(float A[M][M+1],int N);
void leer(float A[M][M+1],int N);
void intercambio(float A[M][M+1],int piv,int N);
void Gauss_Jordan(float A[M][M+1],int N);
void Gauss(void);
void Seidel(void);
void sumaMatrices(void);
void restaMatrices(void);
void determinanteMatriz(void);
void multiplicaMatrices(void);
void margenxy(int x1,int y1,int x2,int y2,int h);
void presentacion(void);
void borrado(int x,int y,int xx,int yy);
void menu(void);
void menuE(void);
void menuM(void);
void menuG(void);
void Inversa(void);
void Traspuesta(void);
int Ni;
char cad[40];
int e,m,g;
/*** Funciones para el metodo de Gauss-Seidel ***/
void limpiar(float A[M][M+1],int N)
{
int i,j;
for(i=0;i<N;i++)
for(j=0;j<N+1;j++)
A[i][j]=0.0;
}
void leer(float A[M][M+1],int N)
{
int i,j;
for(i=0;i<N;i++)
for(j=0;j<N+1;j++)
{
printf("[%d][%d] = ",i,j);
scanf("%D",&A[i][j]);
}
}
void intercambio(float A[M][M+1],int piv,int N)
{
int i,j;
float aux;
i=piv+1;
while(i<N)
{
if (A[i][piv]!= 0)
for(j=piv;j<N+1;j++)
{
aux=A[piv][j];
A[piv][j]=A[i][j];
A[i][j]=aux;
}
i++;
}
if(N==i)
{
puts("No existe una unica solucion");
getch();
}
}
void Gauss_Jordan(float A[M][M+1],int N)
{
int i,j,k;

float pivote,cancel;
for(i=0;i<N;i++)
{
pivote=A[i][i];
if(pivote==0)
{
intercambio(A,i,N);
}
for(j=0;j<N+1;j++)
A[i][j]=A[i][j]/pivote;
for(j=0;j<N;j++)
{
if(j!=i)
{
cancel=-A[j][i];
for(k=0;k<N+1;k++)
A[j][k]=A[j][k]+cancel*A[i][k];
}
}
}
}
/** Crea un Margen **/
void margenxy(int x1,int y1,int x2,int y2,int h)
{
int c,l,lh;
for (c=x1;c<=(x2);c++)
{
for (l=y1;l<=y2;l++)
{
gotoxy(l,x2);printf("");
gotoxy(l,x1);printf("");
gotoxy(y1,c);printf("");
gotoxy(y2,c);printf("");
}
}
gotoxy(y1,x1);printf("");
gotoxy(y2,x1);printf("");
gotoxy(y1,x2);printf("");
gotoxy(y2,x2);printf("");
if(h!=0)
{
for (lh=y1;lh<=y2;lh++)
{
gotoxy(lh,h);printf("");
}
gotoxy(y1,h);printf("");
gotoxy(y2,h);printf("");
}
}
/** Limpia Partes especificas de lapantalla **/
void borrado(int x,int y,int xx,int yy)
{
int i,j;
for(i=x;i<=xx;i++)
for (j=y;j<=yy;j++)
{
gotoxy(j,i);printf(" ");
}
}
/** Crea la presentacion **/
void presentacion()
{
clrscr();
margenxy(1,1,24,80,5);
gotoxy(28,2);printf("Universidad de Guanajuato");
gotoxy(9,3);printf("Facultad de Ingenieria Mecanica, Electrica y Electronica");
gotoxy(35,4);printf("F.I.M.E.E.");
gotoxy(52,22);printf("Prof.: M.I. Sergio Vazquez");
margenxy(21,51,23,78,0);
}

/*** SUMA MATRIZ ***/


void sumaMatrices()
{
int r,c,i,j,x,y;
int a[100][100],b[100][100],ab[100][100];
clrscr();
printf("\n Cuantos renglones forman tu matriz: ");
scanf("%d",&r);
if(r>=100)
{
gotoxy(20,10);printf(" Es demasiado grande la matriz ");
gotoxy(20,11);printf(" Teclea una mas pequea ");
getch();
}
printf("\n Cuantas columnas forman tu matriz: ");
scanf("%d",&c);
if(c>=100)
{
gotoxy(20,10);printf(" Es demasiado grande la matriz ");
gotoxy(20,11);printf(" Teclea una mas pequea ");
getch();
exit(1);
}
printf("\n\t\t Introduce los valores de la matriz A \n ");
for(i=1;i<=r;i++)
{
for(j=1;j<=c;j++)
{
printf("[%d] [%d] :",i,j);
scanf("%d",&a[i][j]);
}
}
printf("\n\t\t Introduce los valores de la matris B \n");
for(i=1;i<=r;i++)
{
for(j=1;j<=c;j++)
{
printf("[%d] [%d] :",i,j);
scanf("%d",&b[i][j]);
}
}
for(i=1;i<=r;i++)
{
for(j=1;j<=c;j++)
{
ab[i][j]=a[i][j]+b[i][j];
}
}
borrado(1,1,24,79);
x=3;
for(i=1;i<=r;i++)
{
x=x+3;
y=10;
for(j=1;j<=c;j++)
{
y=y+4;
gotoxy(y,x);printf("%d",ab[i][j]);
}
}
gotoxy(5,1);printf("\n\t La suma de las matrices nosda ");
getch();
}

/*** RESTA MATRIZ ***/


void restaMatrices()
{
int r,c,i,j,x,y;
int a[100][100],b[100][100],ab[100][100];
clrscr();
printf("\n Cuantos renglones forman tu matriz: ");
scanf("%d",&r);
if(r>=100)
{
gotoxy(20,10);printf(" Es demasiado grande la matriz ");
gotoxy(20,11);printf(" Teclea una mas pequea ");
getch();
exit(1);
}
printf("\n Cuantas columnas forman tu matriz: ");
scanf("%d",&c);
if(c>=100)
{
gotoxy(20,10);printf(" Es demasiado grande la matriz ");
gotoxy(20,11);printf(" Teclea una mas pequea ");
getch();
}
printf("\n\t\t Introduce los valores de la matriz A \n ");
for(i=1;i<=r;i++)
{
for(j=1;j<=c;j++)
{
printf("[%d] [%d] :",i,j);
scanf("%d",&a[i][j]);
}
}
printf("\n\t\t Introduce los valores de la matris B \n");
for(i=1;i<=r;i++)
{
for(j=1;j<=c;j++)
{
printf("[%d] [%d] :",i,j);
scanf("%d",&b[i][j]);
}
}
for(i=1;i<=r;i++)
{
for(j=1;j<=c;j++)
{
ab[i][j]=a[i][j]-b[i][j];
}
}
borrado(1,1,24,79);
x=3;
for(i=1;i<=r;i++)
{
x=x+3;
y=10;
for(j=1;j<=c;j++)
{
y=y+4;
gotoxy(y,x);printf("%d",ab[i][j]);
}
}
gotoxy(5,1);printf("\n\t La resta de las matrices nosda ");
getch();
}
/*** MULTIPLICA MATRIZ ***/
void multiplicaMatrices()
{
int r,c,i,j,x,y,z,m;
int a[100][100],b[100][100],ab[100][100];
clrscr();
printf("\n Cuantos renglones forman tu matriz: ");
scanf("%d",&r);

if(r>=100)
{
gotoxy(20,10);printf(" Es demasiado grande la matriz ");
gotoxy(20,11);printf(" Teclea una mas pequea ");
getch();
}
printf("\n Cuantas columnas forman tu matriz: ");
scanf("%d",&c);
if(c>=100)
{
gotoxy(20,10);printf(" Es demasiado grande la matriz ");
gotoxy(20,11);printf(" Teclea una mas pequea ");
getch();
exit(1);
}
printf("\n\t\t Introduce los valores de la matriz A \n ");
for(i=1;i<=r;i++)
{
for(j=1;j<=c;j++)
{
printf("[%d] [%d] :",i,j);
scanf("%d",&a[i][j]);
}
}
printf("\n\t\t Introduce los valores de la matris B \n");
for(i=1;i<=r;i++)
{
for(j=1;j<=c;j++)
{
printf("[%d] [%d] :",j,i);
scanf("%d",&b[j][i]);
}
}
for(i=1;i<=r;i++)
{
for(j=1;j<=r;j++)
{
m=0;
for(z=1;z<=c;z++)
{
m=m+(a[i][z]*b[z][j]);
}
ab[i][j]=m;
}
}
/*** retorna los valores multiplicados ****/
borrado(1,1,24,79);
x=4;
for(i=1;i<=r;i++)
{
x=x+3;
y=10;
for(j=1;j<=r;j++)
{
y=y+4;
gotoxy(y,x);printf("%d",ab[i][j]);
}
}
gotoxy(5,1);printf("\n\t El resultado de la multiplicacion nosda: ");
getch();
}
/* ** DETERMINANTE MATRIZ ** */
void determinanteMatriz()
{
int n,i,j,z,k;
float d,m;
float a[22][22];
clrscr();
printf("\n Cuantos renglones forman tu matriz :");
scanf("%d",&n);
if(n>=20)

{
gotoxy(20,10);printf(" Es demasiado grande la matriz ");
gotoxy(20,11);printf(" Teclea una mas pequea ");
getch();
}
printf("\n\t\t Introduce los valores de la matriz \n ");
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
{
printf("[%d] [%d] :",i,j);
scanf("%f",&a[i][j]);
}
for(k=0;k<=(n-2);k++)
for(j=2;j<=n;j++)
{
m=a[j+k][1+k]/-a[1+k][1+k];
for(z=1;z<=n;z++)
a[j+k][z+k]+=a[1+k][z+k]*m;
}
d=1;
for(i=1;i<=n;i++)
d*=a[i][i];
/*** retorna el valor de la determinante ****/
borrado(1,1,24,79);
gotoxy(15,3);printf("\n\t El valor de la Determinante es: ");
margenxy(6,25,10,45,0);
gotoxy(30,8);printf("%f",d);getch();
}
/** TRASPUESTA DE UNA MATRIZ **/
void Traspuesta()
{
int n,i,j,x,y;
float a[22][22];
float t[22][22];
clrscr();
printf("\n Cuantos renglones forman tu matriz :");
scanf("%d",&n);
if(n>=20)
{
gotoxy(20,10);printf(" Es demasiado grande la matriz ");
gotoxy(20,11);printf(" Teclea una mas pequea ");
getch();
}
printf("\n\t\t Introduce los valores de la matriz \n ");
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
{
printf("[%d] [%d] :",i,j);
scanf("%f",&a[i][j]);
}
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
{
t[j][i]=0;
}
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
{
t[j][i]=a[i][j];
}
/*** retorna el valor ****/
clrscr();
x=4;
for(i=1;i<=n;i++)
{
x=x+3;
y=5;
for(j=1;j<=n;j++)
{
y=y+10;

gotoxy(y,x);printf("%.2f",t[i][j]);
}
}
gotoxy(5,3);printf("La matriz traspuesta es:");
getch();
}

/* INVERSA DE UNA MATRIZ */


void Inversa()
{
int c,n,i,z,j,k,x,y;
float d,de,m;
float a[22][66];
float md[22][66];
clrscr();
printf("\n Cuantos renglones forman tu matriz :");
scanf("%d",&n);
if(n>=20)
{
gotoxy(20,10);printf(" Es demasiado grande la matriz ");
gotoxy(20,11);printf(" Teclea una mas pequea ");
getch();
exit(1);
}
printf("\n\t\t Introduce los valores de la matriz \n ");
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
{
printf("[%d] [%d] :",i,j);
scanf("%f",&a[i][j]);
}
/* valor de la determinate */
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
{
md[i][j]=a[i][j];
}
for(k=0;k<=(n-2);k++)
for(j=2;j<=n;j++)
{
m=md[j+k][1+k]/-md[1+k][1+k];
for(z=1;z<=n;z++)
md[j+k][z+k]+=md[1+k][z+k]*m;
}
de=1;
for(i=1;i<=n;i++)
de*=md[i][i];
if(de!=0)
{ /* Agrega la Matriz identidad */
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
{
if(i==j)
{
a[i][n+j]=1;
}
else
a[i][n+j]=0;
}
/* diagonal inferior */
for(k=0;k<=(n-1);k++)
{
d=a[1+k][1+k];
for(j=1;j<=(n+n);j++)
{
a[1+k][j]/=d;
}

for(j=2;j<=n;j++)
{
m=a[j+k][1+k]/-a[1+k][1+k];
for(z=1;z<=(n+n);z++)
a[j+k][z+k]+=a[1+k][z+k]*m;
}
}

/* Diagonal Superior */
c=n+1;
for(k=0;k<=(n-1);k++)
{
c=c-1;
for(j=1;j<=n;j++)
{
m=-a[c-j][n-k];
for(z=1;z<=(n+n);z++)
a[c-j][z]+=a[n-k][z]*m;
}
}
clrscr();
x=0;
for(i=1;i<=n;i++)
{
x=x+2;
y=0;
for(j=1;j<=(n+n);j++)
{
y=y+7;
gotoxy(y,x);printf("%.2f",a[i][j]);
}
}
getch();
}
else
{
gotoxy(27,10);printf("La matriz no tiene inversa");
getch();
}
}
/** GAUSS-JORDAN **/
void Gauss()
{
int c,n,i,z,j,k,x,y;
float d,m;
float a[22][22];
clrscr();
printf("\n Cuantos renglones forman tu matriz :");
scanf("%d",&n);
if(n>=20)
{
gotoxy(20,10);printf(" Es demasiado grande la matriz ");
gotoxy(20,11);printf(" Teclea una mas pequea ");
getch();
exit(1);
}
printf("\n\t\t Introduce los valores de la matriz \n ");
for(i=1;i<=n;i++)
for(j=1;j<=n+1;j++)
{
printf("[%d] [%d] :",i,j);
scanf("%f",&a[i][j]);
}

/* diagonal inferior */
for(k=0;k<=(n-1);k++)
{
d=a[1+k][1+k];
for(j=1;j<=(n+1);j++)
{
a[1+k][j]/=d;
}
for(j=2;j<=n;j++)
{
m=a[j+k][1+k]/-a[1+k][1+k];
for(z=1;z<=(n+n);z++)
a[j+k][z+k]+=a[1+k][z+k]*m;
}
}
/* Diagonal Superior */
c=n+1;
for(k=0;k<=(n-1);k++)
{
c=c-1;
for(j=1;j<=n;j++)
{
m=-a[c-j][n-k];
for(z=1;z<=(n+1);z++)
a[c-j][z]+=a[n-k][z]*m;
}
}
clrscr();
x=0;
for(i=1;i<=n;i++)
{
x=x+2;
y=0;
for(j=1;j<=(n+1);j++)
{
y=y+7;
gotoxy(y,x);printf("%.2f",a[i][j]);
}
}
getch();
}
/*** GAUSS-SEIDEL ***/
void Seidel()
{
int i,N;
float A[M][M+1];
borrado(6,2,20,79);
gotoxy(33,7);printf("Instrucciones");
gotoxy(29,9);printf("Para un buen resultado");
gotoxy(17,10);printf("Coloque los valores mas alto en la diagonal");
gotoxy(36,11);printf("principal");
gotoxy(25,15);printf("NOTA: Asi lo requiere el metodo");
getch();
clrscr();
printf("\nNmero de incognitas = ");
scanf("%d",&N);
printf("\n");
limpiar(A,N);
leer(A,N);
Gauss_Jordan(A,N);
clrscr();
printf("\nLas soluciones son:\n");
for(i=0;i<N;i++)
printf("x%d = %f\n",i+1,A[i][N]);
getch();
}

/*** MENU DE LAS MATRICES ***/


void menuM()
{
int opc;
presentacion();
gotoxy(29,9);printf(" Menu de Matrices ");
gotoxy(31,11);printf("[1] Suma [1]");
gotoxy(31,12);printf("[2] Resta [2]");
gotoxy(27,13);printf("[3] Multiplicacion [3]");
gotoxy(28,14);printf("[4] Determinante [4]");
gotoxy(29,15);printf("[5] Traspuesta [5]");
gotoxy(30,16);printf("[6] Inversa [6]");
gotoxy(28,17);printf("[7] Menu General [7]");
gotoxy(35,19);printf("Opcion: ");
gotoxy(42,19);scanf("%d",&opc);
switch (opc)
{
case 1:
sumaMatrices();
break;
case 2:
restaMatrices();
break;
case 3:
multiplicaMatrices();
break;
case 4:
determinanteMatriz();
break;
case 5:
Traspuesta();
break;
case 6:
Inversa();
break;
case 7:
m=2;
break;
default:
gotoxy(30,20);printf("Opcion no vlida");
}
}
/*** MENU DE LAS ECUACIONES ***/
void menuE()
{
int opc;
presentacion();
gotoxy(29,9);printf(" Menu de Ecuaciones ");
gotoxy(28,11);printf("[1] Gauss--Jordan [1]");
gotoxy(29,12);printf("[2] Gauss-Seidel [2]");
gotoxy(29,13);printf("[3] Menu General [3]");
gotoxy(35,15);printf("Opcion :");
gotoxy(43,15);scanf("%d",&opc);
switch (opc)
{
case 1:
Gauss();
break;
case 2:
Seidel();
break;
case 3:
e=2;
break;
default:
gotoxy(30,20);printf("Opcion no vlida");
}
}

10

/*** MENU GENERAL ***/


void menuG()
{
int opc;
clrscr();
presentacion();
gotoxy(29,9);printf(" Menu General ");
gotoxy(31,11);printf("[1] Matrices [1]");
gotoxy(30,12);printf("[2] Ecuaciones [2]");
gotoxy(32,13);printf("[3] Salir [3]");
gotoxy(35,15);printf("Opcion:");
gotoxy(42,15);scanf("%d",&opc);
switch (opc)
{
case 1:
m=1;
do
menuM();
while(m==1);
break;
case 2:
e=1;
do
menuE();
while(e==1);
break;
case 3:
g=2;
break;
default:
gotoxy(30,20);printf("Opcion no vlida");getch();
}
}
main()
{
clrscr();
presentacion();
margenxy(6,25,15,55,9);
gotoxy(33,7);printf(" Trabajo Final ");
gotoxy(31,8);printf(" Metodos Numericos ");
gotoxy(37,10);printf("Alumnos");
gotoxy(26,12);printf("Jose Trinidad Salmeron Gatica");
gotoxy(28,13);printf("Jose Maria Mercado Padron");
gotoxy(26,14);printf("Juan Miguel Hernandez Tamayo");
getch();
g=1;
do
menuG();
while(g==1);
borrado(6,2,20,79);
margenxy(11,29,13,49,0);
gotoxy(30,12);printf(" Estas fuera !!!");getch();
}

11

S-ar putea să vă placă și