使用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进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。