1.节点类
若用模板则实现只能写在h里头
template <class T>
class MyNode{
public:
T* data;
MyNode *pre;
MyNode *next;
public :
MyNode(){
this->data=NULL;
}
MyNode(T *&data){
this->data=data;
}
T* getData(){
return data;
}
virtual ~MyNode(){
// if(pre!=NULL)
// delete pre;
// if(next!=NULL)
// delete next;
}
};
2。链表类
#include "MyNode.h"template <class T>
class MyList
{
private:
int len;//长度
//表头和表尾
MyNode<T> *tail;
public:
MyNode<T>* get(int i){
MyNode<T> *tmp=NULL;
MyNode<T> *pre=NULL;
tmp=head;
for(int j=0;j<i;j++){
if(i==0)
return tmp;
if(tmp->next!=NULL)
{
pre=tmp;
tmp=tmp->next;
}
}
return tmp;
}
MyList(){
len=0;
head=tail=NULL;
// if(head==NULL)
// {
// head=tail=new MyNode<T>();
// }
}
//在表头插入新节点
//注意参数为T*指针类型,如果外面是对象释放了空间,这这里会有问题,
//可用深拷贝数据解决
void insertBefore(T* data){
MyNode<T> *tmp=NULL;
{
tmp=new MyNode<T>(data);head=tail=tmp;
}
else{//在表头插入
tmp=new MyNode<T>(data);tmp->next=head;
head=tmp;
}
len++;
}
int getLen(){
return len;
}
~MyList(){
}
}
;
3.MFC的视图类,调用链表
void CDiffEquView::OnDraw(CDC* pDC)
{
//测试链表
CString *a=new CString("A");
CString *b=new CString("B");
MyList<CString> *mylist=new MyList<CString>();
mylist->insertBefore(a);//传指针
mylist->insertBefore(b);
for(int i=0;i<=mylist->getLen()-1;i++){
CString* tmp=((MyNode<CString>*)mylist->get(i))->getData();
::MessageBox(NULL,tmp->GetBuffer(10)
,"test",2);
}
// TODO: add draw code for native data here
// pDC->TextOut(0, 0, m_str);
// CString str;
// str.LoadString(IDS_TEST);
//
// CSize size = pDC->GetTextExtent(str);
CRect rc;
GetClientRect(&rc);
// pDC->TextOut((rc.Width() - size.cx)/2, (rc.Height() - size.cy)/2, str);
//
// CPen pen(PS_SOLID, 1, RGB(255,0,0));
// pDC->SelectObject(&pen);
//
// CBrush brush(RGB(0,0,0));
// pDC->SelectObject(&brush);
// // CBrush *pBrush = CBrush::FromHandle((HBRUSH)::GetStockObject(NULL_BRUSH));
// // pDC->SelectObject(pBrush);
//
// pDC->BeginPath();
// pDC->Rectangle((rc.Width() - size.cx)/2, (rc.Height() - size.cy)/2,
// (rc.Width() + size.cx)/2, (rc.Height() + size.cy)/2);
// pDC->EndPath();
//
// pDC->SelectClipPath(RGN_DIFF);
// // pDC->SelectClipRgn(&rgn, RGN_AND);
//
// int x = (rc.Width() - 300) / 2;
// int y = (rc.Height() - 300) / 2;
//
// // pDC->Rectangle((rc.Width() - size.cx)/2, (rc.Height() - size.cy)/2,
// // (rc.Width() + size.cx)/2, (rc.Height() + size.cy)/2);
// //
// for (unsigned int i = y, j = x; i <= y + 300 - stc; i+=10, j+= 10)
// {
// pDC->MoveTo(x + stc, i);
// pDC->LineTo(x + 300 - stc, i);
// pDC->MoveTo(j, y + stc);
// pDC->LineTo(j, y + 300 - stc);
// }
// a * x * x + b * x + c
//微分方程y'=y-2*x/y , y(0)=1
double x =0, y = 1;
double h=1.0/STEP;
pDC->MoveTo((int)(rc.Width() * 0.6f), 0);
pDC->LineTo(static_cast<int>(rc.Width() * 0.6f), rc.Height()*0.5f);
pDC->MoveTo(static_cast<int>(rc.Width() * 0.6f), static_cast<int>(rc.Height()*0.5f ));
pDC->LineTo(rc.Width(), static_cast<int>(rc.Height()*0.5f));
CPen pen(PS_SOLID, 3, RGB(255,0,0));
pDC->SelectObject(&pen);
float offx = rc.Width() * 0.6f;
float offy = rc.Height()*0.5f ;//* 0.67f;
CPoint p1, p2;
// y =sin(x*3.14/180)*10;// (-x*x + 2 * x) * 0.2f;
p1.x = static_cast<int>(x + offx);
p1.y = static_cast<int>(y + offy);
pDC->MoveTo(p1);
CString ss;
for(; x < STEP; x++)
{
topLeft.x=0;
topLeft.y=x*20;
bottomRight.x=100;
bottomRight.y=(x+1)*20;
y=y+h*(y-2*h*x/y);
ss.Format("x=%4.3f,y=%4.3f",x*h,y);
// y = sin(x*3.14*10/180)*10;//(-x*x + 2 * x) * 0.2f;
p2.x = static_cast<int>((STEP*h*x + offx));
p2.y = static_cast<int>((y*10 + offy));
rc.SetRect(topLeft,bottomRight);
DrawText(pDC->GetSafeHdc(),ss,100,rc,NULL);
pDC->LineTo(p2);
}
// CFont font;
// font.CreatePointFont(300, "隶书");
//
// pDC->SelectObject(&font);
// m_str.LoadString(IDS_TEST);
// pDC->SetTextColor(m_clrFont);
// pDC->TextOut(0, 0, m_str);
// pDC->MoveTo(50, 0);
// pDC->LineTo(50, rc.Height());
// pDC->MoveTo(0, 200);
// pDC->LineTo(rc.Width(), 200);
*/
}