这个问题50分!!求1+6+9/(4-1)-7*8=?(50分)

  • 主题发起人 主题发起人 linxiwen
  • 开始时间 开始时间
L

linxiwen

Unregistered / Unconfirmed
GUEST, unregistred user!
[red]怎样在Edit1中计算1+6+9/(4-1)-7*8=?[/red]
要求:①Edit1.text='1+6+9/(4-1)-7*8=?'
②按Button1
③使Edit1.text为这个算式的答案。
 
这个要用IntToStr转,把INT转化为str
edit1.text:=IntToStr(1+6+9/(4-1)-7*8);
 
hehe,
it's too easy
 
Edit.Text := Format('%f', [1+6+9/(4-1)-7*8]);
 
Edit1.text:=inttostr(1+6+trunc(9/(4-1))-7*8);
 
http://www.csdn.net/cnshare/soft/4/4573.shtm
去载个公式解析控件
 
to QuickSilver:
只有一个DLL,有说明吗,比如函数有哪些,接口定义规范等
 
解析四则运算公式你可以开两个队列,一个存放运算符,一个存放运算数,
遇到优先级高的运算(例如,( ,* ,/ )就将运算符和运算数入队,遇到同级运算就按顺序进行
直到无法入队为止,计算出队首的这个二元表达式的值,然后出队,将值替换到新的运算数队列首中。
如此反复,直到解析完整个表达式,运算数队列最后一个出队的数字便是表达式的值。
 

后面几位有病呀 only u 的说法多么简单 还用你们那么费劲吗
你们是不是看问题不看别人的回复呀
 
edit1.text:=IntToStr(1+6+9/(4-1)-7*8);
这样不行的,如果表达式改了,你的程序就要重新编译,
那这样叫人家输入表达式有什么意义?
 
ycrj.delphibbs.com的精选控件ForumLib组件中有一个dxfExpressionExplorer控件就是专门做这个的,还能执行PASCAL代码定义函数
 
如果仅仅是解析四则混合运算的话,没必要那么麻烦,几十行代码就搞定了。
 
// built by Liu Yang 2002.1.8
library Expression;
uses Dialogs, Math, SysUtils;
Const
Symbol_Mod='M';
Symbol_Div='D';
Symbol_Shl='L';
Symbol_Shr='R';
Symbol_Or='O';
Symbol_Xor='X';
Symbol_And='A';
function ConvertExpression(ExpressionString:PChar):PChar;
stdcall;
var inputexp:string;
begin
inputexp:=ExpressionString;
//convert input expression to recognize expression
if pos('=',inputexp)=0 then
inputexp:=inputexp+'=' else
inputexp:=Copy(inputexp,1,Pos('=',inputexp));
inputexp:=UpperCase(inputexp);
inputexp:=StringReplace(inputexp,' ','',[rfReplaceAll]);
inputexp:=StringReplace(inputexp,'MOD',Symbol_Mod,[rfReplaceAll]);
inputexp:=StringReplace(inputexp,'DIV',Symbol_Div,[rfReplaceAll]);
inputexp:=StringReplace(inputexp,'AND',Symbol_And,[rfReplaceAll]);
inputexp:=StringReplace(inputexp,'XOR',Symbol_Xor,[rfReplaceAll]);
inputexp:=StringReplace(inputexp,'OR',Symbol_Or,[rfReplaceAll]);
inputexp:=StringReplace(inputexp,'SHL',Symbol_Shl,[rfReplaceAll]);
inputexp:=StringReplace(inputexp,'SHR',Symbol_Shr,[rfReplaceAll]);
inputexp:=StringReplace(inputexp,'(-','(0-',[rfReplaceAll]);
if pos('-',inputexp)=1 then
inputexp:='0'+inputexp;
Result:=PChar(inputexp);
end;

function ParseExpression(ExpressionString:PChar): extended;
stdcall;
var
nextch:char;
nextchpos,position:word;
inputexp:string;
procedure expression(var ev:extended);forward;
procedure readnextch;
begin
repeat
if inputexp[position]='=' then
nextch:='='
else
begin
inc(nextchpos);
inc(position);
nextch:=inputexp[position];
end;
until (nextch<>' ') or eoln;
end;
procedure error(ErrorString:string);
begin
MessageDlg('Unknown expression : '+ErrorString,mterror,[mbok],0);
exit;
end;
procedure number(var nv:extended);
var radix:longint;
snv:string;
function BinToInt(value: string): integer;
var i,size:integer;
begin
// convert binary number to integer
result:=0;
size:=length(value);
for i:=size do
wnto 1 do
if copy(value,i,1)='1'
then
result:=result+(1 shl (size-i));
end;
begin
nv:=0;
snv:='';
while nextch in ['0'..'9','A'..'F'] do
begin
// nv:=10*nv+ord(nextch)-ord('0');
snv:=snv+nextch;
readnextch;
end;
// parse Hex, Bin
if snv<>'' then
if snv[Length(snv)]='B'
then
nv:=BinToInt(Copy(snv,1,Length(snv)-1))
else
if nextch='H' then
begin
nv:=StrToInt('$'+snv);
readnextch;
end
else
nv:=StrToInt(snv);
if nextch='.' then
begin
radix:=10;
readnextch;
while nextch in ['0'..'9'] do
begin
nv:=nv+(ord(nextch)-ord('0'))/radix;
radix:=radix*10;
readnextch;
end;
end;
end;
procedure factor(var fv:extended);
Var Symbol:string;
function CalcN(Value:integer):extended;
var i:integer;
begin
Result:=1;
if Value=0 then
Exit
else
for i:=1 to Value do
Result:=Result*i;
end;
function ParseFunction(var FunctionSymbol:string):boolean;
begin
FunctionSymbol:='';
while not (nextch in ['0'..'9','.','(',')','+','-','*','/','=']) do
begin
FunctionSymbol:=FunctionSymbol+nextch;
readnextch;
end;
if FunctionSymbol='ABS' then
Result:=true else
if FunctionSymbol='SIN' then
Result:=true else
if FunctionSymbol='COS' then
Result:=true else
if FunctionSymbol='TG' then
Result:=true else
if FunctionSymbol='TAN' then
Result:=true else
if FunctionSymbol='ARCSIN' then
Result:=true else
if FunctionSymbol='ARCCOS' then
Result:=true else
if FunctionSymbol='ARCTG' then
Result:=true else
if FunctionSymbol='ARCTAN' then
Result:=true else
if FunctionSymbol='LN' then
Result:=true else
if FunctionSymbol='LG' then
Result:=true else
if FunctionSymbol='EXP' then
Result:=true else
if FunctionSymbol='SQR' then
Result:=true else
if FunctionSymbol='SQRT' then
Result:=true else
if FunctionSymbol='PI' then
Result:=true else
if FunctionSymbol='NOT' then
Result:=true else
if FunctionSymbol='N!' then
Result:=true else
if FunctionSymbol='E' then
Result:=true else
Result:=false;
end;
begin
Case nextch of
'0'..'9' : number(fv);
'(' : begin
readnextch;
expression(fv);
if nextch=')'
then
readnextch else
error(nextch);
end
else
if ParseFunction(Symbol) then
if nextch='(' then
begin
readnextch;
expression(fv);
if Symbol='ABS' then
fv:=abs(fv) else
if Symbol='SIN' then
fv:=sin(fv) else
if Symbol='COS' then
fv:=cos(fv) else
if Symbol='TG' then
fv:=tan(fv) else
if Symbol='TAN' then
fv:=tan(fv) else
if Symbol='ARCSIN' then
fv:=arcsin(fv) else
if Symbol='ARCCOS' then
fv:=arccos(fv) else
if Symbol='ARCTG' then
fv:=arctan(fv) else
if Symbol='ARCTAN' then
fv:=arctan(fv) else
if Symbol='LN' then
fv:=ln(fv) else
if Symbol='LG' then
fv:=ln(fv)/ln(10) else
if Symbol='EXP' then
fv:=exp(fv) else
if Symbol='SQR' then
fv:=sqr(fv) else
if Symbol='SQRT' then
fv:=sqrt(fv) else
if Symbol='NOT' then
fv:=not(Round(fv)) else
if Symbol='N!' then
fv:=CalcN(Round(fv)) else
error(symbol);
if nextch=')' then
readnextch else
error(nextch);
end else
begin
// parse constant
if Symbol='PI' then
fv:=3.14159265358979324 else
if Symbol='E' then
fv:=2.71828182845904523 else
error(symbol);
end else
begin
error(Symbol);
fv:=1;
end;
end;
end;
procedure Power_(var pv:extended);
var
multiop:char;
fs:extended;
begin
factor(pv);
while nextch in ['^'] do
begin
multiop:=nextch;
readnextch;
factor(fs);
case multiop of
'^':if pv<>0.0 then
pv:=exp(ln(pv)*fs) else
error(multiop);
end;
end;
end;
procedure term_(var tv:extended);
var
multiop:char;
fs:extended;
begin
Power_(tv);
while nextch in ['*','/',Symbol_Mod,Symbol_Div,Symbol_And,Symbol_Shl,Symbol_Shr] do
begin
multiop:=nextch;
readnextch;
Power_(fs);
case multiop of
'*':tv:=tv*fs;
'/':if fs<>0.0 then
tv:=tv/fs else
error(multiop);
Symbol_Mod:tv:=round(tv) mod round(fs);
// prase mod
Symbol_Div:tv:=round(tv) div round(fs);
// parse div
Symbol_And:tv:=round(tv) and round(fs);
// parse and
Symbol_Shl:tv:=round(tv) shl round(fs);
// parse shl
Symbol_Shr:tv:=round(tv) shr round(fs);
// parse shr
end;
end;
end;
procedure expression(var ev:extended);
var
addop:char;
fs:extended;
begin
term_(ev);
while nextch in ['+','-',Symbol_Or,Symbol_Xor] do
begin
addop:=nextch;
readnextch;
term_(fs);
case addop of
'+':ev:=ev+fs;
'-':ev:=ev-fs;
Symbol_Or:ev:=round(ev) or round(fs);
// parse or
Symbol_Xor:ev:=round(ev) xor round(fs);
// parse xor
end;
end;
end;
begin
inputexp:=ConvertExpression(ExpressionString);
if pos('=',inputexp)=0 then
inputexp:=ConvertExpression(ExpressionString);
position:=0;
while inputexp[position]<>'=' do
begin
nextchpos:=0;
readnextch;
expression(result);
end;
end;

function ParseExpressionToStr(ExpressionString:PChar):PChar;
stdcall;
var ES:string;
begin
ES:=ExpressionString;
if pos('=',ES)=0
then
ES:=ES+'='
else
ES:=Copy(ES,1,Pos('=',ES));
ES:=ES+FormatFloat('0.000000000000',ParseExpression(ExpressionString));
Result:=PChar(ES);
end;

function Version:PChar;
stdcall;
begin
Result:='Calculator Dll Build 2001.10.25 Made By Liu Yang All Rights Reserved';
end;

Exports
ConvertExpression, ParseExpression, ParseExpressionToStr, Version;
end.
 
这个也行
(*//
标题:计算表达式
说明:加、减、乘、除及括号;请大家多多测试
设计:Zswang
日期:2002-01-26
支持:wjhu111@21cn.com
//*)
///////begin
Source
uses
Math;
procedure Bracket(mText: string;
var nLStr, nCStr, nRStr: string);
var
L, R: Integer;
I: Integer;
B: Boolean;
begin
nLStr := '';
nCStr := '';
nRStr := '';
B := True;
L := 0;
R := 0;
for I := 1 to Length(mText) do
if B then
begin
if mText = '(' then
Inc(L)
else
if mText = ')' then
Inc(R);
if L = 0 then
nLStr := nLStr + mText
else
if L > R then
nCStr := nCStr + mText
else
B := False;
end else
nRStr := nRStr + mText;
Delete(nCStr, 1, 1);
end;
{ Bracket }
function Calc(mText: string): string;
var
vText: string;
function fCalc(mText: string): string;
var
vLStr, vCStr, vRStr: string;
I, J, K, L: Integer;
begin
L := Length(mText);
if Pos('(', mText) > 0 then
begin
Bracket(mText, vLStr, vCStr, vRStr);
Result := fCalc(vLStr + fCalc(vCStr) + vRStr);
end else
if (Pos('+', mText) > 0) or (Pos('-', mText) > 0) then
begin
I := Pos('+', mText);
J := Pos('-', mText);
if I = 0 then
I := L;
if J = 0 then
J := L;
K := Min(I, J);
vLStr := Copy(mText, 1, Pred(K));
vRStr := Copy(mText, Succ(K), L);
if vLStr = '' then
vLStr := '0';
if vRStr = '' then
vRStr := '0';
if I = K then
Result := FloatToStr(StrToFloat(fCalc(vLStr)) + StrToFloat(fCalc(vRStr)))
else
Result := FloatToStr(StrToFloat(fCalc(vLStr)) - StrToFloat(fCalc(vRStr)))
end else
if (Pos('*', mText) > 0) or (Pos('/', mText) > 0) then
begin
I := Pos('*', mText);
J := Pos('/', mText);
if I = 0 then
I := L;
if J = 0 then
J := L;
K := Min(I, J);
vLStr := Copy(mText, 1, Pred(K));
vRStr := Copy(mText, Succ(K), L);
if vLStr = '' then
vLStr := '0';
if vRStr = '' then
vRStr := '0';
if I = K then
Result := FloatToStr(StrToFloat(fCalc(vLStr)) * StrToFloat(fCalc(vRStr)))
else
Result := FloatToStr(StrToFloat(fCalc(vLStr)) / StrToFloat(fCalc(vRStr)))
end else
if Pos('_', mText) = 1 then
Result := FloatToStr(-StrToFloat(fCalc(Copy(mText, 2, L))))
else
Result := FloatToStr(StrToFloat(mText));
end;
var
I, L: Integer;
begin
vText := '';
L := Length(mText);
for I := 1 to L do
if (mText = '-') and (I < L) and (not (mText[Succ(I)] in ['+', '-', '(', ')'])) then
if (I = 1) or ((I > 1) and (mText[Pred(I)] in ['*', '/'])) then
vText := vText + '_'
else
if ((I > 1) and (mText[Pred(I)] in ['+', '-'])) or
((I < L) and (not (mText[Succ(I)] in ['+', '-', '(', ')']))) then
vText := vText + '+_'
else
vText := vText + mText
else
vText := vText + mText;
Result := fCalc(vText);
end;
{ Calc }
///////End Source
///////begin
Demo
procedure TForm1.Button1Click(Sender: TObject);
begin
Edit1.Text := Calc(Edit2.Text);
end;
///////End Demo
/////////////////////////////////////////////又一个
unit m;
interface
uses
Windows, Messages, Math, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;
const
Symbol_Mod = 'M';
Symbol_Div = 'D';
Symbol_Shl = 'L';
Symbol_Shr = 'R';
Symbol_Or = 'O';
Symbol_Xor = 'X';
Symbol_And = 'A';
type
TForm1 = class(TForm)
Button1: TButton;
Edit1: TEdit;
Edit2: TEdit;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;
implementation
{$R *.DFM}
function ConvertExpression(ExpressionString: PChar): PChar;
var
inputexp: string;
begin
inputexp := ExpressionString;
//convert input expression to recognize expression
if pos('=', inputexp) = 0 then
inputexp := inputexp + '='
else
inputexp := Copy(inputexp, 1, Pos('=', inputexp));
inputexp := UpperCase(inputexp);
inputexp := StringReplace(inputexp, ' ', '', [rfReplaceAll]);
inputexp := StringReplace(inputexp, 'MOD', Symbol_Mod, [rfReplaceAll]);
inputexp := StringReplace(inputexp, 'DIV', Symbol_Div, [rfReplaceAll]);
inputexp := StringReplace(inputexp, 'AND', Symbol_And, [rfReplaceAll]);
inputexp := StringReplace(inputexp, 'XOR', Symbol_Xor, [rfReplaceAll]);
inputexp := StringReplace(inputexp, 'OR', Symbol_Or, [rfReplaceAll]);
inputexp := StringReplace(inputexp, 'SHL', Symbol_Shl, [rfReplaceAll]);
inputexp := StringReplace(inputexp, 'SHR', Symbol_Shr, [rfReplaceAll]);
inputexp := StringReplace(inputexp, '(-', '(0-', [rfReplaceAll]);
if pos('-', inputexp) = 1 then
inputexp := '0' + inputexp;
Result := PChar(inputexp);
end;

function ParseExpression(ExpressionString: PChar): extended;
var
nextch: char;
nextchpos, position: word;
inputexp: string;
procedure expression(var ev: extended);
forward;
procedure readnextch;
begin
repeat
if inputexp[position] = '=' then
nextch := '='
else
begin
inc(nextchpos);
inc(position);
nextch := inputexp[position];
end;
until (nextch <> ' ') or eoln;
end;
procedure error(ErrorString: string);
begin
MessageDlg('无法识别的语法 : ' + ErrorString, mterror, [mbok], 0);
exit;
end;
procedure number(var nv: extended);
var
radix: longint;
snv: string;
function BinToInt(value: string): integer;
var
i, size: integer;
begin
// convert binary number to integer
result := 0;
size := length(value);
for i := size do
wnto 1 do
if copy(value, i, 1) = '1'
then
result := result + (1 shl (size - i));
end;
begin
nv := 0;
snv := '';
while nextch in ['0'..'9', 'A'..'F'] do
begin
// nv:=10*nv+ord(nextch)-ord('0');
snv := snv + nextch;
readnextch;
end;
// parse Hex, Bin
if snv <> '' then
if snv[Length(snv)] = 'B'
then
nv := BinToInt(Copy(snv, 1, Length(snv) - 1))
else
if nextch = 'H' then
begin
nv := StrToInt('$' + snv);
readnextch;
end
else
nv := StrToInt(snv);
if nextch = '.' then
begin
radix := 10;
readnextch;
while nextch in ['0'..'9'] do
begin
nv := nv + (ord(nextch) - ord('0')) / radix;
radix := radix * 10;
readnextch;
end;
end;
end;
procedure factor(var fv: extended);
var
Symbol: string;
function CalcN(Value: integer): extended;
var
i: integer;
begin
Result := 1;
if Value = 0 then
Exit
else
for i := 1 to Value do
Result := Result * i;
end;
function ParseFunction(var FunctionSymbol: string): boolean;
begin
FunctionSymbol := '';
while not (nextch in ['0'..'9', '.', '(', ')', '+', '-', '*', '/', '=']) do
begin
FunctionSymbol := FunctionSymbol + nextch;
readnextch;
end;
if FunctionSymbol = 'ABS' then
Result := true
else
if FunctionSymbol = 'SIN' then
Result := true
else
if FunctionSymbol = 'COS' then
Result := true
else
if FunctionSymbol = 'TG' then
Result := true
else
if FunctionSymbol = 'TAN' then
Result := true
else
if FunctionSymbol = 'ARCSIN' then
Result := true
else
if FunctionSymbol = 'ARCCOS' then
Result := true
else
if FunctionSymbol = 'ARCTG' then
Result := true
else
if FunctionSymbol = 'ARCTAN' then
Result := true
else
if FunctionSymbol = 'LN' then
Result := true
else
if FunctionSymbol = 'LG' then
Result := true
else
if FunctionSymbol = 'EXP' then
Result := true
else
if FunctionSymbol = 'SQR' then
Result := true
else
if FunctionSymbol = 'SQRT' then
Result := true
else
if FunctionSymbol = 'PI' then
Result := true
else
if FunctionSymbol = 'NOT' then
Result := true
else
if FunctionSymbol = 'N!' then
Result := true
else
if FunctionSymbol = 'E' then
Result := true
else
Result := false;
end;
begin
case nextch of
'0'..'9': number(fv);
'(':
begin
readnextch;
expression(fv);
if nextch = ')'
then
readnextch
else
error(nextch);
end
else
if ParseFunction(Symbol) then
if nextch = '(' then
begin
readnextch;
expression(fv);
if Symbol = 'ABS' then
fv := abs(fv)
else
if Symbol = 'SIN' then
fv := sin(fv)
else
if Symbol = 'COS' then
fv := cos(fv)
else
if Symbol = 'TG' then
fv := tan(fv)
else
if Symbol = 'TAN' then
fv := tan(fv)
else
if Symbol = 'ARCSIN' then
fv := arcsin(fv)
else
if Symbol = 'ARCCOS' then
fv := arccos(fv)
else
if Symbol = 'ARCTG' then
fv := arctan(fv)
else
if Symbol = 'ARCTAN' then
fv := arctan(fv)
else
if Symbol = 'LN' then
fv := ln(fv)
else
if Symbol = 'LG' then
fv := ln(fv) / ln(10)
else
if Symbol = 'EXP' then
fv := exp(fv)
else
if Symbol = 'SQR' then
fv := sqr(fv)
else
if Symbol = 'SQRT' then
fv := sqrt(fv)
else
if Symbol = 'NOT' then
fv := not (Round(fv))
else
if Symbol = 'N!' then
fv := CalcN(Round(fv))
else
error(symbol);
if nextch = ')' then
readnextch
else
error(nextch);
end
else
begin
// parse constant
if Symbol = 'PI' then
fv := 3.14159265358979324
else
if Symbol = 'E' then
fv := 2.71828182845904523
else
error(symbol);
end
else
begin
error(Symbol);
fv := 1;
end;
end;
end;
procedure Power_(var pv: extended);
var
multiop: char;
fs: extended;
begin
factor(pv);
while nextch in ['^'] do
begin
multiop := nextch;
readnextch;
factor(fs);
case multiop of
'^':
if pv <> 0.0 then
pv := exp(ln(pv) * fs)
else
error(multiop);
end;
end;
end;
procedure term_(var tv: extended);
var
multiop: char;
fs: extended;
begin
Power_(tv);
while nextch in ['*', '/', Symbol_Mod, Symbol_Div, Symbol_And, Symbol_Shl, Symbol_Shr] do
begin
multiop := nextch;
readnextch;
Power_(fs);
case multiop of
'*': tv := tv * fs;
'/':
if fs <> 0.0 then
tv := tv / fs
else
error(multiop);
Symbol_Mod: tv := round(tv) mod round(fs);
// prase mod
Symbol_Div: tv := round(tv) div round(fs);
// parse div
Symbol_And: tv := round(tv) and round(fs);
// parse and
Symbol_Shl: tv := round(tv) shl round(fs);
// parse shl
Symbol_Shr: tv := round(tv) shr round(fs);
// parse shr
end;
end;
end;
procedure expression(var ev: extended);
var
addop: char;
fs: extended;
begin
term_(ev);
while nextch in ['+', '-', Symbol_Or, Symbol_Xor] do
begin
addop := nextch;
readnextch;
term_(fs);
case addop of
'+': ev := ev + fs;
'-': ev := ev - fs;
Symbol_Or: ev := round(ev) or round(fs);
// parse or
Symbol_Xor: ev := round(ev) xor round(fs);
// parse xor
end;
end;
end;
begin
inputexp := ConvertExpression(ExpressionString);
if pos('=', inputexp) = 0 then
inputexp := ConvertExpression(ExpressionString);
position := 0;
while inputexp[position] <> '=' do
begin
nextchpos := 0;
readnextch;
expression(result);
end;
end;

function ParseExpressionToStr(ExpressionString: PChar): PChar;
var
ES: string;
begin
ES := ExpressionString;
if pos('=', ES) = 0
then
ES := ES + '='
else
ES := Copy(ES, 1, Pos('=', ES));
ES := ES + FormatFloat('0.000000000000', ParseExpression(ExpressionString));
Result := PChar(ES);
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
Edit1.Text:=ConvertExpression(pchar(Edit1.text));
Edit2.Text:=floattostr(ParseExpression(Pchar(Edit1.text)));
end;

end.
 
如果用這個的話﹐我在電腦上試了一下行不通edit1.text:=IntToStr(1+6+9/(4-1)-7*8);
改用這個就ok了
edit1.text:=IntToStr(1+6+9 div (4-1)-7*8);
哈哈﹐湊下熱鬧!
 

Similar threads

D
回复
0
查看
1K
DelphiTeacher的专栏
D
D
回复
0
查看
777
DelphiTeacher的专栏
D
D
回复
0
查看
802
DelphiTeacher的专栏
D
D
回复
0
查看
633
DelphiTeacher的专栏
D
D
回复
0
查看
747
DelphiTeacher的专栏
D
后退
顶部