- 论坛徽章:
- 0
|
我写的几句代码,在linux下可以运行,但在vc6.0下会出现有内存不可读的情况,请大家看看是什么情况
代码很简单,没有什么算法,是就用链表类实现的多项式的 加、减、乘法运算而已,请大家看看问题所在
新手,代码里不合理的地方请大家指出 
- //polynode.h
- #ifndef __POLY_NODE__H__
- #define __POLY_NODE__H__
- class Poly_node {
- public:
- Poly_node(double xishu = 0.0, int zhishu = 0);
- Poly_node(Poly_node const &);
- double getxishu() {return xishu;}
- int getzhishu() {return zhishu;}
- void setxishu(double);
- Poly_node & operator =(const Poly_node &);
- bool operator <(const Poly_node &) const;
- bool operator >(const Poly_node &) const;
- bool operator ==(const Poly_node &) const;
- private:
- double xishu;
- int zhishu;
- };
- #endif
- //polynode.cpp
- #include "polynode.h"
- Poly_node::Poly_node(double xishu, int zhishu)
- {
- this->xishu = xishu;
- this->zhishu = zhishu;
- }
- Poly_node::Poly_node(Poly_node const & b)
- {
- Poly_node(b.xishu, b.zhishu);
- }
- void Poly_node::setxishu(double e)
- {
- this->xishu = e;
- }
- Poly_node & Poly_node::operator =(const Poly_node &b)
- {
- if (this == &b)
- return *this;
- this->xishu = b.xishu;
- this->zhishu = b.zhishu;
- return *this;
- }
- bool Poly_node::operator <(const Poly_node & b) const
- {
- return (this->zhishu < b.zhishu);
- }
- bool Poly_node::operator >(const Poly_node & b) const
- {
- return (this->zhishu > b.zhishu);
- }
- bool Poly_node::operator ==(const Poly_node & b) const
- {
- return (this->zhishu == b.zhishu);
- }
- //poly.h
- #include <iostream>
- #include "polynode.h"
- using namespace std;
- struct Node {
- Poly_node data;
- Node *next;
- };
- #ifndef __POLY__H__
- #define __POLY__H__
- class Poly {
- public:
- Poly();
- ~Poly();
- bool insert(Poly_node &);
- bool insert(double xishu, int zhishu);
- Poly & operator =(const Poly &);
- Poly operator +(const Poly &) const;
- Poly operator -(const Poly &) const;
- Poly operator *(const Poly &) const;
- friend ostream & operator <<(ostream &, const Poly &);
- private:
- Node *L;
- bool clear();
- };
- #endif
- //poly.cpp
- #include "poly.h"
- #include <iostream>
- #include <math.h>
- #include <stdlib.h>
- using namespace std;
- Poly::Poly()
- {
- L = new Node;
- L->next = NULL;
- }
- Poly::~Poly()
- {
- clear();
- delete L;
- L = NULL;
- }
- bool Poly::insert(Poly_node &ob)
- {
- Node *temp = new Node;
- if (!temp) {
- cerr << "call memory filed.\n";
- exit(-1);
- }
- temp->data = ob;
- temp->next = NULL;
- Node *p = L;
- if (!(p->next)) {
- p->next = temp;
- return true;
- }
- Node *q = L->next;
- int flag = 0;
- while (q) {
- if (q->data == temp->data) {
- q->data.setxishu(q->data.getxishu() + temp->data.getxishu());
- flag = 1;
- break;
- }
- if (q->data < temp->data) {
- temp->next = p->next;
- p->next = temp;
- flag = 1;
- break;
- }
- p = q;
- q = q->next;
- }
- if (flag)
- return true;
- else {
- p->next = temp;
- return true;
- }
- }
-
- bool Poly::insert(double xishu, int zhishu)
- {
- if (!xishu)
- return false;
- Poly_node ob(xishu, zhishu);
- if (this->insert(ob))
- return true;
- else
- return false;
- }
- Poly & Poly::operator =(const Poly &ob)
- {
- if (this == &ob)
- return *this;
- this->clear();
- Node *p = ob.L->next;;
- while (p) {
- this->insert(p->data);
- p = p->next;
- }
- return *this;
- }
- Poly Poly::operator +(const Poly &ob) const
- {
- Poly temp;
- Node *p = this->L->next;
- Node *q = ob.L->next;
- while (p && q) {
- if (p->data == q->data) {
- temp.insert(p->data.getxishu() + q->data.getxishu(), q->data.getzhishu());
- p = p->next;
- q = q->next;
- } else if (p->data < q->data) {
- temp.insert(q->data);
- q = q->next;
- } else {
- temp.insert(p->data);
- p = p->next;
- }
- }
- Node *plast = NULL;
- if (p)
- plast = p;
- else
- plast = q;
- while (plast) {
- temp.insert(plast->data);
- plast = plast->next;
- }
- return temp;
- }
- Poly Poly::operator -(const Poly &ob) const
- {
- Poly temp;
- Node *p = this->L->next;
- Node *q = ob.L->next;
- while (p && q) {
- if (p->data == q->data) {
- temp.insert(p->data.getxishu() - q->data.getxishu(), q->data.getzhishu());
- p = p->next;
- q = q->next;
- } else if (p->data < q->data) {
- temp.insert(-q->data.getxishu(), q->data.getzhishu());
- q = q->next;
- } else {
- temp.insert(p->data);
- p = p->next;
- }
- }
- while (p) {
- temp.insert(p->data);
- p = p->next;
- }
- while (q) {
- temp.insert(-q->data.getxishu(), q->data.getzhishu());
- q = q->next;
- }
- return temp;
- }
- Poly Poly::operator *(const Poly &ob) const
- {
- Poly temp;
- Node *p = this->L->next;
- while (p) {
- Node *q = ob.L->next;
- while (q) {
- temp.insert(p->data.getxishu() * q->data.getxishu(), p->data.getzhishu() + q->data.getzhishu());
- q = q->next;
- }
- p = p->next;
- }
- return temp;
- }
- //nnd this point is very important
- bool Poly::clear()
- {
- Node *p = NULL;
- while (L->next) {
- p = L->next;
- L->next = p->next;
- delete p;
- p = NULL;
- }
- return true;
- }
- ostream & operator <<(ostream &out, const Poly &ob)
- {
- if (ob.L->next == NULL) {
- out << "NULL";
- return out;
- }
- Node *p = ob.L->next;
- double xishu = p->data.getxishu();
- int zhishu = p->data.getzhishu();
- if (!zhishu)
- out << xishu;
- else {
- if (xishu == -1 && zhishu != 1)
- out << "-x^" << zhishu;
- else if (xishu == -1 && zhishu == 1)
- out << "-x";
- else if (xishu == 1 && zhishu != 1)
- out << "x^" << zhishu;
- else if (xishu != 1 && zhishu == 1)
- out << xishu << "x";
- else if (xishu ==1 && zhishu == 1)
- out << "x";
- else
- out << xishu << "x^" << zhishu;
- }
- p = p->next;
- while (p) {
- double m_xishu = p->data.getxishu();
- int m_zhishu = p->data.getzhishu();
- if (m_xishu > 0)
- out << "+";
- if (!m_zhishu)
- out << m_xishu;
- else {
- if (m_xishu == -1 && m_zhishu != 1)
- out << "-x^" << m_zhishu;
- else if (m_xishu == -1 && m_zhishu == 1)
- out << "-x";
- else if (m_xishu == 1 && m_zhishu != 1)
- out << "x^" << m_zhishu;
- else if (m_xishu != 1 && m_zhishu == 1)
- out << m_xishu << "x";
- else if (m_xishu == 1 && m_zhishu == 1)
- out << "x";
- else
- out << m_xishu << "x^" << m_zhishu;
- }
- p = p->next;
- }
- return out;
- }
- //polytest.cc
- #include <iostream>
- #include "poly.h"
- using namespace std;
- void printmenu();
- void input(Poly &ob);
- int main()
- {
- int choi;
- Poly ob1, ob2, ob3;
- do {
- printmenu();
- cin >> choi;
- switch (choi) {
- case 1:
- cout << "please input your first poly\n";
- input(ob1);
- cout << "plase input your second poly\n";
- input(ob2);
- break;
- case 2:
- cout << "f(x)=" << ob1 << endl << "g(x)=" << ob2 << endl;
- break;
- case 3:
- cout << "f(x)=" << ob1 << endl << "g(x)=" << ob2 << endl;
- ob3 = ob1 + ob2;
- cout << "f(x)+g(x)=" << ob3 << endl;
- break;
- case 4:
- cout << "f(x)=" << ob1 << endl << "g(x)=" << ob2 << endl;
- ob3 = ob1 - ob2;
- cout << "f(x)-g(x)=" << ob3 << endl;
- break;
- case 5:
- cout << "f(x)=" << ob1 << endl << "g(x)=" << ob2 << endl;
- ob3 = ob1 * ob2;
- cout << "f(x)*g(x)=" << ob3 << endl;
- break;
- default:
- break;
- }
- } while (choi != 6);
- }
- void printmenu()
- {
- cout << "---------------\n"
- <<"1-->enter\n"
- <<"2-->print\n"
- << "3-->add\n"
- << "4-->sub\n"
- << "5-->mul\n"
- << "6-->exit\n"
- << "---------------\n"
- << "please enter your choice:";
- }
- void input(Poly &ob)
- {
- cout << "enter xishu and zhishu, 00 for end:" << endl;
- while (1) {
- double xishu;
- int zhishu;
- cin >> xishu >> zhishu;
- if (!xishu && !zhishu)
- break;
- ob.insert(xishu, zhishu);
- }
- }
复制代码 谢谢大家了  |
|