📄 main.cpp
字号:
//*****************************************************//
//
// Unit: main.cpp
//
// Purpose: Demo of Matrix Component
//
// Inherits From: TForm
//
// Project: Matrix Demo
//
// Author: Jeff Hiscock - jhiscock@rapidsi.com
//
// Date: February 2000
//
// Revisions
//
// Copyright Rapid Systems 2000
//******************************************************//
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "main.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "RsMatrixC"
#pragma resource "*.dfm"
TfrmMain *frmMain;
//---------------------------------------------------------------------------
//*******************************************************//
//
// Method: Constructor
//
// Purpose: Form constructor -- setup the grids
//
// Author: Jeff Hiscock
//
// Date: February 2000
//
// Exceptions Thrown:
//
// Parameter Description:
//
// Revisions:
//
// Scope: public
//*********************************************************//
__fastcall TfrmMain::TfrmMain(TComponent* Owner)
: TForm(Owner)
{
g1->RowCount = 3;
g1->ColCount = 3;
g2->RowCount = 3;
g2->ColCount = 3;
int i,j;
for (i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
g1->Cells[i][j] = "0";
g2->Cells[i][j] = "0";
}
}
}
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
//*******************************************************//
//
// Method: edTClick
//
// Purpose: Set top grid size
//
// Author: Jeff Hiscock
//
// Date: February 2000
//
// Exceptions Thrown: none
//
// Parameter Description: Event
//
// Revisions:
//
// Scope: published
//*********************************************************//
void __fastcall TfrmMain::edTClick(TObject *Sender)
{
g1->RowCount = edTR->Text.ToInt();
g1->ColCount = edTC->Text.ToInt();
FillGrid(g1);
}
//---------------------------------------------------------------------------
//*******************************************************//
//
// Method: edBClick
//
// Purpose: Size bottom grid
//
// Author: Jeff Hiscock
//
// Date: February 2000
//
// Exceptions Thrown: None
//
// Parameter Description:
//
// Revisions:
//
// Scope: published
//*********************************************************//
void __fastcall TfrmMain::edBClick(TObject *Sender)
{
g2->RowCount = edBR->Text.ToInt();
g2->ColCount = edBC->Text.ToInt();
FillGrid(g2);
}
//---------------------------------------------------------------------------
//*******************************************************//
//
// Method: FillGrid
//
// Purpose: Initialize the grid with zero's
//
// Author: Jeff Hiscock
//
// Date: February 2000
//
// Exceptions Thrown: None
//
// Parameter Description: TStringGrid g -- grid to size
//
// Revisions:
//
// Scope: private
//*********************************************************//
void __fastcall TfrmMain::FillGrid(TStringGrid* g)
{
//TODO: Add your source code here
int i ,j;
for(i=0;i<g->RowCount;i++)
{
for(j=0;j<g->ColCount;j++)
{
g->Cells[j][i] = "0";
}
}
}
//*******************************************************//
//
// Method: cmdCalculateClick
//
// Purpose: Calculate button event handler
//
// Author: Jeff Hiscock
//
// Date: February 2000
//
// Exceptions Thrown: None
//
// Parameter Description: Event
//
// Revisions:
//
// Scope:
//*********************************************************//
void __fastcall TfrmMain::cmdCalculateClick(TObject *Sender)
{
DoCalculate();
}
//---------------------------------------------------------------------------
//*******************************************************//
//
// Method: DoCalculate
//
// Purpose: Show the calculations
//
// Author: Jeff Hiscock
//
// Date: February 2000
//
// Exceptions Thrown: None all exceptions from the component are caught
//
// Parameter Description:
//
// Revisions:
//
// Scope: private
//*********************************************************//
void __fastcall TfrmMain::DoCalculate()
{
//TODO: Add your source code here
double **m1;
double **m2;
double ret ;
double **m3;
long row1;
long col1;
long row2;
long col2;
long row3 , col3;
bool DontDelete = false;
bool ok = true;
//allocate arrays
int i,j;
row1=g1->RowCount;
col1=g1->ColCount;
row2=g2->RowCount;
col2 = g2->ColCount;
m1 = new double*[row1];
for(int counter=0;counter<row1;counter++)
m1[counter] = new double[col1];
m2 = new double*[row2];
for(int counter=0;counter<row2;counter++)
m2[counter] = new double[col2];
//populate arrays
for (i=0;i<row1;i++)
{
for(j=0;j<col1;j++)
{
try
{
m1[i][j] = StrToFloat(g1->Cells[j][i]);
}
catch (Exception &e)
{
DeletePointer(m1,col1);
DeletePointer(m2,col2);
ShowMessage("Please enter scalars in the top matrix");
return;
}
}
}
for (i=0;i<row2;i++)
{
for(j=0;j<col2;j++)
{
try
{
m2[i][j] = StrToFloat(g2->Cells[j][i]);
}
catch (Exception &e)
{
ShowMessage("Please enter scalars in the bottom matrix");
return;
}
}
}
// get operation
if (rbProduct->Checked)
{
try
{
Matrix->Product((const double**)m1,row1,col1,(const double**)m2,row2,col2,&m3,&row3,&col3);
}
catch (Exception &e)
{
ok = false;
ShowMessage(e.Message);
}
}
else if (rbSum->Checked)
{
try
{
Matrix->Sum((const double**)m1,row1,col1,(const double**)m2,row2,col2,&m3);
row3 = row1;
col3 = col1;
}
catch (Exception &e)
{
ok = false;
ShowMessage(e.Message);
}
}
else if (rbDiff->Checked)
{
try
{
Matrix->Difference((const double**)m1,row1,col1,(const double**)m2,row2,col2,&m3);
row3 = row1;
col3 = col1;
}
catch (Exception &e)
{
ok = false;
ShowMessage(e.Message);
}
}
else if (rbTraceOf->Checked)
{
try
{
DontDelete = true;
ret = Matrix->TraceOf((const double**)m1,row1,col1);
row3 = 1;
col3 = 1;
}
catch (Exception &e)
{
ok = false;
ShowMessage(e.Message);
}
}
else if (rbTranspose->Checked)
{
try
{
Matrix->Transpose((const double**)m1,row1,col1,&m3);
row3 = col1;
col3 = row1;
}
catch (Exception &e)
{
ok = false;
ShowMessage(e.Message);
}
}
if (ok)
{
g3->RowCount = row3;
g3->ColCount = col3;
if (!DontDelete)
{
for (i=0;i<row3;i++)
{
for (j=0;j<col3;j++)
{
g3->Cells[j][i] = FloatToStr(m3[i][j]);
}
}
}
else
{
g3->Cells[0][0] = FloatToStr(ret);
}
}
//cleanup
DeletePointer(m1,row1);
DeletePointer(m2,row2);
if (!DontDelete)
{
if (ok)
{
DeletePointer(m3,row3);
}
}
}
//*******************************************************//
//
// Method: DeletePointer
//
// Purpose: Free up memory for dynamically created arrays
//
// Author: Jeff Hiscock
//
// Date: February 2000
//
// Exceptions Thrown:
//
// Parameter Description: double** value - the pointer
// int rows -- number of rows or first dimmension of the array
//
// Revisions:
//
// Scope: private
//*********************************************************//
void __fastcall TfrmMain::DeletePointer(double** value, int rows)
{
//TODO: Add your source code here
for(int counter=0;counter<rows;counter++)
delete[] value[counter];
delete[] value;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -