三、 详细设计
1.定义程序中所用到的数据及数据结构
数据:
string T; //用于记录查询的程序
string[] T = new string[1]; //用于存储选中的城市 string[] city = new string[n]; //用户存储城市信息 int[,] Railway = new int[m, m]; //用于存储铁路信息 string city1 //用于记录弧头城市 string city2 //用于记录弧尾城市 Control c //用于遍历控件
int[] con = new int[n]; //用于标记被访问过的城市 int[] td = new int[n]; //用于临时记录城市间的距离
int[] dist = new int[n]; //记录指定城市到其它城市的距离 int[,] tag = new int[n, n]; //用于给铁路标号 Point []P = new Point[n+5]; //记录城市的位置信息 int[] visit = new int[n]; //标记城市是否被访问过
bool[, ,] path = new bool[n, n, n]; //记录两城市间通过的城市 Pen pen = new Pen(Color.Green, 5); //定义画笔信息
string[] Target = textBox1.Lines; //记录从textBox1中获取的信息 int[] Selected = new int[n]; //记录选定城市的标号 int[] Pcity = new int[i1]; //用于存储选中的城市 int[] Pdistance = new int[i1]; //用于存储距离 int[,] ln = new int[n, n]; //记录道路信息 int[] set = new int[n]; //记录边的弧头、弧尾
邻接矩阵:
int n; //用于记录城市的数目 int m; //用于记录道路的数目
string[] city = new string[n]; //用户存储城市信息 int[,] Railway = new int[m, m]; //用于存储铁路信息
2.主函数和其他函数的伪码算法:
查询城市信息按钮:
private void button1_Click(object sender, EventArgs e) {
textBox1.Clear();
foreach (Control c in this.Controls) //遍历程序内的控件 {
if (c is GroupBox) {
5
foreach (Control d in c.Controls) //遍历GroupBox1中的所有控件
{
if (d is RadioButton) {
if (((RadioButton)d).Checked == true) {
T = ((RadioButton)d).Text; //获取指定RadioButton空间的Text属性值 } } } } }
textBox1.Text += T; //在文本控件中显示文本信息 string Target;
textBox2.Clear(); //清空textBox2中的文本信息
StreamReader filestream1 = new StreamReader(\Encoding.Default); //从指定文本文件中读取字符 Target = filestream1.ReadLine();
while(Target!=null) //将城市的相关信息写入文本控件textBox2中 {
int flag2 = 0;
if(Target==T) //检测目标文本是否和给定文本相匹配 {
for (; ; ) {
Target = filestream1.ReadLine(); if (Target == \ break; else {
textBox2.Text += Target; } }
flag2 = 1; }
if (flag2 == 1) //是否跳出循环 break;
Target = filestream1.ReadLine(); }
filestream1.Close(); //关闭字节流 }
6
指定城市到其余城市最短距离按钮:
private void button1_Click(object sender, EventArgs e) {
textBox1.Clear(); //清空textBox1中的原有信息
StreamReader filestream1 = new StreamReader(\Encoding.Default); //从c1.txt文件中读取信息
int n = int.Parse(filestream1.ReadLine()); int m = int.Parse(filestream1.ReadLine()); filestream1.Close();
StreamReader filestream2 = new StreamReader(\Encoding.Default); //从c2.txt文件中读取信息
string[] city = new string[n]; for (int i = 0; i < n; i++) {
city[i] = filestream2.ReadLine(); }
filestream2.Close();
StreamReader filestream3 = new StreamReader(\Encoding.Default); //从c3.txt文件中读取信息
int[,] Railway = new int[m, m]; int[,] tag = new int[n, n]; for (int i = 0; i < m; i++) {
for (int j = 0; j < m; j++) {
Railway[i, j] = 10000; } }
for (int i = 0; i < n; i++) //用于记录城市的标号 {
for (int j = 0; j < n; j++) {
tag[i, j] = 0; } }
string city1, city2;
for (int i = 0; i < m; i++) //获取城市间距离信息 {
7
city1 = filestream3.ReadLine(); //获取弧头城市的信息 city2 = filestream3.ReadLine(); //获取弧尾城市的信息 int flag = 0;
for (int j = 0; j < n; j++) {
for (int k = 0; k < n; k++) {
if (city1 == city[j] && city2 == city[k]) {
Railway[j, k] = Railway[k, j] = Convert.ToInt32(filestream3.ReadLine());
tag[j, k] = tag[k, j] = i; flag = 1; break; } }
if (flag == 1) {
break; } } }
filestream3.Close();
foreach (Control c in this.Controls) //遍历所有的控件,寻找groupBox1
{
if (c is GroupBox) {
foreach (Control d in c.Controls) //遍历groupBox1中的控件,寻找RadioButton控件
{
if (d is RadioButton) {
if (((RadioButton)d).Checked == true) {
T[0] = ((RadioButton)d).Text; //记录寻找的RadioButton的Text属性值
} } } } }
8
int [] r=new int[1]; int[] s = new int[1]; int[] con = new int[n]; int[] td = new int[n];
for (int i = 0; i < n; i++) //记录寻找的城市的标号 {
if (city[i] == T[0]) {
r[0] = i; } }
for (int i = 0; i < n; i++) //记录城市是否被访问过 {
con[i] = 0; }
int[] dist = new int[n]; //记录指定城市到其它城市的距离 for (int i = 0; i < n; i++) {
dist[i] = 10000; }
for (int i = 0; i < n; i++) //记录指定城市到直接关联城市的距离 {
if (Railway[r[0], i] < 10000) {
dist[i] = Railway[r[0], i]; } }
dist[r[0]] = 0; //指定城市到自己的距离为0 con[r[0]] = 1; //标记指定城市已被访问过 for (int i = 1; i < n; i++) {
int mini = 10000;
for (int j = 0; j < n; j++) //寻找最小距离 {
if (dist[j] < mini && con[j] == 0) {
mini = dist[j];
s[0] = j; //记录城市的标号 } }
con[s[0]] = 1; //标记城市已被访问过
for (int j1 = 0; j1 < n; j1++) //标记指定城市到其它城市的距离 {
9