/// <summary> /// Draw a new edge. /// </summary> private void Vertex_MouseDown_DrawEdge(object sender, MouseEventArgs e) { if (selectedTool == "buttonEdge") { (sender as Vertex).SetDraggable(false); if (vStart == null) { vStart = (sender as Vertex); } else { vFinish = (sender as Vertex); if (isDirected) { vStart.SetEdge(vFinish); mapMatrix.SetDirectedEdge(vStart.GetNumberIndex(), vFinish.GetNumberIndex()); mapList.SetDirectedEdge(vStart.GetNumberIndex(), vFinish.GetNumberIndex()); } else { vStart.SetEdge(vFinish); vFinish.SetEdge(vStart); mapMatrix.SetUndirectedEdge(vStart.GetNumberIndex(), vFinish.GetNumberIndex()); mapList.SetUndirectedEdge(vStart.GetNumberIndex(), vFinish.GetNumberIndex()); } vStart.SetSelected(false); vFinish.SetSelected(false); vStart.Refresh(); vFinish.Refresh(); ResetBoard(); vStart = null; vFinish = null; } } }
private void ButtonSubmit_Click(object sender, EventArgs e) { bool flag = true; mapMatrix = new AdjacencyMatrix(); mapList = new AdjacencyList(); for (int col = 1; col <= 26; col++) { for (int row = 0; row < 26; row++) { dataGridViewAdjacencyMatrix[col, row].Style = dataGridViewCellStyle_Valid; try { if (dataGridViewAdjacencyMatrix[col, row].Value != null && dataGridViewAdjacencyMatrix[col, row].Value.ToString() != "") { dataGridViewAdjacencyMatrix[col, row].Value = dataGridViewAdjacencyMatrix[col, row].Value.ToString().Trim(); if (dataGridViewAdjacencyMatrix[col, row].Value != null && Convert.ToDouble(dataGridViewAdjacencyMatrix[col, row].Value.ToString()) > 0) // Valid entry { mapMatrix.SetDirectedEdge(col - 1, row, Convert.ToDouble(dataGridViewAdjacencyMatrix[col, row].Value.ToString())); mapList.SetDirectedEdge(col - 1, row, Convert.ToDouble(dataGridViewAdjacencyMatrix[col, row].Value.ToString())); } else if (dataGridViewAdjacencyMatrix[col, row].Value != null && Convert.ToDouble(dataGridViewAdjacencyMatrix[col, row].Value.ToString()) < 0) // Negative edge exception { dataGridViewAdjacencyMatrix[col, row].Style = dataGridViewCellStyle_Invalid; string colName = Convert.ToChar('A' + col - 1).ToString(); string rowName = Convert.ToChar('A' + row).ToString(); MessageBox.Show("Negative weight at row: " + rowName + ", column: " + colName + "!"); flag = false; } } } catch // Invalid weight input { dataGridViewAdjacencyMatrix[col, row].Style = dataGridViewCellStyle_Invalid; string colName = Convert.ToChar('A' + col - 1).ToString(); string rowName = Convert.ToChar('A' + row).ToString(); MessageBox.Show("Invalid input at row: " + rowName + ", column: " + colName + "!"); flag = false; } } } submitSuccessful = flag; }
private void ButtonSubmit_Click(object sender, EventArgs e) { bool flag = true; mapMatrix = new AdjacencyMatrix(); mapList = new AdjacencyList(); for (int vertex = 0; vertex < 26; vertex++) { if (dataGridViewAdjacencyList[1, vertex].Value != null) { string adjacentEdges = dataGridViewAdjacencyList[1, vertex].Value.ToString(); byte[] adjacentEdgesBytes = Encoding.ASCII.GetBytes(adjacentEdges.Replace(" ", string.Empty).Replace(",", "\n")); MemoryStream adjacentEdgesStream = new MemoryStream(adjacentEdgesBytes); using (StreamReader reader = new StreamReader(adjacentEdgesStream)) { try { string newValue; string state = "vertex"; char finishingVertex = '\0'; double weight; while ((newValue = reader.ReadLine()) != null) { if (state == "vertex") { finishingVertex = Convert.ToChar(newValue); if (!(finishingVertex >= 'A' && finishingVertex <= 'Z')) // Invalid vertex name exception { dataGridViewAdjacencyList[1, vertex].Style = dataGridViewCellStyle_Invalid; string vertexName = Convert.ToChar('A' + vertex).ToString(); MessageBox.Show("Invalid input at vertex " + vertexName + "!"); flag = false; continue; } else if (finishingVertex == vertex + 'A') // Self loop exception { dataGridViewAdjacencyList[1, vertex].Style = dataGridViewCellStyle_Invalid; string vertexName = Convert.ToChar('A' + vertex).ToString(); MessageBox.Show("Self loop at vertex " + vertexName + "!"); flag = false; state = "weight"; } else // Valid input { state = "weight"; } } else // if (state == "weight") { if (newValue.Length == 1 && Convert.ToChar(newValue) >= 'A' && Convert.ToChar(newValue) <= 'Z') // Weight is omitted - default weight 1 { if (finishingVertex == vertex + 'A') // Self loop exception { dataGridViewAdjacencyList[1, vertex].Style = dataGridViewCellStyle_Invalid; string vertexName = Convert.ToChar('A' + vertex).ToString(); MessageBox.Show("Self loop at vertex " + vertexName + "!"); flag = false; } else // Valid input { mapMatrix.SetDirectedEdge(vertex, Convert.ToInt32(finishingVertex - 'A'), 1); mapList.SetDirectedEdge(vertex, Convert.ToInt32(finishingVertex - 'A'), 1); finishingVertex = Convert.ToChar(newValue); } state = "weight"; } else // Weight is specified { weight = Convert.ToDouble(newValue); if (weight > 0) // Valid input { mapMatrix.SetDirectedEdge(vertex, Convert.ToInt32(finishingVertex - 'A'), weight); mapList.SetDirectedEdge(vertex, Convert.ToInt32(finishingVertex - 'A'), weight); } else if (weight < 0) // Negative edge exception { dataGridViewAdjacencyList[1, vertex].Style = dataGridViewCellStyle_Invalid; string vStart = Convert.ToChar('A' + vertex).ToString(); string vFinish = finishingVertex.ToString(); MessageBox.Show("Negative weight at edge " + vStart + vFinish + "!"); flag = false; } finishingVertex = '\0'; state = "vertex"; } } } if (finishingVertex != '\0') { mapMatrix.SetDirectedEdge(vertex, Convert.ToInt32(finishingVertex - 'A'), 1); mapList.SetDirectedEdge(vertex, Convert.ToInt32(finishingVertex - 'A'), 1); } } catch { dataGridViewAdjacencyList[1, vertex].Style = dataGridViewCellStyle_Invalid; string vertexName = Convert.ToChar('A' + vertex).ToString(); MessageBox.Show("Invalid input at vertex " + vertexName + "!"); flag = false; } } } } submitSuccessful = flag; }