Write a program in C that carries out simple mathematical operations entered as a command-line strings named "calculator". Implementation: You will receive a single prefix arithmetic expression in text via argv[1]. You can assume the operands and operators will be space-separated. Test for possibility (i.e. no dividing by zero) and is correctly formatted (i.e. has exactly two operands). Your operands are:            Your operators are:        Your errors are:     zero                                        plus                Error: not an operator:     one                                        minus              Error: not an operand:     two                                        multiply             Error: not enough operands     three                                      divide                    Error: too many elements     four                                                                    Error: divide by zero     five     six     seven     eight     nine Common arithmetic expressions with the operator in betwene the operands are called "infix". Prefix notation puts the operand first, so "one plus two" is the infix version and "plus one two" is the prefix version. Be careful to apply context-sensitive operators (like minus and divide) in the correct direction, for example "2 - 1"  in infix is equivalent to "minus two one" in prefix and not "minus one two" You can presume you'll have a single simple expression. There will be no subexpressions, so every expression should have exactly three words: an operator followed by two operands. Once you decode the expression, you should compute it and output the result in double precision (%2.2ld) and return 1. If you find an error, you should print an error message and return -1. e.g.     ./calculator "plus four five"     9.00     ./calculator "divide one four"     0.25     ./calculator "five plus four"     Error: not an operator: two          ./calculator "divide three zero"     Error: divide by zero     ./calculator     Error: not an operator:      ./calculator "plus two one six"     Error: too many elements

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

Write a program in C that carries out simple mathematical operations entered as a command-line strings named "calculator".

Implementation:
You will receive a single prefix arithmetic expression in text via argv[1]. You can assume the operands and operators will be space-separated. Test for possibility (i.e. no dividing by zero) and is correctly formatted (i.e. has exactly two operands).
Your operands are:            Your operators are:        Your errors are:
    zero                                        plus                Error: not an operator: <text>
    one                                        minus              Error: not an operand: <text>
    two                                        multiply             Error: not enough operands
    three                                      divide                    Error: too many elements
    four                                                                    Error: divide by zero
    five
    six
    seven
    eight
    nine
Common arithmetic expressions with the operator in betwene the operands are called "infix". Prefix notation puts the operand first, so "one plus two" is the infix version and "plus one two" is the prefix version. Be careful to apply context-sensitive operators (like minus and divide) in the correct direction, for example "2 - 1"  in infix is equivalent to "minus two one" in prefix and not "minus one two"
You can presume you'll have a single simple expression. There will be no subexpressions, so every expression should have exactly three words: an operator followed by two operands.
Once you decode the expression, you should compute it and output the result in double precision (%2.2ld) and return 1.
If you find an error, you should print an error message and return -1.

e.g.
    ./calculator "plus four five"
    9.00

    ./calculator "divide one four"
    0.25

    ./calculator "five plus four"
    Error: not an operator: two
    
    ./calculator "divide three zero"
    Error: divide by zero

    ./calculator
    Error: not an operator: 

    ./calculator "plus two one six"
    Error: too many elements

Expert Solution
steps

Step by step

Solved in 2 steps with 3 images

Blurred answer
Knowledge Booster
Algebraic Expressions
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education