马上要期末考试了,很多长方体计算题,5分钟写了个长方体表面积以及体积计算小工具,先看效果
输入长宽高,马上出结果
然后看下我们的需求分析
了解当前的计算公式。
设计输入输出
方案设计:
代码编写:
测试:
一气呵成,用时5分钟
代码如下
form代码
object Form5: TForm5
Left = 715
Top = 312
Caption = #38271#26041#20307#34920#38754#31215#19982#20307#31215#35745#31639#24037#20855
ClientHeight = 538
ClientWidth = 666
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -12
Font.Name = 'Segoe UI'
Font.Style = []
Position = poDesigned
TextHeight = 15
object btn1: TBitBtn
Left = 533
Top = 55
Width = 75
Height = 25
Caption = #35745#31639
TabOrder = 0
OnClick = btn1Click
end
object lbledt1: TLabeledEdit
Left = 48
Top = 57
Width = 121
Height = 23
EditLabel.Width = 13
EditLabel.Height = 15
EditLabel.Caption = #38271
TabOrder = 1
Text = ''
TextHint = #36755#20837#38271#24230
end
object lbledt2: TLabeledEdit
Left = 210
Top = 57
Width = 121
Height = 23
EditLabel.Width = 13
EditLabel.Height = 15
EditLabel.Caption = #23485
TabOrder = 2
Text = ''
TextHint = #36755#20837#23485#24230
end
object lbledt3: TLabeledEdit
Left = 371
Top = 57
Width = 121
Height = 23
EditLabel.Width = 13
EditLabel.Height = 15
EditLabel.Caption = #39640
TabOrder = 3
Text = ''
TextHint = #36755#20837#39640#24230
end
object Memo1: TMemo
Left = 0
Top = 200
Width = 666
Height = 338
Align = alBottom
TabOrder = 4
end
object btn2: TBitBtn
Left = 533
Top = 144
Width = 75
Height = 25
Caption = #25302#24335#35745#31639
TabOrder = 5
OnClick = btn2Click
end
object lbledt4: TLabeledEdit
Left = 40
Top = 145
Width = 449
Height = 23
EditLabel.Width = 26
EditLabel.Height = 15
EditLabel.Caption = #24335#23376
TabOrder = 6
Text = ''
TextHint = #36755#20837#35745#31639#24335#23376#25903#25345#21152#20943#20056#38500#28151#21512#36816#31639'96+52+4+8'
OnChange = lbledt4Change
end
end
程序代码
unit Unit5;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.Mask, Vcl.ExtCtrls, expclass,
Vcl.Buttons;
type
TForm5 = class(TForm)
btn1: TBitBtn;
lbledt1: TLabeledEdit;
lbledt2: TLabeledEdit;
lbledt3: TLabeledEdit;
Memo1: TMemo;
btn2: TBitBtn;
lbledt4: TLabeledEdit;
procedure btn1Click(Sender: TObject);
procedure btn2Click(Sender: TObject);
procedure lbledt4Change(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form5: TForm5;
implementation
{$R *.dfm}
procedure TForm5.btn1Click(Sender: TObject);
var
nl,nw,nh:Double;
nbmj,nvol:Double;
begin
nl:=StrToFloatDef(lbledt1.Text,0);
nw:=StrToFloatDef(lbledt2.Text,0);
nH:=StrToFloatDef(lbledt3.Text,0);
nbmj:=(NL*NW+NL*NH+NW*NH)*2;
nvol:=NL*NW*NH;
Memo1.Lines.Add(format('长=%.2f,宽=%.2f 高:%.2f 表面积 :%.2f,体积:%.2f',[nl,NW,NH,nbmj,NVOL]));
end;
procedure TForm5.btn2Click(Sender: TObject);
var
ss:TExpression;
ns:Extended;
begin
try
ss:=TExpression.Create((lbledt4.Text));
ns:=ss.Calc;
if ss.oResok then
Memo1.Lines.Add(Format('%s=%.2f',[lbledt4.Text,ns]))
else
Memo1.Lines.Add(Format('%s 格式不正确',[lbledt4.Text]));
btn2.Enabled:=False;
except
on e:Exception do
Memo1.Lines.Add(e.Message);
end;
end;
procedure TForm5.lbledt4Change(Sender: TObject);
begin
btn2.Enabled:=True;
end;
end.
unit expclass;
interface
uses
SysUtils, Math;
const
CALCERR = -999999999999.99;
type
TExpression = class //表达式 类
private
CurPos: integer; //字符位置
MaxPos: integer; //最大
iExp: string; //输出信息
token: string; //记号
dataList: array of Double;
sz: Double;
function IsDigit(ch: char): boolean; //是不是数字
function IsLetter(ch: char): boolean; //是不是字母
function CheckExp: boolean; // 首先对表达式进行检查
function gettoken: integer; //获取记号
function term: extended; //分组
function factor: extended; //因数
public
oResok: boolean;
constructor Create(AExpression: string);
function Calc: extended;
end;
const
ID = 1; // var
NUM = 2; // numbers
PLUS = 3; // '+'
MINUS = 4; // '-'
TIMERS = 5; // '*'
OVER = 6; // '/'
LPAREN = 7; // '('
RPAREN = 8; // ')'
POWers = 9; // '^'
ERROR = 255; // Errors
OPS = '+-*/()^'; // Oprators 优先级
implementation
function TExpression.IsLetter(ch: Char): boolean;
begin
if (ch in ['a'..'z']) or (ch in ['A'..'Z']) then
result := true
else
result := false;
end;
function TExpression.IsDigit(ch: Char): boolean;
begin
if (ch in ['0'..'9']) or (ch = '.') then
result := True
else
Result := False;
end;
// Get Token and Token Type from curPos
function TExpression.gettoken: integer;
var
Skip: boolean;
begin
result := ERROR; //错误优先级最高 255
Skip := False; // 指示是否忽略指针移动
token := ''; //标记值为 空 ""
while (CurPos <= MaxPos) and (iExp[CurPos] = ' ') do //当前光标小于最大位数 并且 空格 增加一位
inc(CurPos);
// 加入对负号的处理:如果前次token为 '(',或第一个符号就是'-',则减号是负号
if Pos(iExp[CurPos], OPS) > 0 then //运算符处理
begin
case iExp[CurPos] of
'+': result := PLUS;
'-':
begin
if (CurPos > 1) and (iExp[CurPos - 1] <> '(') then
result := MINUS
else
begin
token := '-';
inc(CurPos);
while (CurPos <= MaxPos) and IsDigit(iExp[CurPos]) do
begin
token := token + iExp[CurPos];
sz := StrToFloat(token);
inc(CurPos);
end;
Skip := True;
result := NUM;
end;
end;
'*': result := TIMERS;
'/': result := OVER;
'(': result := LPAREN;
')': result := RPAREN;
'^': result := POWERs;
end;
if not Skip then
inc(CurPos);
end
else
begin
if IsLetter(iExp[CurPos]) then //维持在字母状态
begin
while (CurPos <= MaxPos) and (IsLetter(iExp[CurPos]) or
IsDigit(iExp[CurPos])) do
begin
token := token + iExp[CurPos];
inc(CurPos);
end;
result := ID;
end
else if IsDigit(iExp[CurPos]) then //维持在数字状态
begin
while (CurPos <= MaxPos) and IsDigit(iExp[CurPos]) do
begin
token := token + iExp[CurPos]; // token=2 tcken=5
inc(CurPos);
end;
result := NUM;
end;
end;
if result = ERROR then
oResok := false;
end;
function TExpression.term: extended; //首次计算
var
temp: extended;
begin
result := factor(); // 拆解
while (CurPos <= MaxPos) and ((iExp[CurPos] = '*') or (iExp[CurPos] = '/') or
(iExp[CurPos] = '^'))
do // 乘除号 优先
begin
case gettoken() of
POWers: Result := Power(Result, factor);
TIMERS:
result := result * factor();
OVER:
begin
temp := factor;
if temp <> 0 then
result := result / temp
else
begin
oResok := false;
result := 0;
end;
end;
end;
end;
end;
function TExpression.factor(): extended;
begin
result := 0.0; //等于0?
case gettoken() of //获取元素标记
NUM:
begin
result := StrtoFloatDef(token, 0); //数字1 ,2
sz := result;
end;
LPAREN: //左括号
begin
result := calc;
if gettoken() <> RPAREN then
begin
oResok := false;
result := 0;
end;
end;
POWers:
begin
result := Power(sz, factor);
end;
ID:
begin
if token = 'cos' then
begin
Result := factor();
Result := Cos(Result);
end
else if token = 'sin' then
begin
Result := factor();
Result := Sin(Result);
end
else if token = 'tan' then
begin
Result := factor();
Result := Tan(Result);
end
else if token = 'cot' then
begin
Result := factor();
Result := Cotan(Result);
end
else if token = 'ln' then
begin
Result := factor();
Result := Ln(Result);
end
else if token = 'e' then
begin
Result := 2.718281828459;
// Result := Ln(Result);
end
else if token = 'pi' then
begin
Result := 3.1415926535898;
end
else
Result := ERROR;
end;
end;
end;
function TExpression.CheckExp: boolean;
begin
Result:=Length( Trim(iExp))>0;
// result := true;
end;
constructor TExpression.Create(AExpression: string);
begin
iExp := LowerCase(AExpression);
CurPos := 1;
MaxPos := Length(iExp);
oResok := CheckExp;
end;
function TExpression.Calc: extended; //类函数
begin
if oResok then
begin
result := term(); //优先计算乘除
while (CurPos <= MaxPos) and ((iExp[CurPos] = '+') or (iExp[CurPos] = '-'))
do
//加减号其次 ,获取加号
begin
case gettoken() of
PLUS:
result := result + term();
MINUS:
result := result - term();
end;
end;
end
else
result := 0;
end;
end.
| 留言与评论(共有 0 条评论) “” |