// AntColony.cpp : 定義控制臺應用程序的入口點。 // #include<iostream> #include<math.h> #include<time.h> #include<stdio.h> #include <fstream> #include <string> #include <iostream> #include <vector>using namespace std; #include <fstream> #include <sstream> //使用stringstream需要引入這個頭文件//打印系列 //GD-E-B-AC-HF-FG-EGJ-DI-CIJ//孔坐標 double HoleA[660][2] = { (0, 0) }; double HoleB[788][2] = { (0, 0) }; double HoleC[270][2] = { (0, 0) }; double HoleD[212][2] = { (0, 0) }; double HoleE[95][2] = { (0, 0) }; double HoleF[34][2] = { (0, 0) }; double HoleG[20][2] = { (0, 0) }; double HoleH[6][2] = { (0, 0) }; double HoleI[10][2] = { (0, 0) }; double HoleJ[28][2] = { (0, 0) };int N = 0; int L_n = 0; double Hole[1500][2] = { (0, 0) }; double holeLength = 0; //定義數組長度。int HoleLength[11] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; //記錄讀入數組的長度//從1-10,分別對應A,B-到-J//double C[N][2] = {0 };struct Point {int x;int y; };FILE *fp; char buf[256]; char *p; char X[10] = { '\0' }; char Y[10] = { '\0' }; int CoorX; int CoorY;char HolePattern = '0';//----------上面參數是固定的,下面的參數是可變的----------- //螞蟻數量 #define M 75 //最大循環次數NcMax int NcMax = 1; //信息啟發因子,期望啟發式因子,全局信息素揮發參數,局部信息素揮發參數, 狀態轉移公式中的q0 double alpha = 2, beta = 5, rou = 0.1, alpha1 = 0.1, qzero = 0.1; //-----------問題三結束------------------------------------------------------------------------ //=========================================================================================================== //局部更新時候使用的的常量,它是由最近鄰方法得到的一個長度 //什么是最近鄰方法?:)就是從源節點出發,每次選擇一個距離最短的點來遍歷所有的節點得到的路徑 //每個節點都可能作為源節點來遍歷 double Lnn; //矩陣表示兩孔之間的距離 double allDistance[1500][1500];//=========================================================================================================== //計算兩個孔之間的距離 double calculateDistance(double Hole[][2], int i, int j) {return sqrt(pow((Hole[i][0] - Hole[j][0]), 2.0) + pow((Hole[i][1] - Hole[j][1]), 2.0)); } //=========================================================================================================== //由矩陣表示兩兩城市之間的距離 void calculateAllDistance() {for (int i = 0; i < N; i++){for (int j = 0; j < N; j++){if (i != j){allDistance[i][j] = calculateDistance(Hole, i, j);allDistance[j][i] = allDistance[i][j];}}} }//獲得經過n個城市的路徑長度 double calculateSumOfDistance(int* tour) {double sum = 0;for (int i = 0; i< N; i++){int row = *(tour + 2 * i);int col = *(tour + 2 * i + 1);sum += allDistance[row][col];}return sum; }class ACSAnt;class AntColonySystem { private:double info[1500][1500], visible[1500][1500]; //節點之間的信息素強度,節點之間的能見度 public:AntColonySystem(){}//計算當前節點到下一節點轉移的概率 double Transition(int i, int j);//局部更新規則 void UpdateLocalPathRule(int i, int j);//初始化 void InitParameter(double value);//全局信息素更新 void UpdateGlobalPathRule(int* bestTour, int globalBestLength); };//計算當前節點到下一節點轉移的概率 double AntColonySystem::Transition(int i, int j) {if (i != j){return (pow(info[i][j], alpha) * pow(visible[i][j], beta));}else{return 0.0;} } //局部更新規則 void AntColonySystem::UpdateLocalPathRule(int i, int j) {info[i][j] = (1.0 - alpha1) * info[i][j] + alpha1 * (1.0 / (N * Lnn));info[j][i] = info[i][j]; } //初始化 void AntColonySystem::InitParameter(double value) {//初始化路徑上的信息素強度tao0 for (int i = 0; i < N; i++){for (int j = 0; j < N; j++){info[i][j] = value;info[j][i] = value;if (i != j){visible[i][j] = 1.0 / allDistance[i][j];visible[j][i] = visible[i][j];}}} }//全局信息素更新 void AntColonySystem::UpdateGlobalPathRule(int* bestTour, int globalBestLength) {for (int i = 0; i < N; i++){int row = *(bestTour + 2 * i);int col = *(bestTour + 2 * i + 1);info[row][col] = (1.0 - rou) * info[row][col] + rou * (1.0 / globalBestLength);info[col][row] = info[row][col];} }class ACSAnt { private:AntColonySystem* antColony; protected:int startCity, cururentCity;//初始城市編號,當前城市編號 int allowed[1500];//禁忌表 int Tour[1500][2];//當前路徑 int currentTourIndex;//當前路徑索引,從0開始,存儲螞蟻經過城市的編號 public:ACSAnt(AntColonySystem* acs, int start){antColony = acs;startCity = start;}//開始搜索 int* Search();//選擇下一節點 int Choose();//移動到下一節點 void MoveToNextCity(int nextCity);};//開始搜索 int* ACSAnt::Search() {cururentCity = startCity;int toCity;currentTourIndex = 0;for (int i = 0; i < N; i++){allowed[i] = 1;}allowed[cururentCity] = 0;int endCity;int count = 0;do{count++;endCity = cururentCity;toCity = Choose();if (toCity >= 0){MoveToNextCity(toCity);antColony->UpdateLocalPathRule(endCity, toCity);cururentCity = toCity;}} while (toCity >= 0);MoveToNextCity(startCity);antColony->UpdateLocalPathRule(endCity, startCity);return *Tour; }//選擇下一節點 int ACSAnt::Choose() {int nextCity = -1;double q = rand() / (double)RAND_MAX;//如果 q <= q0,按先驗知識,否則則按概率轉移, if (q <= qzero){double probability = -1.0;//轉移到下一節點的概率 for (int i = 0; i < N; i++){//去掉禁忌表中已走過的節點,從剩下節點中選擇最大概率的可行節點 if (1 == allowed[i]){double prob = antColony->Transition(cururentCity, i);if (prob > probability){nextCity = i;probability = prob;}}}}else{//按概率轉移 double p = rand() / (double)RAND_MAX;//生成一個隨機數,用來判斷落在哪個區間段 double sum = 0.0;double probability = 0.0;//概率的區間點,p 落在哪個區間段,則該點是轉移的方向 //計算概率公式的分母的值 for (int i = 0; i < N; i++){if (1 == allowed[i]){sum += antColony->Transition(cururentCity, i);}}for (int j = 0; j < N; j++){if (1 == allowed[j] && sum > 0){probability += antColony->Transition(cururentCity, j) / sum;if (probability >= p || (p > 0.9999 && probability > 0.9999)){nextCity = j;break;}}}}return nextCity; }//移動到下一節點 void ACSAnt::MoveToNextCity(int nextCity) {allowed[nextCity] = 0;Tour[currentTourIndex][0] = cururentCity;Tour[currentTourIndex][1] = nextCity;currentTourIndex++;cururentCity = nextCity; }//------------------------------------------ //選擇下一個節點,配合下面的函數來計算的長度 int ChooseNextNode(int currentNode, int visitedNode[]) {int nextNode = -1;double shortDistance = 0.0;for (int i = 0; i < N; i++){//去掉已走過的節點,從剩下節點中選擇距離最近的節點 if (1 == visitedNode[i]){if (shortDistance == 0.0){shortDistance = allDistance[currentNode][i];nextNode = i;}if (shortDistance < allDistance[currentNode][i]){nextNode = i;}}}return nextNode; }//給一個節點由最近鄰距離方法計算長度 double CalAdjacentDistance(int node) {double sum = 0.0;int visitedNode[1500];for (int j = 0; j < N; j++){visitedNode[j] = 1;}visitedNode[node] = 0;int currentNode = node;int nextNode;do{nextNode = ChooseNextNode(currentNode, visitedNode);if (nextNode >= 0){sum += allDistance[currentNode][nextNode];currentNode = nextNode;visitedNode[currentNode] = 0;}} while (nextNode >= 0);sum += allDistance[currentNode][node];return sum; }//---------------------------------結束--------------------------------------------- //---------------------------------測試---------------------------------------------- /********************************************************************************************************/ //函數名稱:合并二維數組 //參數1,2。要合并的兩個二維數組,參數3,合并數組1的長度。參數4,合并數組2的長度。 //返回:二維數組的首地址。 /********************************************************************************************************/ void combineArray(double arr_one[][2], double arr_two[][2], int combinelength_one, int combinelength_two, double Hole[][2]) {//申請空間 //double ** combine_array = new double *[combinelength_one + combinelength_two];//for (int i = 0; i < (combinelength_one + combinelength_two); i++)//{// combine_array[i] = new double[2];//}//使用空間 for (int j = 0; j < combinelength_one + combinelength_two; j++){for (int k = 0; k < 2; k++){if (j < combinelength_one){Hole[j][k] = arr_one[j][k];}else{Hole[j][k] = arr_two[j - combinelength_one][k];}}}} /********************************************************************************************************/ void printCombineArray(double Hole[][2], int length) {cout << length << endl;for (int i = 0; i < length; i++){for (int k = 0; k < 2; k++){cout << i << "-" << k << " = " << Hole[i][k] << " ";}cout << endl;} } /********************************************************************************************************/ void combineTest() {//計算要合并的2個數組的總長度。int combinelength_one = sizeof(HoleF) / (2 * 8);int combinelength_two = sizeof(HoleG) / (2 * 8);/*printf("HoleG\n");for (int i = 0; i < 10; i++){for (int k = 0; k < 2; k++){cout << i << "-" << k << " = " << HoleG[i][k] << " ";}cout << endl;}printf("HoleD\n");for (int i = 0; i < 10; i++){for (int k = 0; k < 2; k++){cout << i << "-" << k << " = " << HoleD[i][k] << " ";}cout << endl;}*///Hole = combineArray(HoleF, HoleG, combinelength_one, combinelength_two);//printCombineArray(Hole); } /********************************************************************************************************/void writeData(char name[], int globalTour[][2], int length, double cost, double globalBestLength) {char buf[50];sprintf(buf, " 最后的總花費: %.4lf\0", cost);char globalLength[50];sprintf(globalLength, " 全局路徑長度: %f\0", globalBestLength);FILE *fp;char ch;int i;if ((fp = fopen("data.txt", "a+")) == NULL){printf("Cannot open file strike any key exit!");exit(1);}fwrite("\n", sizeof("\n"), 1, fp);fwrite("\n", sizeof("\n"), 1, fp);char discribut[] = "測試打孔的為:";fwrite(discribut, sizeof(discribut), 1, fp);for (i = 0; name[i] != '\0'; i++){;}fwrite(name, sizeof(char), i, fp);fwrite("\n", sizeof("\n"), 1, fp); for (i = 0; i < length; i++){//cout << globalTour[i][0] << "-";char num[5] = "0";_itoa(globalTour[i][0], num, 10);fwrite(num, sizeof(char), 5, fp);}fwrite("\n", sizeof("\n"), 1, fp);fwrite("\n", sizeof("\n"), 1, fp);for (i = 0; buf[i] != '\0'; i++) //打印花費 {;}fwrite(buf, sizeof(char), i, fp);fwrite("\n", sizeof("\n"), 1, fp);for (i = 0; globalLength[i] != '\0'; i++) //打印全局路徑長度 {;}fwrite(globalLength, sizeof(char), i, fp);fwrite("\n", sizeof("\n"), 1, fp);fwrite("\n", sizeof("\n"), 1, fp);char point_xy[100];sprintf(point_xy, " 起點 (x, y)—— (%.0f,%.0f) \n 終點 (x, y)—— (%.0f,%.0f) \n\0",Hole[0][0], Hole[0][1], Hole[length-1][0], Hole[length-1][1]);for (i = 0; point_xy[i] != '\0'; i++) //打印孔坐標 {;}fwrite(point_xy, sizeof(char), i, fp);char coutStr[] = "按照輸出的打孔數序,順序打印坐標點的位置。\n";for (i = 0; coutStr[i] != '\0'; i++) //打印孔坐標 {;}fwrite(coutStr, sizeof(char), i, fp);fwrite("\n", sizeof("\n"), 1, fp);fwrite("\n", sizeof("\n"), 1, fp);int j = 0;//打印孔坐標的信息。for (i = 0; i < length; i++){//cout << "起點坐標X:" << Hole[0][0] << " " << "起點坐標Y:" << Hole[0][1] << endl;//char num[5] = "0";//_itoa(globalTour[i][0], Hole[i][0], 10);char printGD_one[50] = ""; // sprintf(printGD_one, "第%3d個 坐標是——X: %.3f", i, Hole[i][0]);sprintf(printGD_one, "%7.0f", Hole[i][0]);for (j = 0; printGD_one[j] != '\0'; j++) //打印孔坐標 {;}fwrite(printGD_one, sizeof(char), j, fp);//_itoa(globalTour[i][0], Hole[i][1], 10);char printGD_two[50] = "";//sprintf(printGD_two, " 坐標Y: %.3f\n", Hole[i][1]);sprintf(printGD_two, " %7.0f\n", Hole[i][1]);for (j = 0; printGD_two[j] != '\0'; j++) //打印孔坐標 {;}fwrite(printGD_two, sizeof(char), j, fp);}printf("========================================================\n");fclose(fp);}/********************************************************************************************************/ void actionFunction(char str_Gd[]) {time_t timer, timerl;time(&timer);unsigned long seed = timer;seed %= 56000;srand((unsigned int)seed);//由矩陣表示兩兩城市之間的距離 calculateAllDistance();//蟻群系統對象 AntColonySystem* acs = new AntColonySystem();ACSAnt* ants[M];//螞蟻均勻分布在城市上 for (int k = 0; k < M; k++){ants[k] = new ACSAnt(acs, (int)(k%N));}calculateAllDistance();//隨機選擇一個節點計算由最近鄰方法得到的一個長度 int node = rand() % N;Lnn = CalAdjacentDistance(node);//各條路徑上初始化的信息素強度 double initInfo = 1 / (N * Lnn);acs->InitParameter(initInfo);//全局最優路徑 int globalTour[1500][2];//全局最優長度 double globalBestLength = 0.0;for (int i = 0; i < NcMax; i++){//局部最優路徑 int localTour[1500][2];//局部最優長度 double localBestLength = 0.0;//當前路徑長度 double tourLength;for (int j = 0; j < M; j++){int* tourPath = ants[j]->Search();tourLength = calculateSumOfDistance(tourPath);//局部比較,并記錄路徑和長度 if (tourLength < localBestLength || abs(localBestLength - 0.0) < 0.000001){for (int m = 0; m < N; m++){int row = *(tourPath + 2 * m);int col = *(tourPath + 2 * m + 1);localTour[m][0] = row;localTour[m][1] = col;}localBestLength = tourLength;}}//全局比較,并記錄路徑和長度 if (localBestLength < globalBestLength || abs(globalBestLength - 0.0) < 0.000001){for (int m = 0; m < N; m++){globalTour[m][0] = localTour[m][0];globalTour[m][1] = localTour[m][1];}globalBestLength = localBestLength;}acs->UpdateGlobalPathRule(*globalTour, globalBestLength);//輸出所有螞蟻循環一次后的迭代最優路徑 //cout<<"第 "<<i + 1<<" 迭代最優路徑:"<<localBestLength<<"."<<endl; for (int m = 0; m < N; m++){// cout<<localTour[m][0]<<"."; }cout << endl << "迭代次數————" << i+1 << endl;}//輸出全局最優路徑 cout << " 全局最優路徑長度:" << globalBestLength << endl;cout << " 全局最優路徑起點和終點:" << endl;cout << " 起點坐標X:" << Hole[0][0] << " " << "起點坐標Y:" << Hole[0][1] << endl;cout << " 終點坐標X:" << Hole[N-1][0] << " " << "終點坐標Y:" << Hole[N-1][1] << endl;cout << " 當前迭代迭代次數:" << NcMax << endl;cout << "打印最優路徑坐標點的順序:" << endl;int num_row = 0;for (int m = 0; m < N; m++){if (num_row == 15){num_row = 0;cout << endl;}num_row++;if (m == (N - 1)){//cout << " " << globalTour[m][0] << endl;printf("%3d \n", globalTour[m][0]);}else{//cout << " " << globalTour[m][0] << "->";printf("%3d ->", globalTour[m][0]);}}cout << endl;time(&timerl);int t = timerl - timer;double cost = globalBestLength / 100000 * 25.4* 0.06;//printf ("加工孔需要的花費%f", cost);cout << "加工--" << str_Gd << "--孔需要的花費" << cost << endl;writeData(str_Gd, globalTour, N, cost, globalBestLength); }bool testHole() {if (Hole == NULL){printf("Hole 初始化問題\n");return 0;}else if (N < 0){cout << "不存在點" << endl;return 0;}else if (N == 1){cout << "因為在這個象限,該孔只有一個點,只存在起始點,不存在終止點,自己計算。" << endl << endl;return 0;}else{return 1;} }int TypeTest() {bool singal_out = false;int num_choice = 0;while (!singal_out){Hole[1500][2] = { (0.0, 0.0) };cout << "input number , 0 is exit, others is next action \n "<< endl << "1----GD"<< endl << "2----E"<< endl << "3----A"<< endl << "4----B"<< endl << "5----AC"<< endl << "6----HF"<< endl << "7----FG"<< endl << "8----DI"<< endl << "9----EGJ"<< endl << "10----CIJ"<< endl << "11----設置迭代次數,初始化,默認為1次。"<< endl << "12---C"<< endl << "13---D"<< endl << "14---F"<< endl << "15---G"<< endl << "16---H"<< endl << "17---I"<< endl << "18---J"<< endl << "19---CJ"<< endl << "20---JC"<< endl << "21---EJ"<< endl << " ------------------------ " << endl;cin >> num_choice;switch (num_choice){case 0:{singal_out = true;break;}case 1: //GD { int combinelength_one = HoleLength[7];int combinelength_two = HoleLength[4];N = combinelength_one + combinelength_two;//L_n = N - 1;cout << "點的總數: " << N << endl;combineArray(HoleG, HoleD, combinelength_one, combinelength_two, Hole);//printCombineArray(Hole, N);if (0 == testHole()){return 0;}char str_Gd[3] = "GD";actionFunction(str_Gd);break;}case 2: //E {int combinelength_one = HoleLength[5];for (int j = 0; j < combinelength_one; j++){for (int k = 0; k < 2; k++){Hole[j][k] = HoleE[j][k];}}N = combinelength_one;cout << "點的總數: " << N << endl;if (0 == testHole()){return 0;}char str_Gd[2] = "E";actionFunction(str_Gd);break;}case 3: //A {int combinelength_one = HoleLength[1];for (int j = 0; j < combinelength_one; j++){for (int k = 0; k < 2; k++){Hole[j][k] = HoleA[j][k];}}N = combinelength_one;cout << "點的總數: " << N << endl;if (0 == testHole()){return 0;}if (N == 0){cout << "因為一個點,只存在起始點,不存在終止點,自己計算" << endl;return 0;}char str_Gd[2] = "A";actionFunction(str_Gd);break;}case 4: //B {int combinelength_one = HoleLength[2];for (int j = 0; j < combinelength_one; j++){for (int k = 0; k < 2; k++){Hole[j][k] = HoleB[j][k];}}N = combinelength_one;cout << "點的總數: " << N << endl;if (0 == testHole()){return 0;}char str_Gd[2] = "B";actionFunction(str_Gd);break;}case 5: //AC {int combinelength_one = HoleLength[1];int combinelength_two = HoleLength[3] ;N = combinelength_one + combinelength_two;cout << "點的總數: " << N << endl;combineArray(HoleA, HoleC, combinelength_one, combinelength_two, Hole);if (0 == testHole()){return 0;}char str_Gd[3] = "AC";;actionFunction(str_Gd);break;}case 6: //HF {int combinelength_one = HoleLength[8];int combinelength_two = HoleLength[6] ;N = combinelength_one + combinelength_two;cout << "點的總數: " << N << endl;for (int j = 0; j < N; j++){for (int k = 0; k < 2; k++){if (j < combinelength_one){Hole[j][k] = HoleH[j][k];}else{Hole[j][k] = HoleF[j - combinelength_one][k];}}}if (0 == testHole()){return 0;}char str_Gd[3] = "HF";actionFunction(str_Gd);break;}case 7: //FG {int combinelength_one = HoleLength[6];int combinelength_two = HoleLength[7] ;N = combinelength_one + combinelength_two;cout << "點的總數: " << N << endl;combineArray(HoleF, HoleG, combinelength_one, combinelength_two, Hole);char str_Gd[3] = "FG";actionFunction(str_Gd);break;}case 8: //DI {int combinelength_one = HoleLength[4];int combinelength_two = HoleLength[9] ;N = combinelength_one + combinelength_two;cout << "點的總數: " << N << endl;combineArray(HoleD, HoleI, combinelength_one, combinelength_two, Hole);if (0 == testHole()){return 0;}char str_Gd[3] = "DI";actionFunction(str_Gd);break;}case 9: //EGJ {int combinelength_one = HoleLength[5];int combinelength_two = HoleLength[7] ;int combinelength_thr = HoleLength[10] ;N = combinelength_one + combinelength_two + combinelength_thr;cout << "點的總數: " << N << endl;for (int j = 0; j < N; j++){for (int k = 0; k < 2; k++){if (j < combinelength_one){Hole[j][k] = HoleE[j][k];}else if (j >= combinelength_one && j <combinelength_one + combinelength_two){Hole[j][k] = HoleG[j - combinelength_one][k];}else{Hole[j][k] = HoleJ[j - combinelength_one - combinelength_two][k];}}}//printCombineArray(Hole, N);if (0 == testHole()){return 0;}char str_Gd[4] = "EGJ";actionFunction(str_Gd);break;}case 10: //CIJ {int combinelength_one = HoleLength[3];int combinelength_two = HoleLength[9] ;int combinelength_thr = HoleLength[10] ;N = combinelength_one + combinelength_two + combinelength_thr;cout << "點的總數: " << N << endl;for (int j = 0; j < N; j++){for (int k = 0; k < 2; k++){if (j < combinelength_one){Hole[j][k] = HoleC[j][k];}else if (j >= combinelength_one && j <combinelength_one + combinelength_two){Hole[j][k] = HoleI[j - combinelength_one][k];}else{Hole[j][k] = HoleJ[j - combinelength_one - combinelength_two][k];}}}//printCombineArray(Hole, N);if (0 == testHole()){return 0;}char str_Gd[4] = "CIJ";actionFunction(str_Gd);break;}case 11:{int number_max = 0;cout << "輸入最大迭代次數,默認為1次。" << endl;cin >> number_max;NcMax = number_max; //最大迭代次數break;}case 12:{int combinelength_one = HoleLength[3];for (int j = 0; j < combinelength_one; j++){for (int k = 0; k < 2; k++){Hole[j][k] = HoleC[j][k];}}N = combinelength_one;cout << "點的總數: " << N << endl;if (0 == testHole()){return 0;}char str_Gd[2] = "C";actionFunction(str_Gd);break;}case 13:{int combinelength_one = HoleLength[4];for (int j = 0; j < combinelength_one; j++){for (int k = 0; k < 2; k++){Hole[j][k] = HoleD[j][k];}}N = combinelength_one;cout << "點的總數: " << N << endl;if (0 == testHole()){return 0;}char str_Gd[2] = "D";actionFunction(str_Gd);break;}case 14:{int combinelength_one = HoleLength[6];for (int j = 0; j < combinelength_one; j++){for (int k = 0; k < 2; k++){Hole[j][k] = HoleF[j][k];}}N = combinelength_one;cout << "點的總數: " << N << endl;if (0 == testHole()){return 0;}char str_Gd[2] = "F";actionFunction(str_Gd);break;}case 15:{int combinelength_one = HoleLength[7];for (int j = 0; j < combinelength_one; j++){for (int k = 0; k < 2; k++){Hole[j][k] = HoleG[j][k];}}N = combinelength_one;cout << "點的總數: " << N << endl;if (0 == testHole()){return 0;}char str_Gd[2] = "G";actionFunction(str_Gd);break;}case 16:{int combinelength_one = HoleLength[8];for (int j = 0; j < combinelength_one; j++){for (int k = 0; k < 2; k++){Hole[j][k] = HoleH[j][k];}}N = combinelength_one;cout << "點的總數: " << N << endl;if (0 == testHole()){return 0;}char str_Gd[2] = "H";actionFunction(str_Gd);break;}case 17:{int combinelength_one = HoleLength[9];for (int j = 0; j < combinelength_one; j++){for (int k = 0; k < 2; k++){Hole[j][k] = HoleI[j][k];}}N = combinelength_one;cout << "點的總數: " << N << endl;if (0 == testHole()){return 0;}char str_Gd[2] = "I";actionFunction(str_Gd);break;}case 18:{int combinelength_one = HoleLength[10];for (int j = 0; j < combinelength_one; j++){for (int k = 0; k < 2; k++){Hole[j][k] = HoleJ[j][k];}}N = combinelength_one;cout << "點的總數: " << N << endl;if (0 == testHole()){return 0;}char str_Gd[2] = "J";actionFunction(str_Gd);break;}case 19:{int combinelength_one = HoleLength[3];int combinelength_two = HoleLength[10];combineArray(HoleC, HoleJ, combinelength_one, combinelength_two, Hole);if (0 == testHole()){return 0;}N = combinelength_one + combinelength_two;cout << "點的總數: " << N << endl;if (0 == testHole()){return 0;}char str_Gd[3] = "CJ";actionFunction(str_Gd);break;}case 20:{int combinelength_one = HoleLength[10];int combinelength_two = HoleLength[3];combineArray(HoleJ, HoleC, combinelength_one, combinelength_two, Hole);if (0 == testHole()){return 0;}N = combinelength_one + combinelength_two;cout << "點的總數: " << N << endl;if (0 == testHole()){return 0;}char str_Gd[3] = "JC";actionFunction(str_Gd);break;}case 21:{int combinelength_one = HoleLength[5];int combinelength_two = HoleLength[10];combineArray(HoleE, HoleJ, combinelength_one, combinelength_two, Hole);if (0 == testHole()){return 0;}N = combinelength_one + combinelength_two;cout << "點的總數: " << N << endl;if (0 == testHole()){return 0;}char str_Gd[3] = "EJ";actionFunction(str_Gd);break;}default:return 0;break;}} }//--------------------------第二問-------------------------------------------------- //首先讀分區文件。 template <class Type> Type stringToNum(const string& str) {istringstream iss(str);Type num;iss >> num;return num; }//初始化數組,參數1是該行字符串,參數2是要初始化的數組。參數3是初始化那個數組 void initHolo(string s_l, double init_hole[][2], int singal) {int Xs = s_l.find("X");int Ys = s_l.find("Y");string str = s_l.substr(Xs + 1, Ys);init_hole[HoleLength[singal]][0] = stringToNum<int>(str);str = s_l.substr(Ys + 1);init_hole[HoleLength[singal]][1] = stringToNum<int>(str); }//--------------------------第二問測試函數-------------------------------------------------- void Qusetion_init(string filename) {//string filename = "one.txt"; ifstream fin(filename, _IOS_Nocreate);if (!fin){cout << "File open error!\n";return;}char line[100];string s_l = "";int singal = 0; //信號while (!fin.eof()) //判斷文件是否讀結束 {s_l = "";fin.getline(line, 100);s_l = line;if (-1 != s_l.find("A")){singal = 1;cout << s_l << endl;}else if (-1 != s_l.find("B")){singal = 2;cout << s_l << endl;}else if (-1 != s_l.find("C")){singal = 3;cout << s_l << endl;}else if (-1 != s_l.find("D")){singal = 4;cout << s_l << endl;}else if (-1 != s_l.find("E")){singal = 5;cout << s_l << endl;}else if (-1 != s_l.find("F")){singal = 6;cout << s_l << endl;}else if (-1 != s_l.find("G")){singal = 7;cout << s_l << endl;}else if (-1 != s_l.find("H")){singal = 8;cout << s_l << endl;}else if (-1 != s_l.find("I")){singal = 9;cout << s_l << endl;}else if (-1 != s_l.find("J")){singal = 10;cout << s_l << endl;}else{switch (singal){case 1:{initHolo(s_l, HoleA, singal);HoleLength[singal] = HoleLength[singal] + 1; //計數break;}case 2:{ initHolo(s_l, HoleB, singal);HoleLength[singal] = HoleLength[singal] + 1; //計數break;}case 3:{initHolo(s_l, HoleC, singal);HoleLength[singal] = HoleLength[singal] + 1; //計數break;}case 4:{initHolo(s_l, HoleD, singal);HoleLength[singal] = HoleLength[singal] + 1; //計數break;}case 5:{initHolo(s_l, HoleE, singal);HoleLength[singal] = HoleLength[singal] + 1; //計數break;}case 6:{initHolo(s_l, HoleF, singal);HoleLength[singal] = HoleLength[singal] + 1; //計數break;}case 7:{initHolo(s_l, HoleG, singal);HoleLength[singal] = HoleLength[singal] + 1; //計數break;}case 8:{initHolo(s_l, HoleH, singal);HoleLength[singal] = HoleLength[singal] + 1; //計數break;}case 9:{initHolo(s_l, HoleI, singal);HoleLength[singal] = HoleLength[singal] + 1; //計數break;}case 10:{initHolo(s_l, HoleJ, singal);HoleLength[singal] = HoleLength[singal] + 1; //計數break;}default:break;}}}fin.close();cout << "該分區所對應的————每個點孔的個數:" << endl;for (int m = 1; m < sizeof(HoleLength)/4; m++){HoleLength[m] = HoleLength[m] - 1;cout << HoleLength[m] << ", ";}cout << endl; }void secondQusetion() {//初始化數據。string filename = "";cout << "請選擇分區測試 1————one, 2————two, 3————thr, 4————fur" << endl;int x;cin >> x;switch (x){case 1:filename = "one.txt";break;case 2:filename = "two.txt";break;case 3:filename = "thr.txt";break;case 4:filename = "fur.txt";break;case 5:filename = "1.txt";break;default:cout << "輸入錯誤" << endl;break;}Qusetion_init(filename);TypeTest();/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/}void firstQuestion() {//DataInit();Qusetion_init("1.txt");//combineTest();//printCombineArray(Hole); TypeTest(); }//--------------------------主函數-------------------------------------------------- int main() {int x = 0;cout << "請輸入1或者2,選擇測試種類, 1是第一問,2是第二問 \n";cin >> x;switch (x){case 1:firstQuestion();break;case 2:secondQusetion();break;default:break;}return 0; }
?