[用C++链接Excel,使用BasicExcel库文件]

链接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;
}
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值