Upgrade Hole puncher Mathematical Modeling

// 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;
}

?

轉載于:https://www.cnblogs.com/hgonlywj/p/4842698.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/287459.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/287459.shtml
英文地址,請注明出處:http://en.pswp.cn/news/287459.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

Android之提示Cannot call this method while RecyclerView is computing a layout or scrolling

1 問題 java.lang.IllegalStateException: Cannot call this method while RecyclerView is computing a layout or scrolling androidx.recyclerview.widget.RecyclerView{24d6f3b VFED.V... ......ID 0,657-1074,1911 #7f090143 app:id/recyclerView}, adapter:com.appsinno…

Java8新的異步編程方式 CompletableFuture(三)

前面兩篇文章已經整理了CompletableFuture大部分的特性&#xff0c;本文會整理完CompletableFuture余下的特性&#xff0c;以及將它跟RxJava進行比較。 3.6 Either Either 表示的是兩個CompletableFuture&#xff0c;當其中任意一個CompletableFuture計算完成的時候就會執行。 …

情人節,我表白了CSDN小姐姐后,竟然...【為表白寫了一個繪圖工具,讓我不再手殘】

情人節&#xff0c;我表白了CSDN小姐姐后&#xff0c;竟然…竟然有人看了這篇文。 以下圖片素材由一個還沒寫完的工具繪制&#xff0c;稍后會放在CSDN的代碼倉庫&#xff08;現在能用了&#xff0c;還沒時間改&#xff0c;顏色填充算法還沒寫&#xff0c;有能力的朋友可以修改一…

【小程序】劉一哥課堂隨機點名提問神器V1.0(附源程序)

為了能讓我們的孩子們盡量來教室上課,增強課堂的參與度,激發課堂激情,提高學習效率,一哥也是煞費苦心,于是開發出了這么一款課堂點名提問神器,跟大家分享一下。 打開神器,看到的界面是這樣子的,我很感激有勇氣按時起床并能到教室的每一位有志之士。 點擊【開始】按鈕,…

org.hibernate.HibernateException: No Session found for current thread

spring、springmvc和hibernate整合 在sessionFactory.getCurrentSession()時&#xff0c;出現以下異常 No Session found for current thread但使用sessionFactory.openSession()是沒有任何問題的 嚴重: Servlet.service() for servlet [springDispatcherServlet] in context w…

java mysbatis select_MyBatis SELECT基本查詢實現方法詳解

1、返回一個LISTselect * from tbl_employee where last_name like #{lastName}2、將查詢記錄封裝為一個Mapselect * from tbl_employee where id#{id}返回一條記錄的map&#xff1b;key就是列名&#xff0c;值就是對應的值。3、多條記錄封裝為一個mapMapKey("id")pu…

Git之怎么通過命令修改前面幾次提交的記錄

1 問題 我們平時用gitlab,github發現提交代碼上庫記錄寫錯了&#xff0c;需要修改回來。 2 解決辦法

Git客戶端TortoiseGit(Windows系統)的使用方法

本文環境&#xff1a; 操作系統&#xff1a;Windows XP SP3 Git客戶端&#xff1a;TortoiseGit-1.8.8.0-32bit 一、安裝Git客戶端 全部安裝均采用默認&#xff01; 1. 安裝支撐軟件 msysgit: https://code.google.com/p/msysgit/downloads/list?qfullinstallerofficialgit 當前…

.Net 在容器中操作宿主機

1方案描述 在 docker 容器中想操作宿主機&#xff0c;一般會使用 ssh 的方式&#xff0c;然后 .Net 通過執行遠程 ssh 指令來操作宿主機。本文將使用 交互式 .Net 容器版 中提供的鏡像演示 .Net 在容器中如何操作宿主機。2前期準備 1. 宿主機上生成 ssh key生成 ss…

【看動漫學編程】程序員在異世界生個娃 第1篇:太極村

前言 作者文筆比較水&#xff0c;還請見諒。 以下內容還將使用視頻動態漫畫表現&#xff0c;剪輯完將會貼出鏈接。 小說劇情為劇情需要&#xff0c;過渡到知識點&#xff0c;部分篇幅可能沒有技術知識點還望諒解。 由于沒有經費支持&#xff0c;所以畫出來的東西是我自己用代碼…

【ArcGIS風暴】最牛逼空間數據批處理神器來了:用戶自定義工具箱GeoStorm.tbx

【Warming up】在學習和工作的過程中,作者曾寫過很多采用ArcGIS模型構建器(Model Builder)、Python代碼等批處理方法(感興趣的GISers可以去【測繪地理信息Big風暴專】欄去交流學習指導),大大的減輕了操作壓力,提高了工作效率。今天給大家展示一款神器:自定義工具箱GeoS…

2.6. PostgreSQL表之間連接

到目前為止&#xff0c;我們的查詢一次只訪問了一個表。查詢可以一次訪問多個表&#xff0c;或者用某種方式訪問一個表&#xff0c;而同時處理該表的多個行。一個同時訪問同一個或者不同表的多個行的查詢叫連接查詢。舉例來說&#xff0c;比如你想列出所有天氣記錄以及這些記錄…

Android之Caused by: java.lang.IllegalArgumentException: Failed to find configured root that contains

1 問題 用takePhoto去照相的時候特么的一打開就報這個錯誤 2020-04-09 21:33:49.124 19016-19016/com.appsinnova.android.keepshare E/AndroidRuntime: FATAL EXCEPTION: mainProcess: com.appsinnova.android.keepshare, PID: 19016java.lang.RuntimeException: Unable to …

Linux下c/c++項目代碼覆蓋率的產生方法

最近做了一系列的單元測試相關的工作&#xff0c;除了各種規范及測試框架以外&#xff0c;討論比較多的就是關于代碼覆蓋率的產生&#xff0c;c/c與其他的一些高級語言或者腳本語言相比較而言&#xff0c;例如 Java、.Net和php/python/perl/shell等&#xff0c;由于沒有這些高級…

C# WPF從后臺代碼生成行列可變的表格

z概述WPF常用的表格控件是DataGrid&#xff0c;這個控件在前臺XAML編寫的話&#xff0c;一般列已經固定&#xff0c;然后給每個列去綁定數據&#xff0c;但是如果我的列不固定&#xff0c;隨著運算結果變動呢&#xff1f;這時候DataGrid&#xff0c;就比較難實現這個需求&#…

軟件架構實踐文章鏈接

2019獨角獸企業重金招聘Python工程師標準>>> 架構 InfoQ: 又拍網架構中的分庫設計 SNS網站數據庫技術分析 - 51CTO.COM 數據庫水平切分的實現原理解析 - iBATIS - Java - JavaEye論壇 基于amoeba的mysql分布式數據庫學習&#xff08;一&#xff09; - Java - JavaEy…

【看動漫學編程】程序員在異世界生個娃 第2篇:外掛已準備就緒

前言 作者文筆比較水&#xff0c;還請見諒。 以下內容還將使用視頻動態漫畫表現&#xff0c;剪輯完將會貼出鏈接。 小說劇情為劇情需要&#xff0c;過渡到知識點&#xff0c;部分篇幅可能沒有技術知識點還望諒解。 由于沒有經費支持&#xff0c;所以畫出來的東西是我自己用代碼…

java剪切txt文件_用Java把剪切板的內容實時保存到txt

test類&#xff1a;提示用戶程序已啟動&#xff0c;提示保存位置&#xff0c;清空剪切板。package com.ariya.service;import com.ariya.service.impl.ClipboardServiceImpl;/*** author Ariya* 程序入口*/public class Test {public static void main(String[] args) {Clipboa…

【三維激光掃描】第一章:三維激光掃描入門基礎知識

隨著地理空間信息服務產業的快速發展,地理空間數據的要求越來越高。對地理空間數據的要求正朝著大信息量、高精度、可視化和可挖掘方向發展。地面激光雷達技術是一門新興的測繪技術,已逐漸成為廣大科研和工程技術人員全新的解決問題的手段。地面三維激光掃描技術與全站儀測量…

Android之kotlin里面本地圖片BitmapFactory.decodeFile轉bitmap失敗問題

1 問題 我們手機本地有個圖片文件比如如下 /storage/emulated/0/Android/data/package_name/cache/1586444511539.png 我們需要png轉bitmap&#xff0c;然后設置到ImageView里面顯示 var bitmap BitmapFactory.decodeFile(imagePath);if (bitmap null) returnelse mImagevi…