VC求一組數的最大值最小值總和以及平均值
阿新 • • 發佈:2019-02-06
開發工具:VC6
開發語言:C++
第一步:新建對話方塊應用程式
佈局介面效果
第二步:新增控制元件相關變數
變數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); }