今晚确实很纠结的,七点多回家,项目经理打电话过来,明后天由于公司电力网络维护故休假两天,杯具!

更杯具的:到了九点多,又一电话敲过来,明后天XXO正常上班......

//

本源码来自ourdev论坛,当初自己就是看这套代码,一直从第一行"骂"到最后一行,得狠狠地bs这个不带任何注释的作者,虽然“开源”的精神值得学习.....

下载地址 http://download.csdn.net/detail/wangningyu/7626427

[ MFC-MDI ] 源代码下载:http://cache.ourdev.cn/bbs_upload489681/files_11/ourdev_545797.rar

下载不了的话请邮件联系koma0769#vip.qq.com(替换#为@)

注释没有加完,走马观花一番,关键是两个Compile函数需要花点时间研究,有兴趣的看看!

IOPOINT.h

//File plc.h #ifndef __PLC8_H #define __PLC8_H #define m_nAreRow 100 #define m_nAreCol 16 class CIO:public CObject {DECLARE_SERIAL(CIO); public:virtual void Serialize(CArchive &ar);CString name;CString addr;CString code;int Rows;int Cols;int flag;int fiveflag;int xpoint;int ypoint;CIO(); }; class IOPOINT:public CObject {friend class LIST;friend class PLC; protected:CString name; // 元件名称CString code; // 元件生成的代码()CString addr; // 元件地址(操作数)int xpoint,ypoint; // 元件x、y坐标int flag; // 上下左右连接关系int fiveflag; // 图符的索引号,图符的唯一标记BOOL visit; // IOPOINT *right;IOPOINT *down;IOPOINT *left;IOPOINT *up;IOPOINT *unup;IOPOINT *undown; public:IOPOINT(int x,int y,int fl,CString cd="",CString nm="",CString ad=""){name=nm;code=cd;addr=ad;flag=fl;fiveflag=0;visit=FALSE;xpoint=x;ypoint=y;right=0;left=0;up=0;down=0;unup=0;undown=0;}IOPOINT(){name="";code="";addr="";flag=0;fiveflag=0;visit=FALSE;xpoint=0;ypoint=0;right=0;left=0;up=0;down=0;unup=0;undown=0;}void setname(const CString ioname){name=ioname;} void setaddr(const CString ioaddr) // 设置地址{addr=ioaddr;}void setcode(const CString iocode) // 设置生成的指令{code=iocode;}void setflag(int flg){flag=flg;}void setfiveflag(int fivflg){fiveflag=fivflg;}void setvisit(BOOL x){visit=x;}void setwhere(int x,int y){xpoint=x;ypoint=y;}void getname(CString *result_name) // 获取元件名称{*result_name=name;}void getaddr(CString *result_addr) // 获取无件地址{*result_addr=addr;}void getcode(CString *result_code){*result_code=code;}int getflag(){return flag;}int getfiveflag(){return fiveflag;}int getxpoint(){return xpoint;}int getypoint(){return ypoint;}void changeimg(int,int,CDC *);virtual void setstatu() {}virtual void print(int x,int y,CDC *pDC){changeimg(x,y,pDC);}virtual void put_img(int,int,CDC *){}virtual int testaddr(); }; class LD :public IOPOINT { public:LD(int x,int y,int flg,CString cd="LD",CString nm="",CString ad=""):IOPOINT(x,y,flg,cd,nm,ad){};LD():IOPOINT(){setcode("LD"); }void put_img(int x,int y,CDC *pDC);void print(int x,int y,CDC *pDC); }; class LDI :public IOPOINT { public:LDI(int x,int y,int flg,CString cd="LDI",CString nm="",CString ad=""):IOPOINT(x,y,flg,cd,nm,ad){}LDI():IOPOINT(){setcode("LDI");}void put_img(int x,int y,CDC *pDC);void print(int x,int y,CDC *pDC); }; class OR :public IOPOINT { public:OR(int x,int y,int flg,CString cd="OR",CString nm="",CString ad=""):IOPOINT(x,y,flg,cd,nm,ad){}OR():IOPOINT(){setcode("OR");}void put_img(int x,int y,CDC *pDC);void print(int x,int y,CDC *pDC); }; class ORI :public IOPOINT { public:ORI(int x,int y,int flg,CString cd="ORI",CString nm="",CString ad=""):IOPOINT(x,y,flg,cd,nm,ad){}ORI():IOPOINT(){setcode("ORI");}void put_img(int x,int y,CDC *pDC);void print(int x,int y,CDC *pDC); }; class HOR :public IOPOINT { public:HOR(int x,int y,int flg,CString cd="HOR",CString nm="",CString ad=""):IOPOINT(x,y,flg,cd,nm,ad){}HOR():IOPOINT(){}int testaddr();void print(int x,int y,CDC *pDC); }; class ERECT :public IOPOINT { public:ERECT(int x,int y,int flg,CString cd="ERECT",CString nm="",CString ad=""):IOPOINT(x,y,flg,cd,nm,ad){}ERECT():IOPOINT(){}int testaddr(){return 0;}void print(int x,int y,CDC *pDC); }; class OUTPOINT :public IOPOINT { public:OUTPOINT(int x,int y,int flg,CString cd="OUT",CString nm="",CString ad=""):IOPOINT(x,y,flg,cd,nm,ad){}OUTPOINT():IOPOINT(){setcode("OUT");}int testaddr(){return 0;}void put_img(int x,int y,CDC *pDC);void print(int x,int y,CDC *pDC); }; class ADDTION:public IOPOINT { public:ADDTION(int x,int y,int flg,CString cd="",CString nm="",CString ad=""):IOPOINT(x,y,flg,cd,nm,ad){}ADDTION():IOPOINT(){}int testaddr();void put_img(int x,int y,CDC *pDC);void print(int x,int y,CDC *pDC); }; class LIST:public CObject {friend class PLC; private:IOPOINT *root; public:LIST(){root=0;}IOPOINT * ANBtest(IOPOINT *p);IOPOINT * ANBcompile(IOPOINT *p,IOPOINT *endpoint);IOPOINT * ORBtest(IOPOINT *p,IOPOINT *q);IOPOINT * ORBcompile(IOPOINT *p);int compile(IOPOINT *); }; class PLC:public CObject {friend class CLADDoc; private:int Cols;int Rows;IOPOINT * point_array[m_nAreRow][m_nAreCol];LIST list_array[20];int step; public:IOPOINT * getpoint(int row,int col);int getcols();int getrows();void SetRowCol(int row,int col);PLC();void getstep();int syntax(CString &m_strlist);int testpoint(int row,int col);void insert_point(IOPOINT *n);void remove_point(int x,int y);int compiled(CString &m_strlist);void newfile(); }; #endif

