|
10鱼币
本帖最后由 未来丶梦而已 于 2013-12-13 00:43 编辑
main.cpp#include<windows.h>
#include "Updata.h"
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM) ;
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{
static TCHAR szAppName[] = TEXT("SysMets1");
HWND hwnd;
MSG msg;
WNDCLASS wndclass;
wndclass.style = CS_HREDRAW | CS_VREDRAW;
wndclass.lpfnWndProc = WndProc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = hInstance;
wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
wndclass.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
wndclass.lpszMenuName = NULL;
wndclass.lpszClassName = szAppName;
if(!RegisterClass (&wndclass))
{
MessageBox(NULL, TEXT("This program requires Windows NT!"), szAppName, MB_ICONERROR);
return 0;
}
hwnd = CreateWindow(szAppName, TEXT("Get System Metrics No.1"),
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT,
NULL, NULL, hInstance, NULL);
ShowWindow(hwnd, iCmdShow);
UpdateWindow(hwnd);
while(GetMessage (&msg, NULL,0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
static int cxChar, cxCaps, cyChar,cyClient, iVscrollPos;
HDC hdc;
int i, y;
PAINTSTRUCT ps;
TEXTMETRIC tm;
Engieer engieerTemp;
Leader leaderTemp;
Chairman chairmanTemp;
switch(message)
{
case WM_CREATE:
hdc = GetDC(hwnd);
GetTextMetrics(hdc, &tm);
cxChar = tm.tmAveCharWidth;
cxCaps = (tm.tmPitchAndFamily& 1 ? 3 : 2)*cxChar /2;
cyChar = tm.tmHeight + tm.tmExternalLeading;
ReleaseDC (hwnd, hdc);
SetScrollRange(hwnd, SB_VERT, 0, count, FALSE);
SetScrollPos(hwnd, SB_VERT, iVscrollPos, TRUE);
return 0;
case WM_SIZE:
cyClient = HIWORD(lParam);
return 0;
case WM_VSCROLL:
switch(LOWORD (wParam))
{
case SB_LINEUP:
iVscrollPos -= 1;
break;
case SB_LINEDOWN:
iVscrollPos += 1;
break;
case SB_PAGEUP:
iVscrollPos -= cyClient / cyChar;
break;
case SB_PAGEDOWN:
iVscrollPos += cyClient / cyChar;
case SB_THUMBPOSITION:
iVscrollPos = HIWORD (wParam);
break;
default:
break;
}
iVscrollPos = max(0, min (iVscrollPos, count));
if(iVscrollPos != GetScrollPos (hwnd, SB_VERT))
{
SetScrollPos(hwnd, SB_VERT, iVscrollPos, TRUE);
InvalidateRect(hwnd, NULL, TRUE);
}
return 0;
case WM_PAINT:
hdc = BeginPaint (hwnd, &ps);
Init();
for( i = 0; i < count; i++)
{
y = cyChar * (i - iVscrollPos + 1);
int temp1, temp2;
int *ptr = allfind(i);
temp1 = *ptr++;
temp2 = *ptr;
if(temp1 == 1)
{
engieerTemp = EGet(temp2);
TextOut (hdc, 0, y,
engieerTemp.GetNum(),
lstrlen(engieerTemp.GetNum()));
TextOut (hdc, 10 * cxChar, y,
engieerTemp.GetName(),
lstrlen(engieerTemp.GetName()));
TextOut (hdc, 20 * cxChar , y,
engieerTemp.GetAge(),
lstrlen(engieerTemp.GetAge()));
}
else if(temp1 == 2)
{
leaderTemp = LGet(temp2);
TextOut (hdc, 0, y,
leaderTemp.GetNum(),
lstrlen(leaderTemp.GetNum())
);
}
else if(temp1 == 3)
{
chairmanTemp = CGet(temp2);
TextOut (hdc, 0, y,
chairmanTemp.GetNum(),
lstrlen(leaderTemp.GetNum())
);
}
}
EndPaint(hwnd, &ps);
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc (hwnd, message, wParam, lParam);
}
classbasic.h#include<iostream>
#include<fstream>
using namespace std;
class Staff //员工类
{
protected:
char No[10]; //Staff类中编号
char Name[12]; //Staff类中姓名
char Age[5]; //Staff类中年龄
public:
Staff()
{}
virtual ~Staff()
{}
const char *GetNum()const //返回编号
{
return No;
}
virtual void Input() = 0; //输入函数
virtual void Show()const = 0; //显示数据
virtual void Read(fstream &f)const = 0; //读文件
virtual void Write(fstream &f)const = 0; //写文件
};
class Engieer :virtual public Staff //员工类的派生类工程师类
{
protected:
char Profession[50]; //专业
char title[20]; //职称
public:
void Input()
{
cout<<"编号: "<<endl;
cin>>No;
cout<<"姓名: "<<endl;
cin>>Name;
cout<<"年龄: "<<endl;
cin>>Age;
cout<<"专业: "<<endl;
cin>>Profession;
cout<<"职称: "<<endl;
cin>>title;
}
void Show()const
{
cout<<"编号: "<<No<<endl;
cout<<"姓名: "<<Name<<endl;
cout<<"年龄: "<<Age<<endl;
cout<<"专业: "<<Profession<<endl;
cout<<"职称: "<<title<<endl;
}
void Read (fstream &f) const
{
f.read((char *)this, sizeof(Engieer));
}
void Write (fstream &f) const
{
f.write((char *) this, sizeof(Engieer));
}
};
class Leader :virtual public Staff //员工类的派生类领导类
{
protected:
char job[20]; //职务
char section[20]; //部门
public:
void Input() //输入函数
{
cout<<"编号: "<<endl;
cin>>No;
cout<<"姓名: "<<endl;
cin>>Name;
cout<<"年龄: "<<endl;
cin>>Age;
cout<<"职务: "<<endl;
cin>>job;
cout<<"部门: "<<endl;
cin>>section;
}
void Show()const //显示函数
{
cout<<"编号: "<<No<<endl;
cout<<"姓名: "<<Name<<endl;
cout<<"年龄: "<<Age<<endl;
cout<<"职务: "<<job<<endl;
cout<<"部门: "<<section<<endl;
}
void Read (fstream &f) const //读文件
{
f.read((char *)this, sizeof(Leader));
}
void Write (fstream &f) const //写文件
{
f.write((char *) this, sizeof(Leader));
}
};
class Chairman : public Engieer, public Leader
{
public:
void Input() //输入函数
{
cout<<"编号: "<<endl;
cin>>No;
cout<<"姓名: "<<endl;
cin>>Name;
cout<<"年龄: "<<endl;
cin>>Age;
cout<<"专业: "<<endl;
cin>>Profession;
cout<<"职称: "<<endl;
cin>>title;
cout<<"职务: "<<endl;
cin>>job;
cout<<"部门: "<<endl;
cin>>section;
}
void Show()const //显示函数
{
cout<<"编号: "<<No<<endl;
cout<<"姓名: "<<Name<<endl;
cout<<"年龄: "<<Age<<endl;
cout<<"专业: "<<Profession<<endl;
cout<<"职称: "<<title<<endl;
cout<<"职务: "<<job<<endl;
cout<<"部门: "<<section<<endl;
}
void Read (fstream &f) const //读文件
{
f.read((char *)this, sizeof(Chairman));
}
void Write (fstream &f) const // 写文件
{
f.write((char *) this, sizeof(Chairman));
}
};
Updata.h#include<iostream>
#include<fstream>
#define Index_menber 1000;
#include"classbasic.h"
using namespace std;
int counttemp = 0;
int count = 0;
fstream file;
struct IndexType
{
bool delTag; //删除标志
char num[8]; //编号
int positiong; //在数据文件中的相对位置
char staffType; //人员类型, e:工程师, 1:领导, c:主任工程师
};
class StaffManage
{
private:
IndexType *indexTable; //索引表
int maxSize; //索引表最大索引项目个数
public:
void AddIndexItem(const IndexType &e); //在索引中增加索引项
void AddData(); //增加数据
void SearchData(); //查询数据
void DeleteData(); //删除数据,只做删除标志
void Delete();
void Update();
void Adde();
void Addl();
void Addc();
void Searche(int j);
void Searchl(int p);
void Searchc(int q);
void Deletee(int i);
void Deletel(int p);
void Deletec(int q);
StaffManage();
~StaffManage();
};
StaffManage::StaffManage() //管理类构造函数
{
ifstream indexFile("Staff.idx", ios::binary); //建立输入索引文件
if(!indexFile.fail())
{
int i = 0;
indexFile.seekg(0, ios::end); //指向文件尾
count = indexFile.tellg() / sizeof(IndexType); //索引项个数
maxSize = count +Index_menber;
indexTable = new IndexType[maxSize];
indexFile.seekg(0, ios::beg);
indexFile.read(reinterpret_cast<char *>(&indexTable[i++]), sizeof(IndexType));
while(!indexFile.eof())
{
indexFile.read(reinterpret_cast<char *>(&indexTable[i++]), sizeof(IndexType));
}
indexFile.close();
}
else //对于本地没有索引文件。对索引文件的初始化
{
count = 0;
maxSize = count + Index_menber;
indexTable = new IndexType[maxSize];
}
ifstream iFile("staffdata.dat"); //检查是否存在数据文件
if(iFile.fail())
{
ofstream oFile("staffdata.dat"); //建立数据文件
if(oFile.fail())
{
cout<<"打开文件失败"<<endl;
oFile.close();
}
}
else
{
iFile.close();
}
file.open("staffdata.dat", ios::in | ios::out | ios::binary); //以读写二进制的方式打开数据文件。并hold住
if(file.fail())
{
cout<<"数据文件"<<endl;
}
}
StaffManage::~StaffManage() //析构函数,当用户正常退出时。将数据输入索引文件
{
ofstream indexFile("staff.idx",ios::binary);
for(int i = 0; i < count; i++)
{
indexFile.write(reinterpret_cast<char *> (&indexTable[i]), sizeof(IndexType));
}
indexFile.close();
file.close();
}
void StaffManage::AddIndexItem(const IndexType &e) //增加索引表函数
{
if( count >maxSize||count == maxSize)
{
maxSize = maxSize + Index_menber;
IndexType * tempIndeTable = new IndexType[maxSize];
for(int i = 0; i < count ; i++)
{
tempIndeTable[i] = indexTable[i];
}
delete []indexTable;
indexTable = tempIndeTable;
}
indexTable[count++] = e;
}
void StaffManage::AddData() //增加数据函数
{
char nice;
cout<<"人员类型(e:工程师,l:领导,c:主任工程师): ";
cin>>nice;
switch(nice)
{
case 'e':
case 'E':Adde();
break;
case 'l':
case 'L':Addl();
break;
case 'c':
case 'C':Addc();
break;
}
}
void StaffManage::Adde()
{
Engieer pStaff;
IndexType item;
item.staffType = 'e';
item.delTag = false;
file.seekg(0, ios::end);
pStaff.Input();
strcpy(item.num, pStaff.GetNum());
item.positiong = file.tellg();
AddIndexItem(item);
pStaff.Write(file);
}
void StaffManage::Addl()
{
Chairman pStaff;
IndexType item;
item.staffType = 'l';
item.delTag = false;
file.seekg(0, ios::end);
pStaff.Input();
strcpy(item.num, pStaff.GetNum());
item.positiong = file.tellg();
AddIndexItem(item);
pStaff.Write(file);
}
void StaffManage::Addc()
{
Leader pStaff;
IndexType item;
item.staffType = 'c';
item.delTag = false;
file.seekg(0, ios::end);
pStaff.Input();
strcpy(item.num, pStaff.GetNum());
item.positiong = file.tellg();
AddIndexItem(item);
pStaff.Write(file);
}
Engieer StaffManage::Searche(int j)
{
Engieer pStaff;
file.seekg(indexTable[j].positiong, ios::beg);
pStaff.Read(file);
return pStaff;
}
void StaffManage::Searchl(int p)
{
Leader pStaff;
file.seekg(indexTable[p].positiong, ios::beg);
pStaff.Read(file);
pStaff.Show();
}
void StaffManage::Searchc(int q)
{
Chairman pStaff;
file.seekg(indexTable[q].positiong, ios::beg);
pStaff.Read(file);
pStaff.Show();
}
void StaffManage::SearchData() //查找数据函数
{
IndexType itemTemp;
int j = 1;
cout<<"请输入编号: "<<endl;
cin>>itemTemp.num;
for(int i = 0; i < count; i++)
{
if( 0==strcmp(indexTable[i].num, itemTemp.num))
{
cout<<"找到该人!"<<endl;
j = 0;
if(indexTable[i].staffType == 'e')
{
Searche(i);
}
else if(indexTable[i].staffType=='l')
{
Searchl(i);
}
else if(indexTable[i].staffType == 'c')
{
Searchc(i);
}
break;
}
}
if(j)
{
cout<<"此人不存在."<<endl;
}
}
void StaffManage::Deletee(int i)
{
Engieer pStaff;
file.seekg(indexTable[i].positiong,ios::beg);
pStaff.Read(file);
fstream outFile("temp.dat",ios::app|ios::binary);
outFile.seekg(0, ios::end);
indexTable[counttemp++].positiong = outFile.tellg();
pStaff.Write(outFile);
outFile.close();
}
void StaffManage::Deletel(int p)
{
Leader pStaff;
file.seekg(indexTable[p].positiong,ios::beg);
pStaff.Read(file);
fstream outFile("temp.dat",ios::app|ios::binary);
outFile.seekg(0, ios::end);
indexTable[counttemp++].positiong = outFile.tellg();
pStaff.Write(outFile);
outFile.close();
}
void StaffManage::Deletec(int q)
{
Chairman pStaff;
file.seekg(indexTable[q].positiong,ios::beg);
pStaff.Read(file);
fstream outFile("temp.dat",ios::app|ios::binary);
outFile.seekg(0, ios::end);
indexTable[counttemp++].positiong = outFile.tellg();
pStaff.Write(outFile);
outFile.close();
}
void StaffManage::Delete() //删除函数
{
IndexType itemTemp;
int j = 1;
cout<<"请输入要删除的人员编号: "<<endl;
cin>>itemTemp.num;
for(int i = 0; i < count; i++)
{
if( 0==strcmp(indexTable[i].num, itemTemp.num))
{
cout<<"找到该人, 正在删除数据....."<<endl;
indexTable[i].delTag = true;
DeleteData();
j = 0;
break;
}
}
if(j)
{
cout<<"此人不存在."<<endl;
}
}
void StaffManage::DeleteData() //对数据的实际删除
{
ofstream oFile("temp.dat");
oFile.close();
int error = 0;
for(int i = 0; i < count; i++)
{
if( indexTable[i].delTag == false )
{
if(indexTable[i].staffType == 'e')
{
strcpy(indexTable[i-error].num, indexTable[i].num);
indexTable[i-error].delTag =false;
indexTable[i-error].staffType= 'e';
Deletee(i-1);
}
else if(indexTable[i].staffType=='l')
{
strcpy(indexTable[i-error].num, indexTable[i].num);
indexTable[i-error].delTag =false;
indexTable[i-error].staffType= 'l';
Deletel(i-1);
}
else if(indexTable[i].staffType == 'c')
{
strcpy(indexTable[i-error].num, indexTable[i].num);
indexTable[i-error].delTag =false;
indexTable[i-error].staffType= 'c';
Deletec(i-1);
}
}
else
{
error++;
}
}
count = counttemp;
counttemp = 0;
file.close();
remove("staffdata.dat");
rename("temp.dat","staffdata.dat");
file.open("staffdata.dat",ios::in|ios::out|ios::binary);
cout<<"删除成功."<<endl;
}
Engieer EGet(int pos)
{
Engieer pStaff;
file.seekg(pos, ios::beg);
pStaff.Read(file);
return pStaff;
}
Leader LGet(int pos)
{
Leader pStaff;
file.seekg(pos, ios::beg);
pStaff.Read(file);
return pStaff;
}
Chairman CGet(int pos)
{
Chairman pStaff;
fstream file1;
file1.open("staffdata.dat", ios::in | ios::binary);
file1.seekg(pos, ios::beg);
pStaff.Read(file1);
file1.close();
return pStaff;
}
int * allfind(int i)
{
static int j[2];
IndexType *indexTable;
ifstream indexFile1("Staff.idx", ios::binary);
if(!indexFile1.fail())
{
int p = 0;
int maxSize;
indexFile1.seekg(0, ios::end); //指向文件尾
maxSize = count +Index_menber;
indexTable = new IndexType[maxSize];
indexFile1.seekg(0, ios::beg);
indexFile1.read(reinterpret_cast<TCHAR *>(&indexTable[p++]), sizeof(IndexType));
while(!indexFile1.eof())
{
indexFile1.read(reinterpret_cast<TCHAR *>(&indexTable[p++]), sizeof(IndexType));
}
indexFile1.close();
}
if(indexTable[i].staffType == 'e')
{
j[0] = 1;
j[1] = indexTable[i].positiong;
}
else if(indexTable[i].staffType=='l')
{
j[0] = 2;
j[1] = indexTable[i].positiong;
}
else if(indexTable[i].staffType == 'c')
{
j[0] = 3;
j[1] = indexTable[i].positiong;
}
return j;
}
void Init()
{
StaffManage a;
}
本来是写控制台的。控制台的代码也放一起了。
我想知道为什么用控制台调用Searche函数读文件数据的时候没问题
我想做界面写的EGet函数读文件数据的时候总是读不到正确的数据。
困扰了我很久了。新手写得有的乱。
希望有大虾帮看看。谢谢了。
|
最佳答案
查看完整内容
我給你advice你, 不要post那麼多的code上來,because no one wants to read such a long code.
I suggest you 可以先自己分析,把核心比較少的部份提出來,比較efficient。
|