您好,登錄后才能下訂單哦!
使用MFC怎么實現一個學生選課系統?很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細講解,有這方面需求的人可以來學習下,希望你能有所收獲。
void Cversion02Dlg::OnBnClickedButton1() { UpdateData(true); printmenshu=0;//將顯示的選課門數、已選學分、學費清零 yixuanxf=0; xuefei=0; yixuanms=0;//printmenshu是用來顯示在MFC上,程序里處理的是yixuanms,最后將yxuanms賦給printmenshu,其實可以不用yixuanms,將yixuanms全部替換為printmenshu也可以 if(yixuan.GetItemCount()!=0){//如果yixuan列表不為空,則選課總人數加1。 xuankezrs++; } while ( yixuan.DeleteItem (0));//yixuan列表清空 int i=0,j=0; for(i=0;i<30;i++){ if(ugs[i].chaxunxuehao(xuehao)){ CString edit1,edit2,edit3; edit1=ugs[i].printname(); edit2=ugs[i].printban(); edit3=ugs[i].printxue(); SetDlgItemText(IDC_EDIT2,edit1);//這里也可以直接在控件上添加變量,直接賦給它。就像printxueli=edit3 SetDlgItemText(IDC_EDIT3,edit2); //SetDlgItemText(IDC_EDIT3,edit3); printxueli=edit3; break; } } for(j=0;j<15;j++){ if(gs[j].chaxunxuehao(xuehao)){ CString edit1,edit2,edit3; edit1=gs[j].printname(); edit2=gs[j].printdao(); edit3=gs[j].printxue(); SetDlgItemText(IDC_EDIT2,edit1); SetDlgItemText(IDC_EDIT3,edit2); //SetDlgItemText(IDC_EDIT3,edit3); printxueli=edit3; break; } } if(i==30&&j==15){ INT_PTR nRes; nRes=MessageBox(_T("請輸入正確的學號"),_T("錯誤"),MB_OK|MB_ICONERROR); return; //這里不寫return會直接運行下面的程序 }else{ yixuannum[k]=xuehao;//將找到的學號存在數組里,用來判斷后面輸入的學號是否前面輸入過 k++; } if(k>1){ //這里的k是從1開始的,即當k=1時,只輸入了第一個學號,不用判斷是否已經選過課 for(int q=0;q<k-1;q++){ //這里注意q<k-1,當k=2時表示此時的學號是第二次輸入的,需判斷與第一次輸入的學號是否相同,第一次的學號存在yixuannum[0]里 if(xuehao==yixuannum[q]){ MessageBox(_T("該學生已經選過課了"),_T("錯誤"),MB_OK|MB_ICONERROR); break;//這里如果寫成return,不知道為什么選課總人數不會刷新了 } } } while ( kexuan.DeleteItem (0)); for(int p=0;p<12;p++){ CString klx,kmc,kxh,kxf,krs; klx=c[p].klx(); kmc=c[p].kmc(); krs.Format(_T("%d"),yixuankrs[p]); kxh.Format(_T("%d"),c[p].kxh()); kxf.Format(_T("%d"),c[p].kxf()); kexuan.InsertItem(p,kxh); kexuan.SetItemText(p,1,klx); kexuan.SetItemText(p,2,kmc); kexuan.SetItemText(p,3,kxf); kexuan.SetItemText(p,4,krs); } UpdateData(false); }
“查詢”按鍵:
void Cversion02Dlg::OnBnClickedButton2() { // TODO: 在此添加控件通知處理程序代碼 UpdateData(true); while ( kexuan.DeleteItem (0));//首先將kexuan清空,否則后面產生的列表都會加在原來的列表后面。這里只需循環清掉第一行就可以,因為清掉一行下面一行會自動上來 if(inputkmc.IsEmpty()){//判斷輸入課程名稱是否為空 int q=0,k=0; for(q=0;q<12;q++){ if(inputklx==c[q].klx()){ CString kmc,kxh,kxf,krs;//下面SetItemText中參數是CString類型,必須將各個參數轉為CString kmc=c[q].kmc(); krs.Format(_T("%d"),yixuankrs[q]); //正常應該寫成krs.Format("%d",yixuankrs[q]);但是會報錯,前面加上_T就可以了 kxh.Format(_T("%d"),c[q].kxh()); kxf.Format(_T("%d"),c[q].kxf()); kexuan.InsertItem(k,kxh); kexuan.SetItemText(k,1,inputklx); kexuan.SetItemText(k,2,kmc); kexuan.SetItemText(k,3,kxf); kexuan.SetItemText(k,4,krs); k++; } } if(k==0){ MessageBox(_T("請輸入正確的課程類型"),_T("錯誤"),MB_OK|MB_ICONERROR); for(int p=0;p<12;p++){ //前面已經將列表清空,這里提示錯誤信息之后,將所有課程顯示出來 CString klx,kmc,kxh,kxf,krs; klx=c[p].klx(); kmc=c[p].kmc(); krs.Format(_T("%d"),yixuankrs[p]); kxh.Format(_T("%d"),c[p].kxh()); kxf.Format(_T("%d"),c[p].kxf()); kexuan.InsertItem(p,kxh); kexuan.SetItemText(p,1,klx); kexuan.SetItemText(p,2,kmc); kexuan.SetItemText(p,3,kxf); kexuan.SetItemText(p,4,krs); } } } else if(inputklx.IsEmpty()){//判斷輸入課程類型是否為空 int l=0; for(l=0;l<12;l++){ if(inputkmc==c[l].kmc()){ CString klx,kxh,kxf,krs; klx=c[l].klx(); krs.Format(_T("%d"),yixuankrs[l]); kxh.Format(_T("%d"),c[l].kxh()); kxf.Format(_T("%d"),c[l].kxf()); kexuan.InsertItem(0,kxh); kexuan.SetItemText(0,1,klx); kexuan.SetItemText(0,2,inputkmc); kexuan.SetItemText(0,3,kxf); kexuan.SetItemText(0,4,krs); break; } } if(l==12){ MessageBox(_T("請輸入正確的課程名稱"),_T("錯誤"),MB_OK|MB_ICONERROR); for(int p=0;p<12;p++){ CString klx,kmc,kxh,kxf,krs; klx=c[p].klx(); kmc=c[p].kmc(); krs.Format(_T("%d"),yixuankrs[p]); kxh.Format(_T("%d"),c[p].kxh()); kxf.Format(_T("%d"),c[p].kxf()); kexuan.InsertItem(p,kxh); kexuan.SetItemText(p,1,klx); kexuan.SetItemText(p,2,kmc); kexuan.SetItemText(p,3,kxf); kexuan.SetItemText(p,4,krs); } } } else{ int m=0; for(m=0;m<12;m++){ if(inputkmc==c[m].kmc()&&inputklx==c[m].klx()){//如果輸入課程類型和課程名稱都不為空,判斷課程類型和課程名稱是否匹配 CString kxh,kxf,krs; krs.Format(_T("%d"),yixuankrs[m]); kxh.Format(_T("%d"),c[m].kxh()); kxf.Format(_T("%d"),c[m].kxf()); kexuan.InsertItem(0,kxh); kexuan.SetItemText(0,1,inputklx); kexuan.SetItemText(0,2,inputkmc); kexuan.SetItemText(0,3,kxf); kexuan.SetItemText(0,4,krs); break; } } if(m==12){ MessageBox(_T("請輸入正確的課程類型和課程名稱"),_T("錯誤"),MB_OK|MB_ICONERROR); for(int p=0;p<12;p++){ CString klx,kmc,kxh,kxf,krs; klx=c[p].klx(); kmc=c[p].kmc(); krs.Format(_T("%d"),yixuankrs[p]); kxh.Format(_T("%d"),c[p].kxh()); kxf.Format(_T("%d"),c[p].kxf()); kexuan.InsertItem(p,kxh); kexuan.SetItemText(p,1,klx); kexuan.SetItemText(p,2,kmc); kexuan.SetItemText(p,3,kxf); kexuan.SetItemText(p,4,krs); } } } UpdateData(false); }
“選課”按鍵:
這里有一個關鍵的地方,就是假如用戶選了兩門相同的課,采用的是先添加后刪掉的方法
void Cversion02Dlg::OnBnClickedButton3() { // TODO: 在此添加控件通知處理程序代碼 UpdateData(true); if(printxueli!="本科生"&&printxueli!="研究生"){//防止沒輸學號直接點選課導致程序錯誤 MessageBox(_T("請選輸入學號"),_T("錯誤"),MB_OK|MB_ICONERROR); return; } if(yixuanms<5){ //yixuanms是從0開始的 CString krs; yixuankrs[chosexh-1]++;//這里不能用chosehanghao,要用chosexh-1。因為如果不經過查詢,chosehanghao=chosexh-1,但是如果查詢成功之后,這里chosehanghao不一定等于chosexh-1 krs.Format(_T("%d"),yixuankrs[chosexh-1]); kexuan.SetItemText(chosehanghao,4,krs); int n=0; for(n=0;n<12;n++){ //在這個循環里即使選擇的是同一門課也會添加進yixuan,若是想在這里不添加同一門課,不易做到 if(chosexh==c[n].kxh()){ CString klx,kmc,kxf,kxh; klx=c[n].klx(); kmc=c[n].kmc(); kxf.Format(_T("%d"),c[n].kxf()); kxh.Format(_T("%d"),chosexh); yixuan.InsertItem(yixuanms,kxh); yixuan.SetItemText(yixuanms,1,klx); yixuan.SetItemText(yixuanms,2,kmc); yixuan.SetItemText(yixuanms,3,kxf); yixuanxf=yixuanxf+c[n].kxf(); if(printxueli=="本科生"&&n>=0&&n<=3){ xuefei=xuefei+200*c[n].kxf(); } else if(printxueli=="本科生"&&n>=4&&n<=7){ xuefei=xuefei+150*c[n].kxf(); } else if(printxueli=="本科生"&&n>=8&&n<=11){ xuefei=xuefei+100*c[n].kxf(); } else if(printxueli=="研究生"&&n>=0&&n<=3){ xuefei=xuefei+200*c[n].kxf(); } else if(printxueli=="研究生"&&n>=4&&n<=7){ xuefei=xuefei+180*c[n].kxf(); } else if(printxueli=="研究生"&&n>=8&&n<=11){ xuefei=xuefei+150*c[n].kxf(); } yixuanms++; break; } } } else{ MessageBox(_T("最多選擇5門課"),_T("錯誤"),MB_OK|MB_ICONERROR); } for(int i=0;i<=yixuanms;i++){ //在這里判斷選擇的是否是同一門課,若是同一門課則將其刪掉。相應的學分、選課人數和學費也要減掉 for(int j=i+1;j<=yixuanms;j++){ if(yixuan.GetItemText(i,0)==yixuan.GetItemText(j,0)){ CString str,str1,krs; int strnum=0,strnum1=0; str=yixuan.GetItemText(i,3); strnum=_ttoi(str); yixuanxf=yixuanxf-strnum; str1=yixuan.GetItemText(i,0); strnum1=_ttoi(str1)-1; yixuankrs[chosexh-1]--; krs.Format(_T("%d"),yixuankrs[chosexh-1]); kexuan.SetItemText(chosehanghao,4,krs); if(printxueli=="本科生"&&strnum1>=0&&strnum1<=3){ xuefei=xuefei-200*c[strnum1].kxf(); } else if(printxueli=="本科生"&&strnum1>=4&&strnum1<=7){ xuefei=xuefei-150*c[strnum1].kxf(); } else if(printxueli=="本科生"&&strnum1>=8&&strnum1<=11){ xuefei=xuefei-100*c[strnum1].kxf(); } else if(printxueli=="研究生"&&strnum1>=0&&strnum1<=3){ xuefei=xuefei-200*c[strnum1].kxf(); } else if(printxueli=="研究生"&&strnum1>=4&&strnum1<=7){ xuefei=xuefei-180*c[strnum1].kxf(); } else if(printxueli=="研究生"&&strnum1>=8&&strnum1<=11){ xuefei=xuefei-150*c[strnum1].kxf(); } yixuan.DeleteItem(j); MessageBox(_T("已經選過該門課"),_T("錯誤"),MB_OK|MB_ICONERROR); yixuanms--; break; } } } printmenshu=yixuanms; while ( kexuan.DeleteItem (0)); for(int p=0;p<12;p++){ CString klx,kmc,kxh,kxf,krs; klx=c[p].klx(); kmc=c[p].kmc(); krs.Format(_T("%d"),yixuankrs[p]); kxh.Format(_T("%d"),c[p].kxh()); kxf.Format(_T("%d"),c[p].kxf()); kexuan.InsertItem(p,kxh); kexuan.SetItemText(p,1,klx); kexuan.SetItemText(p,2,kmc); kexuan.SetItemText(p,3,kxf); kexuan.SetItemText(p,4,krs); } UpdateData(false); }
“退課”按鍵:
這里要弄清楚列表項里的行號和序號
void Cversion02Dlg::OnBnClickedButton4() { // TODO: 在此添加控件通知處理程序代碼 UpdateData(true); if(yixuanms>0){ yixuanms--;//點擊退課之后,選課門數-1 CString str,str1,krs; int tuixf=0,tuixh=0; str=yixuan.GetItemText(tuihanghao,3); tuixf=_ttoi(str);//CString轉為int yixuanxf=yixuanxf-tuixf;//已選學分減掉相應的學分 str1=yixuan.GetItemText(tuihanghao,0); tuixh=_ttoi(str1)-1;//這里要注意課程序號是從1開始的,而對象數組和每門課已選人數數組是從0開始的 yixuankrs[tuixh]--;//每門課已選人數-1 if(printxueli=="本科生"&&tuixh>=0&&tuixh<=3){//兩個列表控件屬性已經設置為只能單選一行,所以點擊一次這里只需減一次 xuefei=xuefei-200*c[tuixh].kxf(); } else if(printxueli=="本科生"&&tuixh>=4&&tuixh<=7){ xuefei=xuefei-150*c[tuixh].kxf(); } else if(printxueli=="本科生"&&tuixh>=8&&tuixh<=11){ xuefei=xuefei-100*c[tuixh].kxf(); } else if(printxueli=="研究生"&&tuixh>=0&&tuixh<=3){ xuefei=xuefei-200*c[tuixh].kxf(); } else if(printxueli=="研究生"&&tuixh>=4&&tuixh<=7){ xuefei=xuefei-180*c[tuixh].kxf(); } else if(printxueli=="研究生"&&tuixh>=8&&tuixh<=11){ xuefei=xuefei-150*c[tuixh].kxf(); } printmenshu=yixuanms; yixuan.DeleteItem(tuihanghao); while ( kexuan.DeleteItem (0)); for(int p=0;p<12;p++){ CString klx,kmc,kxh,kxf,krs; klx=c[p].klx(); kmc=c[p].kmc(); krs.Format(_T("%d"),yixuankrs[p]); kxh.Format(_T("%d"),c[p].kxh()); kxf.Format(_T("%d"),c[p].kxf()); kexuan.InsertItem(p,kxh); kexuan.SetItemText(p,1,klx); kexuan.SetItemText(p,2,kmc); kexuan.SetItemText(p,3,kxf); kexuan.SetItemText(p,4,krs); } } else{ MessageBox(_T("已經退完全部課程"),_T("錯誤"),MB_OK|MB_ICONERROR); } UpdateData(false); }
看完上述內容是否對您有幫助呢?如果還想對相關知識有進一步的了解或閱讀更多相關文章,請關注億速云行業資訊頻道,感謝您對億速云的支持。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。