Question

write a C program that evaluate algebraic expressions over real numbers in Reverse Polish Notation (RPN)

Your program must evaluate algebraic expressions over real numbers in Reverse Polish Notation (RPN). + (addition), - (subtraction), * (multiplication) and / (division) should be treated as arithmetic operations. Depending on the selection, numerical values must be entered and displayed in decimal, hexadecimal or binary form.

 

Example: The expression ((3+4) *(7+1.6-12) -5) / (3-8.7)

Will be calculated on your RPN calculator as 3 4 + 7 1.6 + 12 - * 5 - 3 8.7 - /

 

Your RPN computer's user interface should respond to the following keystrokes:

·         Numeric input: decimal digits "0...9", hexadecimal numeric symbols "a...f" and decimal point ".", sign "-", exponent identifier "E" (exponent in base 10)

·         Operators: +, -, *, /

·         Stack management: u (push), o (pop)

·         Changed the representation of numbers in the stack: D (decimal), H (hexadecimal), B (binary)

·         Exit program: "x" (exit)

 

Your RPN calculator should work as follows:

 

·                    Output of a permanent operating and display screen for the RPN calculator.

·                    Receiving any keyboard input in the input line, including numeric input, operand input, command input. Each entry must be completed with the ENTER key. In the example above, the following entries are required one after the other:

3 INPUT 4 INPUT + INPUT 7 INPUT 1.6 INPUT + INPUT 12 INPUT – INPUT …

·                    Saving entered values and calculation results in a stack.

·                    Output the current stack contents to the console. The last value entered into the stack should be displayed on the bottom line of the stack, the other values above in the order in which they were entered. After changing the contents of the stack, display fragments of the previous stack's contents should no longer be visible. Depending on the selected numeric representation, the following output is required in the stack rows:

-decimal (D): real values in proper floating-point representation, left-justified

-hexadecimal (H): integer value parts, two's complement, left justified

-binary(B): integer value components, absolute value right justified, sign left justified

·                    After entering an arithmetic operator, the arithmetic operation is performed. In particular, the arithmetic operator "-" changes the sign of the value on the stack if only one value is stored on the stack (unary operation).

·                    After typing push, the top value in the stack is pushed back onto the stack. If no value was stored on the stack when push was entered, the value 0 is pushed on the stack.

·                    After typing pop, the top value of the stack is removed.

·                    Unauthorized entries should be ignored.

·                    The stack depth must be entered as a program parameter through the console. It must be at least 3 and at most 8.


0 0
Add a comment Improve this question Transcribed image text
✔ Recommended Answer
Answer #1
/*
 Dateiname : POOL2_005.c
 Programm  : UPN-Rechner für Grundrechenarten (dezimal, hexadezimal, binaer)
 Beispielhafte Musterlösung für Aufgabe 005 aus POOL2
 Eingaben  : Parametereingabe bei Programmaufruf Ã¼ber Konsole. Aufruf mit:
             ./POOL2_005 <StackTiefe>
 StackTiefe = 3 ... 8
 Ausgaben  : Rückgabewerte des Programms:
             0 falls Eingabeparameter OK
             -1 falls fehlerhafte oder unvollständige Eingabeparameter
 Autor     : 
 Version   : 01   vom   2012-12-14
 */

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "console.h"

#define SCOL   40  //linke Spalte zur Stackdarstellung auf Konsole
#define SLINE  10  //obere Zeile zur Stackdarstellung auf Konsole
#define ILINE  12  //Eingabezeile auf Konsole
#define ICOL   41  //linke Eingabespalte in Stackdarstellung auf Konsole
#define SWIDTH 32  //max. Breite der Anzeige fuer Stack
#define STACKLINE "[                                ]"  // leere Stackzeile
#define SDEPTH 8   //max. Tiefe der Anzeige fuer Stack


/*
 Funktion  : void ClearLine(int Line)
 Aufgabe   : Loescht die Stackzeile Line auf dem Bildschirm
 Eingaben  : Line = Zeilenindex der zu loeschenden Zeile 
             (oberste Bildschirmzeile: Line=0)
 Ausgaben  : keine
 Autor     : Kreiser
 Version   : 01
 Datum der letzten Ã„nderung: 2012-12-14
 */
