Pascal 语言中如何产生随机四则运算符

来源:百度知道 编辑:UC知道 时间:2024/05/10 12:21:24
正想编一个能随机产生(四位数以内)四则运算题目的程序,不知道怎样随机产生四则运算的符号。
(请高手指点,或提供程序!)
首先感谢朋友们的帮助,但我想补充的是四则混合运算,不是一个式子的!比方说:4*5+6/2=。关键是怎样产生可以运算的运算算+-*/!

产生随机运算符号的方法,是产生0~3的随机数来代替相应的符号,例子程序如下:

{$apptype console}
program exp;
var
a:array [0..3] of char;
begin
a[0]:='+';
a[1]:='-';
a[2]:='*';
a[3]:='/';
randomize;
write(a[random(4)]);
end.

该过程采用了随机数random函数和取整trunc函数:

program xxx(input,output);
var
a:char;{符号}
s:integer;{储存随机数用}
procedure fuhao;{产生随机符号用的过程,可在程序中直接调用}
begin
randomize;
s:=trunc(random(4));{s为0到4的随机数(可取0,不可取4)}
if s=0 then a:='+';
if s=1 then a:='-';
if s=2 then a:='*';
if s=3 then a:='/';
end;
begin
fuhao;
……
……
……
end.

我就直接给产生那段给你
程序就不写了
randomize;
n:=random(4);
case n of
1:write('+');
2:write('-');
3:write('*');
4:write('/');
这是直接产生符号并打印!

随机产生符号可以加上一个过程,因为如果这个东西是、个整个程