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再运算