logo Use CA10RAM to get 10%* Discount.
Order Nowlogo
(5/5)

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

INSTRUCTIONS TO CANDIDATES
ANSWER ALL QUESTIONS

 C/C++ Programming

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.

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 argv are the usual parameters for main() and where is argv[0], and rnum is any real number in decimal format.

If argv[1] is “--help”, display the following.

Usage:

" --help"

display this usage material.

" -rpn"

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

  • If the first command-line argument is “-rpn”, 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

  • 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

  • If all inputs are recognized and no errors arise, print the following to stdout.

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.

Requirements:

  • The program must be well

  • Your programs must compile and run correctly when compiled on the CS department’s csx server and when run from a command line on that server. Use the gcc

  • Submit the source code to the course account on csx using the handin comment-line “handin cs2433 program3 c”.

Available Resources

You should feel free to use the following Web sites. http://www.cplusplus.com/reference/ http://www.cprogrammingexpert.com/C/Tutorial/

Additional material will be given in instructor’s slide sets, but a brief discussion about implementing the stack and about post-fix notation (sometimes called “RPN” or Reverse Polish Notation) follows. Note that RPN is descriptive of how compilers set up computation of expressions in most programming languages.

For simplicity’s sake, your stack does not need to be fully generalized. Rather, it need only be implemented as an array (of 20 doubles) with an index of the first unused entry (the “stack pointer” and also the size of the stack at any point in time) with functions “void push(double fval);” and “double pop();”. At initialization of the stack, the index to the first unused entry will have a value of 0 (zero).  When you read a number (as a double) from stdin you will push this onto the stack (using your push() function) provided the index at that time is not SIZE_OF_STACK (which will be set to 20). If the index is 20 or greater when you call push() you have a stack overflow condition, which is an error, and you must print an appropriate error message. Operators act upon the top two elements on the stack, provided there are sufficient items on the stack. Removing an item from the stack will require use of your pop() function, which will check to see that the stack index is not 0 or negative before returning the top value from the stack and decrementing the stack pointer. If pop() finds the stack empty when called (the index is 0), this is an error condition, and you will need to print an appropriate error message.

 

The arithmetic operations to be performed are as follows. For a + operator:

push(pop() + pop());

For a – operator:

temp = pop(); push(pop() – temp);

For a * operator:

push(pop() * pop());

For a / operator:

temp = pop(); push(pop() / temp);

where temp is a double variable.

Note that division can result in division by zero. This must be checked for and generate a suitable error message, as well as terminating the program, when the condition arises.

Additional operators and operation types will be added in subsequent programming assignments.

Again, when a “=” is found print the value at the top of the stack and return the stack size as follows:

printf(“Result = %f.\n”, pop());

printf(“%i values remain on the stack.\n”, sindex);

Note that the pop() call can result in a stack underflow, as pop() must contain the code that checks for an underflow, and that the push() call can generate a stack overflow, as it must contain the code to detect stack overflows.

Further Discussion

Some terminology for the less experienced is appropriate here. Reference to a “command line” or “command promplt” means the system command line or prompt, not to input to your program while it is running. Command line arguments, such as when starting a program, are accessed via the parameters passed to the main() function, namely the int argc and char* argv[] parameters. Program input from the system keyboard (or “console”) is read by the program using various functions that do not reference argc and argv. In C programs, these functions include scanf(), fgetc(), fgets(), etc., from the stdio.h standard C library. Restrict you code to using the printf() family of functions for output of all types and the scanf() family of functions for standard input.

For the present assignment, pay close attention to the requirements of the assignment. You need to think like someone working for a company developing an application, with you writing parts of the overall suite of code that goes with that application. In such a circumstance, you would be assigned a task and be expected to conform to the stated requirements. In other words, you have to deliver exactly what you are told to deliver, not something you decide to do instead.

Running the program described in this assignment involves providing a command to start the program. As specified, a command line argument of –rpn is required, and further input is to the program, not via the command line.

If you are running the program from within the Dev-C++ IDE, this will involve setting the –rpn command line parameter using the Execute/Parameters menu. (Other IDEs often have the same ability to run programs directly, with similar means for setting command line arguments.)

If you run your program on the csx server (using PuTTY, for example) requires that you enter the -rpn command line argument. Assuming you used the gcc compiler with the command “gcc p3.c –o p3” so that you can run your program using the name p3, you execute your program using the command “p3 -rpn”.

Once the program is running either in an IDE environment or directly on csx or a Windows or OS/X command prompt, input is to be directly from the keyboard.

If implemented correctly, your program will accept the defined tokens: numbers in integer or floating point format and the five defined operators. These may be on a single program input line or spread across multiple input lines. The partial screen-shot on the next page shows two runs on identical input – except for the added returns in the second. This illustrates that your program should accept such inputs and correctly arrive at the same result, given the same numbers, operators and order.

Additional information will be provided in slide sets posted in the coming days.


See the following partial screenshot for samples of inputs and outputs from this program.

(5/5)
Attachments:

Related Questions

. Introgramming & Unix Fall 2018, CRN 44882, Oakland University Homework Assignment 6 - Using Arrays and Functions in C

DescriptionIn this final assignment, the students will demonstrate their ability to apply two ma

. The standard path finding involves finding the (shortest) path from an origin to a destination, typically on a map. This is an

Path finding involves finding a path from A to B. Typically we want the path to have certain properties,such as being the shortest or to avoid going t

. Develop a program to emulate a purchase transaction at a retail store. This program will have two classes, a LineItem class and a Transaction class. The LineItem class will represent an individual

Develop a program to emulate a purchase transaction at a retail store. Thisprogram will have two classes, a LineItem class and a Transaction class. Th

. SeaPort Project series For this set of projects for the course, we wish to simulate some of the aspects of a number of Sea Ports. Here are the classes and their instance variables we wish to define:

1 Project 1 Introduction - the SeaPort Project series For this set of projects for the course, we wish to simulate some of the aspects of a number of

. Project 2 Introduction - the SeaPort Project series For this set of projects for the course, we wish to simulate some of the aspects of a number of Sea Ports. Here are the classes and their instance variables we wish to define:

1 Project 2 Introduction - the SeaPort Project series For this set of projects for the course, we wish to simulate some of the aspects of a number of

Ask This Question To Be Solved By Our ExpertsGet A+ Grade Solution Guaranteed

expert
Atharva PatilComputer science

660 Answers

Hire Me
expert
Chrisantus MakokhaComputer science

681 Answers

Hire Me
expert
AyooluwaEducation

824 Answers

Hire Me
expert
RIZWANAMathematics

573 Answers

Hire Me

Get Free Quote!

273 Experts Online