unit Unit1;interfaceuses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;type
TForm1 = class(TForm)
Ed1: TEdit;
Ed2: TEdit;
Button1: TButton;
procedure Button1Click(Sender: TObject);
function sum(Value:String):string;
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.DFM}procedure TForm1.Button1Click(Sender: TObject);
var ss:currency;
begin
ed2.text := sum(ed1.text);
end;function TForm1.sum(Value:String):string;
const
a = '+';
b = '-';
c = '*';
d = '/';
ea= '(';
eb= ')';
function GetToken(Value:String):String;
var
P1,P2:Integer;
begin
Result := '';
P1 := Pos(ea,Value);
if P1 <> 0 then
begin
P2 := Pos(eb,Value);
Result := Copy(Value,P1+1,P2-P1-1);
end;
end;
function GetResult(Value:String):String;
var
P1,P2,P3,P4,i,nStart,nEnd,P:Integer;
N1,N2 : Real;
Tmp : String;
IsFuShu : Boolean;
begin
IsFuShu := False;
N1 := 0;
N2 := 0;
P := 0;
P1 := Pos(c,Value);
P2 := Pos(d,Value);
P3 := Pos(a,Value);
P4 := Pos(b,Value);
while (P1<>0)or(P2<>0)or(P3<>0)or(P4<>0) do
begin
if (P3<>0) and (P4=0) then
P := P3;
if (P3=0) and (P4<>0)then
P := P4;
if (P3<>0) and (P4<>0) and (P4>P3) then
P := P3;
if (P3<>0) and (P4<>0) and (P4<P3) then
P := P4;
if (P1<>0) and (P2=0) then
P := P1;
if (P1 =0) and (P2<>0)then
P := P2;
if (P1<>0) and (P2<>0) and (P2>P1) then
P := P1;
if (P1<>0) and (P2<>0) and (P1>P2) then
P := P2;
if P <> 0 then
begin
for i := P-1 downto 1 do
begin
if (Value[i]=a)or(Value[i]=b)or(Value[i]=c)
or(Value[i]=d)or(Value[i]=ea)or(i=1) then
begin
if i<>1 then
N1 := StrToFloat(Copy(Value,i+1,P-i-1))
else
N1 := StrToFloat(Copy(Value,1,P-1));
Break;
end;
end;
if i<>1 then
nStart := i+1
else nStart := 1;
for i := P+1 to Length(Value) do
begin
if (Value[i]=a)or(Value[i]=b)or(Value[i]=c)
or(Value[i]=d)or(Value[i]=eb)or(i=Length(Value)) then
begin
if i<>Length(Value) then
N2 := StrToFloat(Copy(Value,P+1,i-P-1))
else
N2 := StrToFloat(Copy(Value,P+1,i));
Break;
end;
end;
if i=Length(Value) then
nEnd := i+1
else
nEnd := i;
if P=P1 then
Tmp := FloatToStr(N1*N2);
if P=P2 then
Tmp := FloatToStr(N1 / N2);
if P=P3 then
Tmp := FloatToStr(N1+N2);
if P=P4 then
begin
if N1<N2 then
begin
IsFuShu := Not IsFuShu;
Tmp := FloatToStr(N2-N1);
end;
if N1>N2 then
Tmp := FloatToStr(N1-N2);
end;
if nEnd <> 1 then
Delete(Value,nStart,nEnd-nStart)
else
Delete(Value,nStart,nEnd);
Insert(Tmp,Value,nStart);
end;
P1 := Pos(c,Value);
P2 := Pos(d,Value);
P3 := Pos(a,Value);
P4 := Pos(b,Value);
end;
if IsFuShu then
Result := '-'+Value
else
Result := Value;
end;
var
S : String;
P1,P2 : Integer;
begin
P1 := Pos(ea,Value);
P2 := Pos(eb,Value);
while GetToken(Value) <> '' do
begin
P1 := Pos(ea,Value);
P2 := Pos(eb,Value);
S:=GetToken(Value);
S:=GetResult(S);
Delete(Value,P1,P2-P1+1);
Insert(S,Value,P1);
end;
Result := GetResult(Value);
end;end.没有现成的vb的源码,修改一下即可,仅供参靠
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;type
TForm1 = class(TForm)
Ed1: TEdit;
Ed2: TEdit;
Button1: TButton;
procedure Button1Click(Sender: TObject);
function sum(Value:String):string;
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.DFM}procedure TForm1.Button1Click(Sender: TObject);
var ss:currency;
begin
ed2.text := sum(ed1.text);
end;function TForm1.sum(Value:String):string;
const
a = '+';
b = '-';
c = '*';
d = '/';
ea= '(';
eb= ')';
function GetToken(Value:String):String;
var
P1,P2:Integer;
begin
Result := '';
P1 := Pos(ea,Value);
if P1 <> 0 then
begin
P2 := Pos(eb,Value);
Result := Copy(Value,P1+1,P2-P1-1);
end;
end;
function GetResult(Value:String):String;
var
P1,P2,P3,P4,i,nStart,nEnd,P:Integer;
N1,N2 : Real;
Tmp : String;
IsFuShu : Boolean;
begin
IsFuShu := False;
N1 := 0;
N2 := 0;
P := 0;
P1 := Pos(c,Value);
P2 := Pos(d,Value);
P3 := Pos(a,Value);
P4 := Pos(b,Value);
while (P1<>0)or(P2<>0)or(P3<>0)or(P4<>0) do
begin
if (P3<>0) and (P4=0) then
P := P3;
if (P3=0) and (P4<>0)then
P := P4;
if (P3<>0) and (P4<>0) and (P4>P3) then
P := P3;
if (P3<>0) and (P4<>0) and (P4<P3) then
P := P4;
if (P1<>0) and (P2=0) then
P := P1;
if (P1 =0) and (P2<>0)then
P := P2;
if (P1<>0) and (P2<>0) and (P2>P1) then
P := P1;
if (P1<>0) and (P2<>0) and (P1>P2) then
P := P2;
if P <> 0 then
begin
for i := P-1 downto 1 do
begin
if (Value[i]=a)or(Value[i]=b)or(Value[i]=c)
or(Value[i]=d)or(Value[i]=ea)or(i=1) then
begin
if i<>1 then
N1 := StrToFloat(Copy(Value,i+1,P-i-1))
else
N1 := StrToFloat(Copy(Value,1,P-1));
Break;
end;
end;
if i<>1 then
nStart := i+1
else nStart := 1;
for i := P+1 to Length(Value) do
begin
if (Value[i]=a)or(Value[i]=b)or(Value[i]=c)
or(Value[i]=d)or(Value[i]=eb)or(i=Length(Value)) then
begin
if i<>Length(Value) then
N2 := StrToFloat(Copy(Value,P+1,i-P-1))
else
N2 := StrToFloat(Copy(Value,P+1,i));
Break;
end;
end;
if i=Length(Value) then
nEnd := i+1
else
nEnd := i;
if P=P1 then
Tmp := FloatToStr(N1*N2);
if P=P2 then
Tmp := FloatToStr(N1 / N2);
if P=P3 then
Tmp := FloatToStr(N1+N2);
if P=P4 then
begin
if N1<N2 then
begin
IsFuShu := Not IsFuShu;
Tmp := FloatToStr(N2-N1);
end;
if N1>N2 then
Tmp := FloatToStr(N1-N2);
end;
if nEnd <> 1 then
Delete(Value,nStart,nEnd-nStart)
else
Delete(Value,nStart,nEnd);
Insert(Tmp,Value,nStart);
end;
P1 := Pos(c,Value);
P2 := Pos(d,Value);
P3 := Pos(a,Value);
P4 := Pos(b,Value);
end;
if IsFuShu then
Result := '-'+Value
else
Result := Value;
end;
var
S : String;
P1,P2 : Integer;
begin
P1 := Pos(ea,Value);
P2 := Pos(eb,Value);
while GetToken(Value) <> '' do
begin
P1 := Pos(ea,Value);
P2 := Pos(eb,Value);
S:=GetToken(Value);
S:=GetResult(S);
Delete(Value,P1,P2-P1+1);
Insert(S,Value,P1);
end;
Result := GetResult(Value);
end;end.没有现成的vb的源码,修改一下即可,仅供参靠
解决方案 »
- 调用应用程序时的参数问题?
- 小弟有时相求:
- !!!!!!!!!!!!!!我他妈的真服了微软的Access了!!!!!!!!!!!!!!!!
- 为什么老是不能用new来添加Winsock,up有分
- 请教有关调用水晶报表的问题,请各位不吝赐教!!!
- (急)True DBGrid Pro 7.0控件中Tutorial 29 - Filter Bar例子在win98繁體下不能正確執行
- sql告售请进,关于一个SQL的问题!急急急急!!!
- 当你的学习出现了严重瓶颈,你将怎么办?
- 救命啊,救命啊,明天程序交货培训,系统安装IE4.0却不能打印HTML文档(IE6.0可以)
- 打开数据库后,如何使用sql语句?小弟是直接使用set db=db=opendatabase("xx.mdb")的
- 为什么我编的浏览器中引用的shdocvw.dll中的text框和按钮不是xp的样式?
- 有关水晶报表的问题,救命!!!!!!!!!!!!!!!!!!!!!!!
假设我们要让使用者在“方程式”栏位中自由输入方程式,然后利用方程式进行计算,则引用ScriptControl控件可以很方便地做到。
( ScriptControl 控件附属于VB 6.0,如果安装后没有看到此一控件,可在光盘的 \Common\Tools\VB\Script 目录底下找此一控件, 其.文件名为Msscript.ocx。)
没有这个Msscript.ocx,可以到 微软网站下载
假设放在窗体上的ScriptControl控件名称为ScriptControl1,则在“计算”按钮的Click事件中编写如下代码:
Dim Statement As String
Statement = "X=" + Text1.Text + vbCrLf + "Y=" + Text2.Text + vbCrLf + ""
MsgBox "计算结果=" & ScriptControl1.ExecuteStatement( Statement )