VC求一组数的最大值最小值总和以及平均值

时间:2021-06-02 15:12:08


开发工具:VC6

开发语言:C++


第一步:新建对话框应用程序

布局界面效果

VC求一组数的最大值最小值总和以及平均值


第二步:添加控件相关变量

变量

	CButton	m_calculate;
	double	m_score_1;
	double	m_score_10;
	double	m_score_2;
	double	m_score_3;
	double	m_score_4;
	double	m_score_5;
	double	m_score_6;
	double	m_score_7;
	double	m_score_8;
	double	m_score_9;
	CString	m_average;
	CString	m_max;
	CString	m_min;
	CString	m_sum;


关联控件

void CMax_minDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CMax_minDlg)
	DDX_Control(pDX, IDC_BUTTON1, m_calculate);
	DDX_Text(pDX, IDC_EDIT1, m_score_1);
	DDX_Text(pDX, IDC_EDIT10, m_score_10);
	DDX_Text(pDX, IDC_EDIT2, m_score_2);
	DDX_Text(pDX, IDC_EDIT3, m_score_3);
	DDX_Text(pDX, IDC_EDIT4, m_score_4);
	DDX_Text(pDX, IDC_EDIT5, m_score_5);
	DDX_Text(pDX, IDC_EDIT6, m_score_6);
	DDX_Text(pDX, IDC_EDIT7, m_score_7);
	DDX_Text(pDX, IDC_EDIT8, m_score_8);
	DDX_Text(pDX, IDC_EDIT9, m_score_9);
	DDX_Text(pDX, IDC_STATIC_average, m_average);
	DDX_Text(pDX, IDC_STATIC_max, m_max);
	DDX_Text(pDX, IDC_STATIC_min, m_min);
	DDX_Text(pDX, IDC_STATIC_sum, m_sum);
	//}}AFX_DATA_MAP
}

第三步:处理数据

void CMax_minDlg::OnButton1() 
{
	UpdateData(TRUE);
	double array[10];
	array[0]=m_score_1;
	array[1]=m_score_2;
	array[2]=m_score_3;
	array[3]=m_score_4;
	array[4]=m_score_5;
	array[5]=m_score_6;
	array[6]=m_score_7;
	array[7]=m_score_8;
	array[8]=m_score_9;
	array[9]=m_score_10;
	double max=0;
	double min=0;
	double sum=0;
	double average=0;
	int index_max = 0;
	int index_min = 0;
	for(int i=0; i<10; i++)
	{
		if(array[i] >= array[index_max])
		{
			index_max=i;
		}
		
		if(array[i] <= array[index_min])
		{
			index_min=i;
		}
		
		sum = sum + array[i];
	}

	max=array[index_max];
	min=array[index_min];

	sum = sum - max - min;
	average = sum/8;

	CString str;

	str.Format("%f",max);
	m_max = str;

	str.Format("%f",min);
	m_min = str;

	str.Format("%f",sum);
	m_sum = str;

	str.Format("%f",average);
	m_average = str;


	UpdateData(FALSE);
}


第四步:运行效果

VC求一组数的最大值最小值总和以及平均值