我写了一个后缀计算器,它从键盘和文本文件中获取价值。如果表达式无效,则会给出错误提示。现在我需要从评估的表达式结果中生成一些统计信息。一旦评估完成,应收集并显示六种不同的统计数据。
在程序终止之前,统计信息应显示如下 -
Evaluations complete
Highest result: 1024.5
Lowest result: -120
Aggregate result: 2212
Average result: 340
Invalid expressions: 6
Valid expressions: 150
注意:如果有效表达式的数量为零,则最高,最低和平均值应显示为n / a。
我正在努力解决这个问题,不知道从哪里开始。任何帮助,将不胜感激。谢谢!
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class PostfixCalc {
public static void main(String[] args) {
String filename = "";
Scanner keyboard = new Scanner(System.in);
System.out.println("Press K for keyboard or F to read expressions from a file");
String user_input = keyboard.nextLine();
String[] ui = user_input.split(" ");
if (ui[0].equals( "k") | ui[0].equals("K")) {
while (true){
System.out.println("Please Enter a Post-Fix Expression (eg: 5 2 *)");
String postfix=keyboard.nextLine();
String [] elements =postfix.split(" ");
if (postfix.equals("")){
System.out.println("Application Closed");
keyboard.close();
System.exit(0);
}
if (elements.length >=3){
try{
float num1, num2;
num1 = Float.parseFloat(elements[0]);
num2 = Float.parseFloat(elements[1]);
if(elements[2].equals("+")){
System.out.println("Total: "+(num1 + num2));
}
else if(elements[2].equals("*")){
System.out.println("Total: "+(num1 * num2));
}
else if(elements[2].equals("/")){
System.out.println("Total: "+(num1 / num2));
}
else if(elements[2].equals("-")){
System.out.println("Total: "+(num1 - num2));
}
else{
System.out.println("Error Invalid Expression: "+ postfix);
}
}
catch(NumberFormatException e){
System.out.println("Error Invalid Expresion: "+postfix);
}
}
else if (elements.length <3) {
System.out.println("Error Invalid Expression: "+ postfix);
}
}
} else if (ui[0].equals("f") || ui[0].equals("F")) {
try {
System.out.println("Please enter file name eg: Demo.txt");
filename = keyboard.nextLine();
Scanner s = new Scanner(new File(filename));
System.out.println("Processing " + filename);
System.out.println();
String line = s.nextLine();
String array[] = line.split(" ");
if (array.length >= 3) {
try {
float array1, array2, total1, total2, total3, total4, array3, array5;
array1 = Float.parseFloat(array[0]);
array2 = Float.parseFloat(array[1]);
total1 = array1 + array2;
total2 = array1 * array2;
total3 = array1 / array2;
total4 = array1 - array2;
if (array[2].equals("+")) {
System.out.println("Postfix Expression: " + line);
System.out.println(array1 + " " + array[2] + " " + array2 + " = " + total1);
System.out.println();
} else if (array[2].equals("*")) {
System.out.println("Postfix Expression: " + line);
System.out.println(array1 + " " + array[2] + " " + array2 + " = " + total2);
System.out.println();
} else if (array[2].equals("/")) {
System.out.println("Postfix Expression: " + line);
System.out.println(array1 + " " + array[2] + " " + array2 + " = " + total3);
System.out.println();
} else if (array[2].equals("-")) {
System.out.println("Postfix Expression: " + line);
System.out.println(array1 + " " + array[2] + " " + array2 + " = " + total4);
System.out.println();
} else {
System.out.println(" Error Invalid Expression: " + line);
System.out.println();
}
} catch (NumberFormatException e) {
System.out.println(" Error Invalid Expresion: " + line);
System.out.println();
}
} else if (array.length < 3) {
System.out.println(" Error Invalid Expression: " + line);
System.out.println();
}
while (s.hasNext()) {
String nl = s.nextLine();
String ar[] = nl.split(" ");
if (ar.length >= 3) {
try {
float ar1, ar2, total1, total2, total3, total4;
ar1 = Float.parseFloat(ar[0]);
ar2 = Float.parseFloat(ar[1]);
total1 = ar1 + ar2;
total2 = ar1 * ar2;
total3 = ar1 / ar2;
total4 = ar1 - ar2;
if (ar[2].equals("+")) {
System.out.println("Postfix Expression: " + nl);
System.out.println(ar1 + " " + ar[2] + " " + ar2 + " = " + total1);
System.out.println();
} else if (ar[2].equals("*")) {
System.out.println("Postfix Expression: " + nl);
System.out.println(ar1 + " " + ar[2] + " " + ar2 + " = " + total2);
System.out.println();
} else if (ar[2].equals("/")) {
System.out.println("Postfix Expression: " + nl);
System.out.println(ar1 + " " + ar[2] + " " + ar2 + " = " + total3);
System.out.println();
} else if (ar[2].equals("-")) {
System.out.println("Postfix Expression: " + nl);
System.out.println(ar1 + " " + ar[2] + " " + ar2 + " = " + total4);
System.out.println();
} else {
System.out.println(" Error Invalid Expression: " + nl);
System.out.println();
}
} catch (NumberFormatException e) {
System.out.println(" Error Invalid Expresion: " + nl);
System.out.println();
}
} else if (array.length < 3) {
System.out.println(" Error Invalid Expression: " + nl);
System.out.println();
}
}
}
catch (FileNotFoundException e) {
System.out.println("Error: That file does not exist, please re-enter: ");
filename = keyboard.nextLine();
Scanner s;
}
} else {
System.out.println("Neither K or F have been entered");
System.out.println("System Terminated");
keyboard.close();
}
}
}
答案 0 :(得分:0)
在执行期间,您必须在某种集合中获取每个已处理操作的结果。
然后,处理完所有操作后,您将计算这些统计信息并输出它们。
我不会更精确,因为它似乎是某种功课。