forked from longkeaimeng/Lab1
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelloworld.java
More file actions
363 lines (358 loc) · 8.43 KB
/
helloworld.java
File metadata and controls
363 lines (358 loc) · 8.43 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
356
357
358
359
360
361
362
363
import java.io.IOException;
import java.util.Scanner;
//first change
class node{
public int quotiety = 1;//系数,正整数
public char str[] = new char[26];//只能以单个字母作为变量
public int exponential[] = new int[26];//次数,只支持10以下的正整数
public int len = 0;
public node next;
}
class Multinomial{
public void quick_sort(node TempNode,int l, int r){//排序函数,对字符数组排序,方便合并同类项
if (l < r)
{
int i = l, j = r, x = TempNode.exponential[l];char y = TempNode.str[l];
while (i < j)
{
while(i < j && TempNode.str[j] >= y)
j--;
if(i < j){
TempNode.exponential[i] = TempNode.exponential[j];
TempNode.str[i] = TempNode.str[j];
i++;
}
while(i < j && TempNode.str[i] < y)
i++;
if(i < j){
TempNode.exponential[j] = TempNode.exponential[i];
TempNode.str[j] = TempNode.str[i];
j--;
}
}
TempNode.exponential[i] = x;
TempNode.str[i] = y;
quick_sort(TempNode,l, i - 1); // 递归调用
quick_sort(TempNode,i + 1, r);
}
}
public int expression(char ch) throws IOException{//读入函数并建立链表储存数据
node temp = head;char pre_ch = '!';int i,num=3;int index = 0;
//pre_ch表示上一个变量名
//num = 0 表示上个是变量或结点开头;num = 1 表示上个是'^';num = 2表示上个是数字;num = 3表示上一个是运算符
if(ch=='\n')
return 0;
do{
if(ch == '*'){
num = 3;
}
else if(ch == '+'){
temp.len = index;
quick_sort(temp,0,temp.len-1);
index = 0;
temp.next = new node();
temp = temp.next;
num = 3;
}
else if(ch == '-'){
if(pre_ch=='!'){
temp.quotiety = -1;num = 3;
}
else{
temp.len = index;
quick_sort(temp,0,temp.len-1);
index = 0;
temp.next = new node();
temp = temp.next;
temp.quotiety = -1;
num = 3;
}
}
else if(ch == '^'){
if(!((65 <= pre_ch && pre_ch <= 90)||(97 <= pre_ch && pre_ch <= 122))){
System.out.println("输入不合法");
return -1;
}
num = 1;
}
else if(48 <= ch && ch <= 57){//字母在前数字在后时,不能省略'*'
if(num == 3){
temp.quotiety = temp.quotiety*(ch-48);
}
else if(num == 1){
for(i=0;i<index;i++){
if(pre_ch != temp.str[i])
continue;
else{
temp.exponential[i]+=(ch-48-1);
break;
}
}
i=0;
num = 1;
}
else if(num == 2){
if(temp.quotiety<0)
temp.quotiety = (temp.quotiety)*10-(ch-48);
else
temp.quotiety = (temp.quotiety)*10+(ch-48);
}
num = 2;
}
else if((65 <= ch && ch <= 90)||(97<=ch && ch <= 122)){
for(i=0;i<index;i++){
if(ch != temp.str[i])
continue;
else{
temp.exponential[i]++;
break;
}
}
if(i == index){
temp.str[index] = ch;
temp.exponential[index] = 1;
index++;
i = 0;
}
num = 0;
pre_ch = ch;
}
else{
System.out.print("输入不合法");
}
ch = (char)System.in.read();
if(ch=='\r'){
temp.len = index;
quick_sort(temp,0,temp.len-1);
return 0;
}
}while(ch!='\r');
return 0;
}
public void combining(node Node){//合并同类项
node NowNode = Node;
node TempNode;
node Temp_preNode;
if(Node.next == null){
return;
}
int i,counter;
do{
TempNode = NowNode.next;Temp_preNode = NowNode;
while(TempNode != null){
String s = String.valueOf(NowNode.str);String t = String.valueOf(TempNode.str);
if(s.equals(t)){
counter = 0;
for(i=0;i<NowNode.len;i++){
if(NowNode.exponential[i]==TempNode.exponential[i]){
counter++;
}
else
break;
}
if(counter == NowNode.len){
NowNode.quotiety += TempNode.quotiety;
Temp_preNode.next = TempNode.next;
TempNode = Temp_preNode.next;
continue;
}
Temp_preNode = Temp_preNode.next;
TempNode = TempNode.next;
}
else{
Temp_preNode = Temp_preNode.next;
TempNode = TempNode.next;
}
}
NowNode = NowNode.next;
}while(NowNode != null);
do{
if(Node.next==null)
break;
if(Node.len != 0){
Node = Node.next;
}
else{
node temp = Node;
while(Node.next != null){
if(Node.next.len == 0){
temp.quotiety += Node.next.quotiety;
Node.next = Node.next.next;
}
else{
Node = Node.next;
}
}
}
}while(Node.next !=null);
}
public void simplify(char ch,int num){//赋值
node TempNode = forother;
do{
for(int i=0;i<TempNode.len;i++){
if(TempNode.str[i]==ch){
TempNode.quotiety*=(num<<(TempNode.exponential[i]-1));
TempNode.exponential[i] = 0;
break;
}
}
int count = 0;
for(int i=0;i<TempNode.len;i++){
count+=TempNode.exponential[i];
}
if(count == 0){
TempNode.len = 0;
}
TempNode = TempNode.next;
}while(TempNode!=null);
}
public void derivative(char x){//求导
node t = forother;
node h;
h=t;
int i=0;
while(t!=null && i<=t.len){//求导函数
if(t.str[i]==x ){
t.quotiety*=t.exponential[i];
t.exponential[i]-=1;
int count = 0;
for(int k=0;k<t.len;k++){
count+=t.exponential[k];
}
if(count == 0){
t.len = 0;
}
t=t.next;
i=0;
continue;
}
i++;
if (i>=t.len ){//找不到x
t.quotiety=0;
t=t.next;
i=0;
}
}
t=h;
}
public void Print(node t){
String res="";
while(t!=null){
if(t.quotiety!=0){//系数不为0,才有东西输出
int s = 0;
for(int i=0;i<t.len;i++){
s+=t.exponential[i];
}
if(t.len==0||s==0){//字符串长度为0或者所有字符的次数都为0,此时
res+=String.valueOf(t.quotiety);
}
else{
if(t.quotiety!=1){
if(t.quotiety == -1)
res+="-";
else
res+=String.valueOf(t.quotiety);
}
for(int temp=0;temp<t.str.length;temp++)
{
if(t.exponential[temp]!=0){//判未知数输出条件
res+=String.valueOf(t.str[temp]);
if(t.exponential[temp]!= 1){//判次数输出条件
res+="^";
res+=String.valueOf(t.exponential[temp]);
}
}
}
}
}
if(t.next!=null && t.next.quotiety>0 && !res.equals("")){
res+="+";
}
t=t.next;
}
if(res.length()!=0)
System.out.println(res);
else
System.out.println(0);
}
public void mulcopy(){//创建一个镜像链表
node temphead=head;
node tempfor=forother;
do{
tempfor.quotiety=temphead.quotiety;
tempfor.len=temphead.len;
for(int i=0;i<temphead.len;i++){
tempfor.exponential[i]=temphead.exponential[i];
tempfor.str[i]=temphead.str[i];
}
if(temphead.next!=null){
temphead=temphead.next;
tempfor.next = new node();
tempfor=tempfor.next;
}
else
break;
}while (true);
}
public node head = new node();//储存链表的头指针
public node forother = new node();//镜像链表的头指针
}
public class helloworld {
public static void main(String[] args) throws IOException{
char ch;Multinomial multinomial1 = null;
Scanner s=new Scanner(System.in);
System.out.println("input something");
while(true){
ch = (char)System.in.read();
if(ch == '!'){
if(multinomial1!=null){
String cmd=s.nextLine();
char nd='y';//随便初始化一个
if(cmd.length()>2 && cmd.substring(0,3).equals("end")){
break;
}
else if (cmd.length()>2 && cmd.substring(0,3).equals("d/d")){
nd=cmd.charAt(3);
multinomial1.derivative(nd);
multinomial1.combining(multinomial1.forother);
multinomial1.Print(multinomial1.forother);
}
else if (cmd.length()>8 && cmd.substring(0, 9).equals("simplify ")){
String now= cmd.substring(9);
String[] cmdlist=now.split(" ");
String[] cmdsplit;
for(int j=0;j<cmdlist.length;j++){
cmdsplit=cmdlist[j].split("=");
char chnow;
int numnow;
chnow=cmdsplit[0].charAt(0);
numnow=Integer.valueOf(cmdsplit[1]);
multinomial1.simplify(chnow,numnow);
}
multinomial1.combining(multinomial1.forother);
multinomial1.Print(multinomial1.forother);
}
else{
System.out.println("输入的命令不合法,请重新输入");
}
}
else{
System.out.println("多项式为空,请先输入多项式");
s.nextLine();
}
}
else{
multinomial1 = new Multinomial();
int num = multinomial1.expression(ch);
if(num < 0)
break;
multinomial1.combining(multinomial1.head);
multinomial1.Print(multinomial1.head);
System.in.read();
}
if(multinomial1!=null)
multinomial1.mulcopy();
}
s.close();
}
}