大家谁能照着我的改个多项式乘法的出来?
我发现很奇怪这程序在c-free上可以编译
但在vc6.0就有17个错!
我自己编的时候发现有错说是内存不能为witten
这是怎么回师?
谁能告我下面这个*& a,我们老师说没有&,指针就不会动进入死循环,这是为什么?
//void polynomial::add_term(term*& a, term*& b)
大家能照着我写的加法给我改个乘法出来吗?我自己改老错
#include <iostream>
#include<math.h>
#include<string>
#include<vector>
using namespace std;
struct term
{
int _exponent;
double _coefficient;
term* _next;
term(int e, double c, term* n = 0)
: _exponent(e), _coefficient(c), _next(n) { };
int exponent(){return _exponent;};
double coefficient(){return _coefficient;};
term* next(){return _next;};
void set_next(term* ptr_n){_next = ptr_n;};
void print(){cout << _coefficient << "x^" << _exponent << " ";}
};
class polynomial
{
public:
polynomial(): h(0), degree(0) { };//default constructor
polynomial(const polynomial& p);//copy constructor
polynomial(vector<double> coef, vector<int> expon);//constructor
~polynomial();
void print() const;
//***********重这里开始的运算符重载**************************
polynomial& operator=(const polynomial& a); //
friend polynomial operator+(const polynomial& a,const polynomial& b);
friend polynomial operator*(const polynomial& a,
const polynomial& b);
friend bool operator==(const polynomial& a,
const polynomial& b);//
friend bool operator!=(const polynomial& a, //
const polynomial& b);
//**********************结束**********************************************
private:
term* h;
int degree;
void prepend(term* t); //从头指针加节点
void add_term(term*& a, term*& b);
void mp_term(term*& a, term*& b);
void release(); //释放连表
void rest_of(term* rest); //add remaining nodes 其余的项接上去
void reverse(); //reverse nodes
};
polynomial &polynomial::operator=(const polynomial& a)//:degree(a.degree)
{
term* new1 = a.h;
term* temp;
h = 0;
while (new1)
{
temp = new term(new1 -> exponent(),
new1 -> coefficient());
prepend(temp);
new1 = new1 -> next();
}
reverse();
return *this;
}
bool operator==(const polynomial& a,const polynomial& b)
{
term* new1=a.h;
term* new2=b.h;
while(new1&&new2)
{
if(new1->exponent()==new2->exponent()&&new1->coefficient()==new2->coefficient() )
{
new1=new1->next();new2=new2->next();
}
else
return false;
}
return true;
}
bool operator!=(const polynomial& a,const polynomial& b)
{
term* new1=a.h;
term* new2=b.h;
while(new1&&new2)
{ new1=new1->next() ;new2=new2->next();
if(new1->exponent()!=new2->exponent()&&new1->coefficient()!=new2->coefficient() )
{
return true;
}
else
return false;
}
}
polynomial operator+(const polynomial& a,const polynomial& b)
{
term* anode = a.h;
term* bnode = b.h;
polynomial c;
// term* cnode=c.h;
// c.release(); //garbage collect c, assumes not a or b
// c.h = 0;
while (anode && bnode) //merge step
c.add_term(anode, bnode);
cout<<"after merge,c:"<<endl;
if (anode)
{
c.rest_of(anode);
}
else if (bnode)
c.rest_of(bnode);
c.reverse();
c.degree = ((c.h) ? c.h -> exponent(): 0);
//传引用回去相当去传指针,没把内容传回去;在这里编译器会自动调用
//copy构造器
return c;
}
//assumes ordering is correct expon[i] < expon[i+1]
polynomial::polynomial(vector<double> coef, vector<int> expon)
{
term* temp = new term(expon[0], coef[0]);
h = 0;
prepend(temp); //create initial node
for (int i = 1; i < expon.size(); ++i) {
temp = new term(expon[i], coef[i]);
prepend(temp); //add node
}
degree = h -> exponent();
}
//构造函数
polynomial::polynomial(const polynomial& p):degree(p.degree)
{
term* new1 = p.h;
term* temp;
h = 0;
while (new1) {//term by term copying
temp = new term(new1 -> exponent(),
new1 -> coefficient());
prepend(temp);
new1 = new1 -> next();
}
// reverse();
}
void polynomial::release()
{
term* temp_ptr = h;
while(h){
h = h -> next();
delete temp_ptr;
}
}
polynomial::~polynomial()
{
release();
}
void polynomial::reverse() //in place
{
term* pred, *succ, *elem;
if (h && (succ = h -> next())) {
pred = 0;
elem = h;
while (succ) {
elem -> set_next(pred);
pred = elem;
elem = succ;
succ = succ -> next();
}
h = elem;
h -> set_next(pred);
}
}
inline void polynomial::prepend(term* t)
{
t -> set_next(h); //h头指针
h = t;
}
void polynomial::print() const
{
term* temp_ptr = h;
if (h == 0) { cout << "0\n"; return; }
while (temp_ptr){
temp_ptr-> print();
temp_ptr = temp_ptr-> next();
}
cout << endl;
}
void polynomial::add_term(term*& a, term*& b)
{
term* c;
if (a -> exponent() > b -> exponent()) { //add a
c = new term(a -> exponent(), a -> coefficient()) ;
a = a -> next();
prepend(c);
}
else if (a -> exponent() < b -> exponent()){ //add b
c = new term(b -> exponent(), b -> coefficient()) ;
b = b -> next();
prepend(c);
}
else { //check on cancellation
if (a -> coefficient() + b -> coefficient() != 0) {
c = new term( a -> exponent(),
a -> coefficient() + b -> coefficient());
prepend(c);
}
a = a -> next();
b = b -> next();
}
}
void polynomial::rest_of(term* rest)
{
term* temp;
while (rest) {
temp = new term(rest -> exponent(),
rest -> coefficient());
prepend(temp);
rest = rest -> next();
}
}
double vc1[4] = {1, 2, 3, -4};
double vc2[4] = {-3, 2, 1, 7};
int ve1[4] = {0, 4, 14, 45};
int ve2[4] = {1, 4, 18, 46};
vector<double> coef1(vc1,vc1+4);
vector<double> coef2(vc2,vc2+4);
vector<int> expo1(ve1,ve1+4);
vector<int> expo2(ve2,ve2+4);
main()
{
polynomial p(coef1,expo1), q( coef2,expo2),s,t,k;
cout << "P(x) = ";
p.print();
cout << "Q(x) = ";
q.print();
s=p;
cout << "s(x) = ";
s.print();
if(p==s)
{ cout<<"p s相等"<<endl; }
else
{cout<<"p s不相等"<<endl; }
if(q!=p)
{ cout<<"p q不相等"<<endl; }
else
{cout<<"p q相等"<<endl; }
(p+q).print();
}
问题点数:0、回复次数:7Top
1 楼foochow(无聊,灌水......)回复于 2005-06-02 21:47:44 得分 0
//编译通过,别的你自己改~~~太多了,看着晕^_^
#include<iostream>
#include<math.h>
#include<string>
#include<vector>
struct term
{
int _exponent;
double _coefficient;
term* _next;
term(int e, double c, term* n = 0)
: _exponent(e), _coefficient(c), _next(n) { };
int exponent(){return _exponent;};
double coefficient(){return _coefficient;};
term* next(){return _next;};
void set_next(term* ptr_n){_next = ptr_n;};
void print(){std::cout << _coefficient << "x^" << _exponent << " ";}
};
class polynomial
{
public:
polynomial(): h(0), degree(0) { };//default constructor
polynomial(const polynomial& p);//copy constructor
polynomial(std::vector<double>coef, std::vector<int> expon);//constructor
~polynomial();
void print() const;
//***********重这里开始的运算符重载**************************
polynomial& operator=(const polynomial& a); //
friend polynomial operator+(const polynomial& a,const polynomial& b);
friend polynomial operator*(const polynomial& a,
const polynomial& b);
friend bool operator==(const polynomial& a,
const polynomial& b);//
friend bool operator!=(const polynomial& a, //
const polynomial& b);
//**********************结束**********************************************
private:
term* h;
int degree;
void prepend(term* t); //从头指针加节点
void add_term(term*& a, term*& b);
void mp_term(term*& a, term*& b);
void release(); //释放连表
void rest_of(term* rest); //add remaining nodes 其余的项接上去
void reverse(); //reverse nodes
};
polynomial&polynomial::operator=(const polynomial& a)//:degree(a.degree)
{
term* new1 = a.h;
term* temp;
h = 0;
while (new1)
{
temp = new term(new1 -> exponent(),
new1 -> coefficient());
prepend(temp);
new1 = new1 -> next();
}
reverse();
return *this;
}
bool operator==(const polynomial&a,const polynomial&b)
{
term*new1=a.h;
term*new2=b.h;
while(new1&&new2)
{
if(new1->exponent()==new2->exponent()&&new1->coefficient()==new2->coefficient() )
{
new1=new1->next();new2=new2->next();
}
else
return false;
}
return true;
}
bool operator!=(const polynomial& a,const polynomial& b)
{
term* new1=a.h;
term* new2=b.h;
while(new1&&new2)
{ new1=new1->next() ;new2=new2->next();
if(new1->exponent()!=new2->exponent()&&new1->coefficient()!=new2->coefficient() )
{
return true;
}
else
return false;
}
}
polynomial operator+(const polynomial& a,const polynomial& b)
{
term* anode = a.h;
term* bnode = b.h;
polynomial c;
// term* cnode=c.h;
// c.release(); //garbage collect c, assumes not a or b
// c.h = 0;
while (anode && bnode) //merge step
c.add_term(anode, bnode);
std::cout<<"after merge,c:"<<std::endl;
if (anode)
{
c.rest_of(anode);
}
else if (bnode)
c.rest_of(bnode);
c.reverse();
c.degree = ((c.h) ? c.h -> exponent(): 0);
//传引用回去相当去传指针,没把内容传回去;在这里编译器会自动调用
//copy构造器
return c;
}
//assumes ordering is correct expon[i] < expon[i+1]
polynomial::polynomial(std::vector<double> coef, std::vector<int> expon)
{
term* temp = new term(expon[0], coef[0]);
h = 0;
prepend(temp); //create initial node
for (int i = 1; i < expon.size(); ++i) {
temp = new term(expon[i], coef[i]);
prepend(temp); //add node
}
degree = h -> exponent();
}
//构造函数
polynomial::polynomial(const polynomial& p):degree(p.degree)
{
term* new1 = p.h;
term* temp;
h = 0;
while (new1) {//term by term copying
temp = new term(new1 -> exponent(),
new1 -> coefficient());
prepend(temp);
new1 = new1 -> next();
}
// reverse();
}
void polynomial::release()
{
term* temp_ptr = h;
while(h){
h = h -> next();
delete temp_ptr;
}
}
polynomial::~polynomial()
{
release();
}
void polynomial::reverse() //in place
{
term* pred, *succ, *elem;
if (h && (succ = h -> next())) {
pred = 0;
elem = h;
while (succ) {
elem -> set_next(pred);
pred = elem;
elem = succ;
succ = succ -> next();
}
h = elem;
h -> set_next(pred);
}
}
inline void polynomial::prepend(term* t)
{
t -> set_next(h); //h头指针
h = t;
}
void polynomial::print() const
{
term* temp_ptr = h;
if (h == 0) {std::cout<<"0\n"; return; }
while (temp_ptr){
temp_ptr-> print();
temp_ptr = temp_ptr-> next();
}
std::cout<<std::endl;
}
void polynomial::add_term(term*& a, term*& b)
{
term* c;
if (a -> exponent() > b -> exponent()) { //add a
c = new term(a -> exponent(), a -> coefficient()) ;
a = a -> next();
prepend(c);
}
else if (a -> exponent() < b -> exponent()){ //add b
c = new term(b -> exponent(), b -> coefficient()) ;
b = b -> next();
prepend(c);
}
else { //check on cancellation
if (a -> coefficient() + b -> coefficient() != 0) {
c = new term( a -> exponent(),
a -> coefficient() + b -> coefficient());
prepend(c);
}
a = a -> next();
b = b -> next();
}
}
void polynomial::rest_of(term* rest)
{
term* temp;
while (rest) {
temp = new term(rest -> exponent(),
rest -> coefficient());
prepend(temp);
rest = rest -> next();
}
}
double vc1[4] = {1, 2, 3, -4};
double vc2[4] = {-3, 2, 1, 7};
int ve1[4] = {0, 4, 14, 45};
int ve2[4] = {1, 4, 18, 46};
std::vector<double> coef1(vc1,vc1+4);
std::vector<double> coef2(vc2,vc2+4);
std::vector<int> expo1(ve1,ve1+4);
std::vector<int> expo2(ve2,ve2+4);
main()
{
polynomial p(coef1,expo1), q( coef2,expo2),s,t,k;
std::cout<< "P(x) = ";
p.print();
std::cout<< "Q(x) = ";
q.print();
s=p;
std::cout << "s(x) = ";
s.print();
if(p==s)
{ std::cout<<"p s相等"<<std::endl; }
else
{std::cout<<"p s不相等"<<std::endl; }
if(q!=p)
{ std::cout<<"p q不相等"<<std::endl; }
else
{std::cout<<"p q相等"<<std::endl; }
(p+q).print();
}
Top
2 楼mostideal(三甲)回复于 2005-06-03 00:14:03 得分 0
upTop
3 楼mccxj(老鼠不逛街)回复于 2005-06-03 00:18:14 得分 0
发现楼上总是在up。。。汗一个。。Top
4 楼bigdog927(wu)回复于 2005-06-03 13:09:05 得分 0
大家加油啊~~~~~~~~~~~~~~
+都写出来了
*就不行吗?Top
5 楼bigdog927(wu)回复于 2005-06-03 13:11:19 得分 0
编译是通过了,但还是有问题Top
6 楼zloves(俺是菜鸟)回复于 2005-06-03 13:30:34 得分 0
dingTop
7 楼icansaymyabc(学习与进步)回复于 2005-06-03 13:35:54 得分 0
根据多年的经验,读程序比写程序困难,改别人的程序比自己写一个程序出来麻烦得多。所以没心思帮你改哦Top




