// 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; }
Upgrade Hole puncher Mathematical Modeling
原文:http://www.cnblogs.com/hgonlywj/p/4842698.html