目录

Show 例子

下表显示了Pascal支持的所有算术运算符。 假设变量A保持10,变量B保持20,则 -

操作者 描述
+ 添加两个操作数 A + B将给出30
- 从第一个减去第二个操作数 A - B将给-10
* 将两个操作数相乘 A * B将给出200
div 分子除以分母 B div A将给出2
mod 模数运算符和整数除法后的余数 B mod A将给出0

以下示例说明了算术运算符 -

program calculator;
var
a,b,c : integer;
d: real;
begin
   a:=21;
   b:=10;
   c := a + b;
   writeln(' Line 1 - Value of c is ', c );
   c := a - b;
   writeln('Line 2 - Value of c is ', c );
   c := a * b;
   writeln('Line 3 - Value of c is ', c );
   d := a/b;
   writeln('Line 4 - Value of d is ', d:3:2 );
   c := a mod b;
   writeln('Line 5 - Value of c is ' , c );
   c := a div b;
      writeln('Line 6 - Value of c is ', c );
end.

请注意,Pascal是非常强类型的编程语言,因此如果您尝试将除法结果存储在整数类型变量中,则会出错。 编译并执行上述代码时,会产生以下结果:

Line 1 - Value of c is 31
Line 2 - Value of c is 11
Line 3 - Value of c is 210
Line 4 - Value of d is 2.10
Line 5 - Value of c is 1
Line 6 - Value of c is 2
↑回到顶部↑
WIKI教程 @2018