国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

Hdu 1237

2019-11-11 06:05:12
字體:
來源:轉載
供稿:網友

簡單計算器

Time Limit: 2000/1000 MS (java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 19484 Accepted Submission(s): 6860

PRoblem Description

讀入一個只包含 +, -, *, / 的非負整數計算表達式,計算該表達式的值。

Input

測試輸入包含若干測試用例,每個測試用例占一行,每行不超過200個字符,整數和運算符之間用一個空格分隔。沒有非法表達式。當一行中只有0時輸入結束,相應的結果不要輸出。

Output

對每個測試用例輸出1行,即該表達式的值,精確到小數點后2位。

Sample Input

1 + 2 4 + 2 * 5 - 7 / 11 0

Sample Output

3.00 13.36

題解:棧原理。先處理乘除,再處理加減。

//Java代碼import java.util.Scanner;public class Main { public static void main(String[] args) { double[] num = new double[201]; char[] sign = new char[201]; Scanner in = new Scanner(System.in); while(in.hasNextLine()){ String str = in.nextLine(); if(str.equals("0")) break; String[]strs = str.split(" "); int j = 0; int k = 0; //處理乘除 for(int i=0;i<strs.length;i++){ if(strs[i].equals("*") || strs[i].equals("/") || strs[i].equals("+") || strs[i].equals("-")){ switch(strs[i]){ case "+": sign[k++] = '+';break; case "-": sign[k++] = '-';break; case "*": num[j-1] = num[j-1]*Integer.valueOf(strs[i+1]); i++; break; case "/": num[j-1] = num[j-1]/Integer.valueOf(strs[i+1]); i++; break; } }else{ num[j++] = Integer.valueOf(strs[i]); } } int l = 0; double res = 0; //處理加減 for(int i=0;i<j;i++){ if(i==0){ res = num[i]; continue; } switch(sign[l]){ case '+': res += num[i]; l++; break; case '-': res -= num[i]; l++; break; } } System.out.println(String.format("%.2f", res)); //初始化數組 for(int i=0;i<num.length;i++){ num[i] = 0; sign[i] = ' '; } } }}
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 金塔县| 通许县| 龙山县| 贵南县| 三江| 洛隆县| 英德市| 东安县| 察雅县| 北碚区| 镇康县| 商南县| 开鲁县| 淳安县| 兰坪| 晋城| 洛川县| 得荣县| 崇义县| 乐陵市| 安丘市| 乌兰浩特市| 阿城市| 潼南县| 东台市| 青神县| 鄱阳县| 永新县| 蚌埠市| 贵州省| 田阳县| 乐都县| 云南省| 荃湾区| 伊春市| 措勤县| 白玉县| 襄垣县| 南召县| 抚州市| 新巴尔虎右旗|