|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
#include <cstdlib>
#include <iostream>
//我用的compiler是微软的visual studio 2013 快爆炸了,帮忙一下吧
using namespace std;
#include "polynomial.h" // 用这个头文件,主要就是要写这个,裡面用类实现多项式的表示,相加和相乘
int main(int argc, char *argv[])
{
Polynomial<double> poly1, poly2;
// polynomial #1
poly1.newItem(Term<double>(5, 15));
poly1.newItem(Term<double>(3, -6));
poly1.newItem(Term<double>(2, -14));
poly1.newItem(Term<double>(0, 13.2));
cout << "Polynomial #1 = ";
poly1.Print();
// polynomial #2
poly2.newItem(Term<double>(6, 10));
poly2.newItem(Term<double>(3, 6));
poly2.newItem(Term<double>(2, 4));
poly2.newItem(Term<double>(1, -2));
cout << "Polynomial #2 = ";
poly2.Print();
// addition
poly1.Add(poly2);
cout << "addition : ";
poly1.Print();
// multiplication
poly1.Add(poly2);
cout << "multiplication : ";
poly1.Print();
system("PAUSE");
return 0;
}
以下是標頭檔的內容,我的想法是多相式是由"项"构成,是"项"所组成的一个表(a list of terms) 于是乎我用二个类(template型式) term 和 list 再定义多项式模块(template polynomial)
template <class T>
class List
{
private:
int capacity;//size
int nItems; // count of items in the list
T *data;//pointer points to the 1st elements of List
public:
List(int initCapacity) // constructor
{
capacity = initCapacity;
data = new T[capacity];
nItems = 0;
}
~List(){ delete []List;
data = NULL; } // destructor
void newItem(T value) // add a new item at the end
{
nItem++;
if (nItem == capacity){//If exceeding the capacity, allocate more needed memo
T *newlist = new T[2*capacity];
for (int i = 0; i <= nitem; i++){
*(newlist + i*sizeof(T)) = *(data + i*sizeof(T));
*(newlist + nitem*sizeof(T)) = value;
}
};
else
*(newlist + nitem*sizeof(T)) = value;
}
T getItem(int index) // retrieve an item
{
T item;
item = *(data+index*sizeof(T));
return item;
}
};
template <class T> Polynomial:public List< Term<T> >
class Polynomial{
public:
void Add(const Polynomial<T>& poly){
int* old = this->data;
int* oldinput = poly.data;
while ((this->data != old + sizeof(T)*this->nItems) && (poly.data != oldinput + sizeof(T)*poly.nItems)){
if ((this->data).exp > (poly.data).exp)
this->data += sizeof(T);
else if (this->data.exp < (poly.data.exp){
for (int j = 0;j<)
poly.data += sizeof(T);
}
else
this->
}
};
void Multi(const Polynomial<T>& poly){
//这裡还没写出来,任何建议和想法都欢迎,在下于此先行谢过各位大侠
};
void Print(const Polynomial<T>& poly){
//这裡还没写出来,任何建议和想法都欢迎,在下于此先行谢过各位大侠
};
};
template <class T>
class Term {
public:
friend polynomial<T>;
Term(int exp,T coef){
this->exp = exp;
this->coef = coef;
};
private:
T coef;
int exp;
};
|
|