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.mzph.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…