⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 proto.cpp

📁 Relief的C++程序
💻 CPP
📖 第 1 页 / 共 2 页
字号:
    while (!feof(f))
    {
        if (fgets(buffer, 0xFF, f) != NULL)
        {
            if (strlen(buffer) > strlen("\n"))
            {
                if (i>width-1)
                {
                    printf("\nMore variable names were detected than data items.");
                    printf("\nPlease correct this problem before proceeding");
                    exit(0);
                }
                sscanf (buffer, "%[a-zA-Z0-9-_;:!@#$%^&*(){}[]]", _varname[i]);
                i++;
            }
        }
    }

    if (i<width) 
    {
        printf("\nFewer variable names than data items were detected.");
        printf("\nPlease correct this problem before proceeding");
        exit(0);
    }

    fclose (f);

    return _varname;

}
void PROTO::free_matrix (MATRIX *_matrix)
{
    UINT i;
    for (i=0; i<_matrix->height; i++)
        free (_matrix->data[i]);

    free (_matrix->data);
    free (_matrix);
}

/*-------------------------------------------------------------------*/

void PROTO::free_tags ( CHAR** varname, UINT width)
{
    UINT i;
    for (i=0; i<width; i++)
        free(varname[i]);
    free (varname);
}

/*-------------------------------------------------------------------*/

void PROTO::free_tree ( NODE  *node )
{
    /*
     *  Frees the memory allocated to a tree structure
     */

    if (node == NULL)
        return;
    else
    {
        free_tree (node->on);
        free_tree (node->off);
        free(node);
    }

}

void PROTO::bubble(Mysort *buff, int len)
{
	int i,j;
    REAL temp;
	int tempindex;
     for( i=len;i>0;i--)
     {
          for(j=len-1;j>0;j--)
          {
               if(buff[j].genedata > buff[j-1].genedata)
               {
                   temp = buff[j].genedata;
				   tempindex=buff[j].index;
                   buff[j].genedata = buff[j-1].genedata;
				   buff[j].index= buff[j-1].index;
                   buff[j-1].genedata = temp;
				   buff[j-1].index=tempindex;
               }
          }
     }

	//////////////////////////////////////////
	FILE *m_fp1;
	m_fp1=fopen("c:\\lihui.txt","w+");
	
	////////////////////////////////////////////////////////////////
	fprintf(m_fp1,"基因个数=%d\n",len);
	for( i=len-1;i>0;i--)
	{
	   // j+=1;
		fprintf(m_fp1,"%d",buff[i].index+1);
		fprintf(m_fp1,"		");
		fprintf(m_fp1,"%f",buff[i].genedata);
	//	fprintf(m_fp1,"		");
	//	if(j>9)
	//	{
			fprintf(m_fp1,"\n");
	//		j=0;
	//	}

	
	}
	
	fclose(m_fp1);


}

MATRIX * PROTO::ReadFormExcel(CString filename,UINT *width, UINT *height)
{ 
	MATRIX *_matrix;
//	HWND hwnd;
//	HWND hwnd1;
	//(CProgressCtrl*)::
//	hwnd=::GetDlgItem(NULL,IDC_PROGRESS1);
	if (::CoInitialize( NULL ) == E_INVALIDARG)
	{
		AfxMessageBox(_T("初始化Com失败!"));
    }
    if ( !this->m_ExcelApp.CreateDispatch(_T("Excel.Application"), NULL))
	{
        AfxMessageBox(_T("创建Excel2000服务失败!"));
        ::CoUninitialize();
    }
	Range range;
	///////////////////////////////////////////////
	this->m_wbsBooks.AttachDispatch(this->m_ExcelApp.GetWorkbooks(), TRUE);
	this->m_wbBook.AttachDispatch(m_wbsBooks.Add(COleVariant(filename)), TRUE );
	this->m_wssSheets.AttachDispatch(m_wbBook.GetWorksheets(),TRUE);
	this->m_wsSheet.AttachDispatch(m_wssSheets.GetItem(_variant_t("Sheet1")),TRUE);
	range.AttachDispatch(m_wsSheet.GetCells()); 
	range.AttachDispatch(range.GetItem (COleVariant((long)1),COleVariant((long)1)).pdispVal );
	COleVariant vResult =range.GetValue2();
	CString str;
	Range usedRange;
	CString mm=m_wsSheet.Get_CodeName();
	usedRange.AttachDispatch(m_wsSheet.GetUsedRange());
	range.AttachDispatch(usedRange.GetRows());
	long iRowNum=range.GetCount();                   //已经使用的行数
		//range.GetColumns
	range.AttachDispatch(usedRange.GetColumns());
	long iColNum=range.GetCount(); 
		//已经使用的列数
	long iStartRow=usedRange.GetRow();               //已使用区域的起始行,从1开始
	long iStartCol=usedRange.GetColumn();
	///////////////////////////////////////////
	UINT i;

    _matrix = (MATRIX*) malloc (sizeof (MATRIX));
    if (!_matrix)
       AfxMessageBox("asdf"); //err_exit (__FILE__, __LINE__);
	/////////////行列转置

    _matrix->width  = iRowNum;
    _matrix->height = iColNum;
	///////////////////////////////////////////////

    _matrix->data = (REAL**) malloc (iRowNum * sizeof (REAL*));
    if (_matrix->data == NULL)
        AfxMessageBox("asdf");//err_exit(__FILE__, __LINE__);

    for (i=0; i<iRowNum; i++)
    {
        _matrix->data[i] = (REAL*) malloc (iColNum * sizeof(REAL));
        if (_matrix->data[i] == NULL)
            AfxMessageBox("asdf");//err_exit(__FILE__, __LINE__);
    }
   ////////////////////////////////////////////////////////////////////////////////////////
   REAL **data = _matrix->data;
   *width=iRowNum;
   *height=iColNum;
   ::progress->SetRange(0,iRowNum*iColNum);
   int temp=iRowNum*iColNum;
   
  // int pos=temp/1000;
   /////////////////////////////////////////////////////////////////////
   int j;
   for( i=iStartCol;i<=iColNum;i++)
		for( j=iStartRow;j<=iRowNum;j++)
		{
				range.AttachDispatch(m_wsSheet.GetCells());
				range.AttachDispatch(range.GetItem (COleVariant((long)j),COleVariant((long)i)).pdispVal );
				COleVariant vResult =range.GetValue2();
			//	pos+=pos;
				progress->StepIt();
				if(vResult.vt == VT_BSTR)       //字符串
				{
					str=vResult.bstrVal;
				
				}
				else if (vResult.vt==VT_R8)     //8字节的数字 
				{
					str.Format("%f",vResult.dblVal);
					if(j==2)
						j=2;
					data[j-1][i-1]=vResult.dblVal;
					if(data[iRowNum-1][i-1]==0)
						normal+=1;
				if(data[iRowNum-1][i-1]==1)
						unnormal+=1;
					if(str=="1.000000")
					{
						
					
					
					}
					else if(str=="0.000000")
					{
						
					
					}
			
					
				}
				else if (vResult.vt==VT_R4)           //var is float type
				{
					str.Format("%10.6f",(double)vResult.fltVal);
					AfxMessageBox("VT_R4");
				}
				else if (vResult.vt==VT_CY) 
				{
        
					str=COleCurrency(vResult).Format();
			
				}
				else if (vResult.vt==VT_I2)  
				{
					str.Format("%d",(int)vResult.iVal);
			

				}
				else if (vResult.vt==VT_BOOL)     //var is  VARIANT_BOOL
				{
					str= (vResult.boolVal==0) ?"FALSE": "TRUE";
					AfxMessageBox("VT_BOOL");
				}

				else if(vResult.vt==VT_DATE)    //时间格式
				{
					SYSTEMTIME st;
					VariantTimeToSystemTime(vResult.date, &st);
				}
				else if(vResult.vt==VT_EMPTY)   //单元格空的
				{
					data[j-1][i-1]=10.00;
					str="";
				}  
		
		
			
		
		
		}
    
	//////////////////////////////////////////////////////////////////////////////////////////
	REAL test1=data[1][0];
    return _matrix;
}

