When it comes to calculating mathematical expressions, different people have their own different approaches. Some use the method of analyzing expressions. That method requires compilation knowledge. The method used below is very simple, and complex operations can be performed without a few codes. If there is an error in the expression, a dialog box can also pop up to indicate the corresponding error. This program uses standard SQL statements, which support many operations, such as: addition, subtraction, multiplication, division, and division; logical operations such as: AND, OR, XOR, NOT; mathematical operations such as: COS, SIN, etc. There are also string operations, but that doesn't make sense. If you type "Left('Abcd',2)", it will calculate "=Ab". Since this program is in accordance with the above-mentioned purpose, there are no restrictions on these, interested friends can check the syntax of the expression in advance and then hand it over to this program for calculation.
{
All rights reserved. Please indicate the source when reprinting
Project: Calculation of mathematical expressions
Author: Huang Tao [email protected]
Purpose: Learn how to use exception classes
}
unit uCalcExpr;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, DB, ADODB;
type
TfrmCalc = class(TForm)
Button1: TButton;
Edit1: TEdit;
procedure Button1Click(Sender: TObject);
procedure Edit1KeyPress(Sender: TObject; var Key: Char);
Private
{ Private declarations }
public
{ Public declarations }
end;
function CalcExpr(Expr :string):string; // Function that calculates mathematical expressions, where Expr is an expression
var
frmCalc: TfrmCalc;
Implementation
{$R *.dfm}
//This is the core of this program, and it depends on it to calculate expressions.
function CalcExpr(Expr :string):string;
var query: TADODataSet;
Begin
qury := TADODataset.Create(nil);
try
qury.ConnectionString := 'Provider=MSDASQL.1;Persist Security Info=False;' +
'Data Source=dBASE Files';
qury.CommandText := 'select '+ Expr + ' as Result';
qury.Open;
result := Expr +' = ' + qury.fieldbyname('Result').AsString;
Finally
query.Free ;
end;
end;
procedure TfrmCalc.Button1Click(Sender: TObject);
Begin
try
edit1.Text := CalcExpr (edit1.Text);
except
on h:exception do begin
showmessage(h.Message);
end;
end;
end;
procedure TfrmCalc.Edit1KeyPress(Sender: TObject; var Key: Char);
Begin
if key = #13 then Button1Click(self);
end;
end.
Summary: From this we can see that there are two methods for catching exceptions. One is to use TRY...EXCEPT...END; the other is to use TRY...FINALLY...END. These two methods of catching exceptions are different, and each has its own uses.
TRY...EXCEPT...END is generally used when we need to catch the error and make corresponding prompts after a certain method is called. For example, when calling a function that calculates an expression in the above program, the function that calculates an expression is likely to throw an exception, so when calling, it must catch its exception, make corresponding prompts, or perform related subsequent processing to avoid It causes system errors and affects the beauty of the program interface. If we simply report an error, we can simply write the corresponding prompt for reporting an error in the middle of EXCEPT...END. If you want to see which kind of error is, you must follow the above practice to catch the error.
TRY...FINALLY...END is generally used to perform related operations after allocating a certain resource. This operation is likely to cause an exception, but we must release this after these operations are completed. kind of resources. With the TRY...FINALLY...END statement, we can place the operation statement completely in TRY...FINALLY after allocating resources, and imagine that this can be executed completely smoothly. If any step cannot be executed, it will jump out of this circle and enter FINALLY without performing it in sequence. In FINALLY...END, we can carry out some aftermath work, such as releasing the resources allocated at the time, because no matter whether the above is executed smoothly or when encountering exceptions, the statements here will be executed.
It can be seen that the above statement is set for catching exceptions, and the following statement is set for resource allocation, but some friends say, what should we do if we want to catch exceptions and recover resources? Unfortunately, Borland does not have TRY...EXCEPT...FINALLY...END. If you want to achieve the above goals, you can only use nesting, but doing that will be very unpleasant.
In short, the rational use of exception classes can greatly reduce our programming and make our program ideas clearer. The program written is more beautiful.