Practice Exercises Java - Learn to program performing exercises with Java

Practice Exercises Java

Learn to program performing exercises with Java

12 Lessons Java with the Solutions - 228 Exercises Java with the solutions
For Beginners, Intermediates and Advanceds


App Practice Exercises Java

The human knowledge belongs to the world
¡The infomation should be free!



Queue Stack Reverse Polish Notation - Practice Exercises Java


Lesson 11:

Dynamic memory management


Exercise 11.4:

Queue Stack Reverse Polish Notation


Objetive:

Create a program that reads from a text file one expression in Reverse Polish Notation like, for example:

3 4 6 5 - + * 6 +
(Result 21)

Each item will be stored in a queue.

Once the queue has all the items stored, you will have to store them from the queue to a stack (using class Queue and Stack provided in Java).

Finally, you will operate with the stack in order to get the correct result of the expression in RPN, and you will show it by console.


Source Code:


package FileTextQueue;
import java.util.*;
public class Main
{
	public static void main(String[] args)
	{
		System.out.print("Introduce el nombre del archivo: ");
		String nombreFichero = new Scanner(System.in).nextLine();

		if (!(new java.io.File(nombreFichero)).isFile())
		{
			System.out.print("El archivo no existe");
			return;
		}

		try
		{
			boolean depurando = true;

			java.io.FileReader ficheroTexto;
		    java.io.BufferedReader ficheroTextoBufferedReader = 
            new java.io.BufferedReader(ficheroTexto);

			ficheroTexto = new java.io.FileReader(nombreFichero);
			String line = " ";

			LinkedList micola = new LinkedList();

			do
			{
				line = ficheroTextoBufferedReader.readLine();
				if (line != null)
				{
					micola.offer(line);
				}
			} while (line != null);

			ficheroTexto.close();

			Stack miPila = new Stack();

			int numero1 = 0, numero2 = 0;

			int cantidadCola = micola.size();

			String valorCola;
			String[] valores_linea;

			for (int i = 0; i < cantidadCola; i++)
			{
				valorCola = (String)micola.poll();
				valores_linea = valorCola.split("[ ]", -1);

				for (int c = 0; c < valores_linea.length; c++)
				{
					switch (valores_linea[c])
					{
						case "+":
							numero1 = (int)miPila.pop();
							numero2 = (int)miPila.pop();

							miPila.push(numero2 + numero1);

							break;
						case "-":
							numero1 = (int)miPila.pop();
							numero2 = (int)miPila.pop();

							miPila.push(numero2 - numero1);
							break;
						case "*":
							numero1 = (int)miPila.pop();
							numero2 = (int)miPila.pop();

							miPila.push(numero2 * numero1);
							break;
						case "/":
							numero1 = (int)miPila.pop();
							numero2 = (int)miPila.pop();

							miPila.push(numero2 / numero1);
							break;
						default:
							// Almacenamos valores enteros
							miPila.push(valores_linea[c]);
							break;
					}
				}

				for (int j = 0; j < miPila.size(); j++)
				{
					System.out.println(miPila.pop());
				}
			}

			if (depurando)
			{
				new Scanner(System.in).nextLine();
			}
		}

		catch (RuntimeException e)
		{
			System.out.println("Error, " + e.getMessage());
		}
	}
}
Exercisey 11.4




Privacy Policy:



Google uses associated advertising companies to serve ads when it visits our website. These companies may use the information they obtain from your visits to this and other websites (not including your name, address, email address, or phone number) to provide you with announcements about products and services that interest you. If you would like to learn more about this practice and know your options to prevent these companies from using this information. Click in... Privacy and Terms of Google.

Cookies

This site uses Google cookies to provide its services, to personalize advertisements and to analyze traffic. Google receives information about your use of this website. More information in... Privacy and Terms of Google.