-
Notifications
You must be signed in to change notification settings - Fork 1
/
Poly.cpp
150 lines (130 loc) · 2.85 KB
/
Poly.cpp
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
typedef struct poly{
int coe, ind;
struct poly *next;
}Poly;
void PolyStart(){
Poly *p,*q;
p = (Poly*)malloc(sizeof(Poly));
p->next=NULL;
cout << "一元多项式运算" << endl;
Slove(p);
Sort(p);
Dis(p);
getch();
}
void Slove(Poly *p){
Poly *q;
string in;
stringstream ss;
int n,coe,ind;
char c='#';
regex pattern("(([+-]|^)\\d+[Xx]\\^\\d+)");
regex pa("([+-]|^)(\\d+)([+-]|$)");
string rep="$1$2x^0$3";
//regex patt("([+-]|^)(\\d+)([Xx])([+-]|$)");
//string replace="$1$2$3^1$4";
regex patt("([Xx])([+-]|$)");
string replace="$1^1$2";
regex patter("([+-]|^)([Xx])");
string re = "$1$101$2$3";
for (n = 0; n < 2; n++){
cout << "请输入多项式:";
cin >> in;
in = regex_replace(in, patter, re);
//格式化字符串:x
in=regex_replace(in, patt, replace);
in = regex_replace(in, pa, rep);
//cout << in << endl;
for (std::sregex_iterator it(in.cbegin(), in.cend(), pattern), q; it != q; ++it){
//cout << "str: " << it->format("$1") << "\n";
//cout << it->format("$1")[0];
ss<<it->format("$1");
if(it->format("$1")[0]=='+'||it->format("$1")[0] == '-') ss>>c;
ss>>coe;
if(c=='-') coe=-coe;
ss>>c;ss>>c;
ss>>ind;
//cout<<coe<<endl<<ind<<endl;
ss.clear();
Process(p,coe,ind);
}
//if(!IsExt()) p = (Poly*)malloc(sizeof(Poly));
}
}
bool IsExt(int n,Poly *p){
Poly *q;
q=p;
while(q!=NULL){
if(n==q->ind) return true;
q=q->next;
}
return false;
}
void Process(Poly *p,int coe,int ind){
Poly *q;
//不存在该指数
if(!IsExt(ind,p)) {
q = (Poly*)malloc(sizeof(Poly));
q->coe=coe;
q->ind=ind;
q->next=NULL;
while(p->next!=NULL) p=p->next;
p->next=q;
}
else{
q=p->next;
while(ind!=q->ind) q=q->next;
q->coe +=coe;
}
}
void Dis(Poly *p){
Poly *q=p;
int num=0;
cout<<"多项式结果为:";
for(q=q->next;q!=NULL;q=q->next){
if(q->coe!=0){
num++;
if(num>1){
if(q->coe<0) cout<<"-";
else cout<<"+";
}
if(abs(q->coe)!=1) cout<<abs(q->coe);
else if(q->ind==0) cout<<abs(q->coe);
if (q->ind == 1)
cout<<"x";
else if(q->ind!=0)
cout << "x^" << q->ind;
//if (q->ind == 1)
// cout << abs(q->coe) << "x";
//else if (q->ind == 0)
// cout << abs(q->coe);
//else
// cout << abs(q->coe) << "x^" << q->ind;
}
}
if(num==0) cout<<0<<endl;
}
void Sort(Poly *p){
Poly *pre,*max,*temp,*j,*inpre,*maxpre,*q;
int flag;
pre = p;
for (q = p->next; q != NULL; q = pre->next->next,pre=pre->next){
flag=0;
max = q;
inpre = pre;
for (j = q; j != NULL; j = j->next,inpre=inpre->next){
if (j->ind > max->ind){
flag=1;
max = j;
maxpre = inpre;
}
}
if(flag==1){
pre->next = max;
maxpre->next = q;
temp = max->next;
max->next = q->next;
q->next = temp;
}
}
}