Part 1. calc: Character operations Write a program that carries out simple mathematical operations entered as command-line strings named 'calc'. Your program will take a single argument and examine the text to determine if the command is correct and if so, compute it. Implementation You will receive a single prefix arithmetic expression in text via argv[1]. You can presume operators and operands will be space-separated. You should check to make sure the operation is possible (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 "3 - 1" in infix is equivalent to "minus three one" in prefix and not "minus one three". 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. ./calc "plus two three" 5.00 ./calc "divide two four" 0.50 ./calc "two plus three" Error: not an operator: two    ./calc "divide five zero" Error: divide by zero ./calc Error: not an operator: ./calc "plus two three four" Error: too many elements Notes: Putting a command line argument in double quotes makes it a single argument, even if it contains spaces. This type of operation, where you have many equivalent possibilities, often lends itself very well to a switch statement. In order to recognize the different number strings, you may want to check out the strcmp() function. Be careful, it only compares one entire string to another. Make sure anything you give to strcmp() is a string. When you have labels that you want to convert to a value, it may be helpful to use definitions. Keep in mind every expression has the same format: operator, a space, an operand, a space, and then the last operand. You can expliot this structure to give your code shape. Keep in mind every error is a failure in an expectation. So, if you are expecting an operand first and you find none, print your error message and return. There is no need to continue. Most error messages are essentially 'else' (or default) cases while you are trying to decode the expression. Some errors (like divide by zero) you need to do a little work to find. Remember you need to actually compute the result. You should always compute as doubles. Be careful! If you do math with integer values like "2", C can do odd things to type. Make sure all your integers end in decimals (i.e. "1.0" and not "1") in order to make sure you have floating-point math. Implement your code in phases! Don't try to do it all at once. First, recognize/decode the operator, add in simple error detection for operator issues. Once that is done add in finding the first operand and simple error checking for it, and then the second operand. Then see if you can compute the operation. On

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

(Please read and answer the question don't copy-paste.)

Part 1. calc: Character operations
Write a program that carries out simple mathematical operations entered as command-line strings named 'calc'.
Your program will take a single argument and examine the text to determine if the command is correct and if so, compute it.

Implementation
You will receive a single prefix arithmetic expression in text via argv[1]. You can presume operators and operands will be space-separated. You should check to make sure the operation is possible (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 "3 - 1" in infix is equivalent to "minus three one" in prefix and not "minus one three".
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.
./calc "plus two three"
5.00

./calc "divide two four"
0.50

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

./calc
Error: not an operator:

./calc "plus two three four"
Error: too many elements

Notes:

Putting a command line argument in double quotes makes it a single argument, even if it contains spaces.

This type of operation, where you have many equivalent possibilities, often lends itself very well to a switch statement.

In order to recognize the different number strings, you may want to check out the strcmp() function. Be careful, it only compares one entire string to another. Make sure anything you give to strcmp() is a string.

When you have labels that you want to convert to a value, it may be helpful to use definitions.

Keep in mind every expression has the same format: operator, a space, an operand, a space, and then the last operand. You can expliot this structure to give your code shape.

Keep in mind every error is a failure in an expectation. So, if you are expecting an operand first and you find none, print your error message and return. There is no need to continue. Most error messages are essentially 'else' (or default) cases while you are trying to decode the expression. Some errors (like divide by zero) you need to do a little work to find.

Remember you need to actually compute the result. You should always compute as doubles. Be careful! If you do math with integer values like "2", C can do odd things to type. Make sure all your integers end in decimals (i.e. "1.0" and not "1") in order to make sure you have floating-point math.

Implement your code in phases! Don't try to do it all at once. First, recognize/decode the operator, add in simple error detection for operator issues. Once that is done add in finding the first operand and simple error checking for it, and then the second operand. Then see if you can compute the operation. Once you have that part working, you are close to done. Add in any other error detection you may have missed

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps with 5 images

Blurred answer
Knowledge Booster
Functions
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
  • SEE MORE 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