Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Problem: Write a program, named p3.c, that behaves in the manner describe in the usage message listed below, either printing the usage message when the
Problem: Write a program, named p3.c, that behaves in the manner describe in the "usage" message listed below, either printing the usage message when the first command-line argument is "--help", or accepting input from stdin. You should write appropriate error messages to stdout if an error arises, whether an unrecognized command-line argument, a stack underflow or overflow, or unrecognized input. The reason for this assignment is to learn to deal with at least some simple input in C, rather than CH, as well as to learn something about C-type string functions. These do see significant use in code closer to the core of the operating system, just as C is used in writing OS kernels and core services. This will continue in programming assignment 4, which will be the last pure C assignment. Use of an array to implement a simple stack is to familiarize you a bit more with C arrays. Naming: Your submitted file is to be named p3.c. Output: Your program's output must be to stdout and of one of the four formats following, assuming argc and argy are the usual parameters for main() and where is argv[O], and rnum is any real number in decimal format. If argv[1] is "--help", display the following. Usage: " --help" display this usage material. " -postfix" The program accepts input from standard input as a sequence of numbers and operators. The numbers (operands, as integers or floating point numbers) read are pushed on a stack until needed. When an operator is read, the required operands are popped from the stack and used to perform the calculation, with the result placed on the stack. Valid operators are +, - , * and /, are interpreted as addition, subtraction, multiplication and division, respectively, as described below. An additional operator is =, which indicates that the value at the top of the stack is popped from the stack and displayed along with the number of values remaining on the stack, whereupon the program terminates. Stack underflows generate an error message and halt the program, as do a stack overflows. Unrecognized input tokens produce error messages and result in program termination, as do unrecognized command line arguments. The size of the stack is 10. Stack operations are performed as indicated here. + : push(pop() + pop(); -: temp = pop(); push(pop) - temp; *: push (pop() * pop(); /: temp = pop(); push (pop() / temp; = : pop the stack top and display it as the result with the number of items remaining on the stack. The above describes the majority of the behavior of the program you are to write. To reiterate and expand important points: If the first command-line argument is --help, subsequent command-line arguments are ignored, the usage message is displayed and the program terminates. If the first command-line argument is -postfix, subsequent command line arguments are ignored and the program proceeds with reading from standard input. Invalid/unrecognized command-line arguments produce a suitable error message, display the usage information above, and terminate the program. If an unrecognized token is read from standard input, a suitable error message for the type of error is to be displayed with the usage text and the program terminated. If all inputs are recognized and no errors arise, print the following to stdout. You are to use a C array of type double to implement your stack, as discussed further below. Result = . values remain on the stack. For all error conditions, print the required output to stderr. Remember that any item delimited by angle brackets (') is to be replaced with the appropriate information, without the angle brackets
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started