KEMBAR78
Clean Code | PDF | Java Platform | Computer Programming
0% found this document useful (0 votes)
172 views5 pages

Clean Code

The document provides code for evaluating a postfix expression in Java. It defines an interface for a stack and implements it with an ArrayStack class. The PostFixEvaluation class contains the evaluatePostfix method that takes a character array of the postfix expression and evaluates it, returning the result.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
172 views5 pages

Clean Code

The document provides code for evaluating a postfix expression in Java. It defines an interface for a stack and implements it with an ArrayStack class. The PostFixEvaluation class contains the evaluatePostfix method that takes a character array of the postfix expression and evaluates it, returning the result.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

1.

Постфикс нотација - Java Задача 3 (0 / 0)


Да се напише алгоритам кој ќе врши евалуација на израз во постфикс нотација. На влез се
чита низа од знаци за изразот (стринг), а на излез се печати вредноста на изразот по
евалуацијата. Име на класата: PostFixEvaluation

Вашето решение:
Програмски јазик: Java

import java.io.BufferedReader;

import java.io.InputStreamReader;

import java.util.NoSuchElementException;

interface Stack<E> {

// Elementi na stekot se objekti od proizvolen tip.

// Metodi za pristap:

public boolean isEmpty ();

// Vrakja true ako i samo ako stekot e prazen.

public E peek ();

// Go vrakja elementot na vrvot od stekot.

// Metodi za transformacija:

public void clear ();


// Go prazni stekot.

public void push (E x);

// Go dodava x na vrvot na stekot.

public E pop ();

// Go otstranuva i vrakja elementot shto e na vrvot na stekot.

class ArrayStack<E> implements Stack<E> {

private E[] elems;

private int depth;

@SuppressWarnings("unchecked")

public ArrayStack (int maxDepth) {

// Konstrukcija na nov, prazen stek.

elems = (E[]) new Object[maxDepth];

depth = 0;

public boolean isEmpty () {

// Vrakja true ako i samo ako stekot e prazen.

return (depth == 0);

}
public E peek () {

// Go vrakja elementot na vrvot od stekot.

if (depth == 0)

throw new NoSuchElementException();

return elems[depth-1];

public void clear () {

// Go prazni stekot.

for (int i = 0; i < depth; i++) elems[i] = null;

depth = 0;

public void push (E x) {

// Go dodava x na vrvot na stekot.

elems[depth++] = x;

public E pop () {

// Go otstranuva i vrakja elementot shto e na vrvot na stekot.


if (depth == 0)

throw new NoSuchElementException();

E topmost = elems[--depth];

elems[depth] = null;

return topmost;

public class PostFixEvaluation {

static int evaluatePostfix(char [] izraz, int n)

// Vasiot kod tuka

public static void main(String[] args) throws Exception{

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

String expression = br.readLine();

char exp[] = expression.toCharArray();

int rez = evaluatePostfix(exp, exp.length);

System.out.println(rez);
br.close();

You might also like