This is my My first math programm....??

dycdyc123 2001-06-26 08:40:19
//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop
#include "math.h"
#include "MathSys.h"
#include "stdio.h"
#include "vcl/inifiles.hpp" //-------------------------------INIfile Is OK!
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
int pt=1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
OnWrite0(N,N);
HangLieShi=1;
}
//---------------------------------------------------------------------------
void TForm1::OnWrite0(int i,int j)
{
String S;
for(int i0=1;i0<=i;i0++)
{S.sprintf("%d",i0);StringGrid1->Cells[0][i0]=S;}
for(int i0=1;i0<=j;i0++)
{S.sprintf("%d",i0);StringGrid1->Cells[i0][0]=S;}

}

void __fastcall TForm1::Edit2KeyDown(TObject *Sender, WORD &Key,
TShiftState Shift)
{
if(Key==13)
{
if(Edit1->Text!=""&&Edit2->Text!="")
{
if(StrToInt(Edit1->Text)<1||StrToInt(Edit2->Text)<1||StrToInt(Edit1->Text)>40||StrToInt(Edit2->Text)>40)
{MessageBox(Handle,"Please Check The Number(1~40)","Note!",0);return ; }

h=StrToInt(Edit1->Text);l=StrToInt(Edit2->Text);

StringGrid1->ColCount=l+1;
StringGrid1->RowCount=h+1;
OnWrite0(h+1,l+1);
}
else MessageBox(Handle,"Please Input the ColNumber or LieNumber","Please!",0);
}
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Edit1KeyDown(TObject *Sender, WORD &Key,
TShiftState Shift)
{

if(Key==13)
{
if(Edit1->Text!=""&&Edit2->Text!="")
{
if(StrToInt(Edit1->Text)<1||StrToInt(Edit2->Text)<1||StrToInt(Edit1->Text)>40||StrToInt(Edit2->Text)>40)
{MessageBox(Handle,"Please Check The Number(1~40)","Note!",0);return ; }

h=StrToInt(Edit1->Text);l=StrToInt(Edit2->Text);

StringGrid1->ColCount=l+1;
StringGrid1->RowCount=h+1;
OnWrite0(h+1,l+1);
}
else MessageBox(Handle,"Please Input the ColNumber or LieNumber","Please!",0);
}

}
//---------------------------------------------------------------------------
bool TForm1::OnRead0(int p)
{
if(p==1)
{
for(int i=0;i<h;i++)
for(int j=0;j<l;j++)
{if(StringGrid1->Cells[j+1][i+1]!="") A[i][j]=StrToFloat(StringGrid1->Cells[j+1][i+1]);
else {return false;}
}
return true;
}
else
return true;
}


void __fastcall TForm1::DieDai1Click(TObject *Sender)
{
if(PanDuan1(1))
{
if(OnRead0(1))
{float Sovle[N][3*N];
float D;
MessageBox(Handle,"Please Confirm 严格主对角占优!","OK",0);
AnsiString S;
if(InputQuery("Input The Start point","The Start Point:",S))
{ for(int i=0;i<N;i++)
Sovle[i][0]=StrToFloat(S);

if(InputQuery("Input The Accurray","Input the Accurrcy:" ,S))
{
int p=0;
D=StrToFloat(S);
do
{
p++;
for(int i=0;i<h;i++)
Sovle[i][p]=Add0(Sovle,i,p)/A[i][i];

}while(MAX0(Sovle,p)>D&&p<3*N-1);

StringGrid1->ColCount=l+2;
StringGrid1->RowCount=h+3;
for(int i=0;i<h;i++)
{
StringGrid1->Cells[i+1][h+2]=FloatToStr(Sovle[i][p]);

}
if(p!=3*N-1)
StringGrid1->Cells[l+1][h+2]=IntToStr(p);
else StringGrid1->Cells[l+1][h+2]="No Sovle";

}
}
}
else {MessageBox(Handle,"Please Finish The Data!","Check",0);return;}
}
else MessageBox(Handle,"No SOVEL or No Def HANG and LIE!","Check",0);
}
//---------------------------------------------------------------------------
bool TForm1::PanDuan1(int i)
{ if(i==1)
return h==l-1?true:false;
if(i==2)
return h==l?true:false;
return true;
}
float TForm1::MAX0(float Z[N][3*N],int q)
{
float crease=fabs(Z[0][q]-Z[0][q-1]);
for(int i0=0;i0<h;i0++)
if(crease<fabs(Z[i0][q-1]-Z[i0][q])) crease=fabs(Z[i0][q-1]-Z[i0][q]);
return crease;

}
float TForm1::Add0(float z[N][3*N],int q,int s)
{
float add1=0.0;
for(int j0=0;j0<q;j0++)
add1=add1+A[q][j0]*z[j0][s];
for(int j0=q+1;j0<l-1;j0++)
add1=add1+A[q][j0]*z[j0][s-1];
add1=A[q][l-1]-add1;

return add1;

}

void __fastcall TForm1::KeLaimu1Click(TObject *Sender)
{
if(PanDuan1(1))
{
if(OnRead0(1))
{

}
else {MessageBox(Handle,"Please Finish The Data!","Check",0);return;}
}
else MessageBox(Handle,"No SOVEL or No Def HANG and LIE!","Check",0);


}
//---------------------------------------------------------------------------

void __fastcall TForm1::XiaoQu1Click(TObject *Sender)
{
Refresh();
if(PanDuan1(1))
{
if(OnRead0(1))
{
HangLieShi=1;
if(Check(h)==0)
{
for(int i=0;i<h;i++)
A[i][l+i]=1;


for(int i=0;i<h;i++)
{
for(int q=i+1;q<h;q++)
for(int j=h+l-1;j>=i;j--)
if(A[i][i]!=0) A[q][j]=A[q][j]-A[i][j]/A[i][i]*A[ q][i];
else {TiaoZhen(i+1);return;}


}
if(Check(h)==0)
{
for(int i=h-1;i>=0;i--)
{
for(int q=i-1;q>=0;q--)
for(int j=h+l-1;j>=i;j--)
if(A[i][i]!=0) A[q][j]=A[q][j]-A[i][j]/A[i][i]*A[ q][i];
else {TiaoZhen(i+1);return;}
}

for(int i=0;i<h;i++)
HangLieShi*=A[i][i];

for(int i=0;i<h;i++)
{
for(int j=0;j<l+h;j++)
if(i!=j)
A[i][j]=A[i][j]/A[i][i];
A[i][i]=1;
}


StringGrid1->ColCount=l+1+h;
StringGrid1->RowCount=h+2+h;
for(int i=0;i<h;i++)
for(int j=0;j<l+h+1;j++)
StringGrid1->Cells[j+1][i+h+2]=FloatToStr(A[i][j]);
}
else {MessageBox(Handle,"No Sovler","Notes",0);HangLieShi=0;return;}
}
else {TiaoZhen(Check(h));return;}
}
else {MessageBox(Handle,"Please Finish The Data!","Check",0);return;}
}
else MessageBox(Handle,"No SOVEL or No Def HANG and LIE!","Check",0);


}
//---------------------------------------------------------------------------

void __fastcall TForm1::Reflash1Click(TObject *Sender)
{
Edit1->Text="";
Edit2->Text="";
StringGrid1->ColCount=N+1;
StringGrid1->RowCount=N+1;
for(int i=0;i<N;i++)
for(int j=0;j<N;j++)
{ A[i][j]=0;tmp[i][j]=0;StringGrid1->Cells[j][i]="";}

pt=1;
OnWrite0(N,N);
StringGrid1->Refresh();
}
//---------------------------------------------------------------------------
int TForm1::Check(int t)
{
for(int i=0;i<t;i++)
if(A[i][i]==0) return i+1;

return 0;

}
void TForm1::TiaoZhen(int pp)
{
AnsiString S;
if(pp!=1)
for(int i=1;i<=l;i++)
{S=StringGrid1->Cells[i][1];StringGrid1->Cells[i][1]=StringGrid1->Cells[i][pp];
StringGrid1->Cells[i][pp]=S;}
else
for(int i=1;i<=l;i++)
{S=StringGrid1->Cells[i][pp];StringGrid1->Cells[i][pp]=StringGrid1->Cells[i][h];
StringGrid1->Cells[i][h]=S;}
XiaoQu1Click(this);
}
void __fastcall TForm1::NiZhen1Click(TObject *Sender)
{

MessageBox(Handle,"Please Read the Qquention!","Check",0);return;


}
//---------------------------------------------------------------------------
void TForm1::Refresh()
{
for(int i=0;i<N;i++)
for(int j=0;j<N;j++)
{A[i][j]=0;tmp[i][j]=0;}
}
void __fastcall TForm1::HangLieShi1Click(TObject *Sender)
{
if(HangLieShi==1)
{ MessageBox(Handle,"Please run XiaoQu","OK",0);return;}
char S[40];
sprintf(S,"%.6f",HangLieShi);
MessageBox(Handle,S,"Result",0);

}
//---------------------------------------------------------------------------

void __fastcall TForm1::Exit1Click(TObject *Sender)
{
Close();
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Save1Click(TObject *Sender)
{
SaveDialog1->Filter="TXT Files(*.TXT)|*.txt";
if(SaveDialog1->Execute())
{
FILE *fp;
float temp;
fp=fopen((SaveDialog1->FileName+".TXT").c_str(),"w");
if(h==0&&l==0){MessageBox(Handle,"The file is ampty!","Warnin" ,1);fclose(fp);return;}
fprintf(fp,"%d\n",h);
fprintf(fp,"%d\n",l);
for(int i=0;i<h;i++)
for(int j=0;j<l;j++)
{temp=StrToFloat(StringGrid1->Cells[j+1][i+1]);if(j!=l-1) fprintf(fp,"%f ",temp);else fprintf(fp,"%f\n",temp);}
fclose(fp);
}
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Open1Click(TObject *Sender)
{
OpenDialog1->Filter="TXT Files(*.TXT)|*.txt";
if(OpenDialog1->Execute())
{
FILE *fp;
float ftmp;
int itmp1,itmp2,i,j;
fp=fopen(OpenDialog1->FileName.c_str(),"r");
if(!fscanf(fp,"%d",&itmp1)||(!fscanf(fp,"%d",&itmp2))) {MessageBox(Handle,"The data File is read error!","Warning",1);fclose(fp);return;}
h=itmp1;
l=itmp2;
Edit1->Text=IntToStr(itmp1);
Edit2->Text=IntToStr(itmp2);

StringGrid1->ColCount=itmp2+1;
StringGrid1->RowCount=itmp1+1;
OnWrite0(itmp1,itmp2);
for(i=0;i<itmp1;i++)
for(j=0;j<itmp2;j++)
{
if(fscanf(fp,"%f",&ftmp)!=EOF) StringGrid1->Cells[j+1][i+1]=FloatToStr(ftmp);
else {MessageBox(Handle,"The data is not Complete!","Waring!",1);fclose(fp);return;}
}

fclose(fp);
}
}
//---------------------------------------------------------------------------

void __fastcall TForm1::StuZongHeAnysys1Click(TObject *Sender)
{
//Refresh();
if(OnRead0(pt))
{ AnsiString S="5";
int Head_del,Last_del;
// float temp[N][N];
PaiXu(h,l,true);
if(InputQuery("Input the Head_DEL Number","The Head_DEL Number(5)",S))
Head_del=StrToInt(S);
else Head_del=5;
if(InputQuery("Input the Last_DEL Number","The Last_DEL Number(5)",S))
Last_del=StrToInt(S);
else Last_del=5;
for(int i=0;i<h;i++)
for(int j=0;j<l;j++)
StringGrid1->Cells[j+1][i+1]=FloatToStr(tmp[i][j]);
StringGrid1->RowCount=h+3;
for(int t=1;t<=l;t++)
StringGrid1->Cells[t][h+2]=FloatToStr(Stu_Aver(t-1,Head_del,Last_del,true));

pt++;
}
else {MessageBox(Handle,"Please Complete the data or the data is not match!","Warning!" ,1);return;}

}
//---------------------------------------------------------------------------
void TForm1::PaiXu(int i,int j,bool mode)
{

float Temp,p;
Swap();
if(mode)
{
for(int j0=0;j0<j;j0++)
{
for(int q=0;q<i;q++)
{
Temp=tmp[q][j0];
for(int i0=q;i0<i;i0++)
if(Temp<tmp[i0][j0]) {p=Temp;Temp=tmp[i0][j0];tmp[i0][j0]=p;}
tmp[q][j0]=Temp; //*(*(tmp+q)+j0)=A[q][j0];
}
}
}
else
{
for(int i0=0;i0<i;i0++)
{
for(int q=0;q<j;q++)
{
Temp=tmp[i0][q];
for(int j0=q;j0<j;j0++)
if(Temp<tmp[i0][j0]) {p=Temp;Temp=tmp[i0][j0];tmp[i0][j0]=p;}
tmp[i0][q]=Temp; //*(*(tmp+i0)+q)=A[i0][q];
}
}

}
}
float TForm1::Stu_Aver(int p,int Head,int Last,bool mode)
{
if(mode)
{
float Sum=0;
for(int i=Head;i<h-Last;i++)
Sum+=tmp[i][p]; //*(*(tmp+i)+p)=A[i][p];
return Sum/(h-Head-Last);
}
else
{float Sum=0;
for(int i=Head;i<l-Last;i++)
Sum+=tmp[p][i]; //A[p][i];
return Sum/(l-Head-Last);

}
}
void __fastcall TForm1::StuZongheHeng1Click(TObject *Sender)
{
//Refresh();
if(OnRead0(pt))
{ AnsiString S="2";
int Head_del,Last_del;
//float emp[N][N];
PaiXu(h,l,false);
if(InputQuery("Input the Head_DEL Number","The Head_DEL Number(5)",S))
Head_del=StrToInt(S);
else Head_del=2;
if(InputQuery("Input the Last_DEL Number","The Last_DEL Number(5)",S))
Last_del=StrToInt(S);
else Last_del=2;
for(int i=0;i<h;i++)
for(int j=0;j<l;j++)
StringGrid1->Cells[j+1][i+1]=FloatToStr(tmp[i][j]);
StringGrid1->ColCount=l+3;
for(int t=1;t<=h;t++)
StringGrid1->Cells[l+2][t]=FloatToStr(Stu_Aver(t-1,Head_del,Last_del,false));

pt++;
}
else {MessageBox(Handle,"Please Complete the data or the data is not match!","Warning!" ,1);return;}


}
//---------------------------------------------------------------------------
void TForm1::Swap()
{

for(int i=0;i<h;i++)
for(int j=0;j<l;j++)
tmp[i][j]=A[i][j];
}
void __fastcall TForm1::Saveini1Click(TObject *Sender)
{
SaveDialog1->Filter="INI Files(*.INI)|*.ini";
if(SaveDialog1->Execute())
{

TIniFile *IniSave=new TIniFile(SaveDialog1->FileName+".ini");
IniSave->WriteString("Col_Row","Row",IntToStr(h));
IniSave->WriteString("Col_Row","Col",IntToStr(l));
for(int i=0;i<h;i++)
for(int j=0;j<l;j++)
IniSave->WriteString("DATA","A["+IntToStr(i)+","+IntToStr(j)+"]",StringGrid1->Cells[j+1][i+1]);
delete IniSave;
}

}
//---------------------------------------------------------------------------

void __fastcall TForm1::Openini1Click(TObject *Sender)
{
OpenDialog1->Filter="INI Files(*.INI)|*.ini";
if(OpenDialog1->Execute())
{
TIniFile *IniOpen=new TIniFile(OpenDialog1->FileName);
h=StrToInt(IniOpen->ReadString("Col_Row","Row","0"));
l=StrToInt(IniOpen->ReadString("Col_Row","Col","0"));
Edit1->Text=IniOpen->ReadString("Col_Row","Row","0");
Edit2->Text=IniOpen->ReadString("Col_Row","Col","0");
StringGrid1->RowCount=h+1;
StringGrid1->ColCount=l+1;
OnWrite0(h,l);
for(int i=0;i<h;i++)
for(int j=0;j<l;j++)
StringGrid1->Cells[j+1][i+1]=IniOpen->ReadString("DATA","A["+IntToStr(i)+","+IntToStr(j)+"]","0");
delete IniOpen;
}

}
//---------------------------------------------------------------------------

But there are some mistakes!Please give me some advices!thinks!
...全文
190 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
wjzhuang 2001-06-27
  • 打赏
  • 举报
回复
agree with holyfire
holyfire 2001-06-27
  • 打赏
  • 举报
回复
StrToFloat(->ReadString(..));
is a best way
it can find out if data isn't a float
as
A[3,6]=b.56
孩皮妞野 2001-06-27
  • 打赏
  • 举报
回复
NOPE.
dycdyc123 2001-06-27
  • 打赏
  • 举报
回复
OH!
You know it is not Int!
Can I use ReadFloat(...);
Or StrToFloat(->ReadString(..));
which???
wjzhuang 2001-06-27
  • 打赏
  • 举报
回复
ReadInteger("DATA","A[3][4]",0);
~~~~~
就可以了
dycdyc123 2001-06-27
  • 打赏
  • 举报
回复
[DATA]
A[0,0]=215
A[0,1]=0.254
A[0,2]=0.025
A[0,3]=0.269
A[0,4]=0.25
A[0,5]=0.014
A[0,6]=6.3
A[1,0]=217
A[1,1]=0.254
A[1,2]=0.0254
A[1,3]=0.147
A[1,4]=0.24
A[1,5]=0.3658
A[1,6]=7.3
A[2,0]=236
A[2,1]=0.214
A[2,2]=0.0254
A[2,3]=0.014
A[2,4]=0.254
A[2,5]=0.004
A[2,6]=7
A[3,0]=125
A[3,1]=0.35
A[3,2]=0.35
A[3,3]=0.254
A[3,4]=
A[3,5]=
A[3,6]=
[Col_Row]
Row=4
Col=7

Just like A[3,4]= it may not read "0" why??wjzhuang(醉里挑灯看剑--程序猪)
luhongjun 2001-06-27
  • 打赏
  • 举报
回复
请说清楚程序出现了什么错误。
编译和链接时有什么错误提示?
不然这么多代码怎么看。
dycdyc123 2001-06-27
  • 打赏
  • 举报
回复
But when like this:
[Col_Row]
Col=
In my programm,
It will not the "0",
why?
It may be not read the default!
wjzhuang 2001-06-27
  • 打赏
  • 举报
回复
third paramer :就是默认的值,

比如:ReadString("Col_Row","Col","is me");
如果
[Col_Row]
Col=
则取出的是is me
如果
[Col_Row]
Col= i am here
则取出的是i am here

smartboyme 2001-06-27
  • 打赏
  • 举报
回复
I regret to say that I have not receive your program!
dycdyc123 2001-06-27
  • 打赏
  • 举报
回复
ReadString("Col_Row","Col","0");
Please tell me the third paramer meaning!
Thinks!
dycdyc123 2001-06-27
  • 打赏
  • 举报
回复
smartboyme(小东):
Are you receive my EXE???
what are you feeling???
dycdyc123 2001-06-27
  • 打赏
  • 举报
回复
OK!
ReadInteger(...)it's right!
But There are some other important!
wjzhuang 2001-06-26
  • 打赏
  • 举报
回复
>>StrToInt(IniOpen->ReadString("Col_Row","Row","0"));
为何不直接写成int类型的数据
读的话也可以使用 ReadInteger(...)来读取
dycdyc123 2001-06-26
  • 打赏
  • 举报
回复
Ok!
smartboyme 2001-06-26
  • 打赏
  • 举报
回复
你发程序发给我吧!
总觉得你的程序怪怪的!
aprise@fm365.com

13,825

社区成员

发帖
与我相关
我的任务
社区描述
C++ Builder相关内容讨论区
社区管理员
  • 基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