IOPOINT.cpp

//FILE:iopoint.CPP #include "stdafx.h" #include "iopoint.h" //extern int CurrentCols; extern FILE *fp; extern int CurH; extern int CurW; extern CString filename; IMPLEMENT_SERIAL(CIO,CObject,1) CIO::CIO() {name="";addr="";code="";Rows=0;Cols=0;flag=0;fiveflag=0;xpoint=0;ypoint=0; } /*IOPOINT 判断无件地址是否有效 */ int IOPOINT::testaddr() {char ch;if ((addr.GetLength()!=4) || addr[2]>'7' || addr[3]>'7')return 1;else{ch=addr[0];switch (ch){case 'X':if (addr<"X000" || addr>"X527")return 1;elsebreak; case 'Y':if (addr<"Y000" || addr>"Y647")return 1;elsebreak;case 'M':if (addr<"M000" || addr>"M977")return 1;elsebreak;case 'C':if (addr<"C060" || addr>"C067" && addr>"C460"|| addr>"C467")return 1;elsebreak;case 'T':if (!((addr>="T050" && addr<="T057") ||(addr>="T450" && addr<="T457") ||(addr>="T550" && addr<="T557") ||(addr>="T650" && addr<="T657") ||(addr>="T700" && addr<="T777")))return 1;else break;default:return 1;}return 0;} } void IOPOINT::changeimg(int x,int y,CDC *pDC) {int x1,y1,x2,y2;if (down!=NULL){x1=x+::CurW;y1=y+::CurH-10;x2=x1;y2=y1+::CurH;pDC->MoveTo(x1,y1);pDC->LineTo(x2,y2);if (down->up!=NULL){x1=x;x2=x1;pDC->MoveTo(x1,y1);pDC->LineTo(x2,y2);}}if (up!=NULL){x1=x;y1=y+::CurH-10;x2=x1;y2=y1-::CurH;pDC->MoveTo(x1,y1);pDC->LineTo(x2,y2);if (up->down!=NULL){x1=x+::CurW;x2=x1;pDC->MoveTo(x1,y1);pDC->LineTo(x2,y2);}}if (fiveflag==5){x1=x+::CurW;y1=y+::CurH-10;x2=x1;y2=y1-::CurH;pDC->MoveTo(x1,y1);pDC->LineTo(x2,y2);}} void LD::put_img(int x,int y,CDC *pDC) {int x1,y1;x1=x;y1=y+::CurH-10;pDC->MoveTo(x1,y1);x1=x1+20;pDC->LineTo(x1,y1);pDC->MoveTo(x1,y1-10);y1=y+::CurH;pDC->LineTo(x1,y1);x1=x1+20;pDC->MoveTo(x1,y1);pDC->LineTo(x1,y1-20);pDC->MoveTo(x1,y1-10);pDC->LineTo(x+::CurW,y1-10); } void LD::print(int x,int y,CDC *pDC) {put_img(x,y,pDC);pDC->SetBkMode(TRANSPARENT);pDC->SetTextAlign(TA_CENTER+TA_BOTTOM);pDC->TextOut(x+::CurW/2,y+40,name);pDC->TextOut(x+::CurW/2,y+60,addr);changeimg(x,y,pDC); } void LDI::put_img(int x,int y,CDC *pDC) {int x1,y1;x1=x;y1=y+::CurH-10;pDC->MoveTo(x1,y1);x1=x1+20;pDC->LineTo(x1,y1);pDC->MoveTo(x1,y1-10);y1=y+::CurH;pDC->LineTo(x1,y1);x1=x1+20;pDC->MoveTo(x1,y1);pDC->LineTo(x1,y1-20);pDC->MoveTo(x1,y1-10);pDC->LineTo(x+::CurW,y1-10);pDC->MoveTo(x1-3,y1-17);pDC->LineTo(x1-17,y1-3); } void LDI::print(int x,int y,CDC *pDC) {put_img(x,y,pDC);pDC->SetBkMode(TRANSPARENT);pDC->SetTextAlign(TA_CENTER+TA_BOTTOM);pDC->TextOut(x+::CurW/2,y+40,name);pDC->TextOut(x+::CurW/2,y+60,addr);changeimg(x,y,pDC); } void OR::put_img(int x,int y,CDC *pDC) {int x1,y1;x1=x;pDC->MoveTo(x1,y-10);y1=y+::CurH-10;pDC->LineTo(x1,y1);x1=x1+20;pDC->LineTo(x1,y1);pDC->MoveTo(x1,y1-10);y1=y+::CurH;pDC->LineTo(x1,y1);x1=x1+20;pDC->MoveTo(x1,y1);pDC->LineTo(x1,y1-20);pDC->MoveTo(x1,y1-10);pDC->LineTo(x+::CurW,y1-10);pDC->LineTo(x+::CurW,y-10); } void OR::print(int x,int y,CDC *pDC) {put_img(x,y,pDC);pDC->SetBkMode(TRANSPARENT);pDC->SetTextAlign(TA_CENTER+TA_BOTTOM);pDC->TextOut(x+::CurW/2,y+40,name);pDC->TextOut(x+::CurW/2,y+60,addr);changeimg(x,y,pDC); } void ORI::put_img(int x,int y,CDC *pDC) {int x1,y1;x1=x;pDC->MoveTo(x1,y-10);y1=y+::CurH-10;pDC->LineTo(x1,y1);x1=x1+20;pDC->LineTo(x1,y1);pDC->MoveTo(x1,y1-10);y1=y+::CurH;pDC->LineTo(x1,y1);x1=x1+20;pDC->MoveTo(x1,y1);pDC->LineTo(x1,y1-20);pDC->MoveTo(x1,y1-10);pDC->LineTo(x+::CurW,y1-10);pDC->MoveTo(x1-3,y1-17);pDC->LineTo(x1-17,y1-3);pDC->MoveTo(x+::CurW,y1-10);pDC->LineTo(x+::CurW,y-10); } void ORI::print(int x,int y,CDC *pDC) {put_img(x,y,pDC);pDC->SetBkMode(TRANSPARENT);pDC->SetTextAlign(TA_CENTER+TA_BOTTOM);pDC->TextOut(x+::CurW/2,y+40,name);pDC->TextOut(x+::CurW/2,y+60,addr);changeimg(x,y,pDC); } void HOR::print(int x,int y,CDC *pDC) {pDC->MoveTo(x,y+::CurH-10);pDC->LineTo(x+::CurW,y+::CurH-10);changeimg(x,y,pDC); } int HOR::testaddr() {return 0; } void ERECT::print(int x,int y,CDC *pDC) {pDC->MoveTo(x+::CurW,y-10);pDC->LineTo(x+::CurW,y+::CurH-10); } void OUTPOINT::put_img(int x,int y,CDC *pDC) {int x1,y1;x1=x+21;y1=y+::CurH-10;pDC->MoveTo(x,y1);pDC->LineTo(x1,y1);CRect rcEllipse(x1,y1-9,x1+18,y1+9);pDC->SelectStockObject(NULL_BRUSH);pDC->Ellipse(rcEllipse);pDC->MoveTo(x1+18,y1);pDC->LineTo(x+::CurW,y1); } void OUTPOINT::print(int x,int y,CDC *pDC) {put_img(x,y,pDC);pDC->SetBkMode(TRANSPARENT);pDC->SetTextAlign(TA_CENTER+TA_BOTTOM);pDC->TextOut(x+::CurW/2,y+40,name);pDC->TextOut(x+::CurW/2,y+60,addr);changeimg(x,y,pDC); } void ADDTION::put_img(int x,int y,CDC *pDC) {pDC->MoveTo(x,y+::CurH-10);pDC->LineTo(x+16,y+::CurH-10);pDC->MoveTo(x+21,y+::CurH-18);pDC->LineTo(x+16,y+::CurH-18);pDC->LineTo(x+16,y+::CurH-1);pDC->LineTo(x+21,y+::CurH-1);pDC->MoveTo(x+37,y+::CurH-1);pDC->LineTo(x+42,y+::CurH-1);pDC->LineTo(x+42,y+::CurH-18);pDC->LineTo(x+37,y+::CurH-18);pDC->MoveTo(x+42,y+::CurH-10);pDC->LineTo(x+::CurW,y+::CurH-10); } void ADDTION::print(int x,int y,CDC *pDC) {put_img(x,y,pDC);pDC->SetBkMode(TRANSPARENT);pDC->SetTextAlign(TA_CENTER+TA_BOTTOM);pDC->TextOut(x+::CurW/2,y+40,name);pDC->TextOut(x+::CurW/2,y+60,addr);pDC->TextOut(x+30,y+77,code);changeimg(x,y,pDC); } int ADDTION::testaddr() {char ch;int i;if (addr.GetLength()>4)// || strlen(name)==0)return 1;for (i=1;i<name.GetLength();i++)if (addr[i]>'7')return 1;switch (flag){/************************************************************8--SET 9--S 10--MC 12--K18--RST 19--R 11--MCR 13--F14--PLS 15--END************************************************************/case 8:case 9:case 18:case 19: {ch=addr[0];switch (ch){case 'Y':if (addr<"Y000" || addr>"Y647")return 1;elsebreak;case 'M':if (addr<"M000" || addr>"M977")return 1;elsebreak;case 'C':if (addr<"C060" || (addr>"C067" &&addr<"C460") || addr>"C467")return 1;elsebreak;default:return 1;}//end of switch(ch)break;}case 10:case 11:case 14: { ch=addr[0];switch (ch){case 'M':if (addr<"M000" || addr>"M777")return 1;elsebreak;default:return 1;}//end of switch(ch)break;}case 12: {if (addr<"K000" || addr>"K777")return 1;break;}case 13: {if (addr<"F670" || addr>"F673")return 1;break;}case 15:break;default:return 1;}// switch(flag);return 0; } IOPOINT * LIST::ANBtest(IOPOINT *p) {IOPOINT *q;IOPOINT *returnpoint=NULL;BOOL flag;flag=FALSE;while (p->unup!=NULL){p=p->unup;q=p;while (q->getflag()!=0 && (q->right!=NULL|| q->getfiveflag()==5)){flag=TRUE;if (q->undown!=NULL){flag=FALSE;returnpoint=q;break;}q=q->right;}if (flag)break;}return returnpoint; } IOPOINT * LIST::ANBcompile(IOPOINT *p,IOPOINT *endpoint) { // IOPOINT *breakpoint;IOPOINT *returnpoint;IOPOINT *temp;IOPOINT point;int j;BOOL NoAnb; // breakpoint=p;j=0;NoAnb=TRUE;while(1){if (NoAnb){switch (p->getflag()){case 1:if (j>0)fprintf(::fp,"%s/t%s/t/t;%s/n","AND",p->addr,p->name);elsefprintf(::fp,"%s/t%s/t/t;%s/n","LD",p->addr,p->name);j++;break;case 2:if (j>0)fprintf(::fp,"%s/t%s/t/t;%s/n","ANI",p->addr,p->name);elsefprintf(::fp,"%s/t%s/t/t;%s/n","LDI",p->addr,p->name);j++;break;default:break;}// end of switch (p->getflag())p->setvisit(TRUE);if (p->down!=NULL){temp=ORBtest(p,&point);if (temp!=NULL){returnpoint=ORBcompile(temp);if (returnpoint==endpoint)return returnpoint;else if (returnpoint->down!=NULL){temp=ORBtest(returnpoint,&point);if (temp!=NULL){returnpoint=ORBcompile(temp);if (returnpoint==endpoint)return returnpoint;}}}}while (p->visit==TRUE)p=p->right;}// end of if (NoAnb)if (p->unup!=NULL){temp=ANBtest(p);if (temp!=NULL){NoAnb=FALSE;returnpoint=ANBcompile(p,temp);if (j>0)fprintf(::fp,"%s/n","ANB");if (returnpoint->down!=NULL){temp=ORBtest(returnpoint,&point);if (temp!=NULL){returnpoint=ORBcompile(temp);if (returnpoint==endpoint)return returnpoint;}}}elseNoAnb=TRUE;}while (p->visit==TRUE)p=p->right;}// end of while(1) } IOPOINT * LIST::ORBtest(IOPOINT *p,IOPOINT *q) {q=p->down;do{p=p->down;while ((q->left!=NULL)&& (q->left->getflag()!=0)&& (q->getfiveflag()!=9)&& (q->left->visit!=TRUE))q=q->left;if (((q->up!=NULL)||(q->getypoint()==0))&&(q->visit==0)&&(q->getflag()!=0))return p;q=q->down;}while (q!=NULL);p=NULL;return p; } IOPOINT * LIST::ORBcompile(IOPOINT *p) {IOPOINT *breakpoint;IOPOINT *result;IOPOINT point;IOPOINT *q,*returnpoint;IOPOINT *temp; // int m;int i,j;BOOL NoAnb;i=0;j=1;q=p;NoAnb=TRUE;if (q->getflag()!=6)i++;while ((q->left!=NULL)&&(q->left->getflag()!=0)&& (q->getfiveflag()!=9) && (q->left->visit!=TRUE)){q=q->left;if (q->getflag()!=6)i++;}returnpoint=p;result=p;breakpoint=q;while(1){if (NoAnb){switch (q->getflag()){case 1:if (i>1){if (j==1)fprintf(::fp,"%s/t%s/t/t;%s/n",q->code,q->addr,q->name);elsefprintf(::fp,"%s/t%s/t/t;%s/n","AND",q->addr,q->name);j++;}elsefprintf(::fp,"%s/t%s/t/t;%s/n","OR",q->addr,q->name);break;case 2:if (i>1){if (j==1)fprintf(::fp,"%s/t%s/t/t;%s/n",q->code,q->addr,q->name);elsefprintf(::fp,"%s/t%s/t/t;%s/n","ANI",q->addr,q->name);j++;}elsefprintf(::fp,"%s/t%s/t/t;%s/n","ORI",q->addr,q->name);break;case 3:case 4:fprintf(::fp,"%s/t%s/t/t;%s/n",q->code,q->addr,q->name);default:break;}//end of switch (q->getflag())q->setvisit(TRUE);if (q==p && j>1)fprintf(::fp,"%s/n","ORB");if (q->down!=NULL){if (q!=p){temp=ORBtest(q,&point);if (temp!=NULL)result=ORBcompile(temp);}if (q==p){temp=ORBtest(q,&point);if (temp!=NULL && breakpoint->getypoint()==point.getypoint())returnpoint=ORBcompile(temp);}}if (q==p)break;elseq=q->right;}// end of if (NoAnb)if (q->unup!=NULL){temp=ANBtest(q);if (temp!=NULL){NoAnb=0;result=ANBcompile(q,temp);if (j>1)fprintf(::fp,"%s/n","ANB");while (q->visit==TRUE)if (q==p)break;elseq=q->right;if (q==p && q->visit==TRUE){if (j>1)fprintf(::fp,"%s/n","ORB");if (result->down!=NULL){temp=ORBtest(result,&point);if (temp!=NULL && breakpoint->getypoint()==point.getypoint())returnpoint=ORBcompile(temp);}break;}}// end of (m!=0)elseNoAnb=1;}//end of if (q->unup!=NULL)}//end of while (1); (q!=p)return returnpoint; } int LIST::compile(IOPOINT *head) {IOPOINT *p;IOPOINT *temp;IOPOINT *q;IOPOINT *returnpoint;IOPOINT point;int flag;BOOL found;BOOL NoAnb;//int r;int i;if ((fp=fopen(::filename,"a+"))==NULL)return 5;p=head;i=0;NoAnb=TRUE;flag=p->getflag();found=FALSE;while(1){while ((flag<7) && (p->right!=NULL)){if (p->unup!=NULL){temp=ANBtest(p);if (temp!=NULL){NoAnb=FALSE;returnpoint=ANBcompile(p,temp);if (i>0)fprintf(::fp,"%s/n","ANB");i++;while (returnpoint->down!=NULL&& (temp=ORBtest(returnpoint,&point))!=NULL){returnpoint=ORBcompile(temp);}while (p->visit==TRUE)p=p->right;flag=p->getflag();}elseNoAnb=TRUE;}if (NoAnb){switch (flag){case 1:if (i==0)fprintf(::fp,"%s/t%s/t/t;%s/n",p->code,p->addr,p->name);elsefprintf(::fp,"%s/t%s/t/t;%s/n","AND",p->addr,p->name);i++;break;case 2:if (i==0)fprintf(::fp,"%s/t%s/t/t;%s/n",p->code,p->addr,p->name);elsefprintf(::fp,"%s/t%s/t/t;%s/n","ANI",p->addr,p->name);i++;break;default:break;}//switch(flag)p->setvisit(TRUE);if (p->down!=NULL){temp=ORBtest(p,&point);if (temp!=NULL){returnpoint=ORBcompile(temp);// while (returnpoint->down!=NULL// && (temp=ORBtest(returnpoint,&point))!=NULL)// {// returnpoint=ORBcompile(temp);// }}}while (p->visit==TRUE)p=p->right;flag=p->getflag();}//if (NoAnb)}//while ((flag<7) && (p->right!=NULL))if (flag>=7){fprintf(fp,"%s/t%s/t/t;%s/n",p->code,p->addr,p->name);p->setvisit(TRUE);}do{if (p->unup!=NULL){temp=p;while (temp->unup!=NULL){temp=temp->unup;q=temp;while (q->getflag()!=0 && q->getflag()<7&& q->right!=NULL)q=q->right;if (q->getflag()>=7){found=TRUE;break;}}if (found){p=temp;flag=p->getflag();break;}}// end of if (p->unup!=NULL)p=p->left;}while(p!=NULL && p->left!=NULL);if (!found)break;elsefound=FALSE;}//end of While(1)fprintf(fp,"/n;***/n/n");fclose(fp);return 0; } PLC::PLC() {newfile();Rows=m_nAreRow;Cols=m_nAreCol; } void PLC::newfile() {UINT i,j;for (i=0;i<m_nAreRow;i++)for (j=0;j<m_nAreCol;j++)point_array[i][j]=NULL;for (i=0;i<20;i++)list_array[i].root=0; } void PLC::getstep() {int i,j;int n;BOOL flag;for (i=0,n=0;i<Rows;i++){flag=TRUE;for (j=0;j<Cols;j++)if ((point_array[i][j] == NULL) || (point_array[i][j]->up!=NULL && j!=0)){flag=FALSE;break;}if (flag){list_array[n].root=point_array[i][0];n++;}}step=n; } int PLC::testpoint(int row,int col) {if (point_array[row][col]==NULL) return 0;else return 1; } void PLC::insert_point(IOPOINT *n) {int i,j;int flag;IOPOINT *temp;i=n->getxpoint();j=n->getypoint();if (n->getfiveflag()!=5){if (point_array[i][j]!=NULL){flag=point_array[i][j]->getfiveflag();if (flag!=5 && flag!=9)remove_point(i,j);if (flag==5 || flag==9){temp=point_array[i][j];n->setfiveflag(flag);n->left=temp->left;n->right=temp->right;n->up=temp->up;n->down=temp->down;n->unup=temp->unup;n->undown=temp->undown;if (n->up!=NULL)n->up->unup=n;if (n->unup!=NULL)n->unup->up=n;if (n->down!=NULL)n->down->undown=n;if (n->undown!=NULL)n->undown->down=n;delete (temp);}}point_array[i][j]=n;if (j==0){if ((i>0) && (point_array[i-1][j]!=NULL)){point_array[i-1][j]->unup=n;n->up=point_array[i-1][j];}if (point_array[i+1][j]!=NULL){point_array[i+1][j]->up=n;n->unup=point_array[i+1][j];}}if (j<Cols-1){if (point_array[i][j+1] != NULL){n->right=point_array[i][j+1];point_array[i][j+1]->left=n;if ((n->right->up !=NULL) && (n->right->up->left !=NULL)){n->right->up->left->down=n;n->undown=n->right->up->left;}if ((i>0) && (point_array[i][j+1]->getfiveflag()==9)&& (point_array[i-1][j]!=NULL)){point_array[i-1][j]->down=n;n->undown=point_array[i-1][j];}}if ((point_array[i+1][j+1]!=NULL)&& ((point_array[i+1][j+1]->up != NULL)|| (point_array[i+1][j+1]->getfiveflag()==9))&& (point_array[i+1][j+1]->left != NULL)){n->down=point_array[i+1][j];point_array[i+1][j]->undown=n;}}if (j>0){if (point_array[i][j-1] != NULL){n->left=point_array[i][j-1];point_array[i][j-1]->right=n;if ((n->left->down != NULL)&& (n->left->down->right != NULL)){point_array[i+1][j]->up=n;n->unup=point_array[i+1][j];}if ((i>0) && ((n->left->getfiveflag()==5)||(n->left->undown!=NULL))&& (point_array[i-1][j]!=NULL)){n->up=point_array[i-1][j];point_array[i-1][j]->unup=n;}}if ((point_array[i+1][j-1]!=NULL)&& ((point_array[i+1][j-1]->undown!=NULL)||(point_array[i+1][j-1]->getfiveflag()==5))&& (point_array[i+1][j]!=NULL)){n->unup=point_array[i+1][j];point_array[i+1][j]->up=n;}if ((i>0) && (point_array[i-1][j-1]!=NULL)&& (point_array[i-1][j-1]->down != NULL)&& (point_array[i-1][j] != NULL)){n->up=point_array[i-1][j];point_array[i-1][j]->unup=n;}} //if (j>0)if (point_array[i+1][j]!=NULL &&point_array[i+1][j]->getfiveflag()==5){n->down=point_array[i+1][j];point_array[i+1][j]->undown=n;}if (point_array[i+1][j]!=NULL &&point_array[i+1][j]->getfiveflag()==9){n->unup=point_array[i+1][j];point_array[i+1][j]->up=n;}if (point_array[i+1][j]!=NULL &&(point_array[i+1][j]->getflag()==3|| point_array[i+1][j]->getflag()==4)){n->down=point_array[i+1][j];point_array[i+1][j]->undown=n;point_array[i+1][j]->up=n;n->unup=point_array[i+1][j];if ((n->left != NULL) && (point_array[i+1][j]->left != NULL)){n->left->down=point_array[i+1][j]->left;point_array[i+1][j]->left->undown=n->left;}if ((n->right != NULL) && (point_array[i+1][j]->right != NULL)){point_array[i+1][j]->right->up=n->right;n->right->unup=point_array[i+1][j]->right;}}/* if ((i>0) && ((n->getflag()==3) || (n->getflag()==4))){if (point_array[i-1][j] != NULL){n->up=point_array[i-1][j];point_array[i-1][j]->unup=n;point_array[i-1][j]->down=n;n->undown=point_array[i-1][j];if ((point_array[i-1][j]->left != NULL) && (n->left != NULL)){point_array[i-1][j]->left->down=n->left;n->left->undown=point_array[i-1][j];}if ((n->right != NULL) && (point_array[i-1][j]->right != NULL)){n->right->up=point_array[i-1][j]->right;point_array[i-1][j]->right->unup=n->right;}} //if (point_array[i-1][j] != NULL)}//if ((i>0) && ((n->getflag()==3) || (n->getflag()==4)))*/}//if (n->getflag()!=5)else{if (i>0){if (point_array[i][j]!=NULL)point_array[i][j]->setfiveflag(5);elsepoint_array[i][j]=n;if (j<Cols-1){if (point_array[i][j+1]!=NULL){if (point_array[i][j+1]->getfiveflag()!=5)point_array[i][j+1]->setfiveflag(9);point_array[i][j]->right=point_array[i][j+1];point_array[i][j+1]->left=point_array[i][j];}else{temp=new IOPOINT;temp->setwhere(i,j+1);temp->setfiveflag(9);point_array[i][j+1]=temp;point_array[i][j]->right=point_array[i][j+1];point_array[i][j+1]->left=point_array[i][j];}if ((point_array[i-1][j+1]!=NULL)&&(point_array[i][j]->right!=NULL)){point_array[i-1][j+1]->unup=point_array[i][j]->right;point_array[i][j]->right->up=point_array[i-1][j+1];}}// end of if (j<Maxcol-1)if (point_array[i-1][j]!=NULL){point_array[i-1][j]->down=point_array[i][j];point_array[i][j]->undown=point_array[i-1][j];}if ((point_array[i+1][j]!=NULL)&& (point_array[i+1][j]->getfiveflag()==5)){point_array[i][j]->down=point_array[i+1][j];point_array[i+1][j]->undown=point_array[i][j];if (j<Cols){point_array[i][j+1]->unup=point_array[i+1][j+1];point_array[i+1][j+1]->up=point_array[i][j+1];}}}//end of if (i>0)}//end of "if(n->getflag()!=5" else } void PLC::remove_point(int x,int y) {int i,j;IOPOINT *temp,*p;i=x;j=y;temp=point_array[i][j];if (temp==NULL) return;if (temp->getfiveflag()==9){p=new IOPOINT;p->setfiveflag(9);if (temp->up!=NULL){p->up=temp->up;p->up->unup=p;}if (temp->unup!=NULL){p->unup=temp->unup;p->unup->up=p;}if (temp->left!=NULL){p->left=temp->left;p->left->right=p;}point_array[i][j]=p;} // if (temp->getfiveflag()==9)else{point_array[i][j]=NULL;if (temp->left != NULL)temp->left->right=NULL;if (temp->right != NULL)temp->right->left=NULL;if (temp->up!=NULL){temp->up->unup=NULL;temp->up=NULL;}if (temp->down!=NULL){temp->down->undown=NULL;temp->down=NULL;}if (temp->unup!=NULL){temp->unup->up=NULL;temp->unup=NULL;}if (temp->undown!=NULL){temp->undown->down=NULL;temp->undown=NULL;}if (temp->getfiveflag()==5){if (temp->right!=NULL)//j<m_nAreCol-1){if (temp->right->getfiveflag()!=5)temp->right->setfiveflag(0);int flag=temp->right->getflag();if (flag==3 || flag==4)temp->right->setflag(flag-2);if (temp->right->up!=NULL){temp->right->up->unup=NULL;temp->right->up=NULL;}if (temp->right->getflag()==0 && temp->right->getfiveflag()!=5){p=temp->right;if (p->unup!=NULL){p->unup->up=NULL;p->unup=NULL;}if (p->down!=NULL){p->down->undown=NULL;p->down=NULL;}if (p->undown!=NULL){p->undown->down=NULL;p->undown=NULL;}delete p;point_array[i][j+1]=NULL;} //if (temp->right->getflag()==0)} // if (temp->right!=NULL)if (temp->left!=NULL && temp->left->getfiveflag()==5){IOPOINT * renew;renew=new IOPOINT;renew->setfiveflag(9);renew->setwhere(i,j);insert_point(renew);}}//if (temp->getfiveflag()==5)}//if (temp->getfiveflag()==9) elsedelete temp; } int PLC::syntax(CString &m_strlist) {IOPOINT *p;int outflag,topflag,result,flag;int i,j;CString str;result=0; // m_list.ResetContent();for (i=0;i<step;i++){outflag=0;topflag=0;p=list_array[i].root;for (j=0;j<Cols;j++){flag=p->getflag();if (flag>=7){outflag++;p=p->right;continue;}if (flag!=6 && outflag)outflag++;if (flag==3 || flag==4 || p->getfiveflag()==5)topflag++;p=p->right;}if (outflag!=1){result=1;str.Format("第 %d 程序段输出语句错误!/r/n",i+1);m_strlist+=str;}if (topflag){result=1;str.Format("第 %d 程序段首行含有 %d 个OR、ORI或ERECT结点!/r/n",i+1,topflag);m_strlist+=str;}}CString addr;int fiveflag;for (i=0;i<100;i++)for (j=0;j<Cols;j++)if (point_array[i][j]!=NULL){flag=point_array[i][j]->getflag();fiveflag=point_array[i][j]->getfiveflag();point_array[i][j]->getaddr(&addr);if ((point_array[i][j]->left==NULL)&& (point_array[i][j]->up==NULL)&& (point_array[i][j]->down==NULL)&& (point_array[i][j]->right==NULL)){result=1;str.Format("第 %d 行,第 %d 列是孤立结点!/r/n",i+1,j+1);m_strlist+=str;}if ((flag==0 && fiveflag!=5 && fiveflag!=9)|| flag==100){result=1;str.Format("第 %d 行,第 %d 列结点类型错误!/r/n",i+1,j+1);m_strlist+=str;}if (addr.GetLength()==0 && flag!=0 && flag!=6 && flag!=12 && flag!=13 && flag!=15){result=1;str.Format("第 %d 行,第 %d 列结点没进行元器件设置!/r/n",i+1,j+1);m_strlist+=str;}point_array[i][j]->getcode(&addr);if ((flag==12 || flag==13) && addr.GetLength()!=4){result=1;str.Format("第 %d 行,第 %d 列结点F、K命令错误!/r/n",i+1,j+1);m_strlist+=str;}}return result; } int PLC::compiled(CString &m_strlist) {int i,j;int flag;CString str;getstep();flag=syntax(m_strlist);if (flag)return flag;if ((fp=fopen(::filename,"w+"))==NULL){str.Format("文件%s不能建立!/r/n",::filename);m_strlist+=str;return 1;}fclose(fp);for (i=0;i<Rows;i++)for (j=0;j<Cols;j++)if (point_array[i][j]!=NULL)point_array[i][j]->setvisit(FALSE);for (i=0;i<step;i++){flag=list_array[i].compile(list_array[i].root);if (flag){str.Format("编译错误!/r/n");m_strlist+=str;return flag;}}int sum=0;for (i=0;i<Rows;i++)for (j=0;j<Cols;j++){if (point_array[i][j]!=NULL){flag=point_array[i][j]->getflag();if (point_array[i][j]->visit==FALSE&& flag!=0 && flag!=6){str.Format("第 %d 行,第 %d 列结点没有编译!/r/n",i+1,j+1);m_strlist+=str;sum++;if (sum==10)return 1;}}}return sum; } void PLC::SetRowCol(int row, int col) {Rows=row;Cols=col; } int PLC::getrows() {return Rows; } int PLC::getcols() {return Cols; } void CIO::Serialize(CArchive &ar) {CObject::Serialize(ar);if (ar.IsStoring()){ar<<name;ar<<addr;ar<<code;ar<<Rows;ar<<Cols;ar<<flag;ar<<fiveflag;ar<<xpoint;ar<<ypoint;}else{ar>>name;ar>>addr;ar>>code;ar>>Rows;ar>>Cols;ar>>flag;ar>>fiveflag;ar>>xpoint;ar>>ypoint;} } IOPOINT * PLC::getpoint(int row, int col) {return (point_array[row][col]); }

XXDoc.cpp

/*菜单编译 —— 生成指令 */ void CLADDoc::OnCompileLad() {// TODO: Add your command handler code hereCString pathname=this->GetPathName(); // 获取文件路径if (pathname=="") {AfxMessageBox("请先保存文件,再进行编译!");return ;}this->OnSaveDocument(pathname); // 保存文档::filename=pathname+this->GetTitle();int i=::filename.Find('.');if (i!=-1)::filename=::filename.Mid(0,i);::filename+=".plc"; // 保存为PLC后缀CString str; try{if (!m_mPlc.compiled(str))str.Format("编译成功!");if (err_dlg){err_dlg->Refresh(str);err_dlg->ShowWindow(SW_RESTORE);}else{err_dlg=new CSyntax(str,AfxGetMainWnd());err_dlg->CenterWindow();err_dlg->ShowWindow(SW_SHOW);}err_dlg->UpdateData(FALSE);}catch(...){POSITION pos=this->GetFirstViewPosition();CView * pView=this->GetNextView(pos);pView->MessageBox("可能梯形图绘制有错、/n或文件不能建立","编译出错",MB_ICONERROR|MB_OK);} }

[开源] PLC梯形图转指令表的算法源代码相关推荐

  1. 西门子plc怎么转到c语言,西门子PLC梯形图和指令表相互转换

    问:西门子plc梯形图和指令表相互转换有哪位朋友知道啊?还有西门子S7-300里的各个数据存储单元有哪些,分别是存储什么数据的? 答:一般你在编辑程序快时,如在插入一个快时,在SIAMTIC管理器中, ...

  2. c语言冒泡排序_图文解析:如何用PLC梯形图实现冒泡排序算法?

    作者注:此程序仅供欣赏,作为科普知识了解即可.实际工程当中PLC一般都有专用的排序指令或函数,不需要我们亲自写排序算法. 什么是冒泡排序? 学习过一点C语言的朋友,对冒泡排序的概念应该并不陌生.冒泡排 ...

  3. 梯形图 c语言代码生成,PLC梯形图中内嵌C语言编程的实现

    针对现有梯形图语言适合实现逻辑控制而不适合实现复杂算法这一问题,从基于构件技术的梯形图实现原理出发,对构件的属性定义以及编译方法进行了分析,提出了一种PLC梯形图内嵌C语言实现混合编程的方法,并在CA ...

  4. 和plc哪个简单点_怎么看PLC梯形图

    梯形图是PLC使用得最多的图形编程语言,被称为PLC的第一编程语言.梯形图与电器控制系统的电路图很相似,具有直观易懂的优点,很容易被工厂电气人员掌握,特别适用于开关量逻辑控制. 梯形图常被称为电路或程 ...

  5. plc梯形图语言c1,plc梯形图编程语言是什么?

    有关plc外部接线或者plc硬件的错误,梯形图语言是plc程序设计中经常用到的编程语言,在继电接触器控制系统电路图的基础上演变而来,梯形图语言特别适用于开关量逻辑控制. 梯形图语言是plc程序设计中使 ...

  6. 变动性算法源代码分析与使用示例(copy_backward、 transform、 replace_copy_if 等)

    首先回顾前面的文章,我们把for_each 归类为非变动性算法,实际上它也可以算是变动性算法,取决于传入的第三个参数,即函数 指针.如果在函数内对容器元素做了修改,那么就属于变动性算法. 变动性算法源 ...

  7. matlab中腐蚀图像的编写,Matlab实现二值图像的腐蚀算法源代码

    标签: 1.二值图像的腐蚀原理:我们知道,二值图像就是0和1组成的矩阵,0为黑1为白,腐蚀作用在1上面也就是图像高光白色部分,然后白色部分往外收缩.腐蚀就是类似于黑色军队反攻白色军队,最终把自己的黑色 ...

  8. quake3中求1/sqrt(x)的算法源代码

    quake3中求1/sqrt(x)的算法源代码如下(未作任何修改): float Q_rsqrt( float number ) {long i;float x2, y;const float thr ...

  9. c语言20s计数器,计数器延时程序与级联程序的plc梯形图实现

    计数器的plc梯形图程序设计 1.应用计数器的延时程序 只要提供一个时钟脉冲信号作为计数器的计数输入信号,计数器就可以实现定时功能,时钟脉冲信号的周期与计数器的设定值相乘就是定时时间. 时钟脉冲信号, ...

  10. 国外一个免费的基于Java的PLC梯形图逻辑编辑与仿真软件

    TRiLOGI 乃是一个可编程序控制器 (PLC) 最常使用的梯形图逻辑编辑与仿真软件. TRiLOGI 6.0 原本是用来编写我 公司 (TRi)所生产的  M- 系列 PLC, 然而其内置的仿真器 ...

最新文章

  1. Android之解析Android Map地图返回的Json数据
  2. Oracle中的字符处理方法
  3. Spring Http Invoker
  4. C++、C#写的WebService相互调用
  5. Arduino LiquidCrystal库函数中文对照
  6. HIVE时间戳错误unix_timestamp时间不同环境相差8小时
  7. LiveVideoStackCon 2018公布优秀出品人与讲师
  8. android tag的使用方法,Android:如何使用Html.TagHandler?
  9. Executor 与 ExecutorService 和 Executors 傻傻分不清
  10. ionic3 隐藏子页面tabs
  11. 道德沦丧的电商推广app开发②
  12. 修改折半查找算法进行范围查找
  13. lodash 源码解读 _.findIndex(obj_array, fn)
  14. 计算机导论在线阅读,计算思维与计算机导论.pdf
  15. 推荐系统的公平性:用户侧与生产侧
  16. 离散数学9:二部图、欧拉图、哈密顿图
  17. IDL调用ENVI-FLAASH大气校正异常退出解决办法
  18. suitecrm上传附件限制修改
  19. 破解安装谷歌翻译软件 Translate Client ( 含文件下载链接 )
  20. 杨义先:大数据的道性

热门文章

  1. 《那些年啊,那些事——一个程序员的奋斗史》六
  2. python拟合曲线_使用python通过点拟合曲线
  3. [渝粤教育] 南昌大学 生物化学 参考 资料
  4. gamit运行时候报错:wrod too long
  5. 查看CAD图纸时怎么将文字隐藏
  6. SpringCloud系列------Config-Server
  7. 牡丹-洛阳牡丹:洛阳牡丹
  8. LaTeX:斜线表头的表格制作
  9. 设计在链式存储结构上交换二叉树中所有结点左右子树的算法
  10. Word表格中文字如何居中?