为了账号安全,请及时绑定邮箱和手机立即绑定

哪位大佬指点一下,看N遍了,就是不知道哪里写错了,感谢!!!


Coordiante.h

#ifndef COORDINATE_H

#define COORDINATE_H

#include<ostream>

using namespace std;


class Coordinate

{

friend ostream &operator<<(ostream &out, Coordinate &coor);


public:

Coordinate(int x=0, int y=0);

void printCoordinate();

bool operator ==(Coordinate &coor);

private:

int m_iX;

int m_iY;


};



Coordinate.cpp


#include"Coordinate.h"

#include <iostream>

using namespace std;


Coordinate::Coordinate(int x, int y)

{

m_iX = x;

m_iY = y;

}

void Coordinate::printCoordinate()

{

cout << "(" << m_iX << "," << m_iY <<")"<< endl;

}


ostream &operator<<(ostream &out, Coordinate &coor)

{

cout << "(" << coor.m_iX << "," << coor.m_iX << ")" << endl;

return out;

}


bool Coordinate::operator ==(Coordinate &coor)

{

if (this->m_iX ==coor.m_iX&&this->m_iY == coor.m_iY)

{

return true;

}

return false;


}



List.h




#ifndef LIST_H

#define LIST_H

#include"Coordinate.h"


class List

{public:

List(int size);

~List();

void ClearList();

bool ListEmpty();

int ListLength();

bool GetElem( int i, Coordinate *e);

int LocateElem(Coordinate *e);

bool PriorElem(Coordinate *currentElem, Coordinate *preElem);

bool NextElem(Coordinate *currentElem, Coordinate *nextElem);

void ListTraverse();

bool ListInsert(int i, Coordinate *e);

bool ListDelete(int i, Coordinate *e);

private:

Coordinate *m_pList;

int m_iSize;

int m_iLength;


};


#endif



List.cpp


#include"List.h"

#include<iostream>


using namespace std;


List::List(int size)

{

m_iSize = size;

m_pList = new Coordinate[m_iSize];

m_iLength = NULL;

}


List::~List()

{

delete []m_pList;

m_pList = NULL;

}


void List::ClearList()

{

m_iLength = 0;

}


bool List::ListEmpty()

{

if (m_iLength == 0)

{

return true;

}

else 

{

return false;

}

//return m_iLength == 0 ? true : false;

}


int List::ListLength()

{

return m_iLength;

}


bool List::GetElem(int i, Coordinate *e)

{

if (i < 0 || i >= m_iSize)

{

return false;

}

*e = m_pList[i];

return true;

}


int List::LocateElem(Coordinate *e)

{

 for (int i=0;i<m_iLength;i++)

if (m_pList[i] == *e)

{

return i;

}

return -1;


bool List::PriorElem(Coordinate *currentElem, Coordinate *preElem)

{

int temp = LocateElem(currentElem);

if (temp == -1)

{

return false;

}

else

{

if (temp == 0)

{

return false;

}

else

{

*preElem = m_pList[temp - 1];

return true;

}

}

}


bool List::NextElem(Coordinate *currentElem, Coordinate *nextElem)

{

int temp = LocateElem(currentElem);

if (temp == -1)

{

return false;

}

else

{

if (temp == m_iLength-1)

{

return false;

}

else

{

*nextElem = m_pList[temp + 1];

return true;

}

}

}


void List::ListTraverse()

{

for (int i = 0; i < m_iLength; i++)

{

cout << m_pList[i] << endl;

}


}


bool List::ListInsert(int i, Coordinate *e)

{

if (i < 0 || i > m_iLength)

{

return false;

}

for (int k = m_iLength - 1; k >= i; k--)

{

m_pList[k + 1] = m_pList[k];

}

m_pList[i] = *e;

m_iLength++;

return true;

}

bool List::ListDelete(int i, Coordinate *e)

{

if (i = 0 || i >= m_iLength)

{

return false;

}

*e = m_pList[i];

for (int k =i+1; k < m_iLength; k++)

{

m_pList[k - 1] = m_pList[k];

}


m_iLength--;

return true;

}


demo.cpp

#include<stdio.h>

#include<stdlib.h>

#include"List.h"

#include"Coordinate.h"

#include<iostream>

using namespace std;

/********************************************/

/*线性表--顺序表

  3 5 7 2 9 1 8

 int main(void)

{   //3 5 7 2 9 1 8

Coordinate e1(3,1);

Coordinate e2(3,5);

Coordinate e3(3,7);

Coordinate e4(3,2);

Coordinate e5(3,9);

Coordinate e6(3,4);

Coordinate e7(3,8);

Coordinate temp(0,0);

List *list1 = new List(20);


list1->ListInsert(0,&e1);

list1->ListInsert(1,&e2);

list1->ListInsert(2,&e3);

list1->ListInsert(3,&e4);

list1->ListInsert(4,&e5);

list1->ListInsert(5,&e6);

list1->ListInsert(6,&e7);




list1->ListTraverse();

cout << endl;



delete list1;





system("pause");

return 0;


}


https://img1.sycdn.imooc.com//5cbc95ac0001742909120780.jpg


正在回答

1 回答

问题已经解决

0 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消

哪位大佬指点一下,看N遍了,就是不知道哪里写错了,感谢!!!

我要回答 关注问题
意见反馈 帮助中心 APP下载
官方微信