📄 inputdata.cpp
字号:
{
GotoDlgCtrl(GetDlgItem(IDC_SEX));
MessageBox("请选择学生性别!","错误",MB_OK|MB_ICONERROR);
itemstr.RemoveAll();
return;
}
GetDlgItemText(IDC_SEG,temp);
if(temp.IsEmpty() || atoi(temp)<=0 || atoi(temp)>100)
{
GotoDlgCtrl(GetDlgItem(IDC_SEG));
MessageBox("非法的学生年龄!","错误",MB_OK|MB_ICONERROR);
itemstr.RemoveAll();
return;
}
else
itemstr.Add(temp);
if(m_xueke.GetCurSel()>=0)
{
m_xueke.GetLBText(m_xueke.GetCurSel(),temp);
itemstr.Add(temp);
}
else
{
GotoDlgCtrl(GetDlgItem(IDC_XUEKE));
MessageBox("请选择学科!","错误",MB_OK|MB_ICONERROR);
itemstr.RemoveAll();
return;
}
GetDlgItemText(IDC_SCORE,temp);
if(temp.IsEmpty() || atoi(temp)<0 || atoi(temp)>1000)
{
GotoDlgCtrl(GetDlgItem(IDC_SCORE));
MessageBox("非法成绩!","错误",MB_OK|MB_ICONERROR);
itemstr.RemoveAll();
return;
}
else
itemstr.Add(temp);
if(m_readem.GetCurSel()>=0)
{
m_readem.GetLBText(m_readem.GetCurSel(),temp);
itemstr.Add(temp);
}
else
{
GotoDlgCtrl(GetDlgItem(IDC_READEM));
MessageBox("备注信息不能为空!","错误",MB_OK|MB_ICONERROR);
itemstr.RemoveAll();
return;
}
if(m_readem.GetCurSel()>0 && atoi(itemstr[(int)itemstr.GetCount()-2])>0)
{
if(MessageBox("缺考学生或舞弊考生不应有成绩,是否继续?","提示",MB_YESNO|MB_ICONASTERISK|MB_DEFBUTTON2)==IDNO)
{
GotoDlgCtrl(GetDlgItem(IDC_SCORE));
itemstr.RemoveAll();
return;
}
}
int r=0,row=2;
//待获取列表字符串及数组字符串
CString liststr,arraystr;
BOOL have=FALSE;//表中是否存在该学生信息,若存在则将行坐标定位于此,同时置该标志为真
while(row<m_score.GetRows() && !have)
{
liststr.Empty();
arraystr.Empty();
for(int n=1;n<6;n++)
{
liststr+=m_score.GetTextMatrix(row,n);
arraystr+=itemstr[n-1];
}
if(liststr==arraystr)
{
r=row;
have=TRUE;
}
row++;
}
if(!have)
{
if(m_score.GetRows()==3 && m_score.GetTextMatrix(2,1).IsEmpty())//获取右侧列表行数,最小为3
r=2;//行下标是从0开始
else
{
r=m_score.GetRows();
m_score.SetRows(r+1);//新行数为现有行数加1
for(int c=0;c<m_score.GetCols();c++)
{
m_score.SetRow(long(r));//增加新行后的最末列
m_score.SetCol(long(c));
m_score.SetCellAlignment(4);
}
}
}
m_score.SetRowHeight(r,320);
//填充前8个字段(单元格)
int x;
for(x=0;x<8;x++)
m_score.SetTextMatrix(r,x+1,itemstr[x]);
//确定成绩所在列号,填充成绩及备注信息
int rc=9,c=0;//rc为列,起始坐标定位为9
while(c==0 && rc<m_score.GetCols())
{
if(m_score.GetTextMatrix(1,rc)==itemstr[x])//此时的X已在上面的循环结束后后加1
c=rc;
rc++;
}
m_score.SetTextMatrix(r,c,itemstr[x+1]);//成绩信息位于科目信息后
m_score.SetTextMatrix(r,m_score.GetCols()-1,itemstr[itemstr.GetCount()-1]);//备注信息位于最末
itemstr.RemoveAll();//清空string型数组
//填充序号
int no=0;
for(int m=2;m<m_score.GetRows();m++)
if(!(m_score.GetTextMatrix(m,1).IsEmpty()))
{
no++;
if(m_score.GetTextMatrix(m,0).IsEmpty())
{
temp.Format("%d",no);
m_score.SetTextMatrix(m,0,temp);
}
}
//增加一行高为0的空行
m_score.SetRows(m_score.GetRows()+1);
m_score.SetRowHeight(m_score.GetRows()-1,0);
GotoDlgCtrl(GetDlgItem(IDC_CLASS));//将光标移动至学籍号处
}
else
MessageBox("配置文件错误!","错误",MB_OK|MB_ICONERROR);
}
BOOL CInputData::ExamNumber(CString strnumber)//校验学号的有效性,参数为接收到的学号
{
BOOL have=FALSE;//有效性标志,初始为假
//获取年级及班字串,再根据两个字串在配置文件中查找对应的班,然后取学籍号检查
if(m_class.GetCurSel()>=0)//构造年级及班字串
{
CString temp,gradestr,classstr;
m_class.GetLBText(m_class.GetCurSel(),temp);
gradestr=temp.Left(temp.Find("级",0)+2);//一个汉字占2字节,找到的是所处的起始位置,所以加2
classstr=temp.Right(temp.GetLength()-temp.Find("级",0)-2);
////从INI文件读取数据
if(theApp.bExist)
{
int row;
theApp.IniFile.GetItemInt("SCHOOL","row",row);//获到行数
CString strvar,stritem,strleft;//strvar为字符串变量,stritem为字符串变量的值,strleft为待截取的左子串
int r=1;
while(r<=row && !have)
{
strvar.Format("var%d",r);//构造列号
r++;
theApp.IniFile.GetItemString("CLASS",strvar,stritem);//获取字符串变量的值
if(stritem.Find(gradestr,0)>=0 && stritem.Find(classstr,0)>=0)//两者都找到,表示此行数据是该班级的配置数据
{
CString strstart,strend;
strstart=stritem.Mid(stritem.ReverseFind(';')+1,stritem.Find("~",0)-stritem.ReverseFind(';')-1);
strend=stritem.Right(stritem.GetLength()-stritem.Find("~",0)-2);
//学籍号的范围必须在已经设定的域内并且位数也在它们之间
if(atoi(strnumber)>=atoi(strstart) && atoi(strnumber)<=atoi(strend) && strnumber.GetLength()>=strstart.GetLength() && strnumber.GetLength()<=strend.GetLength())
have=TRUE;
}
}
}
}
else
{
GotoDlgCtrl(GetDlgItem(IDC_CLASS));
MessageBox("请先选择班级!","错误",MB_OK|MB_ICONERROR);
}
return have;
}
void CInputData::OnBnClickedDel()
{
int row=m_score.GetRow();
if(row>2 && !(m_score.GetTextMatrix(2,1).IsEmpty()))
{
CString temp,message=_T("将要删除如下记录,是否继续?\r\n");
for(int c=1;c<m_score.GetCols();c++)
{
temp+=m_score.GetTextMatrix(row,c);
if(c<m_score.GetCols()-1)
temp+=",";
}
if(MessageBox(message+temp,"警告",MB_YESNO|MB_ICONASTERISK|MB_DEFBUTTON2)==IDYES)
{
if(!(m_score.GetTextMatrix(2,0).IsEmpty()) && m_score.GetRows()==4/*之所以为4,是因为在此时的最末行其实为一高度为0的空行*/ && row==2)//序号不为空并且此行为末行时改为清空数据
{
m_score.RemoveItem(row+1);//删除其下的空行
for(int c=0;c<m_score.GetCols();c++)
m_score.SetTextMatrix(row,c,"");
}
else
{
m_score.RemoveItem(row);
//m_score.RemoveItem(row+1);//删除其下的空行
int no=0;
for(int m=2;m<m_score.GetRows();m++)
{
if(!(m_score.GetTextMatrix(m,1).IsEmpty()))
{
no++;
temp.Format("%d",no);
m_score.SetTextMatrix(m,0,temp);
}
}
}
}
}
else
MessageBox("执行删除前请先选择对应数据!","提示",MB_OK|MB_ICONASTERISK);
}
void CInputData::OnBnClickedDelall()
{
if(MessageBox("此操作将会清空列表内全部数据,是否继续?","提示",MB_YESNO|MB_ICONASTERISK|MB_DEFBUTTON2)==IDYES)
{
m_class.SetCurSel(-1);
SetDlgItemText(IDC_NUMBER,"");
SetDlgItemText(IDC_NAME,"");
SetDlgItemText(IDC_SEG,"");
m_scoreseg.ResetContent();
m_xueke.ResetContent();
m_sex.SetCurSel(-1);
m_readem.SetCurSel(-1);
SetDlgItemText(IDC_SCORE,"");
for(int r=m_score.GetRows()-1;r>1;r--)//表头不清除
{
for(int c=0;c<m_score.GetCols();c++)
m_score.SetTextMatrix(r,c,"");
if(r>2)
m_score.RemoveItem(r);
}
}
}
BEGIN_EVENTSINK_MAP(CInputData, CDialog)
ON_EVENT(CInputData, IDC_LIST1, DISPID_CLICK, CInputData::ClickList1, VTS_NONE)
END_EVENTSINK_MAP()
void CInputData::ClickList1()
{
int row=m_score.GetRow();
m_class.SetCurSel(m_class.FindString(0,m_score.GetTextMatrix(row,2)+m_score.GetTextMatrix(row,3)));
m_scoreseg.SetCurSel(m_scoreseg.FindString(0,m_score.GetTextMatrix(row,4)));
CString temp(_T(""));
if(theApp.bExist)//从配置文件中获取该班学籍号前缀
{
int row;
theApp.IniFile.GetItemInt("SCHOOL","row",row);//获取班级行数
CString str,strnumber;
BOOL per=FALSE;//是否找到对应班级的标志
int r=1;
while(r<=row && !per)//当未查找完并且没找到对应班级时继续,否则退出
{
str.Format("var%d",r);
r++;
theApp.IniFile.GetItemString("CLASS",str,temp);
if(temp.Find(m_score.GetTextMatrix(row,2),0)>=0 && temp.Find(m_score.GetTextMatrix(row,3),0)>=0)//找到年级及班级时取出学籍号前缀
{
per=TRUE;
temp=temp.Left(temp.ReverseFind(';'));
temp=temp.Right(temp.GetLength()-temp.ReverseFind(';')-1);
}
}
}
SetDlgItemText(IDC_NUMBER,m_score.GetTextMatrix(row,5).Right(m_score.GetTextMatrix(row,5).GetLength()-temp.GetLength()));
SetDlgItemText(IDC_NAME,m_score.GetTextMatrix(row,6));
m_sex.SetCurSel(m_sex.FindString(0,m_score.GetTextMatrix(row,7)));
SetDlgItemText(IDC_SEG,m_score.GetTextMatrix(row,8));
m_xueke.SetCurSel(-1);
SetDlgItemText(IDC_SCORE,"");
m_readem.SetCurSel(m_readem.FindString(0,m_score.GetTextMatrix(row,m_score.GetCols()-1)));
}
void CInputData::OnBnClickedSavetxt()
{
//检查表中是否有数据
if(m_score.GetTextMatrix(2,1).IsEmpty())
{
MessageBox("没有任何数据可保存!","提示",MB_OK|MB_ICONASTERISK);
return;
}
//构造文件名
CString filename;
for(int c=1;c<=3;c++)
filename+=m_score.GetTextMatrix(2,c);
filename+="学籍.bin";
//检查是否有同名文件
CString str,path;
path=AfxGetApp()->m_pszHelpFilePath;
str=AfxGetApp()->m_pszExeName;
path=path.Left(path.GetLength()-str.GetLength()-4);
CFileFind find;
BOOL have=find.FindFile(path+filename,0);
find.Close();
if(have)
if(MessageBox("["+path.Left(path.GetLength()-1)+"] 目录下存在文件 <"+filename+"> ,是否覆盖?","提示",MB_YESNO|MB_ICONASTERISK|MB_DEFBUTTON2)==IDYES)
::remove(path+filename);
CFileRandom filerandom;
for(int r=2;r<m_score.GetRows();r++)//列表前两行是表头,故不获取。
{
if(!(m_score.GetTextMatrix(r,1).IsEmpty()))
{
char item[512];
CString temp;
for(int c=1;c<m_score.GetCols();c++)//列表的首列为序号,故不获取。
{
temp+=m_score.GetTextMatrix(r,c);
if(c<m_score.GetCols()-1)
temp+=';';//项间以分号相隔
else
temp+='/';//记录间以星号相隔
}
strcpy(item,(LPCTSTR)temp);//,temp.GetLength()
filerandom.add(item);//追加记录至链表
}
}
filerandom.save(path+filename);//将链表保存至保存文件
MessageBox("已将数据成功保存为以下文件:\r\n"+path+filename,"提示",MB_OK|MB_ICONASTERISK);
}
void CInputData::OnBnClickedNewview()
{
if(theApp.bExist)//如果找开依据配置文件所与选班级构造文件名
{
CString temp;
theApp.IniFile.GetItemString("SCHOOL","name",temp);//获取学校名称
if(m_class.GetCurSel()>=0)
{
CString str;
m_class.GetLBText(m_class.GetCurSel(),str);//获得年级班
temp+=str;
temp+="学籍.bin";
CString records="";
//检验文件是否存在
CString path;
path=AfxGetApp()->m_pszHelpFilePath;
str=AfxGetApp()->m_pszExeName;
path=path.Left(path.GetLength()-str.GetLength()-4);
CFileFind find;
BOOL have=find.FindFile(path+temp,0);
find.Close();
if(have)
{
if(!(m_score.GetTextMatrix(2,1).IsEmpty()))
if(MessageBox("在刷新显示之前请确认数据是否保存!","提示",MB_YESNO|MB_ICONASTERISK|MB_DEFBUTTON2)==IDNO)
return;
CFileRandom filerandom;
filerandom.Read(path+temp);
filerandom.Display(records);
m_score.ShowWindow(SW_HIDE);
//对表初始化为3行,并清空除表头的单元格
m_score.SetRows(3);
for(int c=0;c<m_score.GetCols();c++)
m_score.SetTextMatrix(2,c,"");
//初始化完毕
int r=2,no=0;
while(records.Find('/',0)>=0)
{
//获取记录
temp=records.Left(records.Find('/',0)+1);
records=records.Right(records.GetLength()-temp.GetLength());
temp=temp.Left(temp.GetLength()-1);
//将获取的一条记录以分隔符为标志拆分开并填入表中
if(m_score.GetRows()>3)//大于初始行数时增加1行,行高为320
{
m_score.SetRows(m_score.GetRows()+1);
m_score.SetRowHeight(m_score.GetRows()-1,320);
}
//填序号
no++;
CString nostr;
nostr.Format("%d",no);
m_score.SetTextMatrix(m_score.GetRows()-1,0,nostr);
CString strleft;
for(int c=0;c<m_score.GetCols();c++)
{
m_score.SetRow(r);
m_score.SetCol(c);
m_score.SetCellAlignment(4);
if(c>0)//开始从已获取的字符串变量(temp)中获取单个以分号分隔的子串填充相应单元格
{
if(temp.Find(';',0)>0)//找到分隔符并且不位于首端
{
strleft=temp.Left(temp.Find(';',0)+1);//截取左子串(包含找到的分隔符)
m_score.SetTextMatrix(r,c,strleft.Left(strleft.Find(';')));//对相应单元格赋值为左子串(但不含分隔符)
temp=temp.Right(temp.GetLength()-strleft.GetLength());//迭代改变本身值为右串
}
else
if(temp.Find(';',0)==0)
temp=temp.Right(temp.GetLength()-1);//分隔符位于首端时不截取左串,迭代改变本身值为去除首端分隔符后的右串,此时不对相应单元格赋值。
else
m_score.SetTextMatrix(r,c,temp);//此时没找到分隔符,说明已经是末尾字串,所以对相应单元格直接用其赋值之
}
}
m_score.SetRows(m_score.GetRows()+1);
m_score.SetRowHeight(m_score.GetRows()-1,0);//增加一行高为0的空行
r+=2;
}
m_score.ShowWindow(SW_SHOW);
}
else
MessageBox("["+path.Left(path.GetLength()-1)+"] 目录下不存在文件 <"+temp+">","提示",MB_OK|MB_ICONASTERISK|MB_DEFBUTTON2);
}
else
{
MessageBox("请选择班级后再进行!","提示",MB_OK|MB_ICONASTERISK);
return;
}
}
else
{
MessageBox("当前目录下没有发现配置文件!","提示",MB_OK|MB_ICONASTERISK);
return;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -