Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- import java.util.Stack;
- public class PuntoCuatro {
- Scanner scanner = new Scanner(System.in);
- public static void main(String[] args) {
- new PuntoCuatro().run();
- }
- void proccess(Stack<Character> stack, char newChar)
- {
- Stack<Character> backupSort = new Stack<Character>();
- while(!stack.isEmpty() && Character.toLowerCase(stack.peek()) > Character.toLowerCase(newChar))
- {
- backupSort.push(stack.pop());
- }
- stack.push(newChar);
- while(!backupSort.isEmpty()) stack.push(backupSort.pop());
- }
- boolean isValidLetter(char xCh)
- {
- return Character.isAlphabetic(xCh);
- }
- char forceReadAnLetter()
- {
- System.out.println("Introduzca una letra (si desea terminar la carga ingrese @):");
- char x =' ';
- String piv;
- try
- {
- while((piv=scanner.next()).length() > 1 || !isValidLetter(x=piv.charAt(0)) && x!= '@') System.out.println("Debe introducir una letra.\nIntroduzca letra:");
- }
- catch(Exception ex)
- {
- System.out.println("Error en la carga.");
- }
- return x;
- }
- boolean isVowel(char xCh)
- {
- xCh = Character.toLowerCase(xCh);
- return xCh == 'a' || xCh == 'e' || xCh == 'i' || xCh == 'o' || xCh == 'u';
- }
- void showStackValues(Stack<Character> stack)
- {
- int countConsonants = stack.size();
- System.out.print("Elementos de la pila:");
- Stack<Character> showStack = new Stack<Character>();
- while(!stack.isEmpty())
- {
- char popped = stack.pop();
- if(isVowel(popped)) countConsonants--;
- showStack.push(popped);
- }
- while(!showStack.isEmpty()) System.out.print(showStack.pop() + (stack.isEmpty() ? "" : ","));
- System.out.println("\nConsonantes:" + countConsonants);
- }
- boolean isRandom()
- {
- char x = '\0';
- while(true)
- {
- try
- {
- System.out.println("¿Generar valores aleatorios? (S/N)");
- x =Character.toUpperCase(scanner.next().charAt(0));
- if(x == 'S' || x == 'N') return x == 'S';
- System.out.println("Opción inválida, reintente.");
- }catch (Exception e) {
- System.out.println("Opción inválida, reintente.");
- }
- }
- }
- static int getRandomIntBetweenRange(double min, double max){
- int x = (int) ((int)(Math.random()*((max-min)+1))+min);
- return x;
- }
- char generateRandomLetter()
- {
- return ((getRandomIntBetweenRange(0,17)%16 == 2) ? '@' : (char) (getRandomIntBetweenRange(0, 25)+65));
- }
- void run()
- {
- char newChar;
- Stack<Character> stackLetters = new Stack<Character>();
- boolean random = isRandom();
- while((!random && (newChar = forceReadAnLetter()) != '@') || random && (newChar = generateRandomLetter()) != '@')
- {
- proccess(stackLetters, newChar);
- }
- showStackValues(stackLetters);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment