Question

In: Computer Science

Write a simple Calculator program using Scheme programming language. It should be able to do following...

Write a simple Calculator program using Scheme programming language. It should be able to do following operations: "+", "-", " * *, "/". the format when using the calculator function should be (calculator(operand1 operation operand2)) -> (calculator(2 + 5)) should give the output of 7.

Solutions

Expert Solution

Code:

;;(car List) returns first element of list i.e, operand1
;;(cadr List) returns second element of list i.e, operation
;;(caddr List) returns third element of list i.e, operand2

(define (calculator List) ;;Takes list as input which is of form (operand1 operation operand2)
(cond ((equal? '+ (cadr List)) (add (car List) (caddr List))) ;;If (cadr List) i.e, if second element of list is "+" then add 1st and 3rd operands
((equal? '- (cadr List)) (sub (car List) (caddr List))) ;;If (cadr List) i.e, if second element of list is "-" then subract 1st and 3rd operands
((equal? '* (cadr List)) (mul (car List) (caddr List))) ;;If (cadr List) i.e, if second element of list is "*" then add multiply and 3rd operands
((equal? '/ (cadr List)) (div (car List) (caddr List))) ;;If (cadr List) i.e, if second element of list is "/" then divide 1st and 3rd operands
(else 'not-valid-operation)))


(define (add a b) ;;Returns sum of two operands
(cond ((= a 0) b) ;;if b = 0 returns value of a
((= b 0) a) ;;if a = 0 returns value of b
(else (+ a b)))) ;;else add two operands

(define (sub a b) ;;Returns subraction of two operands
(cond ((= b 0) a) ;;if b = 0 returns value of a
(else (- a b)))) ;;else subract two operands

(define (mul a b) ;;Returns multiplication of two operands
(cond ((= a 0) 0) ;;if a = 0 returns 0
((= b 0) 0) ;;if b = 0 returns 0
(else (* a b)))) ;;else multiply two operands

(define (div a b) ;;Returns division of two operands
(cond ((= a 0) 0) ;;if a = 0 returns 0
((= b 0) 'infinity) ;;if b = 0 returns 'infinity
(else (/ a b)))) ;;else divide two operands

Snapshot of Code and Output:


Related Solutions

TASK: Using stack functions, write a program in C++ language that acts as a simple calculator,...
TASK: Using stack functions, write a program in C++ language that acts as a simple calculator, reading an infix algebraic expression with numbers and simple operations: +, -, *, / , (, and ). The program converts an infix expression into an equivalent postfix expression, and then evaluates the postfix expression, and then prints the result if input expression is correct otherwise prints error messages. Your program must interact with the user until the user quits.    REQUIREMENTS: - Your...
Integer value calculator: Write a program using C++ that acts as a calculator. The program should...
Integer value calculator: Write a program using C++ that acts as a calculator. The program should ask the user for two numbers and an operator. Then the program must print the result using the two input values and the operator. Prompts and input requirements. Ask the user for two integer values and a value that indicates the operation. Likely you will want to choose the normal operators: + - * / %. Output Requirements: Make your output easy to understand,...
Using C as the programming language, Write a concurrent connection-oriented server that can do something simple...
Using C as the programming language, Write a concurrent connection-oriented server that can do something simple for connected clients. It should be able to carry out such processing for the client as many times as the client wants until the client indicates it wishes to end the session. The server should support multiple clients (use telnet as the client in this task). Compile and run the server program. Try and connect to it from multiple other hosts using telnet as...
GPA calculator in C language To understand the value of records in a programming language, write...
GPA calculator in C language To understand the value of records in a programming language, write a small program in a C-based language that uses an array of structs that store student information, including name, age, GPA as a float, and grade level as a string (e.g., “freshmen,” etc.). Note:Code and Output Screenshots
C -Language Create a simple calculator that performs addition, subtraction, multiplication, and division. Your program should...
C -Language Create a simple calculator that performs addition, subtraction, multiplication, and division. Your program should prompt the user for the operation they wish to perform followed by the numbers they wish to operate on. You should have a function for each operation and use branches to determine which function to call. I need this to make any integers given, into decimal numbers, such as 3 to 3.0, or 2 to 2.0, also, so that I can multiply or add...
Design a simple calculator program using C++ which is able to: 1. ADD two decimal numbers...
Design a simple calculator program using C++ which is able to: 1. ADD two decimal numbers 2. MULTIPLY two decimal numbers. The following features must be incorporated in your program. 1. Must have an interface for the user to be able to either select the ADD option or MULTIPLY option or to EXIT the program. NOTE: If the user makes a wrong selection, a display must be shown to inform the user and the user must be given a choice...
Kindly Do the program in C++ language Object Oriented Programming. Objectives  Implement a simple class...
Kindly Do the program in C++ language Object Oriented Programming. Objectives  Implement a simple class with public and private members and multiple constructors.  Gain a better understanding of the building and using of classes and objects.  Practice problem solving using OOP. Overview You will implement a date and day of week calculator for the SELECTED calendar year. The calculator repeatedly reads in three numbers from the standard input that are interpreted as month, day of month, days...
Please code the following, using the language java! Build a simple calculator that ignores order of...
Please code the following, using the language java! Build a simple calculator that ignores order of operations. This “infix” calculator will read in a String from the user and calculate the results of that String from left to right. Consider the following left-to-right calculations: "4 + 4 / 2" "Answer is 4" //not 6, since the addition occurs first when reading from left to right “1 * -3 + 6 / 3” “Answer is 1” //and not -1Start by copying...
Using C# programming language, Write a program that sort three numbers entered by the user using...
Using C# programming language, Write a program that sort three numbers entered by the user using only if and nested if statements. If for instance the user entered 5, 2, and 7; the program should display 2,5,7.
LISP Programming Language Write a Bubble Sort program in the LISP Programming Language called “sort” that...
LISP Programming Language Write a Bubble Sort program in the LISP Programming Language called “sort” that sorts the array below in ascending order.  LISP is a recursive language so the program will use recursion to sort. Since there will be no loops, you will not need the variables i, j, and temp, but still use the variable name array for the array to be sorted.             Array to be sorted is 34, 56, 4, 10, 77, 51, 93, 30, 5, 52 The...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT