主题:紧急请求帮助 怎么用pascal 写一个简单的计算器 ??
屋顶上的轻骑兵
[专家分:0] 发布于 2006-11-10 02:37:00
you are to write a simple calculator. The program will prompt the user to enter mathematical expression and program will display the original expression as well as the result. It will then prompt the person if they wish to run the program again. The expression will be entered in the following format:
<First operand> <Operation> <Second operand>
* Operands will consist of a one or two digit number.
* Operations will consist of: addition (+), subtraction (-), multiplication (*) or integer division (/).
* There is no spaces in the expression.
Pascal functions and procedures that you can use:
For this assignment cannot use any pre-created functions or procedures that convert character arrays to integers. You can however use the 'ord' function to determine the ASCII value for a given character.
谢谢
回复列表 (共4个回复)
沙发
屋顶上的轻骑兵 [专家分:0] 发布于 2006-11-10 02:35:00
里面必须用到 array
* Declares a one-dimensional character array to store the expression
* Displays an introduction
* Prompts the user to enter a mathematical expression
* Reads the expression into the array
* Displays the expression onscreen
* Loops back to third step until the person quits the program
* Displays a sign off exit message
板凳
xunge817 [专家分:30] 发布于 2006-11-16 22:55:00
Program LX;
const g:array[1..2]of set of char=(['+','-'],['*']);
g1:set of char=(['(',')','+','-','*']);
Var
str, o:string;
procedure init;
begin
assign(input,'expression.in');
reset(input);
readln(str);
close(input);
end;
function try3(s:string):integer;
var
n, k, l:integer;
begin
n:=0;
try3:=0;
for k:=1 to 2 do
for l:=length(s) downto 1 do begin
if s[l]='('then inc(n);
if s[l]=')'then dec(n);
if (n=0)and(s[l] in g[k]) then begin
try3:=l;
exit;
end;
end;
end;
function try2(str1,str2,str3:string):string;
var
i, j, code, k, k1:integer;
strr:string;
a:array[1..10]of integer;
tf:boolean;
begin
try2:='';
tf:=false;
val(str1, i, code);
val(str3, j, code);
if str2='+' then k:=i+j;
if str2='-' then k:=i-j;
if str2='*' then k:=i*j;
if k<0 then begin
tf:=true;
k:=(-1)*k;
end;
code:=1;
fillchar(a,sizeof(a),0);
for k1:=1 to 10 do begin
code:=code*10;
a[k1]:=k mod code div (code div 10);
end;
strr:='';
for k1:=1 to 10 do if a[k1]<>0 then code:=k1;
for k1:=1 to code do
case a[k1] of
1:strr:='1'+strr;
2:strr:='2'+strr;
3:strr:='3'+strr;
4:strr:='4'+strr;
5:strr:='5'+strr;
6:strr:='6'+strr;
7:strr:='7'+strr;
8:strr:='8'+strr;
9:strr:='9'+strr;
0:strr:='0'+strr;
end;
if tf then strr:='-'+strr;
try2:=strr;
end;
function try1(s:string):string;
var
v1, v2, v3:string;
p:integer;
begin
v1:='';
v2:='';
v3:='';
p:=try3(s);
if p=0 then begin
if (s[1]='(')and(s[length(s)]=')')then begin
try1:=try1(copy(s,2,length(s)-2));
end;
if (s[1]<>'(')or(s[length(s)]<>')')then try1:=s;
end else begin
v1:=v1+try1(copy(s,1,p-1));
v2:=v2+try1(copy(s,p+1,length(s)-p));
try1:=try2(v1,s[p],v2);
end;
end;
procedure output;
var
f:text;
begin
assign(f,'expression.out');
rewrite(f);
write(f,o);
close(f);
end;
begin
init;
o:=try1(str);
output;
end.
3 楼
sss333 [专家分:340] 发布于 2006-12-02 16:10:00
不懂
4 楼
sss333 [专家分:340] 发布于 2006-12-02 16:13:00
我用了金山快译,还是不懂
我来回复