自學習演算法(JAVA)
阿新 • • 發佈:2019-02-17
/* * 圖3-28 模擬網橋的自學習和轉發過程 * * 網橋1 網橋2 * 1 ________ 2 1 ________ 2 * ————————————————————|________|————————————————————|________|————————————————————— * | | | | | | * |___| |___| |___| |___| |___| |___| * A B C D E F */ package com.fromjava; import java.util.Scanner; public class SelfLearningAlgorithm { public static void main(String[] args){ char[] array1 = { 'A', 'B' }; char[] array2 = { 'C', 'D' }; char[] array3 = { 'E', 'F' }; int[][] a = new int[6][2]; int[][] b = new int[6][2]; int i; int m = 0, n = 0; int k1 = 0; int k2 = 0; int t = 0; char x, y/* , k */; while (true) { System.out.println("請輸入源地址和目的地址: "); Scanner reader = new Scanner(System.in); x = reader.next().charAt(0); y = reader.next().charAt(0); for (i = 0; i < 2; i++) { if (array1[i] == x) m = 1; } for (i = 0; i < 2; i++) { if (array2[i] == x) m = 2; } for (i = 0; i < 2; i++) { if (array3[i] == x) m = 3; } switch (m) { case 1: { for (i = 0; i < k1; i++) if (a[i][0] == x) { break; } if (i == k1) { a[k1][0] = x; a[k1][1] = m; k1++; } for (i = 0; i < k1; i++) { if (a[i][0] == y) { n = a[i][1]; break; } } if (i == k1) System.out.println("網橋1中沒有記錄目的地址 " + y + ",向右轉發。"); else { if (m == n) { System.out.println("網橋1丟棄該幀!"); t = 1; } else System.out.println("不在同一網段中,網橋1向右轉發該幀。"); } //在不同的網段 if (t != 1) { for (i = 0; i < k2; i++) if (b[i][0] == x) { break; } if (i == k2) { b[k2][0] = x; b[k2][1] = m; k2++; } for (i = 0; i < k2; i++) { if (b[i][0] == y) { n = b[i][1]; break; } } if (i == k2) System.out.println("網橋2中沒有記錄目的地址 " + y + ",向右轉發。"); else { if (m == n) System.out.println("網橋2丟棄該幀!"); else System.out.println("不在同一網段中,網橋2向右轉發該幀。"); } } break; } case 2: { //向左轉發 for(i=0;i<k1;i++) if(a[i][0] == x){ break ; } if (i == k1) { a[k1][0] = x; a[k1][1] = m; k1++; } for (i = 0; i < k1; i++) { if (a[i][0] == y) { n = a[i][1]; break; } } if (i == k1) System.out.println("網橋1中沒有記錄目的地址 " + y + ",向左轉發。"); else { if (m == n) { System.out.println("網橋1丟棄該幀!"); } else System.out.println("不在同一網段中,網橋1向左轉發該幀。"); } //向右轉發 for (i = 0; i < k2; i++) if (b[i][0] == x) { break; } if (i == k2) { b[k2][0] = x; b[k2][1] = m -1; k2++; } for (i = 0; i < k2; i++) { if (b[i][0] == y) { n = b[i][1]; break; } } if (i == k2) System.out.println("網橋2中沒有記錄目的地址 " + y + ",向右轉發。"); else { if (1 == n) System.out.println("網橋2丟棄該幀!"); else System.out.println("不在同一網段中,網橋2向右轉發該幀。"); } break ; } case 3: { for (i = 0; i < k2; i++) if (b[i][0] == x) { break; } if (i == k2) { b[k2][0] = x; b[k2][1] = m -1; k2++; } for (i = 0; i < k2; i++) { if (b[i][0] == y) { n = b[i][1]; break; } } if (i == k2) System.out.println("網橋2中沒有記錄目的地址 " + y + ",向左轉發。"); else { if (2 == n){ System.out.println("網橋2丟棄該幀!"); t = 1 ; } else System.out.println("不在同一網段中,網橋2向左轉發該幀。"); } if(t != 1){ for (i = 0; i < k1; i++) if (a[i][0] == x) { break; } if (i == k1) { a[k1][0] = x; a[k1][1] = m-1; k1++; } for (i = 0; i < k1; i++) { if (a[i][0] == y) { n = a[i][1]; break; } } if (i == k1) System.out.println("網橋1中沒有記錄目的地址 " + y + ",向左轉發。"); else { if (2 == n) System.out.println("網橋1丟棄該幀!"); else System.out.println("不在同一網段中,網橋1向左轉發該幀。"); } } break ; } default: break; } System.out.println("網橋1"); System.out.println("--------------"); for(i=0 ;i<k1;i++){ System.out.print(" "+(char)a[i][0]); System.out.print(" "+a[i][1]); System.out.println(); } System.out.println("--------------"); System.out.println("網橋2"); System.out.println("--------------"); for(i=0 ;i<k2;i++){ System.out.print(" "+(char)b[i][0]); System.out.print(" "+b[i][1]); System.out.println(); } System.out.println("--------------"); } } }