当前位置: 首页 > 文档资料 > Pascal 中文教程 >

Show 例子 1

优质
小牛编辑
126浏览
2023-12-01

下表显示了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