Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C program Here is my stack implementation by using it make a postfix calculator by following explanation: ------------------------ double_stack_array.c #include #include #include double_stack.h #define MAX_SIZE

C program

Here is my stack implementation by using it make a postfix calculator by following explanation:

------------------------

"double_stack_array.c"

#include  #include  #include "double_stack.h" #define MAX_SIZE 100 struct double_stack_struct{ double Array[MAX_SIZE]; int head; }; //allocating space double_stack* create_stack(){ double_stack *stack = (double_stack*)malloc(sizeof(double_stack)); stack->head = -1; return stack; } int push(double_stack * stack, double d ){ if(stack->head == MAX_SIZE- 1){ printf("Array is full"); //stack overflow return 0; } stack->Array[++stack->head] = d; return 1; } int empty(double_stack* stack) { return (stack->head == -1) ? 1 : 0; } double get_value_at_head(double_stack* stack) { return stack->Array[stack->head]; } int pop(double_stack* stack,double *d) { if(stack->head == -1){ return 0; } *d = stack->Array[stack->head--]; return 1; }

__________ "double_stack.h"

#ifndef DOUBLE_STACK_H #define DOUBLE_STACK_H typedef struct double_stack_struct double_stack; double_stack* create_stack(); int empty(double_stack* stack); int push(double_stack* stack,double d); int pop(double_stack* stack,double*d); double get_value_at_head(double_stack* stack); #endif ----------

To get the chance to also use your stack implementation, you must now implement a calculator. The easiest to implement is one that uses postfix notation (also sometimes called reverse Polish notation).4 Such a calculator takes one argument at a time and puts it on a stack, and when an operator (plus, minus, etc.) is given as an argument, it is applied to the top two stack elements. Postfix notation is admittedly more difficult for the user when calculating something compared to regular infix notation, at least if you are above. However, it is easier to implement, because you don't have to deal with parentheses in the expressions. With postfix notation, you write e.g. 3 4 + instead of 3 + 4 . The expression 3 - 4 * 5 with infix notation can be interpreted both as (3-4)*5 or 3-(4*5), but with postfix notation the former is 3 4 - 5 * and the latter 3 4 5 * - .Your calculators must handle the four methods of calculation (addition, subtraction, division, multiplication) and use double for the calculations. An example of an instruction sequence is as follows:

1 read input from standard input, 2 if it's a number, push it onto the stack, 3 otherwise (it is an operator, e.g. plus or minus): (a) pop two elements from the stack, (b) apply the operator to both (eg, if the operator was + , the calculated sum of the two popped elements), (c) push the result back onto the stack, d) print the result

In the example above, 3 4 5 * - , the following happens: 3 are read in and pushed, 4 are read in and pushed, 5 is read in and pushed, * is read, pops 5 and 4 , calculates 4 5 = 20, pushes and prints 20 , - is read in, pops 20 and 3 , calculates 3 20 = 17, pushes and prints -17.

Tip: Read in a string with scanf (use "%s" for that). Then use the function strtod from stdlib.h to convert the string to a floating point number (if possible). The strtod function works like atof but provides better error checking. Error checking is done by sending an extra argument: a char** that points to the character where strtod stopped converting. If it points to a '\0' character, then the entire string has been converted. Create a char *check; and call double d = strtod( string, &check ) . If *check == '\0' then strtod succeeded in reading in a number. Otherwise, the string contains something else.

Exit if scanf returns something < 1, i.e. if it was not possible to read any more input.

The user can also exit the program by pressing ctrl+c on the keyboard. It is not something you need to implement, but you can always do that to terminate the program that is running.

Below is an example of what your calculator should be able to do. Make sure your program follows the same structure.

./task2 4 3 / =1.333333 10.6 - =-9.266667

You have to handle if an operator is placed on a stack with less than two elements (stack underflow), so that a user can make at least a small error and still continue without the program crashing.

For example, if the user types 3 + 4 +, a program without controls could crash at the first plus sign. (Then two elements should be popped, but there is only one on the stack.) You can choose to handle such a case either by popping the third and then doing nothing (in which case the sequence above gives no result) or by pushing back the third (in which case the sequence above returns the result 7).

However, you don't need to consider a user who is deliberately trying to input strange things into the program, in this task. You can assume that there will only be numeric values or something from + - * / .

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Refactoring Databases Evolutionary Database Design

Authors: Scott Ambler, Pramod Sadalage

1st Edition

0321774515, 978-0321774514

More Books

Students also viewed these Databases questions

Question

Proficiency with Microsoft Word, Excel, PowerPoint

Answered: 1 week ago

Question

Experience with SharePoint and/or Microsoft Project desirable

Answered: 1 week ago