📜  LISP 中的算术运算符

📅  最后修改于: 2022-05-13 01:54:54.090000             🧑  作者: Mango

LISP 中的算术运算符

算术运算运算符用于执行数学运算,如加法、减法、乘法和除法。

LISP 中有 7 个算术运算运算符,如下表所示:

OperatorSyntaxDescription
Addition Operator(+)+ num1 num2Add the two numbers
Subtraction Operator(-)– num1 num2Subtract the second number from the first number
Multiplication(*)* num1 num2Multiply two numbers
Division(/)/  num1 num2Divide the two numbers
Modulus(mod) mod num1 num2Get the remainder of two numbers
Increment(incf)incf  num valueIncrement number by given value
Decrement(decf)decf num valueDecrement number by given value

示例 1:演示算术运算运算符的LISP 程序

Lisp
;set value 1 to 300
; set value 2 to 600
(setq val1 300)
(setq val2 600)
  
;addition operation
(print (+ val1 val2))
  
;subtraction operation
(print (- val1 val2))
  
;multiplication operation
(print (* val1 val2))
  
;division operation
(print (/ val1 val2))
  
;modulus operation
(print (MOD val1 val2))
  
;increment a by 10
(print (incf val1 val2))
  
;decrement b by 20
(print (decf val1 val2))


Lisp
;set value 1 to 30
; set value 2 to 15
(setq val1 30)
(setq val2 15)
  
;addition operation
(print (+ val1 val2))
  
;subtraction operation
(print (- val1 val2))
  
;multiplication operation
(print (* val1 val2))
  
;division operation
(print (/ val1 val2))
  
;modulus operation
(print (MOD val1 val2))
  
;increment a by 10
(print (incf val1 val2))
  
;decrement b by 20
(print (decf val1 val2))


输出:



900  
-300  
180000  
1/2  
300  
900  
300 

示例 2:

Lisp

;set value 1 to 30
; set value 2 to 15
(setq val1 30)
(setq val2 15)
  
;addition operation
(print (+ val1 val2))
  
;subtraction operation
(print (- val1 val2))
  
;multiplication operation
(print (* val1 val2))
  
;division operation
(print (/ val1 val2))
  
;modulus operation
(print (MOD val1 val2))
  
;increment a by 10
(print (incf val1 val2))
  
;decrement b by 20
(print (decf val1 val2))

输出:

45  
15  
450  
2  
0  
45  
30