public Matriz crearMatriz(int tamX, int tamY) { int contadorX = 1, contadorY = 0; x = tamX; y = tamY; nodoMatriz nodoAux = raiz; nodoMatriz tempAuxX = raiz; nodoMatriz auxiliar = raiz; while (contadorY < tamY) { while (contadorX < tamX) { nodoMatriz nuevoNodo = new nodoMatriz(0, contadorX, contadorY); tempAuxX.right = nuevoNodo; nuevoNodo.left = tempAuxX; tempAuxX = nuevoNodo; if (contadorY > 0) { auxiliar = auxiliar.right; nuevoNodo.up = auxiliar; auxiliar.down = nuevoNodo; } //MessageBox.Show("Insertando X: " + Convert.ToString(contadorX) + ", Y: " + Convert.ToString(contadorY)); contadorX++; } if ((contadorY + 1) != tamY) { contadorX = 0; contadorY++; auxiliar = nodoAux; nodoMatriz nuevoY = new nodoMatriz(0, contadorX, contadorY); nodoAux.down = nuevoY; nuevoY.up = nodoAux; nodoAux = nuevoY; tempAuxX = nodoAux; //MessageBox.Show("Insertando X: " + Convert.ToString(contadorX) + ", Y: " + Convert.ToString(contadorY)); contadorX = 1; } else { contadorY++; } } return(this); }
public int getValor() { nodoMatriz auxX = raiz; nodoMatriz auxY = raiz; int valor = 0; for (int j = 0; j < y; j++) { for (int i = 0; i < x; i++) { valor = valor + auxX.numero; auxX = auxX.right; } auxY = auxY.down; auxX = auxY; } return(valor); }
public void asignarDato(int x, int y, int dato) { Matriz mat = this; nodoMatriz aux = raiz; if ((x == 0) && (y == 0)) { raiz.numero = dato; } else { if (y == 0) { while (aux.getX() < x) { aux = aux.right; } } else if (x == 0) { while (aux.getY() < y) { aux = aux.down; } } else { while (aux.getY() < y) { while (aux.getX() < x) { aux = aux.right; } aux = aux.down; } } aux.numero = dato; } }
public void multiplicarMatrices(NodoUsuario usuario) { try { Pila userPila = usuario.pila; Cola userCola = usuario.cola; Matriz matrizPila = pila.pop(); Matriz matrizCola = cola.desenconlar(); nodoMatriz nodoMatrizCola = matrizCola.raiz; nodoMatriz nodoMatrizPila = matrizPila.raiz; nodoMatriz auxCola = nodoMatrizCola; nodoMatriz auxPila = nodoMatrizPila; string cadena = "graph { node[shape= box]; {rank=same;"; int resultado = 0; if (matrizCola.getX() == matrizPila.getY()) { while (auxCola != null) { while (nodoMatrizCola != null) { resultado += (nodoMatrizCola.numero * nodoMatrizPila.numero); nodoMatrizCola = nodoMatrizCola.right; nodoMatrizPila = nodoMatrizPila.down; } Console.Write("Coordenada: " + auxPila.getX().ToString() + "," + auxCola.getY().ToString() + "; Dato: " + resultado.ToString()); cadena += auxPila.getX().ToString() + auxCola.getY().ToString() + "[label = " + resultado.ToString() + "]"; resultado = 0; auxPila = auxPila.right; if (auxPila != null) { nodoMatrizPila = auxPila; nodoMatrizCola = auxCola; } else { auxCola = auxCola.down; nodoMatrizCola = auxCola; auxPila = matrizPila.raiz; nodoMatrizPila = auxPila; //CAMBIA DE FILA EN LA MATRIZ DE LA COLA Y PILA REGRESA A RAIZ if (auxCola != null) { cadena += "}{rank=same;"; } else { cadena += "}"; } } } string aux = ""; for (int i = 0; i < matrizCola.getY(); i++) { for (int j = 0; j < matrizPila.getX(); j++) { if (i == 0) { if (j == matrizPila.getX() - 1) { aux += j.ToString() + i.ToString() + " "; } else { aux += j.ToString() + i.ToString() + "--"; } } else { if (j == matrizPila.getX() - 1) { aux += j.ToString() + i.ToString() + "--" + j.ToString() + (i - 1).ToString() + " "; } else { aux += j.ToString() + i.ToString() + "--" + j.ToString() + (i - 1).ToString() + " "; aux += j.ToString() + i.ToString() + "--"; } } } } cadena += aux; cadena += "}"; Graficar graficar = new Graficar(); graficar.Construir(cadena, "Matriz"); graficar.GraficarEstructura("Matriz.txt", "C:/EDD"); Imagenes image = new Imagenes("Grafica de Matriz", "C:/EDD/Matriz.jpg"); image.Show(); } else { MessageBox.Show("Es imposible multiplicar entre si tales matrices" + ", así que el número de columnas de la matriz A (" + matrizCola.getX().ToString() + ") no equivale al número de filas de la matriz B(" + matrizPila.getY().ToString() + ")"); } } catch (Exception) { MessageBox.Show("Error al querer operar"); } }
public Matriz(int fil, int col) { raiz = new nodoMatriz(0, 0, 0); x = fil; y = col; }