-
Notifications
You must be signed in to change notification settings - Fork 0
/
satest.java
86 lines (76 loc) · 2.64 KB
/
satest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.Scanner;
import java.util.StringTokenizer;
public class satest {
public static void main(String[] args) {
boolean mode = false; // Console mode
if (args.length > 0) if (!args[0].equals("-")) mode = true; // File mode
if (mode) {
System.out.println("File mode was selected.");
String fOutName;
if (args.length > 1) fOutName=args[1]; else fOutName="output.txt";
StringBuffer buffer = new StringBuffer();
try (FileInputStream fileIn = new FileInputStream(args[0]);
FileOutputStream fileOut = new FileOutputStream(fOutName);
BufferedReader reader = new BufferedReader(new InputStreamReader(fileIn, "UTF8"));
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(fileOut, "UTF8"));){
int ch; // code of one character
while ((ch = reader.read()) > -1) buffer.append((char)ch);
writer.write(parseStr(buffer.toString()));
System.out.println("Result was successfully recorded to file!");
} catch (Exception e) {
System.out.println(e.toString());
}
}
else {
System.out.println("Console mode was selected.");
System.out.println("Use the commands: add (or sum), mul (multiplication), ma (a*b+c), quit (for exit)");
Scanner scan = new Scanner(System.in);
while (true) {
System.out.print("Input a command: ");
String str = scan.nextLine();
if (str.equals("quit")) {
System.out.println("Good bye!");
break;
}
System.out.println(parseStr(str));
}
}
}
private static String parseStr(String str) {
StringTokenizer st = new StringTokenizer(str," ");
String com = st.nextToken();
int [] arr = new int[st.countTokens()];
for(int i = 0; i<arr.length; i++)
try{
arr[i] = Integer.parseInt(st.nextToken());
}catch(NumberFormatException ex) {
return "Incorrect number format!";
}
if (com.equals("add") || com.equals("sum")) return "Result: "+add(arr);
else if (com.equals("mul")) return "Result: "+ mul(arr);
else if (com.equals("ma")) if (arr.length == 3) return "Result: "+ ma(arr); else
return "This command has 3 arguments!";
else return "Unknown command!";
}
private static long add(int[] arr) {
long res = 0;
for (Integer num: arr) res = res + num;
return res;
}
private static long mul(int[] arr) {
long res = 1;
for (Integer num: arr) res = res * num;
return res;
}
private static long ma(int[] arr) {
long res = 0;
res = arr[0]*arr[1]+arr[2];
return res;
}
}