-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPolynomial.cpp
118 lines (110 loc) · 2.89 KB
/
Polynomial.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
#include <iostream>
#include <cmemory>
#include "DataStruct.h"
class PolyList_Class
{
public:
PolyList_Class()
{
memset(ppoint, '\0', 10*sizeof(PolyPointer);
point=-1;
}
void add(char polyname, char* exp)
{
point++;
ppoint[point].name=polyname;
ppoint[point].p= new polynomial;
ppoint[point].p->setp(exp);
}
polynomial* sear(char polyname)
{
int i;
if(point>=0)
{
for(i=0; i<=point; i++)
if(ppoint[i].name == polyname)
return ppoint[i].p;
cout<<"ERROR! There is no such a polynomial!"<<endl;
}
else
cout<<"ERROR! There is no polynomials!"<<endl,return -1;
}
private:
struct PolyPointer
{
char name;
polynomial* p;
};
PolyPointer ppoint[10];
int point;
};
#define 1 IN
#define 0 OUT
class polynomial
{
public:
polynomial(int a=20) //By default, suppose the max time is 20
{
poly= new int[a];
memset(poly,'\0', a*sizeof(int));
}
void setp(char* s) /*By NOW, the function only support the case of coefficients&time less than 10 and not less than 0 */
{
int i;
int state=OUT;//point out whether cursor if in a term or not
while(s[i]!='\0')
{
if(s[i]=='+')
{
state=OUT;
continue;
}
if(state==OUT)
{
if('1'<=s[i] && s[i]<='9')
{
state=IN;
if( s[i+1]=='\0' || s[i+1]=='+')
poly[0]+=s[i];
else if( s[i+1]=='x' && (s[i+2]=='\0' || s[i+2]=='+') )
poly[1]+=s[i];
else if( s[i+1]=='x' && ('0'<=s[i+2] && s[i+2]<='9') )
poly[s[i+2]-48]+=s[i];
else
cout<<"INPUT ERROR!"<<endl;
}
else if(s[i]=='x')
{
state=IN;
if(s[i+1]=='^' && '0'<=s[i+2] && s[i+2]<='9')
poly[s[i+2]-48]++;
else if(s[i+1]=='\0' || s[i+1]=='+')
poly[1]++;
else
cout<<"INPUT ERROR"<<endl;
}
else
cout<<"INPUT ERROR"<<endl;
}
i++;
}
}//set the polynomial from INPUT
void print()
{
int i;
if(n>0)
for(i=n;i>0;i--)
cout<<poly[i]<<"x^"<<i<<'+';
cout<<poly[0]<<endl;
}//print the polynamial to OUTPUT
int* show()
{
return poly;
}//diliver the polynomial the another procedure
fraction value(double x0)
{
}//calculate the value of the poly at an exact point
private:
int* poly;
int n;//time
};