翻译电话号码

来源:互联网 发布:cad编程 编辑:程序博客网 时间:2024/04/28 01:38

 电话号码 one two 。。。nine zero
翻译成1  2 。。9 0

中间会有double

 

例如输入:OneTwoThree
输出:123

输入:OneTwoDoubleTwo
输出:1222

输入:1Two2输出:ERROR

输入:DoubleDoubleTwo输出:ERROR

第三题:有空格,非法字符,两个Double相连,Double位于最后一个单词都错误


package com.jhl.test;


import java.util.HashMap;
import java.util.Stack;


public class GetResult11 {


/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String result=getResult("OneDoubleThreeDoubleDoubleFive");
System.out.println(result);
}
    public static String getResult(String input){
    HashMap<String,Integer> map=new HashMap<String,Integer>();
    map.put("One", 1);
    map.put("Two", 2);
    map.put("Three", 3);
    map.put("Four", 4);
    map.put("Five", 5);
    map.put("Six", 6);
    map.put("Seven", 7);
    map.put("Eight", 8);
    map.put("Nine", 9);
    map.put("Zero", 0);
    Stack<String> stack=new Stack<String>();
    for(int i=0;i<input.length();i++){
    char c=input.charAt(i);
    if(Character.isLetter(c)){
    if(stack.isEmpty()){
    stack.push(String.valueOf(c));
    }else{
    String temp=stack.peek();
    if(temp.length()==1){
    String temp1=stack.pop();
    String temp2=String.valueOf(c);
    stack.push(temp1+temp2);
    }else if(temp.length()==2){
    String temp1=stack.pop();
    String temp2=String.valueOf(c);
    stack.push(temp1+temp2);
    }else if(temp.length()==3){
    if(temp.equals("One")||temp.equals("Two")||temp.equals("Six")){
    stack.push(String.valueOf(c));
    }else{
    String temp1=stack.pop();
        String temp2=String.valueOf(c);
        stack.push(temp1+temp2);
    }
    }else if(temp.length()==4){
    if(temp.equals("Four")||temp.equals("Five")||temp.equals("Nine")||temp.equals("Zero")){
    stack.push(String.valueOf(c));
    }else{
    String temp1=stack.pop();
        String temp2=String.valueOf(c);
        stack.push(temp1+temp2);
    }
    }else if(temp.length()==5){
    if(temp.equals("Three")||temp.equals("Seven")||temp.equals("Eight")){
    stack.push(String.valueOf(c));
    }else{
    String temp1=stack.pop();
        String temp2=String.valueOf(c);
        stack.push(temp1+temp2);
    }
    }else{
    if(temp.equals("Double")){
    stack.push(String.valueOf(c));
    }
    }
    }
    }else{
    return "ERROR";
    }
    }
   
    System.out.println(stack);
    String result="";
    Stack<String> checkstack=new Stack<String>();
    String temp=stack.peek();
    if(temp.equals("Double")){
    return "ERROR";
    }else{
    while(!stack.isEmpty()){
    String temp1=stack.pop();
    if(checkstack.isEmpty()){
    checkstack.push(temp1);
    }else{
    String temp3=checkstack.peek();
    if(temp3.equals("Double")){
    if(temp1.equals("Double")){
    return "Error";
    }else{
    checkstack.push(temp1);
    }
    }else{
    checkstack.push(temp1);
    }
    }
    }
    System.out.println(checkstack);
    Stack<String> word=new Stack<String>();
    while(!checkstack.isEmpty()){
    String temp6=checkstack.pop();
    if(word.isEmpty()){
    word.push(temp6);
    }else{
    String temp7=word.peek();
    if(temp7.equals("Double")){
    word.pop();
    word.push(temp6);
    word.push(temp6);
    }else{
    word.push(temp6);
    }
   
    }
    }
   
    System.out.println(word);
    while(!word.isEmpty()){
    String temp4=word.pop();
    int value=map.get(temp4);
    result=value+result;
    }
    return result;
    }
   
   
   
   
   
    }
}

原创粉丝点击