链接Excel文件后,计算出每一组值的平均值求最短路径
#include "BasicExcel.hpp"
#include <sstream>
#include <math.h>
using namespace YExcel;
int main(int argc, char* argv[])
{
BasicExcel e;
// Load a workbook with one sheet, display its contents and save into another file.
e.Load("example1.xls");
BasicExcelWorksheet* sheet1 = e.GetWorksheet("Sheet5");
if (sheet1)
{
stringstream ss;
double Rec_x;
double Rec_y;
string Rec_str;
int Rec_index = 0;
int Rec_n = 0;
double Rec_sum_x = 0;
double Rec_sum_y = 0;
double Rec_avg_x = 0;
double Rec_avg_y = 0;
double Rec_avg_array_x[50];
double Rec_avg_array_y[50];
double Rec_return = 0;
size_t maxRows = sheet1->GetTotalRows();
size_t maxCols = sheet1->GetTotalCols();
//----------------------------------------------------------------------------------
cout << "Dimension of " << sheet1->GetAnsiSheetName() << " (" << maxRows << ", " << maxCols << ")" << endl;
cout<<" ";
for (size_t c = 0; c<maxCols; ++c) /*printf("%10d", c )*/;
cout << endl;
for (size_t r = 0; r<maxRows; ++r)
{
/*printf("%10d", r + 1);*/
for (size_t c = 0; c<maxCols; ++c)
{
BasicExcelCell* cell = sheet1->Cell(r, c);
switch (cell->Type())
{
case BasicExcelCell::UNDEFINED:
cout << " ";
break;
case BasicExcelCell::INT:
cout << cell->GetInteger() << " ";
break;
case BasicExcelCell::DOUBLE:
cout << cell->GetDouble() << " ";
break;
case BasicExcelCell::STRING:
cout << cell->GetString() << " ";
break;
case BasicExcelCell::WSTRING:
cout << cell->GetWString() << " ";
break;
}
}
cout << endl;
}
// -----------------------------------------------------------------------
//--------------------------计算平均值-------------------------------------
for (size_t r = 0; r < maxRows; ++r)
{
BasicExcelCell* cell_name = sheet1->Cell(r, 0);
BasicExcelCell* cell_Index = sheet1->Cell(r, 1);
BasicExcelCell* cell_x = sheet1->Cell(r, 2);
BasicExcelCell* cell_y = sheet1->Cell(r, 3);
/*cout << "cell_name" << cell_name->GetDouble() << endl;
cout << "cell_Index" << cell_Index->GetInteger() << endl;
cout << "cell_x->GetString()" << cell_x->GetString() << endl;
cout << "cell_y->GetString()" << cell_y->GetString() << endl;*/
for (;(int)cell_name->GetDouble() != Rec_index; Rec_index++)
{
}
if ((int)cell_name->GetDouble() == Rec_index)
{
ss << cell_x->GetString();
ss >> Rec_x;
ss.clear();
ss << cell_y->GetString();
ss >> Rec_y;
ss.clear();
Rec_sum_x += Rec_x;
Rec_sum_y += Rec_y;
Rec_n++;
if (r + 1 == maxRows)
{
Rec_avg_x = Rec_sum_x / Rec_n;
Rec_avg_y = Rec_sum_y / Rec_n;
Rec_avg_array_x[Rec_index - 1] = Rec_avg_x;
Rec_avg_array_y[Rec_index - 1] = Rec_avg_y;
cout << Rec_index << endl;
cout << "Rec_avg_x" << Rec_avg_x << endl;
cout << "Rec_avg_y" << Rec_avg_y << endl;
}
}
else {
Rec_avg_x = Rec_sum_x / Rec_n;
Rec_avg_y = Rec_sum_y / Rec_n;
Rec_avg_array_x[Rec_index - 1] = Rec_avg_x;
Rec_avg_array_y[Rec_index - 1] = Rec_avg_y;
cout << Rec_index << endl;
cout << "Rec_avg_x" << Rec_avg_x << endl;
cout << "Rec_avg_y" << Rec_avg_y << endl;
Rec_avg_x = 0;
Rec_avg_y = 0;
Rec_sum_x = 0;
Rec_sum_y = 0;
Rec_n = 0;
Rec_index++;
/*if ((int)cell_name->GetDouble() == Rec_index)
{
}
else { Rec_index++; }*/
for (;(int)cell_name->GetDouble() != Rec_index; Rec_index++)
{
}
ss << cell_x->GetString();
ss >> Rec_x;
ss.clear();
ss << cell_y->GetString();
ss >> Rec_y;
ss.clear();
Rec_sum_x += Rec_x;
Rec_sum_y += Rec_y;
Rec_n++;
}
}
//---------------------计算最终值-------------------------------------
Rec_index = 1;
for (size_t r = 0; r < maxRows; ++r)
{
BasicExcelCell* cell_name = sheet1->Cell(r, 0);
BasicExcelCell* cell_Index = sheet1->Cell(r, 1);
BasicExcelCell* cell_x = sheet1->Cell(r, 2);
BasicExcelCell* cell_y = sheet1->Cell(r, 3);
/*cout << "cell_name" << cell_name->GetDouble() << endl;
cout << "cell_Index" << cell_Index->GetInteger() << endl;
cout << "cell_x->GetString()" << cell_x->GetString() << endl;
cout << "cell_y->GetString()" << cell_y->GetString() << endl;*/
if ((int)cell_name->GetDouble() == Rec_index)
{
ss << cell_x->GetString();
ss >> Rec_x;
ss.clear();
ss << cell_y->GetString();
ss >> Rec_y;
ss.clear();
Rec_return += pow(Rec_x - Rec_avg_array_x[Rec_index - 1], 2) + pow(Rec_y - Rec_avg_array_y[Rec_index - 1], 2);
if (r + 1 == maxRows)
{
Rec_return = sqrt(Rec_return);
cout << Rec_index << endl;
cout << "Rec_return" << Rec_return << endl;
}
}
else {
Rec_return = sqrt(Rec_return);
cout << Rec_index << endl;
cout << "Rec_return" << Rec_return << endl;
Rec_return = 0;
Rec_index++;
/* if ((int)cell_name->GetDouble() == Rec_index)
{
}
else { Rec_index++; }*/
for (;(int)cell_name->GetDouble() != Rec_index; Rec_index++)
{
}
ss << cell_x->GetString();
ss >> Rec_x;
ss.clear();
ss << cell_y->GetString();
ss >> Rec_y;
ss.clear();
Rec_return += pow(Rec_x - Rec_avg_array_x[Rec_index - 1], 2) + pow(Rec_y - Rec_avg_array_y[Rec_index - 1], 2);
}
}
}
system("pause");
return 0;
}