-
Notifications
You must be signed in to change notification settings - Fork 2
/
ComputePoly4[1].java
285 lines (275 loc) · 8.01 KB
/
ComputePoly4[1].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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
import java.awt.List;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
//1111
//2222
////asdfad
///2017-9-5
////september 15 second
public class ComputePoly4 {
//filter all the void character.
public static int isdigit(char c){
if (c >= 48 && c <= 57)
return 1;
else {
return 0;
}
}// if the current char is a number
public static boolean voidFormula(String str){
Pattern neg_p = Pattern.compile("[0123456789]");
Matcher neg_m = neg_p.matcher(str);
if (!neg_m.find()){
System.out.println("该输入没有数字,不能计算");
return true;
}
return false;
}
public static boolean formatError(String str){
String newsc = str.replaceAll("\\s+", "");
if (newsc.charAt(0) == '+' || newsc.charAt(0) == '-' ){
newsc = newsc.substring(1);
}
newsc += "+";
String string = newsc;
Pattern neg_p = Pattern.compile("(\\{(\\(-?[0-9]+,[0-9]+\\),)*\\(-?[0-9]+,[0-9]+\\)\\}(\\+|-))");
Matcher neg_m = neg_p.matcher(string);
while(neg_m.find()){
newsc = newsc.replace(neg_m.group(), "");
}
if(newsc.length() != 0){
System.out.println("格式错误");
return true;
}
return false;
}
public static boolean illegalChar(String str){
Pattern neg_p = Pattern.compile("[^ \\{\\}\\(\\)\\+-0123456789]");
Matcher neg_m = neg_p.matcher(str);
if (neg_m.find()){
System.out.println("输入了不合法字符");
return true;
}
return false;
}
public static boolean bracketsUnmatch(String str){
char []origin = str.toCharArray();
int i = 0;
ArrayList<Character> brackets = new ArrayList<Character>();
for(i = 0; i < str.length(); i++){
if((brackets.size() == 0 && (origin[i] == '(' || origin[i] == '{' || origin[i] == ')' || origin[i] == '}')) || origin[i] == '(' || origin[i] == '{'){
brackets.add(origin[i]);
}
else if (origin[i] == ')'){
if ((Character)brackets.get(brackets.size() - 1) == '(')
brackets.remove(brackets.size() - 1);
else
brackets.add(origin[i]);
}
else if (origin[i] == '}'){
if ((Character)brackets.get(brackets.size() - 1) == '{')
brackets.remove(brackets.size() - 1);
else
brackets.add(origin[i]);
}//the match of the brackets
}
if (brackets.size() > 0) {
System.out.println("括号不匹配");
return true;
}
return false;
}
public static boolean minusSquare(String str){
String newsc = str.replaceAll("\\s+", "");
if (newsc.charAt(0) == '+' || newsc.charAt(0) == '-' ){
newsc = newsc.substring(1);
}
newsc += "+";
String string = newsc;
Pattern neg_p = Pattern.compile("(\\{(\\(-?[0-9]+,[0-9]+\\),)*\\(-?[0-9]+,[0-9]+\\)\\}(\\+|-))");
Matcher neg_m = neg_p.matcher(string);
while(neg_m.find()){
newsc = newsc.replace(neg_m.group(), "");
}
if(newsc != null){
Pattern p = Pattern.compile("(\\(-?[0-9]+,-[0-9]+\\))");
Matcher m = p.matcher(newsc);
if (m.find()){
System.out.println("阶数为负");
return true;
}
}
return false;
}
public static boolean tooLong(String str){
String newsc = str.replaceAll("\\s+", "");
Pattern p = Pattern.compile("(0{0,}[1-9]{9,})");
Pattern q = Pattern.compile("(,0{0,}[1-9]{7,}\\))");
Matcher m = p.matcher(newsc);
Matcher n = q.matcher(newsc);
if (m.find() || n.find()){
System.out.println("数字过长");
return true;
}
return false;
}
public static void Compute(String str){
int []formula = new int[1000000];
//
int i = 0;
int nums_ver = 0, nums_hor = 0;
char []origin = str.toCharArray();
char operand = '+';
int temp = 0, temp1 = 0, temp2 = 0;
boolean isMinus = false;
for (i = 0; i < str.length(); i++){
if (origin[i] == '}'){
nums_ver++;
}
else if ( ((origin[i] == '+' || origin[i] == '-') && (i == 0)) || (origin[i] == '+' && origin[i - 1] == '}' && origin[i + 1] == '{')
|| (origin[i] == '-' && origin[i - 1] == '}' && origin[i + 1] == '{')
){
operand = origin[i];
}// let the operand in
else{
if (origin[i] == '-' && origin[i - 1] == '(' && isdigit(origin[i + 1]) != 0 ){
isMinus = true;
}
if( isdigit(origin[i]) != 0 ){
temp = 10 * temp + origin[i] - 48;
}//if is digit
if( origin[i] == ','){
temp1 = temp;
temp = 0;
if (isMinus) {
temp1 = -temp1;
isMinus = false;
};
continue;
}//the input of XiShu is finished, get one XiShu
if( origin[i] == ')' ){
temp2 = temp;
temp = 0;
if (operand == '+')
formula[temp2] += temp1;
else if(operand == '-')
formula[temp2] -= temp1;
if (temp2 > nums_hor)
nums_hor = temp2;
continue;
}//the input of ZhiShu is finished, get one ZhiShu
}
}
System.out.print("{");
for (i = 0; i < nums_hor + 1; i++){
if (nums_ver == 1) nums_ver = 0;//special occation
if (formula[i] != 0){
if (i != nums_hor ){
System.out.print("(");
System.out.print(formula[i]);
System.out.print(",");
System.out.print(i);
System.out.print("),");
}
else{
System.out.print("(");
System.out.print(formula[i]);
System.out.print(",");
System.out.print(i);
System.out.print(")");
}
}
}
System.out.print("}");
}
public static void main(String []args){
//all the methods are specified here
System.out.println("please enter: ");
String str=new Scanner(System.in).nextLine();
String newsc = str.replaceAll("\\s+", "");
boolean error = formatError(newsc) |
illegalChar(newsc) |
bracketsUnmatch(newsc) |
minusSquare(newsc) |
tooLong(newsc) |
voidFormula(newsc);
if (!error){
Compute(newsc);
}
}
}
/*public static int ErrorFind(String str){
if (illegalChar(str)){
System.out.println("输入了不合法字符");
return 1;
}
if (formatError(str)){
if(bracketsUnmatch(str)){
System.out.println("格式错误:括号不匹配");
return 3;
}
System.out.println("格式错误");
return 2;
}
if(minusSquare(str)){
System.out.println("阶数为负");
return 4;
}
if (tooLong(str)){
System.out.println("数字过长");
return 5;
}
return 0;
}//judge if the raw string is legal*/
/*
public static void main(String args[]){
byte[] buffer=new byte[512];
try {
System.out.print("请你输入: ");
System.in.read(buffer);//input your data, and ends with a "Return" key.
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String str=new String(buffer);
System.out.println("what you input is : "+str);
}
*/
/*Scanner sc=new Scanner(System.in).nextLine();
System.out.print("1输入字符:");
String m=sc.next();
System.out.println("你输入的是"+m);*/
/*BufferedReader bf=new BufferedReader(new InputStreamReader(System.in));
System.out.print("2输入字符: ");
String s=null;
try {
s=bf.readLine();
} catch (IOException e) { e.printStackTrace();}
System.out.println("你输入的是"+s);*/
/*Pattern pos_p = Pattern.compile("[0-9\\.]+");
Pattern neg_p = Pattern.compile("-[0-9\\.]+");
Matcher pos_m = pos_p.matcher(str);
Matcher neg_m = neg_p.matcher(str);
int i = 0;
String j = new String();
while(pos_m.find() || neg_m.find()){
if (pos_m.find()) j = pos_m.group();
else if (neg_m.find()) j = neg_m.group();
i = Integer.parseInt(j);
System.out.print(i+",");
}*/
/*for(i = 0; i < nums_hor + 1; i++){
for (j = 0, formula[nums_ver][i] = formula[0][i]; j < nums_ver - 1; j++){
operand.get(j);
if ((Character)operand.get(j) == '+'){
formula[nums_ver][i] += formula[j + 1][i];
}
else if ((Character)operand.get(j) == '-'){
formula[nums_ver][i] -= formula[j + 1][i];
}
}
}//compute the formula*/