void ClearLine(int Line){
	CURPOS(Line, SCOL); CURCLR2EOL;
	CURPOS(Line, SCOL); printf("[                                ]");
	CURPOS(Line, ICOL);
}

/*
 Funktion  : void PrintStack(double* Stack, int StackTiefe, int StackIndex, 
                            char CalcMode)
 Aufgabe   : Gibt den gesamten Stack auf dem Bildschirm aus
 Eingaben  : Stack = Zeiger auf den Stack (Vektor mit double-Werten)
             StackTiefe = Groesse des Stacks (Anzahl der Speicherstellen)
             StackIndex = Index der letzten belegten Speicherstelle im Stack
             CalcMode = 'D' (dezimal), 'H' (hexadezimal), 'B' (binaer)
 Ausgaben  : keine
 Autor     : Kreiser
 Version   : 01
 Datum der letzten Ã„nderung: 2012-12-14
 */
void PrintStack(double* Stack, int StackTiefe, int StackIndex, char CalcMode){
    
}

/*
 Funktion  : void Bildschirmmaske(int StackTiefe)
 Aufgabe   : Zeigt den Bildschirm des UPN-Rechners an
 Eingaben  : StackTiefe = Groesse des Stacks (Anzahl der Speicherstellen)
 Ausgaben  : keine
 Autor     : Kreiser
 Version   : 01
 Datum der letzten Ã„nderung: 2012-12-14
 */
void Bildschirmmaske(int StackTiefe){
	int j;
	int i=3;  //Zeilenposition fuer Stackdarstellung auf Console
    CLS;
	CURPOS(1,   1); printf("UPN-Rechner - (c) 2012-12 F07");
	CURPOS(2,   1); printf("-------------------------------------------------------------------------");
	CURPOS(i++, 1); printf("Programm verlassen               (x)");
	CURPOS(i++, 1); printf("Operatoren                 (+ - * /)");
	CURPOS(i++, 1); printf("Ziffern          (01234567890abcdef)");
	CURPOS(i++, 1); printf("Dezimalpunkt | Vorzeichen    (. | -)");
	CURPOS(i++, 1); printf("Exponentkennung                  (E)");
	CURPOS(i++, 1); printf("Stackoperationen Push | Pop  (u | o)");
	CURPOS(i++, 1); printf("Eingabe abschliessen         (ENTER)");
	CURPOS(i++, 1); printf("Binaermodus                      (B)");
	CURPOS(i++, 1); printf("Dezimalmodus                     (D)");
	CURPOS(i++, 1); printf("Hexadezimalmodus                 (H)");
    i=ILINE;
    // Darstellungsfeld fuer Eingaben, Breite 30 Zeichen + Klammern
    CURPOS(i--, SCOL); printf(STACKLINE);
	CURPOS(i--, SCOL); printf("----------------------------------");
	//X                     0123456789          0123456789          0123456789          0123456789
	//X                               0123456789          0123456789          0123456789
	for(j=0;j<StackTiefe;j++){
		CURPOS(i--, SCOL); printf(STACKLINE);
	}
	CURPOS(ILINE+1, 1); printf("-------------------------------------------------------------------------\n");
}

int main(int argc, char *argv[]){
	int Ausgabe=0;    // Rueckgabewert von main()
    
    return(Ausgabe);
}


