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

📄 guyihua.cpp

📁 实现SVM的分类可以选择SVM类型
💻 CPP
📖 第 1 页 / 共 2 页
字号:
		MessageBox("不协调的上下界关系");
		return ;	}	fp=fopen(filename,"r");		if(fp==NULL)	{		//fprintf(stderr,"can't open file %s\n", argv[i]);		//exit(1);		CString strs;
		strs.Format("不能打开文件 %s\n",m_guyihuain);
		MessageBox(strs);
		return ;	}    max_line_len = 1024;	int max_index;
	y_max = -DBL_MAX;    y_min = DBL_MAX;
	int i,index;	line = (char *) malloc(max_line_len*sizeof(char));#define SKIP_TARGET\	while(isspace(*p)) ++p;\	while(!isspace(*p)) ++p;#define SKIP_ELEMENT\	while(*p!=':') ++p;\	++p;\	while(isspace(*p)) ++p;\	while(*p && !isspace(*p)) ++p;		/* assumption: min index of attributes is 1 */	/* pass 1: find out max index of attributes */	max_index = 0;	while(readline(fp)!=NULL)	{		char *p=line;		SKIP_TARGET		while(sscanf(p,"%d:%*f",&index)==1)		{			max_index = max(max_index, index);			SKIP_ELEMENT		}			}		feature_max = (double *)malloc((max_index+1)* sizeof(double));	feature_min = (double *)malloc((max_index+1)* sizeof(double));		if(feature_max == NULL || feature_min == NULL)	{		//fprintf(stderr,"can't allocate enough memory\n");		//exit(1);		MessageBox("不能分配足够的内存");
		return ;	}	for(i=0;i<=max_index;i++)	{		feature_max[i]=-DBL_MAX;		feature_min[i]=DBL_MAX;	}	rewind(fp);	/* pass 2: find out min/max value */	while(readline(fp)!=NULL)	{		char *p=line;		int next_index=1;		double target;		double value;		sscanf(p,"%lf",&target);		y_max = max(y_max,target);		y_min = min(y_min,target);				SKIP_TARGET		while(sscanf(p,"%d:%lf",&index,&value)==2)		{			for(i=next_index;i<index;i++)			{				feature_max[i]=max(feature_max[i],0);				feature_min[i]=min(feature_min[i],0);			}						feature_max[index]=max(feature_max[index],value);			feature_min[index]=min(feature_min[index],value);			SKIP_ELEMENT			next_index=index+1;		}				for(i=next_index;i<=max_index;i++)		{			feature_max[i]=max(feature_max[i],0);			feature_min[i]=min(feature_min[i],0);		}		}	rewind(fp);	/* pass 2.5: save/restore feature_min/feature_max */		if(p2->GetCheck())//restore_filename改的	{		FILE *fp = fopen(restore_filename,"r");		int idx, c;		double fmin, fmax;				if(fp==NULL)		{			//fprintf(stderr,"can't open file %s\n", restore_filename);			//exit(1);		    CString strs;
		    strs.Format("不能打开文件 %s\n",m_guyihuaout);
		    MessageBox(strs);
		    return ;		}		if((c = fgetc(fp)) == 'y')		{			fscanf(fp, "%lf %lf\n", &y_lower, &y_upper);			fscanf(fp, "%lf %lf\n", &y_min, &y_max);			y_scaling = 1;		}		else			ungetc(c, fp);		if (fgetc(fp) == 'x') {			fscanf(fp, "%lf %lf\n", &lower, &upper);			while(fscanf(fp,"%d %lf %lf\n",&idx,&fmin,&fmax)==3)			{				if(idx<=max_index)				{					feature_min[idx] = fmin;					feature_max[idx] = fmax;				}			}		}		fclose(fp);	}		if(!p2->GetCheck())//save_filename改	{		FILE *fp = fopen(save_filename,"w");		if(fp==NULL)		{			//fprintf(stderr,"can't open file %s\n", save_filename);			//exit(1);		    CString strs;
		    strs.Format("不能打开文件 %s\n",m_guyihuaout);
		    MessageBox(strs);
		    return ;		}		if(y_scaling)		{			fprintf(fp, "y\n");			fprintf(fp, "%.16g %.16g\n", y_lower, y_upper);			fprintf(fp, "%.16g %.16g\n", y_min, y_max);		}		fprintf(fp, "x\n");		fprintf(fp, "%.16g %.16g\n", lower, upper);		for(i=1;i<=max_index;i++)		{			if(feature_min[i]!=feature_max[i])				fprintf(fp,"%d %.16g %.16g\n",i,feature_min[i],feature_max[i]);		}		fclose(fp);	}		/* pass 3: scale */	while(readline(fp)!=NULL)	{		char *p=line;		int next_index=1;		int index;		double target;		double value;				sscanf(p,"%lf",&target);		output_target(target);		SKIP_TARGET		while(sscanf(p,"%d:%lf",&index,&value)==2)		{			for(i=next_index;i<index;i++)				output(i,0);						output(index,value);			SKIP_ELEMENT			next_index=index+1;		}				for(i=next_index;i<=max_index;i++)			output(i,0);		//printf("\n");		inf+="\n";	}	free(line);	fclose(fp);	fg.Write(inf ,inf.GetLength());//写文本	fg.Close();	inf.Empty();	MessageBox("归一化完成");	ShellExecute(NULL , NULL ,m_jieguo,NULL ,NULL , SW_NORMAL);//打开文本	return;
}

