S
shhw
Unregistered / Unconfirmed
GUEST, unregistred user!
哪一位老大可以给小弟一个程序,实现功能为判断括号是否配对,主要包括下面三种:()、[]、{}。
要求用C++来完成。
下面是用Delphi做的 ,能帮助改写成VC++也可以
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Edit1: TEdit;
Button1: TButton;
Label1: TLabel;
Button2: TButton;
procedure Button1Click(Sender: TObject);
private
//查找字符串中给定的字符,并返回出现的次数
function LookupStr(Astr,AfindStr:string;Var ADegree:integer ):Boolean;
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var s,Stemp,Stemp1,Stemp3:string;
i,x,leftLength,rigthLength:integer;
leftbool,rigtbool:boolean;
begin
//记录提前设置好的括号
Stemp := '(),{},[]';
//把输入的表达式赋给变量S
s:= Edit1.Text
//给X赋值-2
x:= -2;
for i:= 1 to length(Stemp)do
begin
x:= x+3;
Stemp1 := copy(Stemp,x,1);
//截取前括号
Stemp3 := copy(Stemp,x+1,1);
//截取后括号
//判断表达式中括号的个数
leftbool := LookupStr(s,Stemp1,leftLength);
rigtbool := LookupStr(s,Stemp3,rigthLength);
if (not leftbool) And (not rigtbool) then
Continue
else
begin
if leftLength <> rigthLength then
begin
Application.MessageBox(Pchar('您输入的表达式中 "'+Stemp1+'" 不配对'), '提示', MB_ICONINFORMATION+MB_SYSTEMMODAL);
Break;
end;
end;
leftLength := pos(Stemp1,s);
//计算前括号的长度
rigthLength := pos(Stemp3,s);//计算后括号的长度
//判断表达式中的括号是否配对
if (leftLength > rigthLength) then
begin
ShowMessage('您输入的表达式中 "'+Stemp1+'" 书写错误');
break;
end;
leftLength:=0;
rigthLength := 0;
end;
end;
function TForm1.LookupStr(Astr, AfindStr: string;
var ADegree: integer): Boolean;
var i:integer;
stemp:string;
begin
Result := false;
for i:= 1 to length(Astr)do
begin
stemp := copy(Astr ,i,1);
if stemp = AfindStr then
begin
ADegree := ADegree +1;
Result := true;
end;
end;
end;
end.
要求用C++来完成。
下面是用Delphi做的 ,能帮助改写成VC++也可以
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Edit1: TEdit;
Button1: TButton;
Label1: TLabel;
Button2: TButton;
procedure Button1Click(Sender: TObject);
private
//查找字符串中给定的字符,并返回出现的次数
function LookupStr(Astr,AfindStr:string;Var ADegree:integer ):Boolean;
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var s,Stemp,Stemp1,Stemp3:string;
i,x,leftLength,rigthLength:integer;
leftbool,rigtbool:boolean;
begin
//记录提前设置好的括号
Stemp := '(),{},[]';
//把输入的表达式赋给变量S
s:= Edit1.Text
//给X赋值-2
x:= -2;
for i:= 1 to length(Stemp)do
begin
x:= x+3;
Stemp1 := copy(Stemp,x,1);
//截取前括号
Stemp3 := copy(Stemp,x+1,1);
//截取后括号
//判断表达式中括号的个数
leftbool := LookupStr(s,Stemp1,leftLength);
rigtbool := LookupStr(s,Stemp3,rigthLength);
if (not leftbool) And (not rigtbool) then
Continue
else
begin
if leftLength <> rigthLength then
begin
Application.MessageBox(Pchar('您输入的表达式中 "'+Stemp1+'" 不配对'), '提示', MB_ICONINFORMATION+MB_SYSTEMMODAL);
Break;
end;
end;
leftLength := pos(Stemp1,s);
//计算前括号的长度
rigthLength := pos(Stemp3,s);//计算后括号的长度
//判断表达式中的括号是否配对
if (leftLength > rigthLength) then
begin
ShowMessage('您输入的表达式中 "'+Stemp1+'" 书写错误');
break;
end;
leftLength:=0;
rigthLength := 0;
end;
end;
function TForm1.LookupStr(Astr, AfindStr: string;
var ADegree: integer): Boolean;
var i:integer;
stemp:string;
begin
Result := false;
for i:= 1 to length(Astr)do
begin
stemp := copy(Astr ,i,1);
if stemp = AfindStr then
begin
ADegree := ADegree +1;
Result := true;
end;
end;
end;
end.