|
发表于 2013-12-13 16:08:27
|
显示全部楼层
1.#include <iostream>
2.#include<algorithm>
3.using namespace std;
4.
5.class Polynomial;
6.class Term{//多项式的每一项
7. friend Polynomial;
8.public:
9. float coef;//系数
10. int exp;//指数
11.};
12.
13.class Polynomial{//多项式类
14. friend ostream & operator<<(ostream &o,const Polynomial & poly);
15.public:
16. Polynomial();
17. Polynomial(const Polynomial & poly);
18. ~Polynomial();
19. Polynomial operator+(const Polynomial & poly);//多项式加法
20. Polynomial operator*(const Polynomial & poly);//多项式乘法
21. float Eval(float x);//数x代入多项式求值
22. void NewTerm(float coef,int exp);//添加一项,若有相同的指数项,则合并
23.private:
24. void insertTerm(const Term & term);//项的有序插入
25.private:
26. Term *termArray;//非零系数项数组
27. int capacity;//数组大小
28. int terms;//非零系数的项数
29.};
30.
31.Polynomial::Polynomial()
32.{
33. this->terms=0;
34. this->capacity=10;
35. termArray=new Term[this->capacity];
36.}
37.
38.Polynomial::Polynomial(const Polynomial & b)
39.{
40. this->terms=0;
41. this->capacity=b.capacity;
42. termArray = new Term[this->capacity];
43. for(int i=0;i<b.terms;i++){
44. NewTerm(b.termArray[i].coef,b.termArray[i].exp);
45. }
46.}
47.
48.Polynomial::~Polynomial()
49.{
50. delete [] termArray;
51.}
52.
53.Polynomial Polynomial::operator+(const Polynomial & b)
54.{
55. Polynomial c;
56. int aPos=0;
57. int bPos=0;
58. while(aPos<terms && bPos<b.terms){
59. if(termArray[aPos].exp == b.termArray[bPos].exp){
60. float coef=termArray[aPos].coef+b.termArray[bPos].coef;
61. if(coef)c.NewTerm(coef,termArray[aPos].exp);
62. aPos++;bPos++;
63. }else if(termArray[bPos].exp < b.termArray[bPos].exp){
64. c.NewTerm(b.termArray[bPos].coef,b.termArray[bPos].exp);
65. bPos++;
66. }else{
67. c.NewTerm(termArray[aPos].coef,termArray[aPos].exp);
68. aPos++;
69. }
70. }
71. while (aPos < terms){
72. c.NewTerm(termArray[aPos].coef,termArray[aPos].exp);
73. aPos++;
74. }
75. while (bPos < b.terms){
76. c.NewTerm(b.termArray[bPos].coef,b.termArray[bPos].exp);
77. bPos++;
78. }
79. return c;
80.}
81.
82.Polynomial Polynomial::operator*(const Polynomial & b)
83.{
84. Polynomial c;
85. for(int i=0; i<terms; i++){
86. for(int j=0; j<b.terms; j++){
87. float coef = termArray[i].coef*b.termArray[j].coef;
88. int exp = termArray[i].exp + b.termArray[j].exp;
89. c.NewTerm(coef,exp);
90. }
91. }
92. return c;
93.}
94.void Polynomial::NewTerm(float coef, int exp)
95.{
96. if(terms == capacity){
97. capacity *= 2;
98. Term *tmp = new Term[capacity];
99. copy(termArray,termArray+terms,tmp);
100. delete [] termArray;
101. termArray = tmp;
102. }
103. Term ATerm;
104. ATerm.coef=coef;ATerm.exp=exp;
105. insertTerm(ATerm);
106.}
107.void Polynomial::insertTerm(const Term & term)
108.{
109. int i;
110. for(i=0; i<terms && term.exp<termArray[i].exp; i++){
111. }
112. if(term.exp == termArray[i].exp){
113. termArray[i].coef += term.coef;
114. if(!termArray[i].coef){
115. for(int j=i; j<terms-1; j++)
116. termArray[j]= termArray[j+1];
117. terms--;
118. }
119. }else{
120. for(int j=terms-1; j>=i;j--)
121. termArray[j+1]=termArray[j];
122. termArray[i] = term;
123. terms++;
124. }
125.}
126.
127.float Polynomial::Eval(float x)
128.{
129. float res=0.0;
130. for(int i=0;i<terms; i++){
131. res += termArray[i].coef * pow(x,termArray[i].exp);
132. }
133. return res;
134.}
135.
136.ostream & operator<<(ostream & o,const Polynomial & poly)
137.{
138. for(int i=0;i<poly.terms-1;i++){
139. o<<poly.termArray[i].coef<<"x^"<<poly.termArray[i].exp<<" + ";
140. }
141. o<<poly.termArray[poly.terms-1].coef<<"x^"<<poly.termArray[poly.terms-1].exp;
142. return o;
143.}
144.
145.void test()
146.{
147. Polynomial p1;
148. p1.NewTerm(3,2);
149. p1.NewTerm(2.1,3);
150.
151. Polynomial p2;
152. p2.NewTerm(1,2);
153. p2.NewTerm(1,3);
154. p2.NewTerm(5,1);
155.
156. cout<<"("<<p1<<") + ("<<p2<<") = "<<p1+p2<<endl;
157. cout<<"F(x=2) = "<<(p1+p2).Eval(2)<<endl;
158. cout<<"("<<p1<<") * ("<<p2<<") = "<<p1 * p2<<endl;
159.}
160.
161.int main()
162.{
163. test();
164. system("Pause");
165. return 0;
166.}
|
|