char* Cguyihua::readline(FILE *input){	int len;		if(fgets(line,max_line_len,input) == NULL)		return NULL;	while(strrchr(line,'\n') == NULL)	{		max_line_len *= 2;		line = (char *) realloc(line, max_line_len);		len = strlen(line);		if(fgets(line+len,max_line_len-len,input) == NULL)			break;	}	return line;}void Cguyihua::output_target(double value){	if(y_scaling)	{		if(value == y_min)			value = y_lower;		else if(value == y_max)			value = y_upper;		else value = y_lower + (y_upper-y_lower) *			     (value - y_min)/(y_max-y_min);	}	CString ab;	//printf("%g ",value);	ab.Format("%g ",value);	inf+=ab;}void Cguyihua::output(int index, double value){	/* skip single-valued attribute */	if(feature_max[index] == feature_min[index])		return;	if(value == feature_min[index])		value = lower;	else if(value == feature_max[index])		value = upper;	else		value = lower + (upper-lower) * 			(value-feature_min[index])/			(feature_max[index]-feature_min[index]);	if(value != 0)	{		//printf("%d:%g ",index, value);		CString ab;	    ab.Format("%d:%g ",index, value);	    inf+=ab;	}}
void Cguyihua::OnBnClickedCheck1()
{
    CButton* p1 = (CButton*)GetDlgItem(IDC_CHECK1);
	if (p1->GetCheck())
	{	
		y_scaling = 1;
		GetDlgItem(IDC_EDIT1)->ShowWindow(1);
        GetDlgItem(IDC_EDIT15)->ShowWindow(1);
	}
	else
	{	
        y_scaling = 0;
		GetDlgItem(IDC_EDIT1)->ShowWindow(0);
        GetDlgItem(IDC_EDIT15)->ShowWindow(0);
	}
}



void Cguyihua::OnBnClickedCheck2()
{
     CButton* p2 = (CButton*)GetDlgItem(IDC_CHECK2);
	if (p2->GetCheck())
	{	
		GetDlgItem(IDC_EDIT1)->ShowWindow(0);GetDlgItem(IDC_STATIC0)->ShowWindow(0);
        GetDlgItem(IDC_EDIT15)->ShowWindow(0);GetDlgItem(IDC_STATIC1)->ShowWindow(0);
		GetDlgItem(IDC_EDIT17)->ShowWindow(0);GetDlgItem(IDC_STATIC2)->ShowWindow(0);
        GetDlgItem(IDC_EDIT18)->ShowWindow(0);GetDlgItem(IDC_STATIC3)->ShowWindow(0);
		GetDlgItem(IDC_CHECK1)->ShowWindow(0);
	}
	else
	{	
		GetDlgItem(IDC_EDIT17)->ShowWindow(1);GetDlgItem(IDC_STATIC0)->ShowWindow(1);
        GetDlgItem(IDC_EDIT18)->ShowWindow(1);GetDlgItem(IDC_STATIC1)->ShowWindow(1);
		GetDlgItem(IDC_CHECK1)->ShowWindow(1);GetDlgItem(IDC_STATIC2)->ShowWindow(1);
		Cguyihua::OnBnClickedCheck1();GetDlgItem(IDC_STATIC3)->ShowWindow(1);
	}

}

void Cguyihua::OnClose()
{
	// TODO: 在此添加消息处理程序代码和/或调用默认值
	//(CwinsvmDlg*)(GetParent()->GetParent()->m)
    //AfxGetMainWnd()->
	//(CwinsvmDlg*)AfxGetApp()->GetMainWnd()->m
 	CDHtmlDialog::OnClose();
	CwinsvmDlg* pApp = (CwinsvmDlg*)AfxGetApp()->GetMainWnd();
	delete pApp->m_guyihua;
	pApp->m_guyihua=NULL;

}

void Cguyihua::OnOK()
{
	// TODO: 在此添加专用代码和/或调用基类

	CDHtmlDialog::OnOK();
	CwinsvmDlg* pApp = (CwinsvmDlg*)AfxGetApp()->GetMainWnd();
	delete this;
	pApp->m_guyihua=NULL;

}

void Cguyihua::OnBnClickedButton7()
{
	CFileDialog fdlg(false,NULL,NULL,OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,"保存文件 (*.txt)|*.txt||");
	if(fdlg.DoModal()!=IDOK)
	{	
		return;
	}
	m_jieguo=fdlg.GetPathName();	SetDlgItemText(IDC_EDIT16,m_jieguo);
}

⌨️ 快捷键说明

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