void PROTO::Relief(MATRIX *matrix, UINT x, UINT y, UINT target, UINT K)
{

    UINT n_vars = matrix->width, n_samples = matrix->height, i, j, split;
    REAL **data = matrix->data;
    Mysort*sort= (Mysort*) malloc (n_vars * sizeof(Mysort));
	REAL*weight =(REAL*) malloc (n_vars * sizeof(REAL));
	for(i=0;i<n_vars;i++)
		weight[i]=0.0000;
	REAL test;
   
	progress2->SetRange(0,n_vars*n_samples);
	int k1=0;
	int m=0;
	int e=0;
	REAL temp1=0.00;
	REAL temp2=0.00;
     REAL**temphit1 = (REAL**) malloc (n_vars * sizeof (REAL*));
    if (temphit1 == NULL)
        AfxMessageBox("asdf");//err_exit(__FILE__, __LINE__);

    for (i=0; i<n_vars; i++)
    {
       temphit1[i] = (REAL*) malloc (n_samples * sizeof(REAL));
        if (temphit1 == NULL)
            AfxMessageBox("asdf");//err_exit(__FILE__, __LINE__);
    }
	////////////////////////
    REAL**temphit2 = (REAL**) malloc (n_vars * sizeof (REAL*));
    if (temphit2 == NULL)
        AfxMessageBox("asdf");//err_exit(__FILE__, __LINE__);

    for (i=0; i<n_vars; i++)
    {
       temphit2[i] = (REAL*) malloc (n_samples * sizeof(REAL));
        if (temphit2 == NULL)
            AfxMessageBox("asdf");//err_exit(__FILE__, __LINE__);
    }
	temphit1[0][0]=0;
	REAL a,b,a1,b1;
	int lihui=0;
	int lihui1=0;
		for (j=0; j<n_samples; j++) //样本遍历
	
			for (i=0; i<n_vars; i++)//属性号
			{
			temphit1[i][j]=0.0;
			temphit2[i][j]=0.0;
			
			
			}

	for(k1=0;k1<n_samples;k1++)
	{//1
		for (j=0; j<n_samples; j++) //样本遍历
		{//2
			for (i=0; i<n_vars; i++)//属性号
			{//3
				
				if (i != target) 
				{//4
					a1=data[i][k1];
					b1=data[i][j];
					progress2->StepIt();
					if(data[target][j]==data[target][k1])
					{
					
					
					 
						temphit1[i][k1]=temphit1[i][k1]+(a1-b1)*(a1-b1);
						
				 
				 
					}
					else
					{
						
						temphit2[i][k1]=temphit2[i][k1]+(a1-b1)*(a1-b1);
					
					//	miss[k1]=miss[k1]+(data[i][k1]-data[i][j])*(data[i][k1]-data[i][j]);
					//	miss[k1]=miss[k1]+(data[i][k1]-data[i][j])*(data[i][k1]-data[i][j]);
				 
			
				 
					}

				} //4
			}//3
	

		}//2
		

	}//1
	   
	 
	  for(k1=0;k1<n_samples;k1++)
		{

			for (m=0; m<n_vars-1; m++)//属性号
			{
				for(e=0;e<K;e++)
				{
					temp1+=(temphit1[m][e]-data[m][k1])*(temphit1[m][e]-data[m][k1]);
					temp2+=(temphit2[m][e]-data[m][k1])*(temphit2[m][e]-data[m][k1]);
					
				}
			temp1/=K;
			temp2/=K;
			weight[m]=weight[m]-temp1+temp2;
			temp1=0;
			temp2=0;
						
			}
		
		}
	FILE *m_fp1;
	m_fp1=fopen("c:\\lihuirelief.txt","w+");
	
	////////////////////////////////////////////////////////////////
	fprintf(m_fp1,"基因个数=%d\n",n_vars-1);
	for( i=0;i<n_vars;i++)
	{
	   // j+=1;
		fprintf(m_fp1,"%f",weight[i]);
		fprintf(m_fp1,"		");
	
	
			fprintf(m_fp1,"\n");


	
	}
	
	fclose(m_fp1);
     



}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -