鱼C论坛

 找回密码
 立即注册
查看: 2400|回复: 2

c++,实训作业求解.

[复制链接]
发表于 2013-12-13 00:39:21 | 显示全部楼层 |阅读模式
10鱼币
本帖最后由 未来丶梦而已 于 2013-12-13 00:43 编辑

main.cpp
  1. #include<windows.h>
  2. #include "Updata.h"

  3. LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM) ;

  4. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
  5. PSTR szCmdLine, int iCmdShow)

  6. {
  7. static TCHAR szAppName[] = TEXT("SysMets1");
  8. HWND hwnd;
  9. MSG msg;
  10. WNDCLASS wndclass;

  11. wndclass.style = CS_HREDRAW | CS_VREDRAW;
  12. wndclass.lpfnWndProc = WndProc;
  13. wndclass.cbClsExtra = 0;
  14. wndclass.cbWndExtra = 0;
  15. wndclass.hInstance = hInstance;
  16. wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  17. wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
  18. wndclass.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
  19. wndclass.lpszMenuName = NULL;
  20. wndclass.lpszClassName = szAppName;

  21. if(!RegisterClass (&wndclass))
  22. {
  23. MessageBox(NULL, TEXT("This program requires Windows NT!"), szAppName, MB_ICONERROR);

  24. return 0;

  25. }

  26. hwnd = CreateWindow(szAppName, TEXT("Get System Metrics No.1"),
  27. WS_OVERLAPPEDWINDOW,
  28. CW_USEDEFAULT, CW_USEDEFAULT,
  29. CW_USEDEFAULT, CW_USEDEFAULT,
  30. NULL, NULL, hInstance, NULL);

  31. ShowWindow(hwnd, iCmdShow);
  32. UpdateWindow(hwnd);

  33. while(GetMessage (&msg, NULL,0, 0))
  34. {
  35. TranslateMessage(&msg);
  36. DispatchMessage(&msg);
  37. }
  38. return msg.wParam;
  39. }

  40. LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
  41. {
  42. static int cxChar, cxCaps, cyChar,cyClient, iVscrollPos;
  43. HDC hdc;
  44. int i, y;
  45. PAINTSTRUCT ps;
  46. TEXTMETRIC tm;
  47. Engieer engieerTemp;
  48. Leader leaderTemp;
  49. Chairman chairmanTemp;


  50. switch(message)
  51. {
  52. case WM_CREATE:
  53. hdc = GetDC(hwnd);

  54. GetTextMetrics(hdc, &tm);
  55. cxChar = tm.tmAveCharWidth;
  56. cxCaps = (tm.tmPitchAndFamily& 1 ? 3 : 2)*cxChar /2;
  57. cyChar = tm.tmHeight + tm.tmExternalLeading;
  58. ReleaseDC (hwnd, hdc);

  59. SetScrollRange(hwnd, SB_VERT, 0, count, FALSE);
  60. SetScrollPos(hwnd, SB_VERT, iVscrollPos, TRUE);

  61. return 0;

  62. case WM_SIZE:
  63. cyClient = HIWORD(lParam);
  64. return 0;

  65. case WM_VSCROLL:
  66. switch(LOWORD (wParam))
  67. {
  68. case SB_LINEUP:
  69. iVscrollPos -= 1;
  70. break;
  71. case SB_LINEDOWN:
  72. iVscrollPos += 1;
  73. break;
  74. case SB_PAGEUP:
  75. iVscrollPos -= cyClient / cyChar;
  76. break;
  77. case SB_PAGEDOWN:
  78. iVscrollPos += cyClient / cyChar;
  79. case SB_THUMBPOSITION:
  80. iVscrollPos = HIWORD (wParam);
  81. break;
  82. default:
  83. break;
  84. }

  85. iVscrollPos = max(0, min (iVscrollPos, count));

  86. if(iVscrollPos != GetScrollPos (hwnd, SB_VERT))
  87. {
  88. SetScrollPos(hwnd, SB_VERT, iVscrollPos, TRUE);
  89. InvalidateRect(hwnd, NULL, TRUE);
  90. }
  91. return 0;
  92. case WM_PAINT:
  93. hdc = BeginPaint (hwnd, &ps);
  94. Init();
  95. for( i = 0; i < count; i++)
  96. {
  97. y = cyChar * (i - iVscrollPos + 1);
  98. int temp1, temp2;
  99. int *ptr = allfind(i);
  100. temp1 = *ptr++;
  101. temp2 = *ptr;
  102. if(temp1 == 1)
  103. {
  104. engieerTemp = EGet(temp2);

  105. TextOut (hdc, 0, y,
  106. engieerTemp.GetNum(),
  107. lstrlen(engieerTemp.GetNum()));

  108. TextOut (hdc, 10 * cxChar, y,
  109. engieerTemp.GetName(),
  110. lstrlen(engieerTemp.GetName()));

  111. TextOut (hdc, 20 * cxChar , y,
  112. engieerTemp.GetAge(),
  113. lstrlen(engieerTemp.GetAge()));

  114. }
  115. else if(temp1 == 2)
  116. {
  117. leaderTemp = LGet(temp2);
  118. TextOut (hdc, 0, y,
  119. leaderTemp.GetNum(),
  120. lstrlen(leaderTemp.GetNum())
  121. );
  122. }
  123. else if(temp1 == 3)
  124. {
  125. chairmanTemp = CGet(temp2);
  126. TextOut (hdc, 0, y,
  127. chairmanTemp.GetNum(),
  128. lstrlen(leaderTemp.GetNum())
  129. );
  130. }

  131. }
  132. EndPaint(hwnd, &ps);
  133. return 0;


  134. case WM_DESTROY:
  135. PostQuitMessage(0);
  136. return 0;
  137. }
  138. return DefWindowProc (hwnd, message, wParam, lParam);
  139. }


复制代码
classbasic.h
  1. #include<iostream>
  2. #include<fstream>
  3. using namespace std;

  4. class Staff //员工类
  5. {
  6. protected:
  7. char No[10]; //Staff类中编号
  8. char Name[12]; //Staff类中姓名
  9. char Age[5]; //Staff类中年龄
  10. public:
  11. Staff()
  12. {}
  13. virtual ~Staff()
  14. {}
  15. const char *GetNum()const //返回编号
  16. {
  17. return No;
  18. }
  19. virtual void Input() = 0; //输入函数
  20. virtual void Show()const = 0; //显示数据
  21. virtual void Read(fstream &f)const = 0; //读文件
  22. virtual void Write(fstream &f)const = 0; //写文件
  23. };

  24. class Engieer :virtual public Staff //员工类的派生类工程师类
  25. {
  26. protected:
  27. char Profession[50]; //专业
  28. char title[20]; //职称
  29. public:
  30. void Input()
  31. {
  32. cout<<"编号: "<<endl;
  33. cin>>No;

  34. cout<<"姓名: "<<endl;
  35. cin>>Name;

  36. cout<<"年龄: "<<endl;
  37. cin>>Age;

  38. cout<<"专业: "<<endl;
  39. cin>>Profession;

  40. cout<<"职称: "<<endl;
  41. cin>>title;
  42. }
  43. void Show()const
  44. {
  45. cout<<"编号: "<<No<<endl;
  46. cout<<"姓名: "<<Name<<endl;
  47. cout<<"年龄: "<<Age<<endl;
  48. cout<<"专业: "<<Profession<<endl;
  49. cout<<"职称: "<<title<<endl;
  50. }
  51. void Read (fstream &f) const
  52. {
  53. f.read((char *)this, sizeof(Engieer));
  54. }
  55. void Write (fstream &f) const
  56. {
  57. f.write((char *) this, sizeof(Engieer));
  58. }

  59. };

  60. class Leader :virtual public Staff //员工类的派生类领导类
  61. {
  62. protected:
  63. char job[20]; //职务
  64. char section[20]; //部门
  65. public:
  66. void Input() //输入函数
  67. {
  68. cout<<"编号: "<<endl;
  69. cin>>No;

  70. cout<<"姓名: "<<endl;
  71. cin>>Name;

  72. cout<<"年龄: "<<endl;
  73. cin>>Age;

  74. cout<<"职务: "<<endl;
  75. cin>>job;

  76. cout<<"部门: "<<endl;
  77. cin>>section;

  78. }

  79. void Show()const //显示函数
  80. {
  81. cout<<"编号: "<<No<<endl;
  82. cout<<"姓名: "<<Name<<endl;
  83. cout<<"年龄: "<<Age<<endl;
  84. cout<<"职务: "<<job<<endl;
  85. cout<<"部门: "<<section<<endl;
  86. }

  87. void Read (fstream &f) const //读文件
  88. {
  89. f.read((char *)this, sizeof(Leader));
  90. }
  91. void Write (fstream &f) const //写文件
  92. {
  93. f.write((char *) this, sizeof(Leader));
  94. }

  95. };

  96. class Chairman : public Engieer, public Leader
  97. {
  98. public:
  99. void Input() //输入函数
  100. {

  101. cout<<"编号: "<<endl;
  102. cin>>No;

  103. cout<<"姓名: "<<endl;
  104. cin>>Name;

  105. cout<<"年龄: "<<endl;
  106. cin>>Age;

  107. cout<<"专业: "<<endl;
  108. cin>>Profession;

  109. cout<<"职称: "<<endl;
  110. cin>>title;

  111. cout<<"职务: "<<endl;
  112. cin>>job;

  113. cout<<"部门: "<<endl;
  114. cin>>section;

  115. }
  116. void Show()const //显示函数
  117. {
  118. cout<<"编号: "<<No<<endl;
  119. cout<<"姓名: "<<Name<<endl;
  120. cout<<"年龄: "<<Age<<endl;
  121. cout<<"专业: "<<Profession<<endl;
  122. cout<<"职称: "<<title<<endl;
  123. cout<<"职务: "<<job<<endl;
  124. cout<<"部门: "<<section<<endl;
  125. }

  126. void Read (fstream &f) const //读文件
  127. {
  128. f.read((char *)this, sizeof(Chairman));
  129. }
  130. void Write (fstream &f) const // 写文件
  131. {
  132. f.write((char *) this, sizeof(Chairman));
  133. }

  134. };
