免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1618 | 回复: 4
打印 上一主题 下一主题

求救::如题,看代码 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2010-06-06 21:44 |只看该作者 |倒序浏览
我写的几句代码,在linux下可以运行,但在vc6.0下会出现有内存不可读的情况,请大家看看是什么情况

代码很简单,没有什么算法,是就用链表类实现的多项式的  加、减、乘法运算而已,请大家看看问题所在

新手,代码里不合理的地方请大家指出

  1. //polynode.h

  2. #ifndef __POLY_NODE__H__
  3. #define __POLY_NODE__H__
  4. class Poly_node {
  5.         public:
  6.                 Poly_node(double xishu = 0.0, int zhishu = 0);
  7.                 Poly_node(Poly_node const &);
  8.                 double getxishu() {return xishu;}
  9.                 int getzhishu() {return zhishu;}
  10.                 void setxishu(double);
  11.                 Poly_node & operator =(const Poly_node &);
  12.                 bool operator <(const Poly_node &) const;
  13.                 bool operator >(const Poly_node &) const;
  14.                 bool operator ==(const Poly_node &) const;
  15.         private:
  16.                 double xishu;
  17.                 int zhishu;
  18. };
  19. #endif

  20. //polynode.cpp

  21. #include "polynode.h"

  22. Poly_node::Poly_node(double xishu, int zhishu)
  23. {
  24.         this->xishu = xishu;
  25.         this->zhishu = zhishu;
  26. }

  27. Poly_node::Poly_node(Poly_node const & b)
  28. {
  29.         Poly_node(b.xishu, b.zhishu);
  30. }

  31. void Poly_node::setxishu(double e)
  32. {
  33.         this->xishu = e;
  34. }

  35. Poly_node & Poly_node::operator =(const Poly_node &b)
  36. {
  37.         if (this == &b)
  38.                 return *this;
  39.         this->xishu = b.xishu;
  40.         this->zhishu = b.zhishu;
  41.         return *this;
  42. }
  43. bool Poly_node::operator <(const Poly_node & b) const
  44. {
  45.         return (this->zhishu < b.zhishu);
  46. }

  47. bool Poly_node::operator >(const Poly_node & b) const
  48. {
  49.         return (this->zhishu > b.zhishu);
  50. }

  51. bool Poly_node::operator ==(const Poly_node & b) const
  52. {
  53.         return (this->zhishu == b.zhishu);
  54. }
  55. //poly.h

  56. #include <iostream>
  57. #include "polynode.h"
  58. using namespace std;

  59. struct Node {
  60.         Poly_node data;
  61.         Node *next;
  62. };

  63. #ifndef __POLY__H__
  64. #define __POLY__H__

  65. class Poly {
  66.         public:
  67.                 Poly();
  68.                 ~Poly();
  69.                 bool insert(Poly_node &);
  70.                 bool insert(double xishu, int zhishu);
  71.                 Poly & operator =(const Poly &);
  72.                 Poly operator +(const Poly &) const;
  73.                 Poly operator -(const Poly &) const;
  74.                 Poly operator *(const Poly &) const;
  75.                 friend ostream & operator <<(ostream &, const Poly &);
  76.         private:
  77.                 Node *L;
  78.                 bool clear();
  79. };
  80. #endif
  81. //poly.cpp
  82. #include "poly.h"
  83. #include <iostream>
  84. #include <math.h>
  85. #include <stdlib.h>
  86. using namespace std;

  87. Poly::Poly()
  88. {
  89.         L = new Node;
  90.         L->next = NULL;
  91. }

  92. Poly::~Poly()
  93. {
  94.         clear();
  95.         delete L;
  96.         L = NULL;
  97. }

  98. bool Poly::insert(Poly_node &ob)
  99. {
  100.         Node *temp = new Node;
  101.         if (!temp) {
  102.                 cerr << "call memory filed.\n";
  103.                 exit(-1);
  104.         }
  105.         temp->data = ob;
  106.         temp->next = NULL;
  107.         Node *p = L;
  108.         if (!(p->next)) {
  109.                 p->next = temp;
  110.                 return true;
  111.         }

  112.         Node *q = L->next;
  113.         int flag = 0;
  114.         while (q) {
  115.                 if (q->data == temp->data) {
  116.                         q->data.setxishu(q->data.getxishu() + temp->data.getxishu());
  117.                         flag = 1;
  118.                         break;
  119.                 }
  120.                 if (q->data < temp->data) {
  121.                         temp->next = p->next;
  122.                         p->next = temp;
  123.                         flag = 1;
  124.                         break;
  125.                 }
  126.                 p = q;
  127.                 q = q->next;
  128.         }
  129.         if (flag)
  130.                 return true;
  131.         else {
  132.                 p->next = temp;
  133.                 return true;
  134.         }
  135. }
  136.        
  137. bool Poly::insert(double xishu, int zhishu)
  138. {
  139.         if (!xishu)
  140.                 return false;
  141.         Poly_node ob(xishu, zhishu);
  142.         if (this->insert(ob))
  143.                 return true;
  144.         else
  145.                 return false;
  146. }

  147. Poly & Poly::operator =(const Poly &ob)
  148. {
  149.         if (this == &ob)
  150.                 return *this;
  151.         this->clear();
  152.         Node *p = ob.L->next;;
  153.         while (p) {
  154.                 this->insert(p->data);
  155.                 p = p->next;
  156.         }
  157.         return *this;
  158. }

  159. Poly Poly::operator +(const Poly &ob) const
  160. {
  161.         Poly temp;
  162.         Node *p = this->L->next;
  163.         Node *q = ob.L->next;
  164.         while (p && q) {
  165.                 if (p->data == q->data) {
  166.                         temp.insert(p->data.getxishu() + q->data.getxishu(), q->data.getzhishu());
  167.                         p = p->next;
  168.                         q = q->next;
  169.                 } else if (p->data < q->data) {
  170.                         temp.insert(q->data);
  171.                         q = q->next;
  172.                 } else {
  173.                         temp.insert(p->data);
  174.                         p = p->next;
  175.                 }
  176.         }
  177.         Node *plast = NULL;
  178.         if (p)
  179.                 plast = p;
  180.         else
  181.                 plast = q;
  182.         while (plast) {
  183.                 temp.insert(plast->data);
  184.                 plast = plast->next;
  185.         }

  186.         return temp;
  187. }

  188. Poly Poly::operator -(const Poly &ob) const
  189. {
  190.         Poly temp;
  191.         Node *p = this->L->next;
  192.         Node *q = ob.L->next;
  193.         while (p && q) {
  194.                 if (p->data == q->data) {
  195.                         temp.insert(p->data.getxishu() - q->data.getxishu(), q->data.getzhishu());
  196.                         p = p->next;
  197.                         q = q->next;
  198.                 } else if (p->data < q->data) {
  199.                         temp.insert(-q->data.getxishu(), q->data.getzhishu());
  200.                         q = q->next;
  201.                 } else {
  202.                         temp.insert(p->data);
  203.                         p = p->next;
  204.                 }
  205.         }
  206.         while (p) {
  207.                 temp.insert(p->data);
  208.                 p = p->next;
  209.         }
  210.         while (q) {
  211.                 temp.insert(-q->data.getxishu(), q->data.getzhishu());
  212.                 q = q->next;
  213.         }
  214.         return temp;
  215. }

  216. Poly Poly::operator *(const Poly &ob) const
  217. {
  218.         Poly temp;
  219.         Node *p = this->L->next;
  220.         while (p) {
  221.                 Node *q = ob.L->next;
  222.                 while (q) {
  223.                         temp.insert(p->data.getxishu() * q->data.getxishu(), p->data.getzhishu() + q->data.getzhishu());
  224.                         q = q->next;
  225.                 }
  226.                 p = p->next;
  227.         }
  228.         return temp;
  229. }

  230. //nnd this point is very important
  231. bool Poly::clear()
  232. {
  233.         Node *p = NULL;
  234.         while (L->next) {
  235.                 p = L->next;
  236.                 L->next = p->next;
  237.                 delete p;
  238.                 p = NULL;
  239.         }
  240.         return true;
  241. }

  242. ostream & operator <<(ostream &out, const Poly &ob)
  243. {
  244.         if (ob.L->next == NULL) {
  245.                 out << "NULL";
  246.                 return out;
  247.         }
  248.         Node *p = ob.L->next;
  249.         double xishu = p->data.getxishu();
  250.         int zhishu = p->data.getzhishu();

  251.         if (!zhishu)
  252.                 out << xishu;
  253.         else {
  254.                 if (xishu == -1 && zhishu != 1)
  255.                         out << "-x^" << zhishu;
  256.                 else if (xishu == -1 && zhishu == 1)
  257.                         out << "-x";
  258.                 else if (xishu == 1 && zhishu != 1)
  259.                         out << "x^" << zhishu;
  260.                 else if (xishu != 1 && zhishu == 1)
  261.                         out << xishu << "x";
  262.                 else if (xishu ==1 && zhishu == 1)
  263.                         out << "x";
  264.                 else
  265.                         out << xishu << "x^" << zhishu;
  266.         }
  267.         p = p->next;
  268.         while (p) {
  269.                 double m_xishu = p->data.getxishu();
  270.                 int m_zhishu = p->data.getzhishu();

  271.                 if (m_xishu > 0)
  272.                         out << "+";
  273.                 if (!m_zhishu)
  274.                         out << m_xishu;
  275.                 else {
  276.                         if (m_xishu == -1 && m_zhishu != 1)
  277.                                 out << "-x^" << m_zhishu;
  278.                         else if (m_xishu == -1 && m_zhishu == 1)
  279.                                 out << "-x";
  280.                         else if (m_xishu == 1 && m_zhishu != 1)
  281.                                 out << "x^" << m_zhishu;
  282.                         else if (m_xishu != 1 && m_zhishu == 1)
  283.                                 out << m_xishu << "x";
  284.                         else if (m_xishu == 1 && m_zhishu == 1)
  285.                                 out << "x";
  286.                         else
  287.                                 out << m_xishu << "x^" << m_zhishu;
  288.                 }
  289.                 p = p->next;
  290.         }
  291.         return out;
  292. }
  293. //polytest.cc
  294. #include <iostream>
  295. #include "poly.h"
  296. using namespace std;

  297. void printmenu();
  298. void input(Poly &ob);

  299. int main()
  300. {
  301.         int choi;
  302.         Poly ob1, ob2, ob3;
  303.         do {
  304.                 printmenu();
  305.                 cin >> choi;
  306.                 switch (choi) {
  307.                         case 1:
  308.                                 cout << "please input your first poly\n";
  309.                                 input(ob1);
  310.                                 cout << "plase input your second poly\n";
  311.                                 input(ob2);
  312.                                 break;
  313.                         case 2:
  314.                                 cout << "f(x)=" << ob1 << endl << "g(x)=" << ob2 << endl;
  315.                                 break;
  316.                         case 3:
  317.                                 cout << "f(x)=" << ob1 << endl << "g(x)=" << ob2 << endl;
  318.                                 ob3 = ob1 + ob2;
  319.                                 cout << "f(x)+g(x)=" << ob3 << endl;
  320.                                 break;
  321.                         case 4:
  322.                                 cout << "f(x)=" << ob1 << endl << "g(x)=" << ob2 << endl;
  323.                                 ob3 = ob1 - ob2;
  324.                                 cout << "f(x)-g(x)=" << ob3 << endl;
  325.                                 break;
  326.                         case 5:
  327.                                 cout << "f(x)=" << ob1 << endl << "g(x)=" << ob2 << endl;
  328.                                 ob3 = ob1 * ob2;
  329.                                 cout << "f(x)*g(x)=" << ob3 << endl;
  330.                                 break;
  331.                         default:
  332.                                 break;
  333.                 }
  334.         } while (choi != 6);
  335. }

  336. void printmenu()
  337. {
  338.         cout << "---------------\n"
  339.                 <<"1-->enter\n"
  340.                 <<"2-->print\n"
  341.                 << "3-->add\n"
  342.                 << "4-->sub\n"
  343.                 << "5-->mul\n"
  344.                 << "6-->exit\n"
  345.                 << "---------------\n"
  346.                 << "please enter your choice:";
  347. }

  348. void input(Poly &ob)
  349. {
  350.         cout << "enter xishu and zhishu, 00 for end:" << endl;
  351.         while (1) {
  352.                 double xishu;
  353.                 int zhishu;
  354.                 cin >> xishu >> zhishu;
  355.                 if (!xishu && !zhishu)
  356.                         break;
  357.                 ob.insert(xishu, zhishu);
  358.         }
  359. }
复制代码
谢谢大家了

论坛徽章:
0
2 [报告]
发表于 2010-06-06 22:53 |只看该作者
本帖最后由 没本 于 2010-06-06 23:04 编辑

调试发现你delete同一个指针两次,或者堆被破坏了。我写程序是尽量避免使用链表的,比如你这个程序就根本没必要用链表。

论坛徽章:
0
3 [报告]
发表于 2010-06-06 23:24 |只看该作者
回复 2# 没本


    我都调了半天了,链表是题中要求用的。
没本大师,能不能说说哪里的问题,具体一点, 我都看晕了看这几句代码,linux下不会出错,但题必须要在VC6.0下运行。
具体说说问题的所在,我好改改

论坛徽章:
0
4 [报告]
发表于 2010-06-06 23:32 |只看该作者
错在这行上,你看看怎么改写吧。
                                ob3 = ob1 + ob2;

论坛徽章:
0
5 [报告]
发表于 2010-06-07 01:31 |只看该作者
回复 4# 没本


    解决了,谢谢你,又看了一遍,是个低级错误。没有写拷贝构造函数
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP