




免费预览已结束,剩余39页可下载查看
下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
/ Student.cpp : Defines the entry point for the console application./#include stdafx.h#include console.h#include student.h#include studentui.hCStudentUI theUI;CStudentFile theFile(student.dat);/ 定义命令函数void DoAddRec(void);void DoDelRec(void);void DoListAllRec(void);void DoFindRec(void);void main()const int nItemNum = 7;char *strItemnItemNum = Add a student data record, Delete a student data record,-, List all data records, Find a student data record,-,Exit ;theUI._SetOptionsTitle( Main Menu );for (;) int nIndex = theUI._GetOptions(strItem,0,0,nItemNum);switch(nIndex)case 0:/ Add a student data recordDoAddRec();break;case 1:/ Delete a student data recordDoDelRec();break;case 2:/ List all data recordsDoListAllRec();break;case 3:/ Find a student data recordDoFindRec();break;break;case 4:/ Exitreturn;void DoAddRec(void)CStudentRec rec;if ( theUI.InputStuRec( rec ) ) theFile.Add( rec );DoListAllRec();void DoDelRec(void)CStudentRec rec;char strID80, str80= No find the record of ;strcpy(strID, theUI._InputBox( Input Deleted Student ID , 0, 0 );if (strID) int nIndex = theFile.Seek( strID, rec );if (nIndex=0) theFile.Delete( strID );DoListAllRec(); else strcat( str, strID );strcat( str, ! );theUI._MessageBox( Notice , str, 1 );void DoListAllRec(void)int nCount = theFile.GetRecCount();CStudentRec *stu;stu = new CStudentRecnCount;theFile.GetStuRec( stu );theUI.DispStuRecs( stu, nCount );delete nCountstu;void DoFindRec(void)CStudentRec rec;char strID80, str80= No find the record of ;strcpy(strID, theUI._InputBox( Input Finded Student ID , 0, 0 );if (strID) int nIndex = theFile.Seek( strID, rec );if (nIndex=0)theUI.DispStuRecs( &rec, 1 );else strcat( str, strID );strcat( str, ! );theUI._MessageBox( Notice , str, 1 );#include #include #include #include class CStudentRecpublic:CStudentRec()chFlag = N;/ 默认构造函数CStudentRec();/ 默认析构函数friendostream& operator ( istream& is, CStudentRec& stu );CStudentRec& operator = (CStudentRec &stu)/ 赋值运算符重载strncpy(strName, stu.strName, 20);strncpy(strID, stu.strID, 10);for (int i=0; i3; i+)fScorei = stu.fScorei;fAve = stu.fAve;chFlag = stu.chFlag;return *this;charchFlag;/ 标志,A表示正常,N表示空charstrName20;/ 姓名charstrID10;/ 学号floatfScore3;/ 三门成绩floatfAve;/ 总平均分;/ CStudentRec类的实现ostream& operator ( istream& is, CStudentRec& stu )char name20,id10;is.read(&stu.chFlag, sizeof(char);is.read(name, sizeof(name);is.read(id, sizeof(id);is.read(char*)stu.fScore, sizeof(float)*3);is.read(char*)&stu.fAve, sizeof(float);strncpy(stu.strName, name, sizeof(name);strncpy(stu.strID, id, sizeof(id);return is;class CStudentFilepublic:CStudentFile(char* filename);CStudentFile();voidAdd(CStudentRec stu);/ 添加记录voidDelete(char* id);/ 删除学号为id的记录voidUpdate(int nRec, CStudentRec stu);/ 更新记录号为nRec的内容,nRec从0开始 intSeek(char* id, CStudentRec &stu);/ 按学号查找, 返回记录号,-1表示没有找到 intGetRecCount(void);/ 获取文件中的记录数intGetStuRec( CStudentRec* data );/ 获取所有记录,返回记录数private:char*strFileName;/ 文件名;/ CStudentFile类的实现CStudentFile:CStudentFile(char* filename)strFileName = new charstrlen(filename)+1;strcpy(strFileName, filename);CStudentFile:CStudentFile()if (strFileName) delete strFileName;void CStudentFile:Add(CStudentRec stu)/ 打开文件用于添加fstream file(strFileName, ios:out|ios:app|ios:binary );filestu;file.close();void CStudentFile:Delete(char *id)CStudentRec temp;int nDel = Seek(id, temp); if (nDel0) return;/ 设置记录中的chFlag为Ntemp.chFlag = N;Update( nDel, temp );void CStudentFile:Update(int nRec, CStudentRec stu)fstream file(strFileName, ios:in|ios:out|ios:binary);/ 二进制读写方式if (!file) coutthe strFileName file cant open !n;return ; int nSize = sizeof(CStudentRec) - 1;file.seekg( nRec * nSize);filestu;file.close();int CStudentFile:Seek(char* id, CStudentRec& stu)/ 按学号查找 int nRec = -1;fstream file(strFileName, ios:in|ios:nocreate);/ 打开文件用于只读if (!file) coutthe strFileNamestu;if (strcmp(id, stu.strID) = 0) & (stu.chFlag = A)nRec = i;break;i+;file.close();return nRec;intCStudentFile:GetRecCount(void)fstream file(strFileName, ios:in|ios:nocreate);/ 打开文件用于只读if (!file) coutthe strFileNamedata;if (data.chFlag = A)nRec+;file.close();return nRec;int CStudentFile:GetStuRec( CStudentRec* data)fstream file(strFileName, ios:in|ios:nocreate);/ 打开文件用于只读if (!file) coutthe strFileNamestu;if (stu.chFlag = A) datanRec = stu;nRec+;file.close();return nRec;/ 文件studentui.h中的内容class CStudentUI: public CConUIpublic:CStudentUI();bool InputStuRec(CStudentRec &stu);/ 通过键盘输入记录void DispStuRecs(CStudentRec *stu, int nNum);/ 显示nNum个记录private:void DispListHead(int nRow = 0);/ 显示表头;CStudentUI:CStudentUI()_SetMainFrameTitle(Management For The Student Scores);_InitMainFrame(7);DispListHead();bool CStudentUI:InputStuRec(CStudentRec &stu)bool bRes = false;char *str5 = Name:, Student ID:, Score 1:, Score 2:, Score 3:;_SetMultiInputTitle( Input student record data );bRes = _InputMultiBox(str, 0, 0, 20, str, 5); if (bRes)strncpy(stu.strName, str0, 20);strncpy(stu.strID, str1, 10);stu.fAve = (float)0.0;for (int i=0; i3; i+) stu.fScorei = (float)atof(stri+2);stu.fAve += stu.fScorei;stu.fAve = float(stu.fAve/3.0);stu.chFlag = A;return bRes;void CStudentUI:DispListHead(int nRow)int nSizeX, nSizeY;_GetWindowSize(&nSizeX, &nSizeY);/ 获得窗口的大小_SaveSettings();_SetBackColor(15);/ 背景色为白色_SetForeColor(0);/ 文本色为黑色_FillBox(0, nRow, nSizeX-1, 1, false);/ 画背景水平条_SetCursorPos(0, nRow);cout.setf(ios:left);coutsetw(10) Rec NOsetw(20)Student Namesetw(10) ID;coutsetw(10)Score 1setw(10)Score 2setw(10)Score 3;coutAveragenMaxLine,/ 则可按PageUp和PageDown向上和向下翻页/ 若nNumnMaxLine,则按ESC键退出const int nMaxLine = 20;int nStart, nEnd, nPage = 0, nMaxPages, nRow=0;nMaxPages = (nNum-1)/nMaxLine;/ 最大可显示的页数unsigned int ch;int nSizeX, nSizeY, nBkColor, nForeColor;for (;) nStart = nPage * nMaxLine;nEnd = nStart + nMaxLine;if (nEnd=nNum) nEnd = nNum;nRow = 0;_ClearWindow();for (int i=nStart; inEnd; i+) if (stui.chFlag = A) _SetCursorPos( 1, nRow );nRow+;cout.setf(ios:left);coutsetw(10)i+1setw(20)stui.strNamesetw(10)stui.strID;coutsetw(10)stui.fScore0setw(10)stui.fScore1setw(10)stui.fScore2;coutstui.fAve;cout.flush();/ 必须有这行代码,否则显示不出来if (nMaxPages = 0) break;else / 显示提示信息,背景为黄色,前景色为黑色nBkColor = _GetBackColor();nForeColor = _GetForeColor();_SetBackColor( 14 );/ 黄色_SetForeColor( 0 );/ 黑色_GetWindowSize( &nSizeX, &nSizeY );_FillBox( 0, nSizeY-1, nSizeX, 1, false );_SetCursorPos( 1, nSizeY-1 );coutPAGE: nPage RECS: nNum;cout.flush();_SetForeColor( nBkColor );cout PL. press PAGEUP or;cout.flush();/ 恢复原来的颜色设置_SetBackColor( nBkColor );_SetForeColor( nForeColor );for(;)ch = _GetKeyChar();/ 当按下ESC退出if (ch = VK_ESCAPE) return;if (ch = VK_PRIOR ) / PAGEUP键nPage-;if (nPagenMaxPages) nPage = nMaxPages;break;/*用于控制台窗口界面设计,版本1.02002 - 20032006.5(1) 添加了控制台窗口的字体设计,(2) 添加了边框型式,(3) 添加主框架窗口的界面*/#include #include #include #include #include #include typedef struct CONSOLE_FONT DWORD index; COORD dim; *PCONSOLE_FONT; typedef BOOL (WINAPI *GetConsoleFontInfoFunc)(HANDLE,BOOL,DWORD,PCONSOLE_FONT); typedef COORD (WINAPI *GetConsoleFontSizeFunc)(HANDLE, DWORD); typedef BOOL (WINAPI *GetCurrentConsoleFontFunc)(HANDLE, BOOL, PCONSOLE_FONT); typedef DWORD (WINAPI *GetNumberOfConsoleFontsFunc)(); typedef BOOL (WINAPI *SetConsoleFontFunc)(HANDLE, DWORD); GetConsoleFontInfoFunc pGetConsoleFontInfo; GetConsoleFontSizeFunc pGetConsoleFontSize; GetCurrentConsoleFontFunc pGetCurrentConsoleFont; GetNumberOfConsoleFontsFunc pGetNumberOfConsoleFonts; SetConsoleFontFunc pSetConsoleFont; PCONSOLE_FONT fonts = NULL; int GetAvailableFonts(HANDLE hCon,PCONSOLE_FONT *fonts) int fontcount = pGetNumberOfConsoleFonts(); *fonts = new CONSOLE_FONTfontcount; pGetConsoleFontInfo(hCon,0,fontcount,*fonts); return fontcount; BOOL SetFont(HANDLE hCon,int index) if (!pSetConsoleFont(hCon,index) return FALSE; return TRUE; BOOL Init() HINSTANCE hLib = NULL; BOOL bRet = TRUE; hLib = LoadLibrary(KERNEL32.DLL); if (hLib = NULL) return FALSE; pGetConsoleFontInfo = (GetConsoleFontInfoFunc)GetProcAddress(hLib,GetConsoleFontInfo); pGetConsoleFontSize = (GetConsoleFontSizeFunc)GetProcAddress(hLib,GetConsoleFontSize); pGetCurrentConsoleFont = (GetCurrentConsoleFontFunc)GetProcAddress(hLib,GetCurrentConsoleFont); pGetNumberOfConsoleFonts = (GetNumberOfConsoleFontsFunc)GetProcAddress(hLib,GetNumberOfConsoleFonts); pSetConsoleFont = (SetConsoleFontFunc)GetProcAddress(hLib,SetConsoleFont); return bRet; class CConsolepublic:CConsole();CConsole();void _ClearWindow(void);/ 清除当前窗口文本,并将光标移至左上角,即位置(0,0)void _DefineWindow(int left, int top, int right, int bottom);/ 重新定义一个窗口,使得所有操作都与这个窗口有关void _GetConwinSize(int *sizex, int *sizey);/ 返回控制台窗口的大小void _GetWindowSize(int *sizex, int *sizey);/ 返回当前窗口的大小void _SaveWindow(void);/ 将当前窗口内容保存到内存中void _SaveWindow(CHAR_INFO *buf);/ 将当前窗口内容保存到指定内存中void _PutWindow(int absX, int absY);/ 将内存的内容写到指定位置处,(absX,absY)是绝对坐标void _PutWindow(int absX, int absY, CHAR_INFO *buf);/ 将指定内容写到指定位置处,(absX,absY)是绝对坐标void _DrawBox(int x, int y, int length, int height, int mode = 0);/ 在指定位置(x, y)绘制一个长为length,宽为height的框,/ 当mode为0是单线,1为双线,2为混合,其它为单线void _FillBox(int x, int y, int length, int height, bool solid = true);/ 填充指定范围的区域,若solid为true则擦除原来区域内容, 否则不擦除void _DrawCharLine(int x, int y, int length, char ch);/ 在指定位置(x, y)绘制一个长为length字符线条,字符由ch指定void _SaveSettings(void);/ 保存当前的属性:光标、颜色和窗口void _LoadSettings(void);/ 恢复_SaveSettings保存的属性void _ShowCursor(bool show = true);/ 显示/隐藏光标void _OutText(char *str, int nch = -1);/ 在当前光标处输出nch个字符void _OutTextXY(int x, int y, char *str, int nch = -1);/ 在指定位置处输出nch个字符void _SetCursorPos(int x, int y);/ 将光标移动到指定位置void _GetCursorPos(int *x, int *y);/ 获取当前光标的位置void _SetBackColor(int color);/ 设置当前背景色int _GetBackColor(void);/ 获取当前背景色void _SetForeColor(int color);/ 设置当前前景色int _GetForeColor(void);/ 获取当前前景色/ 当color = 0表示 黑色/ 当color = 1表示 蓝色/ 当color = 2表示 绿色/ 当color = 3表示 青色/ 当color = 4表示 红色/ 当color = 5表示 洋红/ 当color = 6表示 综色/ 当color = 7表示 淡灰/ 当color = 8表示 深灰/ 当color = 9表示 淡蓝/ 当color = 10 表示 淡绿/ 当color = 11 表示 淡青/ 当color = 12 表示 淡红/ 当color = 13 表示 淡洋红/ 当color = 14 表示 黄色/ 当color = 15 表示 白色unsigned int _GetKeyChar(void);/ 返回用户按键的字符,不等待,没有按键时返回0,/ 若是非打印字符,返回虚拟键码int _GetMouse(int *mx, int *my, int *state);/ 获取鼠标位置(*mx, *my),鼠标按钮操作状态*state, 1时为单击,2时为双击/ 返回鼠标操作的按钮,5为最右键,1为最左键,2为第2个键,依次类推,一直到4/ 没有鼠标信息或不在当前窗口范围时返回-1,没有按下鼠标按钮,返回0private:HANDLEhOut;/ 输出句柄HANDLEhIn;/ 输入句柄SMALL_RECTrcWindow;/ 窗口WORDbkColor16;/ 背景颜色WORDfoColor16;/ 前景颜色intbkColorIndex, foColorIndex;/ 颜色索引值intnSaveColor2;/ 保存颜色intnSavePos2;/ 保存光标位置boolbSaveShow;/ 保存光标是否显示SMALL_RECT rcSave;/ 保存窗口boolbSaved;/ 是否可以调用_LoadSettingsCHAR_INFOcharInfo100*40;/ 保存窗口内容unsigned int nMaxCols, nMaxRows;/ 最大的行和列;/ CConsole类实现代码CConsole:CConsole()hOut = GetStdHandle(STD_OUTPUT_HANDLE);/ 获取标准输出设备句柄hIn = GetStdHandle(STD_INPUT_HANDLE);/ 获取标准输入设备句柄 / 设置默认字体PCONSOLE_FONT fonts = NULL; HANDLE hConsole = GetStdHandle( STD_ERROR_HANDLE ); Init(); int count = GetAvailableFonts(hConsole,&fonts); SetFont(hConsole,fontscount-1.index); SetConsoleOutputCP(437);/ 设置代码页SMALL_RECT rc = 0,0, 80-1, 25-1;rcWindow = rc;/ 定义默认的窗口大小COORD size = 80, 25;SetConsoleScreenBufferSize(hOut,size);/ 设置缓冲区大小SetConsoleWindowInfo(hOut,true ,&rc);/ 显示全部控制台窗口/ 定义颜色foColor0 = bkColor0 = 0;foColor1 = FOREGROUND_BLUE;foColor2 = FOREGROUND_GREEN;foColor3 = FOREGROUND_BLUE | FOREGROUND_GREEN;foColor4 = FOREGROUND_RED;foColor5 = FOREGROUND_RED | FOREGROUND_BLUE;foColor6 = FOREGROUND_RED | FOREGROUND_GREEN;foColor7 = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;bkColor1 = BACKGROUND_BLUE;bkColor2 = BACKGROUND_GREEN;bkColor3 = BACKGROUND_BLUE | BACKGROUND_GREEN;bkColor4 = BACKGROUND_RED;bkColor5 = BACKGROUND_RED | BACKGROUND_BLUE;bkColor6 = BACKGROUND_RED | BACKGROUND_GREEN;bkColor7 = BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE;for (int i=0; i=7; i+)foColori+8 = foColori + FOREGROUND_INTENSITY; bkColori+8 = bkColori + BACKGROUND_INTENSITY; bkColorIndex = 15;/ 白色foColorIndex = 0; / 黑色SetConsoleTextAttribute(hOut, bkColorbkColorIndex|foColorfoColorIndex);_ClearWindow();_SetCursorPos(0, 0);bSaved = false;nMaxCols = nMaxRows = 0;CConsole:CConsole()CloseHandle(hOut);/ 关闭标准输出设备句柄CloseHandle(hIn);/ 关闭标准输入设备句柄void CConsole:_DrawCharLine(int x, int y, int length, char ch)COORD pos = rcWindow.Left + x, rcWindow.Top + y;CONSOLE_SCREEN_BUFFER_INFO bInfo; GetConsoleScreenBufferInfo( hOut, &bInfo );WORD att = bInfo.wAttributes;for (int i = 0; i5) mode = 0;if (mode = 0) / 单线chBox0 = (char)0xda;/ 左上角点chBox1 = (char)0xbf;/ 右上角点chBox2 = (char)0xc0;/ 左下角点chBox3 = (char)0xd9;/ 右下角点chBox4 = (char)0xc4;/ 水平chBox5 = (char)0xc4;/ 水平chBox6 = (char)0xb3;/ 坚直 else if (mode = 1)/ 双线chBox0 = (char)0xc9;/ 左上角点chBox1 = (char)0xbb;/ 右上角点chBox2 = (char)0xc8;/ 左下角点chBox3 = (char)0xbc;/ 右下角点chBox4 = (char)0xcd;/ 水平chBox5 = (char)0xcd;/ 水平chBox6 = (char)0xba;/ 坚直 else if (mode = 2)/ 混合, 上双余单chBox0 = (char)0xd5;/ 左上角点chBox1 = (char)0xb8;/ 右上角点chBox2 = (char)0xc0;/ 左下角点chBox3 = (char)0xd9;/ 右下角点chBox4 = (char)0xcd;/ 上水平chBox5 = (char)0xc4;/ 下水平chBox6 = (char)0xb3;/ 坚直 else if (mode = 3)/ 混合, 上单双余chBox0 = (char)0xd6;/ 左上角点chBox1 = (char)0xb7;/ 右上角点chBox2 = (char)0xc8;/ 左下角点chBox3 = (char)0xbc;/ 右下角点chBox4 = (char)0xc4;/ 上水平chBox5 = (char)0xcd;/ 下水平chBox6 = (char)0xba;/ 坚直 else if (mode = 4)/ 混合, 下单余双chBox0 = (char)0xc9;/ 左上角点chBox1 = (char)0xbb;/ 右上角点chBox2 = (char)0xd3;/ 左下角点chBox3 = (char)0xbd;/ 右下角点chBox4 = (char)0xcd;/ 上水平chBox5 = (char)0xc4;/ 下水平chBox6 = (char)0xba;/ 坚直 else if (m
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 劳动合同样本 自动辞职
- 公寓赠送区域合同样本
- 2025届江苏省启东市高考适应性考试物理试卷含解析
- 中标转让合同样本
- 出租美甲店合同标准文本
- 个人发票购销合同样本
- 个人资产转移合同样本
- 个人珠宝抵押合同样本
- 农田除虫合同标准文本
- 别墅院子转让合同范例
- 变位齿轮与变位齿轮传动
- 二级精神病医院评价细则
- TGIA 004-2020 垃圾填埋场地下水污染防治技术指南
- GB/T 148-1997印刷、书写和绘图纸幅面尺寸
- 《思想道德与法治》 课件 第三章 弘扬中国精神
- 人教版小学数学四年级下册平均数教学教材课件
- (冀教版)二年级美术下册课件-洞的联想
- (更新版)中国移动政企行业认证题库大全-上(单选题汇总-共3部分-1)
- 中国古钱币课件5(宋元明清)
- 2022年小升初入学考试数学真题重庆市巴川中学初一新生入学水平测试
- 品质控制计划(QC工程图)
评论
0/150
提交评论