复制代码

Updata.h
  1. #include<iostream>
  2. #include<fstream>
  3. #define Index_menber 1000;
  4. #include"classbasic.h"
  5. using namespace std;
  6. int counttemp = 0;
  7. int count = 0;
  8. fstream file;
  9. struct IndexType
  10. {
  11. bool delTag; //删除标志
  12. char num[8]; //编号
  13. int positiong; //在数据文件中的相对位置
  14. char staffType; //人员类型, e:工程师, 1:领导, c:主任工程师
  15. };

  16. class StaffManage
  17. {
  18. private:
  19. IndexType *indexTable; //索引表
  20. int maxSize; //索引表最大索引项目个数
  21. public:
  22. void AddIndexItem(const IndexType &e); //在索引中增加索引项
  23. void AddData(); //增加数据
  24. void SearchData(); //查询数据
  25. void DeleteData(); //删除数据,只做删除标志
  26. void Delete();
  27. void Update();
  28. void Adde();
  29. void Addl();
  30. void Addc();
  31. void Searche(int j);
  32. void Searchl(int p);
  33. void Searchc(int q);
  34. void Deletee(int i);
  35. void Deletel(int p);
  36. void Deletec(int q);
  37. StaffManage();
  38. ~StaffManage();
  39. };


  40. StaffManage::StaffManage() //管理类构造函数
  41. {
  42. ifstream indexFile("Staff.idx", ios::binary); //建立输入索引文件
  43. if(!indexFile.fail())
  44. {
  45. int i = 0;
  46. indexFile.seekg(0, ios::end); //指向文件尾
  47. count = indexFile.tellg() / sizeof(IndexType); //索引项个数
  48. maxSize = count +Index_menber;
  49. indexTable = new IndexType[maxSize];
  50. indexFile.seekg(0, ios::beg);

  51. indexFile.read(reinterpret_cast<char *>(&indexTable[i++]), sizeof(IndexType));
  52. while(!indexFile.eof())
  53. {
  54. indexFile.read(reinterpret_cast<char *>(&indexTable[i++]), sizeof(IndexType));
  55. }
  56. indexFile.close();

  57. }
  58. else //对于本地没有索引文件。对索引文件的初始化
  59. {
  60. count = 0;
  61. maxSize = count + Index_menber;
  62. indexTable = new IndexType[maxSize];
  63. }

  64. ifstream iFile("staffdata.dat"); //检查是否存在数据文件
  65. if(iFile.fail())
  66. {
  67. ofstream oFile("staffdata.dat"); //建立数据文件
  68. if(oFile.fail())
  69. {
  70. cout<<"打开文件失败"<<endl;
  71. oFile.close();
  72. }
  73. }
  74. else
  75. {
  76. iFile.close();
  77. }
  78. file.open("staffdata.dat", ios::in | ios::out | ios::binary); //以读写二进制的方式打开数据文件。并hold住
  79. if(file.fail())
  80. {
  81. cout<<"数据文件"<<endl;
  82. }
  83. }
  84. StaffManage::~StaffManage() //析构函数,当用户正常退出时。将数据输入索引文件
  85. {
  86. ofstream indexFile("staff.idx",ios::binary);
  87. for(int i = 0; i < count; i++)
  88. {
  89. indexFile.write(reinterpret_cast<char *> (&indexTable[i]), sizeof(IndexType));
  90. }
  91. indexFile.close();
  92. file.close();
  93. }

  94. void StaffManage::AddIndexItem(const IndexType &e) //增加索引表函数
  95. {

  96. if( count >maxSize||count == maxSize)
  97. {
  98. maxSize = maxSize + Index_menber;
  99. IndexType * tempIndeTable = new IndexType[maxSize];
  100. for(int i = 0; i < count ; i++)
  101. {
  102. tempIndeTable[i] = indexTable[i];

  103. }
  104. delete []indexTable;
  105. indexTable = tempIndeTable;

  106. }
  107. indexTable[count++] = e;
  108. }

  109. void StaffManage::AddData() //增加数据函数
  110. {
  111. char nice;
  112. cout<<"人员类型(e:工程师,l:领导,c:主任工程师): ";
  113. cin>>nice;
  114. switch(nice)
  115. {
  116. case 'e':
  117. case 'E':Adde();
  118. break;
  119. case 'l':
  120. case 'L':Addl();
  121. break;
  122. case 'c':
  123. case 'C':Addc();
  124. break;
  125. }

  126. }

  127. void StaffManage::Adde()
  128. {
  129. Engieer pStaff;
  130. IndexType item;
  131. item.staffType = 'e';
  132. item.delTag = false;
  133. file.seekg(0, ios::end);
  134. pStaff.Input();
  135. strcpy(item.num, pStaff.GetNum());
  136. item.positiong = file.tellg();
  137. AddIndexItem(item);
  138. pStaff.Write(file);
  139. }

  140. void StaffManage::Addl()
  141. {
  142. Chairman pStaff;
  143. IndexType item;
  144. item.staffType = 'l';
  145. item.delTag = false;
  146. file.seekg(0, ios::end);
  147. pStaff.Input();
  148. strcpy(item.num, pStaff.GetNum());
  149. item.positiong = file.tellg();
  150. AddIndexItem(item);
  151. pStaff.Write(file);
  152. }

  153. void StaffManage::Addc()
  154. {
  155. Leader pStaff;
  156. IndexType item;
  157. item.staffType = 'c';
  158. item.delTag = false;
  159. file.seekg(0, ios::end);
  160. pStaff.Input();
  161. strcpy(item.num, pStaff.GetNum());
  162. item.positiong = file.tellg();
  163. AddIndexItem(item);
  164. pStaff.Write(file);
  165. }

  166. Engieer StaffManage::Searche(int j)
  167. {
  168. Engieer pStaff;
  169. file.seekg(indexTable[j].positiong, ios::beg);
  170. pStaff.Read(file);
  171. return pStaff;
  172. }
  173. void StaffManage::Searchl(int p)
  174. {
  175. Leader pStaff;
  176. file.seekg(indexTable[p].positiong, ios::beg);
  177. pStaff.Read(file);
  178. pStaff.Show();
  179. }
  180. void StaffManage::Searchc(int q)
  181. {
  182. Chairman pStaff;
  183. file.seekg(indexTable[q].positiong, ios::beg);
  184. pStaff.Read(file);
  185. pStaff.Show();
  186. }
  187. void StaffManage::SearchData() //查找数据函数
  188. {
  189. IndexType itemTemp;
  190. int j = 1;
  191. cout<<"请输入编号: "<<endl;
  192. cin>>itemTemp.num;
  193. for(int i = 0; i < count; i++)
  194. {
  195. if( 0==strcmp(indexTable[i].num, itemTemp.num))
  196. {
  197. cout<<"找到该人!"<<endl;
  198. j = 0;
  199. if(indexTable[i].staffType == 'e')
  200. {
  201. Searche(i);
  202. }
  203. else if(indexTable[i].staffType=='l')
  204. {
  205. Searchl(i);
  206. }
  207. else if(indexTable[i].staffType == 'c')
  208. {
  209. Searchc(i);
  210. }

  211. break;
  212. }
  213. }
  214. if(j)
  215. {
  216. cout<<"此人不存在."<<endl;
  217. }
  218. }


  219. void StaffManage::Deletee(int i)
  220. {
  221. Engieer pStaff;
  222. file.seekg(indexTable[i].positiong,ios::beg);
  223. pStaff.Read(file);
  224. fstream outFile("temp.dat",ios::app|ios::binary);
  225. outFile.seekg(0, ios::end);
  226. indexTable[counttemp++].positiong = outFile.tellg();
  227. pStaff.Write(outFile);
  228. outFile.close();
  229. }
  230. void StaffManage::Deletel(int p)
  231. {
  232. Leader pStaff;
  233. file.seekg(indexTable[p].positiong,ios::beg);
  234. pStaff.Read(file);
  235. fstream outFile("temp.dat",ios::app|ios::binary);
  236. outFile.seekg(0, ios::end);
  237. indexTable[counttemp++].positiong = outFile.tellg();
  238. pStaff.Write(outFile);
  239. outFile.close();
  240. }

  241. void StaffManage::Deletec(int q)
  242. {
  243. Chairman pStaff;
  244. file.seekg(indexTable[q].positiong,ios::beg);
  245. pStaff.Read(file);
  246. fstream outFile("temp.dat",ios::app|ios::binary);
  247. outFile.seekg(0, ios::end);
  248. indexTable[counttemp++].positiong = outFile.tellg();
  249. pStaff.Write(outFile);
  250. outFile.close();
  251. }
  252. void StaffManage::Delete() //删除函数
  253. {
  254. IndexType itemTemp;
  255. int j = 1;
  256. cout<<"请输入要删除的人员编号: "<<endl;
  257. cin>>itemTemp.num;
  258. for(int i = 0; i < count; i++)
  259. {
  260. if( 0==strcmp(indexTable[i].num, itemTemp.num))
  261. {
  262. cout<<"找到该人, 正在删除数据....."<<endl;
  263. indexTable[i].delTag = true;
  264. DeleteData();
  265. j = 0;
  266. break;
  267. }
  268. }
  269. if(j)
  270. {
  271. cout<<"此人不存在."<<endl;
  272. }

  273. }

  274. void StaffManage::DeleteData() //对数据的实际删除
  275. {
  276. ofstream oFile("temp.dat");
  277. oFile.close();
  278. int error = 0;
  279. for(int i = 0; i < count; i++)
  280. {
  281. if( indexTable[i].delTag == false )
  282. {

  283. if(indexTable[i].staffType == 'e')
  284. {
  285. strcpy(indexTable[i-error].num, indexTable[i].num);
  286. indexTable[i-error].delTag =false;
  287. indexTable[i-error].staffType= 'e';
  288. Deletee(i-1);
  289. }
  290. else if(indexTable[i].staffType=='l')
  291. {
  292. strcpy(indexTable[i-error].num, indexTable[i].num);
  293. indexTable[i-error].delTag =false;
  294. indexTable[i-error].staffType= 'l';
  295. Deletel(i-1);
  296. }
  297. else if(indexTable[i].staffType == 'c')
  298. {
  299. strcpy(indexTable[i-error].num, indexTable[i].num);
  300. indexTable[i-error].delTag =false;
  301. indexTable[i-error].staffType= 'c';
  302. Deletec(i-1);
  303. }
  304. }
  305. else
  306. {
  307. error++;
  308. }
  309. }
  310. count = counttemp;
  311. counttemp = 0;
  312. file.close();
  313. remove("staffdata.dat");
  314. rename("temp.dat","staffdata.dat");
  315. file.open("staffdata.dat",ios::in|ios::out|ios::binary);
  316. cout<<"删除成功."<<endl;
  317. }



  318. Engieer EGet(int pos)
  319. {
  320. Engieer pStaff;
  321. file.seekg(pos, ios::beg);
  322. pStaff.Read(file);
  323. return pStaff;
  324. }
  325. Leader LGet(int pos)
  326. {
  327. Leader pStaff;
  328. file.seekg(pos, ios::beg);
  329. pStaff.Read(file);
  330. return pStaff;
  331. }
  332. Chairman CGet(int pos)
  333. {
  334. Chairman pStaff;
  335. fstream file1;
  336. file1.open("staffdata.dat", ios::in | ios::binary);
  337. file1.seekg(pos, ios::beg);
  338. pStaff.Read(file1);
  339. file1.close();
  340. return pStaff;
  341. }
  342. int * allfind(int i)
  343. {
  344. static int j[2];
  345. IndexType *indexTable;
  346. ifstream indexFile1("Staff.idx", ios::binary);
  347. if(!indexFile1.fail())
  348. {
  349. int p = 0;
  350. int maxSize;
  351. indexFile1.seekg(0, ios::end); //指向文件尾
  352. maxSize = count +Index_menber;
  353. indexTable = new IndexType[maxSize];
  354. indexFile1.seekg(0, ios::beg);
  355. indexFile1.read(reinterpret_cast<TCHAR *>(&indexTable[p++]), sizeof(IndexType));
  356. while(!indexFile1.eof())
  357. {
  358. indexFile1.read(reinterpret_cast<TCHAR *>(&indexTable[p++]), sizeof(IndexType));
  359. }
  360. indexFile1.close();
  361. }

  362. if(indexTable[i].staffType == 'e')
  363. {
  364. j[0] = 1;
  365. j[1] = indexTable[i].positiong;

  366. }
  367. else if(indexTable[i].staffType=='l')
  368. {
  369. j[0] = 2;
  370. j[1] = indexTable[i].positiong;
  371. }
  372. else if(indexTable[i].staffType == 'c')
  373. {
  374. j[0] = 3;
  375. j[1] = indexTable[i].positiong;
  376. }
  377. return j;
  378. }

  379. void Init()
  380. {
  381. StaffManage a;
  382. }
复制代码
本来是写控制台的。控制台的代码也放一起了。
我想知道为什么用控制台调用Searche函数读文件数据的时候没问题
我想做界面写的EGet函数读文件数据的时候总是读不到正确的数据。
困扰了我很久了。新手写得有的乱。
希望有大虾帮看看。谢谢了。


最佳答案

查看完整内容

我給你advice你, 不要post那麼多的code上來,because no one wants to read such a long code. I suggest you 可以先自己分析,把核心比較少的部份提出來,比較efficient。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2013-12-13 00:39:22 | 显示全部楼层
我給你advice你, 不要post那麼多的code上來,because no one wants to read such a long code.
I suggest you 可以先自己分析,把核心比較少的部份提出來,比較efficient。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2013-12-17 03:14:30 | 显示全部楼层
file.seekg(indexTable[j].positiong, ios::beg);
pStaff.Read(file);
|
file干吗传参带入还写成全局的,C++风格,封装文件描述符企不更好,
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-5-12 23:23

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表