-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdemo.java
More file actions
355 lines (306 loc) · 6.98 KB
/
demo.java
File metadata and controls
355 lines (306 loc) · 6.98 KB
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
package demo;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
public class demo {
static boolean logopen = false;
/**
* 主函数
* @param arg
*/
public static void main(String[] arg){
dosomething();
}
/**
* 程序入口
*/
public static void dosomething(){
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String t=null;
String r = null;
String x = null;
try {
t = in.readLine();
} catch (IOException e) {
log("输入输出错误...");
}
try {
r = in.readLine();
} catch (IOException e) {
log("输入输出错误...");
}
try {
x = in.readLine();
} catch (IOException e) {
log("输入输出错误...");
}
//尝试解析字符串
linkList list = getLink(t);
linkList listr = getLink(r);
linkList listx = getLink(x);
System.out.println("链表公式:"+list);
System.out.println("链表公式:"+listr);
System.out.println("链表公式:"+listx);
linkList[] o = {
list,
listr,
listx
};
linkList re = mergeList(o);
System.out.println("结果:"+re.toString());
}
/**
* 合并多个公式
* @param linkListArr
* @return
*/
public static linkList mergeList(linkList[] linkListArr){
//合并结果链表
linkList resultLink = new linkList();
for(int i=0;i<linkListArr.length;i++){
linkList curr = linkListArr[i];
node currHead = curr.head;
while(currHead != null){
int cx = currHead.bean.x;
int cn = currHead.bean.n;
log(i+"/"+cx+"X^"+cn);
boolean nhasN = currHead.bean.hasN;
for(int j = 0;j<linkListArr.length;j++){
node temp = linkListArr[j].head;
if(temp != null){
while(true){
log("扫描节点:"+j+" ---- "+temp.bean.x+(temp.bean.hasN ? ("X^"+temp.bean.n):" 常数"));
if(temp.bean.hasN == nhasN&&temp.bean.n == cn&&temp != currHead){
if(nhasN){
log("找到未知数幂相等的节点...。准备删除");
} else {
log("常数...");
}
cx = temp.bean.x+cx;
linkListArr[j].delete(temp);
}
if(temp.next != null){
temp = temp.next;
} else {
break;
}
}
}
}
resultLink.add(new node(new bean(cx, nhasN, cn)));
if(currHead.next != null){
currHead = currHead.next;
} else {
break;
}
}
}
return resultLink;
}
/**
* 转换字符串为链表
* @param str
* 需要转换的字符串 《5X^3-6X^7+3X^9-8X+9X^4》
* @return
*/
public static linkList getLink(String str){
log("开始解析公式...");
List<String> tArr = null;
try{
//在输入的公式中提取
tArr = getArr(str);
} catch(Exception e){
log("公式解析错误...:"+e.getMessage());
return null;
}
//将子字符串转换成链表节点数据
linkList list=new linkList();
for(int k=0;k<tArr.size();k++){
String temp = tArr.get(k);
if(temp.contains("X")){//子字符串包含未知数X,
int x=0;
int n=0;
boolean hasN=true;
if(temp.contains("^")) {//指数大于或等于1 《5X^3》
String[] xArr = temp.split("[X][//^]");
log("length:"+xArr.length);
log(xArr.length+"\\"+xArr[0]+"\\"+xArr[1]);
if(xArr[0].equals(""))
x=1;
else
x=Integer.parseInt(xArr[0]);
n=Integer.parseInt(xArr[1]);
list.add(new node(new bean(x, hasN, n)));
} else {//未指定指数,指数等于1 《5X》
String[] xArr = temp.split("[X]");
log("length:"+xArr.length);
log(xArr.length+"\\"+xArr[0]);
if(xArr[0].equals(""))
x=1;
else
x=Integer.parseInt(xArr[0]);
n=1;
list.add(new node(new bean(x, hasN, n)));
}
} else { //该字串属于常数
int x=0;
int n=0;
boolean hasN=false;
x = Integer.parseInt(temp);
list.add(new node(new bean(x, hasN, n)));
}
}
return list;
}
/**
* 正系数切分
* @param str 需要切分的字符串
* @return
* @throws Exception
*/
public static List<String> getArr(String str) throws Exception{
ArrayList<String> list = new ArrayList<>();
String[] temp = str.split("[+]");
if(temp!=null&&temp.length>0){
for(int i=0;i<temp.length;i++){
if(!temp[i].contains("-")){
list.add("+"+temp[i]);
} else {
List<String> f = getFZrr("+"+temp[i]);
for(int j=0;j<f.size();j++){
list.add(f.get(j));
}
}
}
}
return list;
}
/**
* 负系数切分
* @param str 需要切分的字符串
* @return
* @throws Exception
*/
public static List<String> getFZrr(String str) throws Exception{
ArrayList<String> list = new ArrayList<>();
String[] temp = str.split("[-]");
if(temp!=null&&temp.length>0)
for(int i=0;i<temp.length;i++){
if(temp[i].contains("+")){
list.add(temp[i]);
} else{
list.add("-"+temp[i]);
}
}
return list;
}
/**
* 定义链表
* 简单的链表实现
* @author ayumiwind
*
*/
public static class linkList{
//链表头
public node head = null;
/**
* 在链表中添加一个节点
* @param bean
*/
public void add(node node){
if(head == null){
this.head = node;
} else {
node curr = head;
while(curr.next!=null){
curr = curr.next;
}
curr.next = node;
}
}
/**
* 删除一个节点
* @param node
*/
public void delete(node node){
boolean isok = false;
if(head == node){
if(head.next ==null){
head = null;
} else {
node temp = head.next;
head = temp;
}
isok = true;
} else if(head.next != null){
node pre = head;
node curr = head.next;
while(true&&curr!=null){
if(curr == node){
pre.next = curr.next;
}
pre = curr;
curr = curr.next;
}
}
log(isok ? "成功删除一个节点...":"在链表中没有找到该节点...");
}
/**
* 输出该链表的公式
*/
@Override
public String toString(){
String result = "";
node curr = head;
if(head!=null){
while(curr!=null){
if(curr!=null&&curr.bean!=null) {
result+=curr.bean.x>0 ? ("+"+curr.bean.x) : curr.bean.x;
result+=curr.bean.hasN ? ("X^"+curr.bean.n) : "";
}
curr=curr.next;
}
}
return result;
}
}
/**
* 链表节点
* @author ayumiwind
*
*/
public static class node{
//链表节点数据域
public bean bean;
//链表节点指针域
public node next;
public node(bean bean){
this.bean = bean;
this.next = null;
}
}
/**
* 链表节点数据
* @author ayumiwind
*
*/
public static class bean{
//系数
public int x;
//是否包含未知数
public boolean hasN;
//未知数指数,如果有
public int n;
public bean(int x,boolean hasN,int n){
this.x=x;
this.hasN=hasN;
this.n=n;
}
}
public static void log(String log){
if(logopen)
System.out.println(log);
}
}