source: the type of code
answered by: anonymous
Add a comment
Know the answer?
Add Answer to:
write a C program that evaluate algebraic expressions over real numbers in Reverse Polish Notation (RPN)
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Similar Homework Help Questions
  • You are to write a program that implements a Reverse Polish Notation Calculator in C using...

    You are to write a program that implements a Reverse Polish Notation Calculator in C using BISON and FLEX, You only have to edit the BISON and FLEX files. Link to the files to start and have a general view of the program: https://www.dropbox.com/sh/83yzs66jhftqj5b/AABZcY9Qwl84JdUFnYpQaZk9a?dl=0 Reverse Polish Notation is a mathematical notation in which every operator follows all of its operands. It is sometimes called postfix notation, and does not require any parentheses as long as each operator has a fixed...

  • For this project, I need help developing a Java program that will act as an RPN (reverse polish notation) calculator. I...

    For this project, I need help developing a Java program that will act as an RPN (reverse polish notation) calculator. I need it to be able to do the following + add the top two items * multiply the top two items - subtract the top item from the next item / integer divide the second item by the top item % find the integer remainder when dividing the second item by the top item m unary minus -- negate...

  • **URGENT** Please help with this MIPS program? (calculator program that uses values from a user's input...

    **URGENT** Please help with this MIPS program? (calculator program that uses values from a user's input file and solves them; needs order of operations) Write a MIPS Assembly Language program to read hexadecimal values and operations (one per line) from a file. The file name should be requested and read in from the console. Once the ascii number has been read in, convert it to internal, binary, storage (decimal). The input format may be either fixed-format, 32-bit, twos compliment values,...

  • Infix Expression Evaluator For this project, write a C program that will evaluate an infix expression. The algorithm REQ...

    Infix Expression Evaluator For this project, write a C program that will evaluate an infix expression. The algorithm REQUIRED for this program will use two stacks, an operator stack and a value stack. Both stacks MUST be implemented using a linked list. For this program, you are to write functions for the linked list stacks with the following names: int isEmpty (stack); void push (stack, data); data top (stack); void pop (stack); // return TRUE if the stack has no...

  • Infix Expression Evaluator For this project, write a C program that will evaluate an infix expression. The algorithm REQ...

    Infix Expression Evaluator For this project, write a C program that will evaluate an infix expression. The algorithm REQUIRED for this program will use two stacks, an operator stack and a value stack. Both stacks MUST be implemented using a linked list. For this program, you are to write functions for the linked list stacks with the following names: int isEmpty (stack); void push (stack, data); data top (stack); void pop (stack); // return TRUE if the stack has no...

  • To write a C program (not C++) that converts numbers between Decimal and IEEE-754 format and...

    To write a C program (not C++) that converts numbers between Decimal and IEEE-754 format and vice versa. Inputs: Number in Decimal format (including special case of 0) Number in IEEE-754 format (including special cases) Output: Equivalent number in IEEE-754 format Equivalent number in Decimal Specification: The program converts a number based on choosing from a menu of choices, where each choice calls the appropriate procedure, where the choices are: Decimal to IEEE-754 conversion IEEE-754 to Decimal conversion Quit program...

  • You must write a C program that prompts the user for two numbers (no command line...

    You must write a C program that prompts the user for two numbers (no command line input) and multiplies them together using “a la russe” multiplication. The program must display your banner logo as part of a prompt to the user. The valid range of values is 0 to 6000. You may assume that the user will always enter numerical decimal format values. Your program should check this numerical range (including checking for negative numbers) and reprompt the user for...

  • i need help with a mips program to to covert roman numerals to real numbers Lab 4: Roman Numeral Conversion Part A: Due...

    i need help with a mips program to to covert roman numerals to real numbers Lab 4: Roman Numeral Conversion Part A: Due Sunday, 19 May 2019, 11:59 PM Due Friday, 24 May 2019, 11:59 PM Part B: Minimum Submission Requirements Ensure that your Lab4 folder contains the following files (note the capitalization convention): o Diagram.pdf o Lab4. asm O README.txt Commit and push your repository Lab Objective In this lab, you will develop a more detailed understanding of how...

  • Write a C program which will display the contents of a file in base-16 (hexadecimal) and...

    Write a C program which will display the contents of a file in base-16 (hexadecimal) and in ASCII. Complete the following tasks: Obtain the name of the input file from the command line. If the command-line is “./hexdump xxx.bin” then argv[1] will contain “xxx.bin”. Open the file for binary input Print the entire file, 16-bytes per line. Each line should begin with an 8-digit hexadecimal offset into the file. This is the count of the bytes that you have already...

  • Must be done in python and using linux mint Write a program to create a text...

    Must be done in python and using linux mint Write a program to create a text file which contains a sequence of test scores. Each score will be between 0 and 100 inclusive. There will be one value per line, with no additional text in the file. Stop adding information to the file when the user enters -1 The program will first ask for a file name and then the scores. Use a sentinel of -1 to indicate the user...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT