生活随笔
收集整理的這篇文章主要介紹了
【编程题】简单的四则运算
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
問題描述:?輸入一個只包含個位數字的簡單四則運算表達式字符串,計算該表達式的值
注: 1、表達式只含?+,?-,?*,?/, (, ),?四則運算符
2、表達式數值只包含個位整數(0-9),且不會出現0作為除數的情況
3、要考慮加減乘除按通常四則運算規定的計算優先級
4、除法用整數除法,即僅保留除法運算結果的整數部分。比如8/3=2。輸入表達式保證無0作為除數情況發生
5、輸入字符串一定是符合題意合法的表達式,其中只包括數字字符和四則運算符字符,除此之外不含其它任何字符,不會出現計算溢出情況
? 要求實現函數:?
int?calculate(int?len,char?*expStr)
【輸入】 int?len:?字符串長度;
char?*expStr:?表達式字符串;
【輸出】 無
【返回】 計算結果
? 示例?
1) 輸入:char?*expStr?=?“1+4*5-8/3”
函數返回:19
2) 輸入:char?*expStr?=?“8/3*3”
函數返回:6?
1 package MyTest;
2
3 /**
4 * 簡單的四則運算,每個參與運算的數字都在0-9之間。
5 */
6
7 import java.util.*
;
8
9 public class FourOps {
10
11 public static void main(String[] args) {
12 Scanner in =
new Scanner(System.in);
13 while(in.hasNext()){
14 String expretion =
in.next();
15 int length =
expretion.length();
16 int result =
compute(length, expretion);
17 System.out.println(result);
18 }
19
20 }
21
22 /**
23 * 該函數有兩個功能
24 * 1. 首先把一個正常的中綴表達式,轉化為一個后綴表達式
25 * 2. 通過后綴表達式計算表達式的值
26 * @param length
27 * @param expretion
28 * @return
29 */
30 private static int compute(
int length, String expretion) {
31 int result = 0
;
32 List<Character> oneOps =
new LinkedList<>
();
33 oneOps.add('+'
);
34 oneOps.add('-'
);
35 List<Character> twoOps =
new LinkedList<>
();
36 twoOps.add('*'
);
37 twoOps.add('/'
);
38 LinkedList<Character> ops =
new LinkedList<>();
//用作棧,前面一定也要是LinkedList
39 StringBuffer changedExp =
new StringBuffer();
40 for(
int i = 0; i < length; i++
){
41 Character temp =
expretion.charAt(i);
42 if(temp >= '0' && temp <= '9'
)
43 changedExp.append(temp);
44 else{
45 if(ops.isEmpty())
46 ops.push(temp);
47 else{
48 if(temp == '('
)
49 ops.push(temp);
50 else{
51 if(oneOps.contains(temp)){
52 // if(ops.peek() != '(')
53 //原來用的if,考慮在遇到+-的時候應該把棧里的運算符都pop出來,現在改用while
54 while(!
ops.isEmpty()){
55 if(ops.peek() == '('
)
56 break;
57 changedExp.append(ops.pop());
58 }
59 ops.push(temp);
60 }
61 else if(twoOps.contains(temp) && twoOps.contains(ops.peek())){
//為了連續的/法
62 changedExp.append(ops.pop());
63 ops.push(temp);
64 }
65 else if(temp != ')'
){
66 ops.push(temp);
67 }
68 else{
//')'的情況
69 while(ops.peek() != '('
){
70 changedExp.append(ops.pop());
71 }
72 ops.pop();
73 }
74 }
75 }
76 }
77 }
78 while(!
ops.isEmpty()){
79 changedExp.append(ops.pop());
80 }
81 String changedExpStr =
changedExp.toString();
82 System.out.println(changedExpStr);
//輸出轉化后的后綴表達式
83
84 //用后綴表達式計算
85 LinkedList<Integer> nums =
new LinkedList<>
();
86 for(
int i = 0; i < changedExpStr.length(); i++
){
87 Character temp =
changedExpStr.charAt(i);
88 if(temp >= '0' && temp <= '9'
){
89 nums.push(temp-'0'
);
90 }
91 else{
92 int a =
nums.pop();
93 int b =
nums.pop();
94 switch(temp){
95 case '+': nums.push(a+b);
break;
96 case '-': nums.push(b-a);
break;
97 case '*': nums.push(a*b);
break;
98 case '/': nums.push(b/a);
break;
99 }
100 }
101 }
102 result =
nums.pop();
103 return result;
104 }
105
106 }
?
轉載于:https://www.cnblogs.com/focusonepoint/p/5755117.html
總結
以上是生活随笔為你收集整理的【编程题】简单的四则运算的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。