首页 > 代码库 > C语言课程设计 Win32应用程序
C语言课程设计 Win32应用程序
问题描述:
请设计一个职工信息管理程序,以方便人事部门对本单位职工的管理,该程序应该具有以下功
能:
(1)能从键盘输入职工的信息 。
(2)给定职工号,显示职工的信息。
(3)给定工作部门,显示该部门的职工信息。
(4)给定职工号,修改职工的信息。
(5)给定职工号,删除职工信息。
题目要求:
(1)按照分析、设计、编码、调试、测试的软件过程完成这个应用程序。
(2)职工信息应该包含职工号、姓名、工作部门、职称、入厂时间、工资。
(3)为程序设计windows 窗口,在该窗口上以按钮的形式为用户提供“菜单”,通过点击各个功
能项对应的按钮完成操作。
输入要求:
(1)用户可以根据需求,选定相应的操作项目。进入每个操作后,通过窗口的文本框,从键盘输
入相应的信息。程序根据用户输入的信息完成相应的处理,实现要求的功能。
(2)能对输入的数据进行简单的校验,例如,入厂时间必须是合法的日期格式,职工号是唯一的
(一个职工号对应一个职工的职工信息)。
输出要求:
(1)应用程序运行后,要在屏幕上显示一个按钮形式的“菜单”。
(2)要求用户输入数据时,给出清晰、明确的提示信息,包括输入的数据内容、格式以及结束方
式等。
(3)在程序完成处理后,要清楚地给出程序的处理结果。例如,在给定职工号删除职工信息时,
如果该职工不存在,要提示没能删除,如果删除成功要提示删除成功。
实现要求:
(1)在程序中使用链表存储职工信息。
(2)采用模块化程序设计的方法,将程序中的各项功能用函数实现。
提示:
(1)使用结构体表示职工信息,一个结点保存一条职工信息。
扩展功能:
(1)提供一些统计功能。例如统计每个部门的人数,统计平均工资、统计各职称的人数。
(2)职工信息从文件读入。
(3)将职工信息保存到文件中。
代码如下:
1 #include <windows.h> 2 #include<string.h> 3 #include<stdio.h> 4 #include"resource.h" 5 LRESULT WINAPI WinProc(HWND hWnd,UINT Msg,WPARAM wParam,LPARAM lParam);//函数声明 6 /* 基本功能 */ 7 void initWorker(); 8 void addWorker(); 9 void showAllInfo(); 10 void clearText(); 11 void clearCache(); 12 void deleteWorker(); 13 void searchWorker(); 14 void modifyWorker(); 15 void searchDepartmentWorker(); 16 void showFormat(struct worker *p); 17 void saveLocalFile(); 18 void readLocalFile(); 19 void statisticsInfo(); 20 21 struct inputhwnd 22 { 23 int x1; 24 int x2; 25 int x3; 26 int x4; 27 int menu_name; 28 }hwndArray[9]= {{110, 145, 150, 20,NUM_INPUT},{110, 200, 150, 20,NAME_INPUT},{110, 255, 150, 20,DEPART_INPUT}, 29 {110, 310, 150, 20,JOBTITLE_INPUT},{110, 365, 50, 20,YEAR_INPUT},{110, 420, 150, 20,WAGE_INPUT}, 30 {182, 365, 25, 20,MONTH_INPUT},{232, 365, 25, 20,DATE_INPUT},{320, 120, 900, 400,BIGTEXTBOX}}; 31 /* 按钮 结构体 */ 32 struct inputhwndbutton 33 { 34 char buttonName[30]; 35 int x1; 36 int x2; 37 int x3; 38 int x4; 39 int buttonNum; 40 }buttonArray[12]={{"添加职工",190,550,80,30,ADD_BUTTON},{"按职工号查询",300,550,120,30,SEARCHNUM_BUTTON},{"修改信息",450,550,100,30,MODIFY_BUTTON}, 41 {"删除职工",580,550,80,30,DELETE_BUTTON},{"显示所有职工信息",690,550,140,30,SHOWINFO_BUTTON},{"按部门查询",860,550,100,30,SEARCHDEPART_BUTTON}, 42 {"清空显示",990,550,100,30,CLEARTEXT_BUTTON},{"版权声明",1,622,100,30,COPYRIGHT_BUTTON},{"退出程序",1190,622,100,30,EXIT_BUTTON}, 43 {"保存到本地",900,90,100,30,SAVELOCALFILE_BUTTON},{"从本地读",1100,90,100,30,READLOCALFILE_BUTTON},{"统计",1120,550,100,30,STATISTICS_BUTTON}}; 44 /* 矩形框 结构体 */ 45 struct showrect 46 { 47 char rectName[50]; 48 int top; 49 int right; 50 int bottom; 51 int left; 52 }rectArray[10]={{"[ 职工信息输入框 ]",220,300,40,100},{"职工号:",270,120,40,20},{"姓名:",380,120,40,20}, 53 {"工作部门:",490,120,40,20},{"职称:",600,120,40,20},{"入厂时间:",710,120,40,20}, 54 {"工资:",820,120,40,20},{"年",710,50,40,290},{"月",710,50,40,390},{"日",710,50,40,490}}; 55 RECT rect[10]; 56 HDC hDC; 57 HWND hWnd; 58 PAINTSTRUCT paint; 59 static char str_num[20],str_name[20],str_depart[20],str_jobTitle[20],str_year[10],str_month[6],str_date[6],str_wage[20],str_showAll[1000]; 60 char title[100]={"职工号\t\t姓名\t\t工作部门\t\t\t职称\t\t入厂时间\t\t工资\r\n\r\n"}; 61 struct worker 62 { 63 long num; //职工号 64 char name[20]; //姓名 65 char department[20];//工作部门 66 char jobTitle[20]; //职称 67 int year; 68 int month; 69 int date; 70 long wage; //工资 71 worker *next; 72 }; 73 struct worker *headp; 74 void initWorker() 75 { 76 headp = new worker; 77 headp->next=NULL; 78 } 79 int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nShowCmd) 80 { 81 char *cName = "myWindow";//定义个字符指针表示窗口类的名字 82 WNDCLASS wc;//定义变量 83 MSG Msg; 84 wc.cbClsExtra = 0; 85 wc.cbWndExtra = 0; 86 wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);;//设置窗口背景为白色 87 wc.hCursor = NULL;//窗口的光标不设置 88 wc.hIcon = NULL; 89 wc.hInstance = hInstance;//当前程序的句柄,hInstance是由主函数传递 90 wc.lpfnWndProc = WinProc;//窗口处理过程的窗口函数。 91 wc.lpszClassName =(LPSTR)cName;//窗口类的名字。 92 wc.lpszMenuName = NULL;//目录名,不设置 93 wc.style = CS_HREDRAW | CS_VREDRAW; //窗口类的风格 94 RegisterClass(&wc);//在系统中注册窗口 95 hWnd = CreateWindow(cName,TEXT("职工管理程序"),WS_OVERLAPPEDWINDOW,25, 25, 1300, 690,NULL, NULL, hInstance, NULL) ; 96 ShowWindow(hWnd,nShowCmd);//显示窗口 97 UpdateWindow(hWnd);//更新窗口 98 initWorker(); 99 while(GetMessage(&Msg,NULL,0,0)) 100 { 101 TranslateMessage(&Msg);//翻译消息 102 DispatchMessage(&Msg);//分派消息 103 } 104 return Msg.message;//程序结束后返回消息 105 } 106 LRESULT WINAPI WinProc(HWND hWnd,UINT Msg,WPARAM wParam,LPARAM lParam) 107 {//处理消息过程 108 static HWND btnHwnd; 109 static HWND inputHwnd[9]; 110 static HWND hwndbutton[12]; 111 switch(Msg)//对消息进行判断 112 { 113 case WM_PAINT: 114 hDC=BeginPaint(hWnd,&paint);//获取设备上下文 115 for(int i =0;i<10;i++) 116 { 117 rect[i].top = rectArray[i].top; 118 rect[i].right = rectArray[i].right; 119 rect[i].bottom = rectArray[i].bottom; 120 rect[i].left = rectArray[i].left; 121 DrawText(hDC,rectArray[i].rectName,-1,&rect[i],DT_SINGLELINE|DT_LEFT|DT_VCENTER); 122 } 123 EndPaint(hWnd,&paint); 124 return 0; 125 case WM_CREATE: //响应窗口的创建事件 126 //gg(((LPCREATESTRUCT) lParam) -> hInstance); 127 for(int i = 0;i<9;i++) 128 { 129 inputHwnd[i]=CreateWindow(TEXT("edit"),NULL,WS_CHILD |WS_VISIBLE|WS_BORDER|ES_LEFT|ES_MULTILINE,hwndArray[i].x1,hwndArray[i].x2,hwndArray[i].x3,hwndArray[i].x4,hWnd,(HMENU)hwndArray[i].menu_name,((LPCREATESTRUCT) lParam)->hInstance,NULL); 130 } 131 for(int i =0 ;i<12;i++) 132 { 133 hwndbutton[i]=CreateWindow(TEXT("BUTTON"),buttonArray[i].buttonName,WS_CHILD|WS_VISIBLE,buttonArray[i].x1,buttonArray[i].x2,buttonArray[i].x3,buttonArray[i].x4,hWnd,(HMENU)buttonArray[i].buttonNum,((LPCREATESTRUCT) lParam) -> hInstance,NULL); 134 } 135 return 0; 136 case WM_COMMAND://响应命令 137 { 138 switch(LOWORD(wParam)) 139 { 140 case ADD_BUTTON: 141 { 142 clearCache(); 143 addWorker(); 144 return 0; 145 } 146 case SEARCHNUM_BUTTON: 147 { 148 clearCache(); 149 searchWorker(); 150 return 0; 151 } 152 case MODIFY_BUTTON: 153 { 154 clearCache(); 155 modifyWorker(); 156 return 0; 157 } 158 case DELETE_BUTTON: 159 { 160 clearCache(); 161 deleteWorker(); 162 return 0; 163 } 164 case SHOWINFO_BUTTON: 165 { 166 clearCache(); 167 showAllInfo(); 168 return 0; 169 } 170 case SEARCHDEPART_BUTTON: 171 { 172 clearCache(); 173 searchDepartmentWorker(); 174 return 0; 175 } 176 case CLEARTEXT_BUTTON: 177 { 178 clearCache(); 179 clearText(); 180 return 0; 181 } 182 case COPYRIGHT_BUTTON: 183 { 184 MessageBox(NULL,TEXT("本程序归属CCSoft所有,并保留一切权力,任何\n个人或者单位必须经过CCSoft的个人授权、不限\n于书面形式。如有侵权行为,必当追究。"),TEXT("版权声明"),MB_OK); 185 return 0; 186 } 187 case EXIT_BUTTON: 188 { 189 PostQuitMessage(0);//退出消息队列 190 return 0;//返回0,结束函数 191 } 192 /* 扩展功能:保存在本地 */ 193 case SAVELOCALFILE_BUTTON: 194 { 195 clearCache(); 196 saveLocalFile(); 197 return 0; 198 } 199 /* 扩展功能:写在本地 */ 200 201 /**fun()[];*/ 202 case READLOCALFILE_BUTTON: 203 { 204 clearCache(); 205 readLocalFile(); 206 /* 用函数指针的方式 */ 207 /*fun()[3]*/ 208 return 0; 209 } 210 /* 扩展功能:简单的统计 */ 211 case STATISTICS_BUTTON: 212 { 213 clearCache(); 214 statisticsInfo(); 215 return 0; 216 } 217 } 218 return 0; 219 } 220 case WM_DESTROY://如果是点击关闭窗口时的消息 221 PostQuitMessage(0);//退出消息队列 222 return 0;//返回0,结束函数 223 } 224 //如果是其余的消息,调用默认消息处理函数,将消息该函数处理并将返回值返回 225 return DefWindowProc(hWnd,Msg,wParam,lParam); 226 } 227 void clearText() 228 { 229 230 SetDlgItemText(hWnd,NUM_INPUT,str_num); 231 SetDlgItemText(hWnd,NAME_INPUT,str_name); 232 SetDlgItemText(hWnd,DEPART_INPUT,str_depart); 233 SetDlgItemText(hWnd,JOBTITLE_INPUT,str_jobTitle); 234 SetDlgItemText(hWnd,YEAR_INPUT,str_year); 235 SetDlgItemText(hWnd,MONTH_INPUT,str_month); 236 SetDlgItemText(hWnd,DATE_INPUT,str_date); 237 SetDlgItemText(hWnd,WAGE_INPUT,str_wage); 238 SetDlgItemText(hWnd,BIGTEXTBOX,str_showAll); 239 } 240 void gg( HINSTANCE hInstance) 241 { 242 243 } 244 245 void clearCache() 246 { 247 ZeroMemory(str_num,sizeof(str_num)); 248 ZeroMemory(str_name,sizeof(str_name)); 249 ZeroMemory(str_depart,sizeof(str_depart)); 250 ZeroMemory(str_jobTitle,sizeof(str_jobTitle)); 251 ZeroMemory(str_year,sizeof(str_year)); 252 ZeroMemory(str_month,sizeof(str_month)); 253 ZeroMemory(str_date,sizeof(str_date)); 254 ZeroMemory(str_wage,sizeof(str_wage)); 255 ZeroMemory(str_showAll,sizeof(str_showAll)); 256 } 257 void addWorker() 258 { 259 char str_temp[50]; 260 ZeroMemory(str_temp,sizeof(str_temp)); 261 struct worker *newworker; 262 struct worker *assistp,*followp; 263 followp=headp; 264 assistp=headp->next; 265 newworker = new worker; 266 GetDlgItemText(hWnd,NUM_INPUT,str_num,sizeof(str_num)/sizeof(char)); 267 newworker->num = atol(str_num); 268 if(newworker->num == 0 || newworker->num<0) 269 { 270 MessageBox(NULL,TEXT("保存失败!输入职工号不合法!请检查输入信息"),TEXT("系统提示"),MB_OK); 271 delete newworker; 272 } 273 else 274 { 275 while(assistp!=NULL && assistp->num!=newworker->num) 276 { 277 assistp=assistp->next; 278 followp=followp->next; 279 } 280 if(assistp==NULL) 281 { 282 GetDlgItemText(hWnd,NAME_INPUT,newworker->name,sizeof(str_num)/sizeof(char)); 283 GetDlgItemText(hWnd,DEPART_INPUT,newworker->department,sizeof(str_num)/sizeof(char)); 284 GetDlgItemText(hWnd,JOBTITLE_INPUT,newworker->jobTitle,sizeof(str_num)/sizeof(char)); 285 GetDlgItemText(hWnd,YEAR_INPUT,str_year,sizeof(str_num)/sizeof(char)); 286 GetDlgItemText(hWnd,MONTH_INPUT,str_month,sizeof(str_num)/sizeof(char)); 287 GetDlgItemText(hWnd,DATE_INPUT,str_date,sizeof(str_num)/sizeof(char)); 288 GetDlgItemText(hWnd,WAGE_INPUT,str_wage,sizeof(str_num)/sizeof(char)); 289 newworker->wage=atoi(str_wage); 290 newworker->year=atoi(str_year); 291 newworker->month=atoi(str_month); 292 newworker->date=atoi(str_date); 293 newworker->next = NULL; 294 if(newworker->year<1972 || newworker->year>2017 || newworker->month<1 || newworker->month >12 || newworker->date <1 ||newworker->date>31) 295 { 296 MessageBox(NULL,TEXT("保存失败!您输入的日期不合法,请检查职工信息,并重试!"),("系统提示"),MB_OK); 297 delete newworker; 298 } 299 else if(newworker->wage<0 | newworker->wage > 1000000) 300 { 301 MessageBox(NULL,TEXT("保存失败!您输入的工资不合法,请检查职工信息,并重试!"),TEXT("系统提示"),MB_OK); 302 delete newworker; 303 } 304 else 305 { 306 /* 理清楚概念不要挂错了地方。 */ 307 followp->next=newworker;//将指向首节点的指针,向右移动 308 followp=followp->next; 309 MessageBox(NULL,TEXT("保存成功!"),TEXT("系统提示"),MB_OK); 310 } 311 } 312 else 313 { 314 strcat(str_temp,"保存失败!职工号为"); 315 ltoa(newworker->num,str_num,10); 316 strcat(str_temp,str_num); 317 strcat(str_temp,"的职工已存在,请勿重复保存!"); 318 MessageBox(NULL,str_temp,"系统提示",MB_OK); 319 delete newworker; 320 } 321 } 322 } 323 void showAllInfo() 324 { 325 int flag = 0; 326 worker *searchp; 327 searchp=headp->next; 328 if(searchp==NULL) 329 { 330 MessageBox(NULL,TEXT("请先输入职工信息!"),TEXT("系统提示"),MB_OK); 331 } 332 strcat(str_showAll,title); 333 while(searchp!=NULL) 334 { 335 flag = 1; 336 showFormat(searchp); 337 searchp=searchp->next; 338 } 339 if(flag==1) 340 { 341 SetDlgItemText(hWnd,10,str_showAll); 342 } 343 } 344 /* 函数名:showFormat(struct worker *p) 345 * 用来统一文本输出格式 346 为什么当初用那种显示方式 主要是因为简单,但是对于用户来说 可能影响了用户的使用体验。 347 */ 348 void showFormat(struct worker *p) 349 { 350 ltoa(p->num,str_num,10); 351 strcat(str_showAll,str_num); 352 /* 该判断 用来对齐 字符串 */ 353 if(strlen(str_num)<6) 354 { 355 //MessageBox(NULL,TEXT("测试判断"),TEXT("提示"),MB_OK); 356 strcat(str_showAll,"\t"); 357 } 358 strcat(str_showAll,"\t"); 359 strcat(str_showAll,p->name); 360 strcat(str_showAll,"\t\t"); 361 strcat(str_showAll,p->department); 362 if(strlen(p->department)<8) 363 { 364 strcat(str_showAll,"\t\t\t"); 365 } 366 else 367 { 368 strcat(str_showAll,"\t\t"); 369 } 370 //strcat(str_showAll,"\t"); 371 strcat(str_showAll,p->jobTitle); 372 strcat(str_showAll,"\t\t"); 373 ltoa(p->year,str_year,10); 374 strcat(str_showAll,str_year); 375 strcat(str_showAll,"年"); 376 ltoa(p->month,str_month,10); 377 strcat(str_showAll,str_month); 378 strcat(str_showAll,"月"); 379 ltoa(p->date,str_date,10); 380 strcat(str_showAll,str_date); 381 strcat(str_showAll,"日"); 382 strcat(str_showAll,"\t"); 383 ltoa(p->wage,str_wage,10); 384 strcat(str_showAll,str_wage); 385 strcat(str_showAll,"\r\n"); 386 strcat(str_showAll,"\r\n"); 387 } 388 void deleteWorker() 389 { 390 char str_temp[50]; 391 ZeroMemory(str_temp,sizeof(str_temp)); 392 long s; 393 worker *deletep,*shadowp; 394 shadowp=headp; 395 deletep=headp->next; 396 GetDlgItemText(hWnd,NUM_INPUT,str_num,sizeof(str_num)/sizeof(char)); 397 s=atol(str_num); 398 if(s==0 || s<0) 399 { 400 MessageBox(NULL,TEXT("请输入正确的职工号再删除!"),TEXT("系统提示"),MB_OK); 401 } 402 else 403 { 404 while(deletep!=NULL && deletep->num!=s) 405 { 406 deletep=deletep->next; 407 shadowp=shadowp->next; 408 } 409 if(deletep!=NULL) 410 { 411 if(deletep->next==NULL) 412 { 413 shadowp->next =NULL; 414 delete deletep;; 415 MessageBox(NULL,TEXT("删除成功"),TEXT("系统提示"),MB_OK); 416 } 417 else 418 { 419 shadowp->next=deletep->next; 420 delete deletep; 421 MessageBox(NULL,TEXT("删除成功"),TEXT("系统提示"),MB_OK); 422 } 423 } 424 else 425 { 426 strcat(str_temp,"职工号为:"); 427 strcat(str_temp,str_num); 428 strcat(str_temp,"的职工不存在,无法删除!"); 429 MessageBox(NULL,str_temp,TEXT("系统提示"),MB_OK); 430 } 431 } 432 } 433 void searchWorker() 434 { 435 long s; 436 char str_temp[50]; 437 ZeroMemory(str_temp,sizeof(str_temp)); 438 struct worker *assistp; 439 assistp=headp->next; 440 GetDlgItemText(hWnd,NUM_INPUT,str_num,sizeof(str_num)/sizeof(char)); 441 s=atol(str_num); 442 if(s==0 || s<0) 443 { 444 MessageBox(NULL,TEXT("请输入正确的职工号!"),TEXT("系统提示"),MB_OK); 445 } 446 else 447 { 448 /* long s = atol(str1); 直接用!=进行比较 没毛病 */ 449 450 while(assistp!=NULL && assistp->num!=s) 451 { 452 assistp=assistp->next; 453 } 454 if(assistp!=NULL) 455 { 456 strcat(str_showAll,title); 457 showFormat(assistp); 458 SetDlgItemText(hWnd,10,str_showAll); 459 } 460 else 461 { 462 strcat(str_temp,"职工号为:"); 463 strcat(str_temp,str_num); 464 strcat(str_temp,"的职工不存在!"); 465 MessageBox(NULL,str_temp,TEXT("系统提示"),MB_OK); 466 } 467 } 468 469 } 470 void modifyWorker() 471 { 472 struct worker *assistp; 473 assistp=headp->next; 474 char str_temp[50]; 475 long s; 476 GetDlgItemText(hWnd,NUM_INPUT,str_num,sizeof(str_num)/sizeof(char)); 477 s=atol(str_num); 478 if(s==0 || s<0) 479 { 480 MessageBox(NULL,TEXT("请输入正确的职工号!"),TEXT("系统提示"),MB_OK); 481 } 482 else 483 { 484 while(assistp!=NULL && assistp->num!=s) 485 { 486 assistp=assistp->next; 487 } 488 if(assistp!=NULL) 489 { 490 GetDlgItemText(hWnd,NAME_INPUT,assistp->name,sizeof(str_num)/sizeof(char)); 491 GetDlgItemText(hWnd,DEPART_INPUT,assistp->department,sizeof(str_num)/sizeof(char)); 492 GetDlgItemText(hWnd,JOBTITLE_INPUT,assistp->jobTitle,sizeof(str_num)/sizeof(char)); 493 //GetDlgItemText(hWnd,5,newworker->date,sizeof(str1)/sizeof(char)); 494 GetDlgItemText(hWnd,YEAR_INPUT,str_year,sizeof(str_num)/sizeof(char)); 495 GetDlgItemText(hWnd,WAGE_INPUT,str_wage,sizeof(str_num)/sizeof(char)); 496 GetDlgItemText(hWnd,MONTH_INPUT,str_month,sizeof(str_num)/sizeof(char)); 497 GetDlgItemText(hWnd,DATE_INPUT,str_date,sizeof(str_num)/sizeof(char)); 498 assistp->wage = atoi(str_wage); 499 assistp->year = atoi(str_month); 500 assistp->month = atoi(str_month); 501 assistp->date = atoi(str_date); 502 MessageBox(NULL,TEXT("修改成功!"),TEXT("系统提示"),MB_OK); 503 } 504 else 505 { 506 strcat(str_temp,"职工号为:"); 507 strcat(str_temp,str_num); 508 strcat(str_temp,"的职工不存在,无法修改其信息!"); 509 MessageBox(NULL,str_temp,TEXT("系统提示"),MB_OK); 510 } 511 } 512 assistp=headp->next; 513 } 514 515 void searchDepartmentWorker() 516 { 517 int flag= 0; 518 worker *searchp; 519 searchp=headp->next; 520 GetDlgItemText(hWnd,DEPART_INPUT,str_depart,sizeof(str_num)/sizeof(char)); 521 if(str_depart=="\0") 522 { 523 MessageBox(NULL,TEXT("请输入正确的部门!"),TEXT("系统提示"),MB_OK); 524 } 525 else 526 { 527 strcat(str_showAll,title); 528 while(searchp!=NULL) 529 { 530 if(strcmp((searchp->department),str_depart)==0) 531 { 532 flag =1; 533 showFormat(searchp); 534 /* 如果 和比对的 字符串 相等的话 , 向前移动一个在进行比较 */ 535 searchp=searchp->next; 536 537 } 538 else 539 { 540 /* 如果 和比对的 字符串 不相等的话 , 向前移动一个在进行比较 */ 541 /* 注意这个不能漏,必定出错。 */ 542 searchp=searchp->next; 543 } 544 } 545 SetDlgItemText(hWnd,10,str_showAll); 546 //searchp=headp->next; 547 if(flag == 0) 548 { 549 MessageBox(NULL,TEXT("没有找到该部门的职工!"),TEXT("系统提示"),MB_OK); 550 } 551 } 552 } 553 554 /* 扩展功能 */ 555 void saveLocalFile() 556 { 557 int flag = 0; 558 FILE *fp; 559 fp=fopen("C:\\workerDatabase.txt","w"); 560 if(fp==NULL) 561 { 562 MessageBox(NULL,TEXT("无法保存在本地!"),TEXT("系统警告"),MB_OK); 563 exit(0); 564 } 565 worker *searchp; 566 searchp=headp->next; 567 if(searchp==NULL) 568 { 569 MessageBox(NULL,TEXT("职工信息为空!请先录入职工信息!"),TEXT("系统警告"),MB_OK); 570 exit(0); 571 } 572 while(searchp!=NULL) 573 { 574 fprintf(fp,"%ld %s %s %s %d %d %d %ld\n",searchp->num,searchp->name,searchp->department,searchp->jobTitle,searchp->year,searchp->month,searchp->date,searchp->wage); 575 searchp=searchp->next; 576 flag = 1; 577 } 578 fclose(fp); 579 if(flag == 1) 580 { 581 MessageBox(NULL,TEXT("写入本地成功!"),TEXT("系统提示"),MB_OK); 582 } 583 else 584 { 585 MessageBox(NULL,TEXT("写入本地失败!"),TEXT("系统提示"),MB_OK); 586 } 587 } 588 void readLocalFile() 589 { 590 FILE *p; 591 int flag = 0; 592 struct worker *newworker;//用来保存读入的新职员 593 struct worker *followp;//用来移动指针 594 followp=headp; 595 worker *shadowp;//定义一个影子指针 用来指向 倒数第二个节点。因为从文本读入的时候最后一行是空行,要丢弃。 596 if((p=fopen("C:\\workerDatabase.txt","r"))==NULL) 597 { 598 MessageBox(NULL,TEXT("打开本地数据文件失败!"),TEXT("系统警告"),MB_OK); 599 exit(0); 600 } 601 while(!feof(p)) 602 { 603 newworker = (struct worker*)malloc(sizeof(struct worker )); 604 fscanf(p,"%ld %s %s %s %d %d %d %ld",&newworker->num,newworker->name,newworker->department,newworker->jobTitle,&newworker->year,&newworker->month,&newworker->date,&newworker->wage); 605 newworker->next = NULL; 606 followp->next = newworker; 607 shadowp = followp; 608 followp = newworker; 609 /* 在这里-842156451是用来标记一个未赋值的变量。利用这一点,来判断读取是否成功*/ 610 if(newworker->wage != -842150451) 611 { 612 flag = 1; 613 } 614 } 615 /* 删除最后一个节点*/ 616 delete newworker; 617 followp = shadowp; 618 shadowp->next = NULL; 619 if(flag == 1) 620 { 621 622 MessageBox(NULL,TEXT("从本地读取到程序成功!"),TEXT("系统提示"),MB_OK); 623 } 624 else 625 { 626 MessageBox(NULL,TEXT("从本地读取到程序失败"),TEXT("系统提示"),MB_OK); 627 } 628 629 fclose(p); 630 } 631 void statisticsInfo() 632 { 633 char temp1[30],temp2[30]; 634 int NumOfPeoCount=0; 635 double sumWage = 0; 636 double averageWage=0; 637 worker *searchp; 638 searchp=headp->next; 639 if(headp->next==NULL) 640 { 641 MessageBox(NULL,TEXT("请先输入职工信息!"),TEXT("系统提示"),MB_OK); 642 } 643 else 644 { 645 while(searchp!=NULL) 646 { 647 NumOfPeoCount++; 648 sumWage += searchp->wage; 649 searchp=searchp->next; 650 } 651 averageWage = sumWage/(double)NumOfPeoCount; 652 ltoa(NumOfPeoCount,temp1,10); 653 ltoa(averageWage,temp2,10); 654 strcat(str_showAll,"总计有:"); 655 strcat(str_showAll,temp1); 656 strcat(str_showAll,"人\t"); 657 strcat(str_showAll,"平均工资为:"); 658 strcat(str_showAll,temp2); 659 strcat(str_showAll,"\t\n"); 660 SetDlgItemText(hWnd,10,str_showAll); 661 } 662 }
resource.h头文件如下:
1 /* INPUT NUMBER */ 2 #define NUM_INPUT 1 3 #define NAME_INPUT 2 4 #define DEPART_INPUT 3 5 #define JOBTITLE_INPUT 4 6 #define YEAR_INPUT 5 7 #define WAGE_INPUT 6 8 #define MONTH_INPUT 7 9 #define DATE_INPUT 8 10 #define BIGTEXTBOX 10 11 /* BUTTON NUMBER */ 12 #define ADD_BUTTON 13 13 #define SEARCHNUM_BUTTON 14 14 #define MODIFY_BUTTON 15 15 #define DELETE_BUTTON 16 16 #define SHOWINFO_BUTTON 17 17 #define SEARCHDEPART_BUTTON 18 18 #define CLEARTEXT_BUTTON 19 19 #define COPYRIGHT_BUTTON 20 20 #define EXIT_BUTTON 21 21 #define SAVELOCALFILE_BUTTON 100 22 #define READLOCALFILE_BUTTON 101 23 #define STATISTICS_BUTTON 111
C语言课程设计 Win32应用程序