📄 list.cpp
字号:
//Begin of file list.cpp
#include <iostream.h>
#include <math.h>
#include "list.h"
void List::Append(float x=0, float y=0) //建立链表
{
Node *pt=new Node(x,y); //动态为新节点分配内存
if(list==0) //空链表时
list=pt; //链表头指向节点
else //链表不空时
(end())->next=pt; //节点追加在链尾
}
int List::Print() //输出链表
{
if(list==0) //处理空链表
{
cout<<"empty"<<endl;
return 0;
}
cout<<"("; //处理非空链表
int cnt=0;
Node *pt=list; //pt指向链表头
while(pt)
{
if(++cnt%40==1&&cnt!=1)
cout<<endl;
cout<<(pt->p_node).GetX()<<","<<(pt->p_node).GetY()<<";";
pt=pt->next; //pt指向下一个节点
}
cout<<")"<<endl;
return cnt;
}
int List::GetLength() //链表节点统计
{
int cnt=0;
Node *pt=list;
if(pt==0) return cnt; //处理空链表
else cnt+=1;
while(pt->next!=0) //节点统计
{
pt=pt->next;
cnt+=1;
}
return cnt; //返回节点个数
}
float List::Linefit() //线性回归计算
{
float av_x,av_y; //定义变量
float L_xx,L_yy,L_xy;
av_x=0; //X的平均值
av_y=0; //Y的平均值
L_xx=0; //Lxx
L_yy=0; //Lyy
L_xy=0; //Lxy
int n_point=GetLength(); //获取链表节点数
if((n_point)==0) //处理空链表
{
cout<<"Have NO Point to fit!"<<endl;
return 0;
}
Node *pt=list;
Point l_point;
for(int i=0;i<n_point;i++) //计算X、Y的平均值
{
l_point=pt->p_node;
av_x+=l_point.X/n_point; //友元访问私有数据
av_y+=l_point.Y/n_point;
pt=pt->next;
}
pt=list;
for(i=0;i<n_point;i++) //计算Lxx、Lyy和Lxy
{
l_point=pt->p_node;
L_xx+=(l_point.X-av_x)*(l_point.X-av_x); //友元访问私有数据
L_yy+=(l_point.Y-av_y)*(l_point.Y-av_y);
L_xy+=(l_point.X-av_x)*(l_point.Y-av_y);
pt=pt->next;
}
cout<<"This line can be fitted by y=ax+b."<<endl;
cout<<"a="<<L_xy/L_xx; //输出回归系数a
cout<<" b="<<av_y-L_xy*av_x/L_xx<<endl; //输出回归系数b
return float(L_xy/sqrt(L_xx*L_yy)); //返回相关系数r
}
Node *List::end() //返回链尾
{
Node *prv,*pt;
prv=pt=list;
while(pt)
{
prv=pt;
pt=pt->next;
}
return prv;
}
//End of file list.cpp
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -