ShowDlg.cpp
资源名称:DNA4.rar [点击查看]
上传用户:kanghaisi
上传日期:2017-04-20
资源大小:5727k
文件大小:10k
源码类别:
DNA
开发平台:
Visual C++
- // ShowDlg.cpp : implementation file
- //
- #include "stdafx.h"
- #include "DNA.h"
- #include "ShowDlg.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #define maxNode 20//最大顶点数
- #define codeLen 20//边的编码长度
- static char code[maxNode][codeLen+1]={//边的编码
- "CAACCCAAAACCTGGTAGAG",
- "ATATCGCGGGTTCAACGTGC",
- "ACGTTGACATGCAGGATCGA",
- "TATAGTACTGATCTGTAGTC",
- "AACCTGGTACCAGACTTGAC",
- "TGGTTTGGACTGGTCAAGTT",
- "TATAGCGCATGCAGGATCGA"
- };
- static int AdjMatrix[maxNode][maxNode];
- int arc;
- char s[maxNode*maxNode][maxNode*codeLen+1];//保存所有生成路径的编码
- char newstr[maxNode*codeLen+1];
- char result[maxNode][maxNode+1];//保存正确结果路径
- char resultold[maxNode][maxNode+1];//传统方法保存正确结果 路径
- int answer=0;//最终路径数
- int answerold=0;//传统方法的最终路径数
- bool flagold[maxNode];
- int travalold[maxNode];
- //bool flag[maxNode];//标志每个顶点是否出现过
- //int traval[maxNode];//保存路径
- #endif
- /////////////////////////////////////////////////////////////////////////////
- // CShowDlg dialog
- CShowDlg::CShowDlg(CWnd* pParent /*=NULL*/)
- : CDialog(CShowDlg::IDD, pParent)
- {
- //{{AFX_DATA_INIT(CShowDlg)
- m_result = _T("");
- m_graph = _T("");
- m_resultold = _T("");
- m_head = 0;
- m_tail = 0;
- m_vertex = 0;
- m_arc = 0;
- m_multiresult = _T("");
- //}}AFX_DATA_INIT
- }
- void CShowDlg::DoDataExchange(CDataExchange* pDX)
- {
- CDialog::DoDataExchange(pDX);
- //{{AFX_DATA_MAP(CShowDlg)
- DDX_Control(pDX, IDC_MULTITHREADRUN, m_multithreadrun);
- DDX_Control(pDX, IDC_RUNOLD, m_runold);
- DDX_Control(pDX, IDC_FINISH, m_finish);
- DDX_Control(pDX, IDC_ADD, m_add);
- DDX_Control(pDX, IDC_RUNOK, m_runok);
- DDX_Text(pDX, IDC_RESULT, m_result);
- DDX_Text(pDX, IDC_GRAPH, m_graph);
- DDX_Text(pDX, IDC_RESULTOLD, m_resultold);
- DDX_Text(pDX, IDC_HEAD, m_head);
- DDX_Text(pDX, IDC_TAIL, m_tail);
- DDX_Text(pDX, IDC_VERTEX, m_vertex);
- DDX_Text(pDX, IDC_ARC, m_arc);
- DDX_Text(pDX, IDC_MULTIRESULT, m_multiresult);
- //}}AFX_DATA_MAP
- }
- BEGIN_MESSAGE_MAP(CShowDlg, CDialog)
- //{{AFX_MSG_MAP(CShowDlg)
- ON_BN_CLICKED(IDC_RUNOK, OnRunok)
- ON_BN_CLICKED(IDC_ADD, OnAdd)
- ON_BN_CLICKED(IDC_FINISH, OnFinish)
- ON_BN_CLICKED(IDC_RUNOLD, OnRunold)
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // CShowDlg message handlers
- void CShowDlg::init()
- {
- char fileName[1][10];//文件名,保存形成的路径
- int u,v;
- char str[2*codeLen+1];
- FILE *fpTest,*fp2;
- strcpy(fileName[0],"0.txt");//将0.txt赋值给fileName[0]
- if(!(fpTest=fopen("Test.txt","r")))
- {
- MessageBox("不能打开Test.txt文件!");
- return ;
- }
- if(!(fp2=fopen(fileName[0],"w")))
- {
- MessageBox("不能打开0.txt文件!");
- return ;
- }
- //将test.txt文件中的点的个数和边的条数分别赋值给nodeN和edgeN
- // fscanf(fpTest,"%d%d",&nodeN,&edgeN);
- while(arc--)
- {
- fscanf(fpTest,"%d%d",&u,&v); //将边的信息依次读入u,v中
- char temp[codeLen+1];
- int k;
- //对边进行编码
- if(u!=0)//u不是起点
- {
- //顶点编码的后部分
- for(k=0;k<codeLen/2;k++)
- temp[k]=code[u][k+codeLen/2];
- temp[k]=' ';
- strcpy(str,temp);
- }
- else
- strcpy(str,code[u]);//起点的全部编码
- if(v!=m_vertex-1)//v不是终点
- {
- //编码的前部分
- for(k=0;k<codeLen/2;k++)
- temp[k]=code[v][k];
- temp[k]=' ';
- strcat(str,temp);
- }
- else
- strcat(str,code[v]);
- fprintf(fp2,"%sn",str);//将str的内容写入fp2中
- }
- fclose(fpTest);
- fclose(fp2);
- return ;
- }
- int CShowDlg::getid(char *str, int n)
- {
- int i;
- for(i=0;i<n;i++)
- if(strcmp(str,code[i])==0)
- {
- return i;
- }
- return -1;
- }
- bool CShowDlg::str_compare(char *str, int n)
- {
- bool flag[maxNode];//标志每个顶点是否出现过
- int traval[maxNode];//保存路径
- char s[codeLen+1];
- int i,k,j,len;
- for(i=0;i<codeLen;i++)
- s[i]=str[i];
- s[i]=' ';
- //如果str第一个顶点不是顶点0,则舍弃
- if(strcmp(s,code[0])!=0)
- return false;
- len=strlen(str);
- k=0;
- for(i=len-codeLen;i<len;i++)
- s[k++]=str[i] ;
- s[k]=' ';
- //如果str最后一个顶点不是顶点nodeN-1,则舍弃
- if(strcmp(s,code[n-1])!=0)
- return false;
- //将所有的flag均设置为false
- memset(flag,false,sizeof(flag));
- flag[0]=flag[n-1]=true;
- k=0;
- j=0;
- traval[j++]=0;
- for(i=codeLen;i<len-codeLen;i++)
- {
- s[k++]=str[i];
- if(k==codeLen)
- {
- s[k]=' ';
- k=0;
- int t=getid(s,n);
- flag[t]=true;
- traval[j++]=t;
- }
- }
- for(i=0;i<n;i++)
- if(!flag[i])
- return false;
- traval[n-1]=n-1;
- {
- for(i=0;i<n;i++)
- result[answer][i]=traval[i];
- answer++;
- }
- return true;
- }
- int CShowDlg::head(char *str, char *strCode)//判断编码的头部是否匹配
- {
- int i;
- char temp[11],temps[11];
- for(i=0;i<codeLen/2;i++)
- temp[i]=str[i];
- temp[i]=' ';
- for(i=0;i<codeLen/2;i++)
- temps[i]=strCode[i+codeLen/2];
- temps[i]=' ';
- return strcmp(temp,temps)==0;
- }
- int CShowDlg::tail(char *str, char *strCode)
- {
- int i,k,len;
- char temp[11],temps[11];
- len=strlen(str);
- for(i=len-codeLen/2,k=0;i<len;i++)
- temp[k++]=str[i];
- temp[k]=' ';
- len=strlen(strCode);
- for(i=0,k=0;i<codeLen/2;i++)
- temps[k++]=strCode[i];
- temps[k]=' ';
- return strcmp(temp,temps)==0;
- }
- void CShowDlg::connect(int n)
- {
- FILE *fp1,*fpi,*fpj;
- int i,j,m;
- char fileName[maxNode][15];
- char select[codeLen+1];//加入的点
- for(i=0;i<n;i++)
- {
- fileName[i][0]=i+'0';
- fileName[i][1]=' ';
- strcat(fileName[i],".txt");
- }
- char str[maxNode*codeLen+1];
- bool flaghead,flagtail;
- fp1=fopen(fileName[0],"r");
- i=0;
- //读入边
- while(!feof(fp1))
- {
- fscanf(fp1,"%s",s[i]);
- i++;
- }
- m=i-1;
- fclose(fp1);
- for(i=1;i<n-1;i++)
- {
- strcpy(select,code[i]);//将第i个顶点的编码赋值给select
- fpi=fopen(fileName[i-1],"r");
- fpj=fopen(fileName[i],"w");
- while(!feof(fpi))
- {
- fscanf(fpi,"%s",str);//将长度为i-1的串读入str中
- if(feof(fpi))break;
- flaghead=flagtail=false;
- if(head(str,select))//str的头部与第i个顶点的编码匹配
- flaghead=true;
- if(tail(str,select))//str的尾部与第i个顶点的编码匹配
- flagtail=true;
- for(j=0;j<m;j++)
- {
- if(head(s[j],select)&&flagtail)
- {
- strcpy(newstr,str);
- strcat(newstr,s[j]);
- fprintf(fpj,"%sn",newstr);
- }
- if(tail(s[j],select)&&flaghead)
- {
- strcpy(newstr,s[j]);
- strcat(newstr,str);
- fprintf(fpj,"%sn",newstr);
- }
- }
- }
- fclose(fpi);
- fclose(fpj);
- }
- return ;
- }
- void CShowDlg::gettravel(int n)
- {
- char str[maxNode*codeLen+1];
- FILE *fp;
- int i;
- char fileName[maxNode][10];
- //将文件名分别赋值为i.txt(i=0,1,...,n-1)
- for(i=0;i<n;i++)
- {
- fileName[i][0]=i+'0';
- fileName[i][1]=' ';
- strcat(fileName[i],".txt");
- }
- fp=fopen(fileName[n-2],"r");
- while(!feof(fp))
- {
- fscanf(fp,"%s",str);
- if(feof(fp))break;
- if(strlen(str)!=n*codeLen)
- continue;
- str_compare(str,n);
- }
- fclose(fp);
- return ;
- }
- void CShowDlg::OnRunok()
- {
- // TODO: Add your control notification handler code here
- int i,j,n;
- n=m_vertex;
- init();
- connect(m_vertex);
- gettravel(m_vertex);
- m_result="";
- CString strTemp;
- for(i=0;i<answer;i++)
- {
- for(j=0;j<n;j++)
- {
- strTemp.Format("%d",result[i][j]);
- m_result+=strTemp;
- m_result+=" ";
- }
- m_result+="rn";
- i++;
- }
- UpdateData(FALSE);
- }
- void CShowDlg::OnAdd()
- {
- // TODO: Add your control notification handler code here
- UpdateData(TRUE);
- FILE *fpTest;
- if(!(fpTest=fopen("Test.txt","a")))
- {
- MessageBox("不能打开Test.txt文件!");
- }
- fprintf(fpTest,"%d %dn",m_head,m_tail);
- fclose(fpTest);
- // fscanf(fpi,"%s",str);//将长度为i-1的串读入str中
- UpdateData(FALSE);
- }
- void CShowDlg::OnFinish()
- {
- // TODO: Add your control notification handler code here
- UpdateData(TRUE);
- int u,v,n;
- FILE *fpTest;
- if(!(fpTest=fopen("Test.txt","r")))
- {
- MessageBox("不能打开Test.txt文件!");
- }
- n=m_vertex;
- arc=m_arc;
- for(u=0;u<m_vertex;u++)
- for(v=0;v<m_vertex;v++)
- AdjMatrix[u][v]=0;
- while(!feof(fpTest))
- {
- fscanf(fpTest,"%d%d",&u,&v);
- AdjMatrix[u][v]=1;
- }
- fclose(fpTest);
- m_graph="";
- CString strTemp;
- for(u=0;u<m_vertex;u++)
- {
- for(v=0;v<m_vertex;v++)
- {
- strTemp.Format("%d",AdjMatrix[u][v]);
- m_graph+=strTemp;
- m_graph+=" ";
- }
- m_graph+="rn";
- }
- UpdateData(FALSE);
- }
- void CShowDlg::OnRunold()
- {
- // TODO: Add your control notification handler code here
- int i,j;
- solve();
- m_resultold="";
- CString strTemp;
- for(i=0;i<answerold;i++)
- {
- for(j=0;j<m_vertex;j++)
- {
- strTemp.Format("%d",resultold[i][j]);
- m_resultold+=strTemp;
- m_resultold+=" ";
- }
- m_resultold+="rn";
- }
- UpdateData(FALSE);
- return ;
- }
- void CShowDlg::Tradtion(int t,int d)
- {
- int i;
- travalold[d]=t;
- if(d==m_vertex-1)
- {
- if(travalold[m_vertex-1]==m_vertex-1)
- {
- for(i=0;i<m_vertex;i++)
- resultold[answerold][i]=travalold[i];
- }
- answerold++;
- return ;
- }
- for(i=1;i<m_vertex;i++)
- if(!flagold[i]&&AdjMatrix[t][i])
- {
- flagold[i]=true;
- Tradtion(i,d+1);
- flagold[i]=false;
- }
- return ;
- }
- void CShowDlg::solve()
- {
- int i;
- memset(flagold,false,sizeof(flagold));
- flagold[0]=true;
- travalold[0]=0;
- for(i=1;i<m_vertex;i++)
- if(AdjMatrix[0][i])
- {
- flagold[i]=true;
- Tradtion(i,1);
- flagold[i]=false;
- }
- return ;
- }