读书人

BCB抓到Excel容不能干算?

发布时间: 2013-01-07 10:02:24 作者: rapoo

BCB抓到Excel容不能作算??
//需使用OpenDialog1, Memo1
Form1->Hide();

AnsiString filepath;

if(OpenDialog1->Execute()) //若案成功
{
filepath= OpenDialog1->FileName.c_str(); //把案路存 filepath
}


//declaration of variables of type variant 宣告
Variant XL,v0,v1,vcell;
int i,j,count=StrToInt(InputBox("Excel","入料:",""));


//a string where you will temporarily put the content of a single Cell 存Excel格料
AnsiString tmp,x[1000],y[1000],z[1000 ];

//create an object which is an excel application and store it to XL
XL=Variant::CreateObject("excel.application");

//Set the Excel Application as invisible once you have opened it Excel用程序置不可
XL.OlePropertySet("Visible",false);

//Get the workbooks while has a path stored in “file” variable and open it.

XL.OlePropertyGet("Workbooks").OleProcedure("Open", filepath.c_str() );

//Get the Sheet which has a title “Sheet1〃

v0=XL.OlePropertyGet("Sheets","Sheet1");

//Get the Cells of that particular Sheet. 取Excel格料表。

v1=v0.OlePropertyGet("Cells");



for(i=10;i<=count;i++)
{
vcell=v1.OlePropertyGet("Item",i,2);//定要取的位置 at row i and column j
x[i]=vcell.OlePropertyGet("Value");

vcell=v1.OlePropertyGet("Item",i,3);
y[i]=vcell.OlePropertyGet("Value");

vcell=v1.OlePropertyGet("Item",i,4);
z[i]=vcell.OlePropertyGet("Value");
//重 上行定要的字
//印出

}
XL.OleProcedure("Quit"); //案
XL=Unassigned;

float Vector_N[3],Vector_U[3],Vector_V[3],x0,y0,z0; //向量N,U,V 心x0,y0,z0


float a,b,c,temp_N,temp_U;

a=StrToFloat(Edit1->Text);
b=StrToFloat(Edit1->Text);
c=StrToFloat(Edit1->Text);

temp_N=sqrt(pow(a,2)+pow(b,2)+pow(c,2)); //求N向量
Vector_N[0]=a/temp_N;
Vector_N[1]=b/temp_N;
Vector_N[2]=c/temp_N;

x0=(x[count]+x[1])/2;
y0=(y[count]+y[1])/2;
z0=(z[count]+z[1])/2;

temp_U=sqrt(pow((x[0]-x0),2)+pow((y[0]-y0),2)+pow((z[0]-z0),2));//求U向量

Vector_U[0]=(x[0]-x0)/temp_U;
Vector_U[1]=(y[0]-y0)/temp_U;
Vector_U[2]=(z[0]-z0)/temp_U;

Vector_V[0]=(Vector_N[1]*Vector_U[2]-Vector_N[2]*Vector_U[1]);//求V向量
Vector_V[1]=(Vector_N[2]*Vector_U[0]-Vector_N[0]*Vector_U[1]);
Vector_V[2]=(Vector_N[0]*Vector_U[1]-Vector_N[1]*Vector_U[0]);

// float H[3][3]={{Vector_U[0],Vector_V[0],x0},
// {Vector_U[1],Vector_V[1],y0},
// {Vector_U[2],Vector_V[2],z0}};




//3x3反矩//n矩大小,source原始矩,data反矩之後
//本副函式用於3*3 矩

float detA,H_Inverse[3][3];
{
float detA = Vector_U[0]*Vector_V[1]*z0
+Vector_U[1]*Vector_V[2]*x0
+Vector_U[2]*Vector_V[0]*Vector_V[0]
-Vector_U[0]*Vector_V[0]*Vector_V[2]
-Vector_U[1]*Vector_V[0]*z0
-Vector_U[2]*x0*Vector_V[1];
if(detA != 0)
{
H_Inverse[0][0] = (Vector_V[1]*z0 -
Vector_V[0]*Vector_V[2])/detA;
H_Inverse[0][1] = (x0*Vector_V[2] -


Vector_V[0]*z0)/detA;
H_Inverse[0][2] = (Vector_V[0]*Vector_V[0] -
Vector_V[1]*x0)/detA;
H_Inverse[1][0] = (Vector_U[2]*Vector_V[0] -
Vector_U[1]*z0)/detA;
H_Inverse[1][1] = (Vector_U[0]*z0 -
x0*Vector_U[2])/detA;
H_Inverse[1][2] = (Vector_U[1]*x0 -
Vector_U[0]*Vector_V[0])/detA;
H_Inverse[2][0] = (Vector_U[1]*Vector_V[2] -
Vector_U[2]*Vector_V[1])/detA;
H_Inverse[2][1] = (Vector_V[0]*Vector_U[2] -
Vector_U[0]*Vector_V[2])/detA;
H_Inverse[2][2] = (Vector_U[0]*Vector_V[1] -
Vector_V[0]*Vector_U[1])/detA;
}
}

float X[1000],Y[1000];
for (i=10;i<=count;i++)
{
X[i]=x[i]*H_Inverse[0][0]+x[i]*H_Inverse[0][1]+x[i]*H_Inverse[0][2];
Y[i]=y[i]*H_Inverse[1][0]+y[i]*H_Inverse[1][1]+y[i]*H_Inverse[1][2];


Memo1->Lines->Add("X[i]\t Y[i]" );
}
//入最小平方法

float H,K,P,R1,R2,R3,C1,C2,C3,C4,C5,C6,C7,C8,C9,det,R;
for(i=10;i<=count;i++)
{
C1+=2*pow(X[i],2);
C2+=2*X[i]*Y[i];
C3+=-(X[i]);
C4+=2*X[i]*Y[i];
C5+=2*pow(Y[i],2);
C6+=-(Y[i]);
C7+=-2*(X[i]);
C8+=2*(Y[i]);
C9+=-1*count;
R1+=X[i]*X[i]*X[i]+X[i]*pow(Y[i],2);
R2+=pow(X[i],2)*Y[i]+Y[i]*Y[i]*Y[i];
R3+=pow(X[i],2)*pow(Y[i],2);
}

det=C1*C5*C9+C2*(-2*C6)*C3+(-2*C3)*C2*C6-C3*C5*(-2*C3)-C6*(-2*C6)*C1-C9*C2*C2; //行列式值
H=(C5*C9-C6*(-2*C6))/det*R1-(C2*C9-C3*(-2*C6))/det*R2+(C2*C6-C3*C5)/det*R3; //心x座
K=-((C2*C9-C6*(-2*C3))/det*R1-(C1*C9-C3*(-2*C3))/det*R2+(C1*C6-C3*C2)/det*R3); //心y座
P=(C2*(-2*C6)-C5*(-2*C3))/det*R1-(C1*(-2*C6)-C2*(-2*C3))/det*R2+(C1*C5-C2*C2)/det*R3;
R=sqrt(pow(H,2)+pow(K,2)-P); //半


}


如上述
就是excel座抓出後做反矩後入最小平方法
行示 Could not variant of type (String) into type(Double)
我看得懂是什意思
想的是如何解??

[解决办法]
要转换,Str.ToDouble()
------解决方案--------------------


读取的数据先强制转换成double再运算

读书人网 >C++ Builder

热点推荐