Mathematical expression calculation - How to capture abnormalities

xiaoxiao2021-03-06  140

In the process of writing procedures, it is inevitable that there will be a variety of situations, and some cases may not be what we want. Some people may use a lot of judgment to strictly control the program so that they do not have an abnormal situation, but doing so may increase the difficulty of writing procedures. In fact, we can also use a simpler way, that is abnormal. Reasonable use exceptions in the program will make you save a lot of troubles in the write program. In Delphi, Borland has defined a lot of abnormal classes for us, we can use them directly. But if we don't deserve an abnormal, you will make the interface of the program seventeen, and below will introduce two commonly used exception capture methods.

About calculating mathematical expressions, different people have their own different practices. Some use analytical expressions, that method requires compilation knowledge, the method used below is simple, and there is no complicated operation. If there is an error in the expression, you can also pop up the dialog prompt and point out the corresponding error. This procedure is the standard SQL statement, supports many operations, basic operations such as plus, minus, multiply, divided, unless; logical operations such as: And, OR, XOR, NOT; mathematical operations such as COS, SIN, etc. There is also a string operation, but it doesn't make sense, if you enter "Left ('ABCD', 2)", it will calculate "= ab". Because this app, this is the purpose of the above, there is no limit to these, and interested friends can check the syntax of the expression in advance, and then hand it over to this program.

{All rights reserved Please indicate the source project: the calculation of the mathematical expression: Tao Hunter@Shentong.com.cn Objective: To learn how to use the exception class} 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; // Computational Mathematical Expression of the function, where expr is the expression var frmcalc: tfrmcalc;

IMPLEMENTATION

{$ R * .dfm}

// This is the core of this program, calculating expression of Function Calcexpr (expr: string): string; var Qury: 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 Qury .Free; end; project: = calce: tobject); begin try edit1.text: = cagcexpr (edit1.text); Except on h: Exception do begin ShowMessage (H.MESSAGE); END; ;

Procedure tfrmcalc.edit1keypress (Sender: Tobject; var key: char); begin if key = # 13 dam1click (self);

End.

Summary: This shows that there are two ways to capture abnormal capture, one is to use try ... ExcePt ... END; the other is Try .... Finally .... End. These two methods of capturing abnormalities are different, each has its own use.

Try ... Except ... END is generally used to capture its errors and make the corresponding prompts after calling some way. As in the above program calculating the function of the expression, the function of calculating the expression is likely to throw an exception, so it must capture its exceptions when the call is called, and the corresponding prompt is made, or the relevant subsequent processing is required. It causes the system error to affect the aesthetics of the program interface. If we are just a simple report, you can simply write the corresponding prompt of the error in ExcePt ... End. If you want to see which error is to capture the error.

Try .... finally .... END is generally used, we have related operations after allocating a certain resource, and this operation is likely to cause an exception, but we must release this after these operations. Resources. With Try .... finally .... End, after this statement, we can rest assured that the operation statement is completely in Try .... finally after allocating resources, and the imagination can be fully successfully implemented. The situation is to operate, because if it doesn't do it, it will jump out of this circle into Finally, and it will not perform it in order. In Finally .... and we can do some afterwards, such as released the resource allocated, because no matter what is successful, or encounters an exception to execute the statement in this.

It can be seen that the above statement is set to capture an abnormality. The following statement is set for resource allocation, but some friends say, if we want to capture an exception, what should I do if I want to recover the resources? Unfortunately, Borland did not use the statement like Try ... End ... End, if you want to reach the above purpose, you can only use nested, but you will do it very well.

In short, reasonable use of anomalous class can greatly reduce our programming, so that our program is more clever. The program written is more beautiful.

转载请注明原文地址:https://www.9cbs.com/read-127358.html

New Post(0)