设为首页 收藏本站
查看: 925|回复: 0

[经验分享] Meal WaitPerson and Chef

[复制链接]

尚未签到

发表于 2015-11-26 01:58:07 | 显示全部楼层 |阅读模式
import java.util.Stack;
import java.util.regex.Pattern;
import java.util.regex.Matcher;
import java.util.Map;
import java.util.HashMap;
public class ArithmeticExpressionEvaluation {
private static Stack<Character> theStack;
private static char charKey=(char)97;
private  static Map<Character,String>  hashMap=new HashMap<Character,String>();
private static String result;
public static void main(String[] args) {
if(args.length<1) {
System.out.println(&quot; please enter the input arithmetic expression string  that needs to be evaluated.&quot;+
&quot;the  legal operators include '+', '-','*' and '/' ,and you can also add '(' and ')' .&quot;+
&quot;all numbers must above zero, the program doesn't support negative values now &quot;
);
System.exit(0);
}
System.out.println(&quot;the original input is :&quot;+args[0]);
String internalStr=processBeforeEvaluation(args[0]);
System.out.println(&quot;the internal expression is :&quot;+internalStr);
String postfixStr= translateToPostfixExpression(internalStr);
System.out.println(&quot;the postfix expression is :&quot;+postfixStr);
double result=evaluatePostfixExpression(postfixStr);
System.out.println(&quot;the final result is :&quot;+result);
}
private static String processBeforeEvaluation(String input) {
Matcher legal=Pattern.compile(&quot;[^0-9.*+/\\()-]&quot;).matcher(input);
if(legal.find()) {
System.out.println(&quot;Please enter legal arithmetic expression string.&quot;);
System.exit(0);
}
StringBuffer sbuf = new StringBuffer();
Matcher m=Pattern.compile(&quot;\\d+(\\.\\d+)?&quot;).matcher(input);
while(m.find()) {
String temp=m.group(0);
hashMap.put(charKey,temp);
m.appendReplacement(sbuf,charKey+&quot;&quot;);
charKey=(char)(charKey+1);
}
m.appendTail(sbuf);
return sbuf.toString();
}

private static String translateToPostfixExpression(String input) {
theStack=new Stack<Character>();
result=&quot;&quot;;
for(int j=0; j<input.length(); j++) {
char ch=input.charAt(j);
switch(ch) {
case '+':
case '-':
case '*':
case '/':
gotOper(ch);
break;
case '(':
theStack.push(ch);
break;
case ')':
gotParen(ch);
break;
default :
result+=ch;
break;
}  //end switch
} //end for loop
while(!theStack.empty()) {
result+=theStack.pop();
}
return result;
}
private static void gotOper(char opThis) {
while(!theStack.empty()) {
char opTop=theStack.pop();
if(opTop=='(') {
theStack.push(opTop);
break;
} else {
if(opThis=='*'||opThis=='/') {   //如果是本次是乘除,栈里最多可弹出一个乘号
if(opTop=='+'||opTop=='-') {
theStack.push(opTop);
} else {
result+=opTop;
}
break;
} else {            //如果是本次是加减,栈里最多可弹出一次乘除,再加一次加减
result+=opTop;
}
} //end else
}//end while
theStack.push(opThis);
}//end gotOper()

private static void gotParen(char ch) {
while(!theStack.empty()) {
char chx=theStack.pop();
if(chx=='(')
break;
else
result+=chx;
} //end while
}

private static double evaluatePostfixExpression(String input) {
System.out.println(&quot;the content of the hashMap is :&quot;+hashMap);
char ch;
double num1,num2,interAns=0;
String str1,str2;
theStack=new Stack<Character>(); //重置栈,后缀表达式求值是将数字放入栈中
try {
for(int j=0; j<input.length(); j++) {
ch=input.charAt(j);
if(ch>=97) {  //j代表一个double型的数字
theStack.push(ch);
} else {
str2 =hashMap.get(theStack.pop());
str1=hashMap.get(theStack.pop());
num2=Double.parseDouble(str2);
num1=Double.parseDouble(str1);
switch(ch) {
case '+':
interAns=num1+num2;
break;
case '-':
interAns=num1-num2;
break;
case '*':
interAns=num1*num2;
break;
case '/':
interAns=num1/num2;
break;
default :
interAns=0;
break;
} //end switch
hashMap.put(charKey,interAns+&quot;&quot;);
theStack.push(charKey);
charKey=(char)(charKey+1);
} //end else
} //end for loop
str1=hashMap.get(theStack.pop());
interAns=Double.parseDouble(str1);
} catch(Exception e) {
System.out.println(&quot;please enter legal numbers!&quot;);
e.printStackTrace();
}
return interAns;
}


}

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-143560-1-1.html 上篇帖子: zoj 3778 Talented Chef 贪心 下篇帖子: 【codechef】Chef and the Cards(dp,推论)
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表