c语言实训 写了一个 带windows界面的 图书管理程序。但程序后边有问题,求指教 谢谢!
#include <windows.h>#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct book_msg{
char ISBN[10],book_name[22],publish[22],writer[22] ,year[4],mouth[2],day[2],price[6];//分别为书号,书名,出版社,作者,出版时间,价格
struct book_msg *next;
}BOOK;//定义链表节点的结构体类型取别名BOOK
HWND hWnd;
BOOK head;//定义头指针
BOOK *p,*tail;
BOOK b[10];
FILE *fp;//文件指针
FILE* file();//读写文件
void clean();//清屏
int no();
BOOK* findISBN();//查找ISBN对应的图书
char filename[22];
int n=0,i,j;
char ISBN[10];
char temp1[200],temp[200];
LRESULT WINAPI WinProc(HWND hWnd,UINT Msg,WPARAM wParam,LPARAM lParam);//函数声明
RECT rect;
RECT rect1;
HDC hDC;
PAINTSTRUCT paint;
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nShowCmd)
{
char *cName = "myWindow";//定义个字符指针表示窗口类的名字
WNDCLASS wc;//定义变量
MSG Msg;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);//设置窗口背景为白色
wc.hCursor = NULL;//窗口的光标不设置
wc.hIcon = NULL;//窗口图标设置
wc.hInstance = hInstance;//当前程序的句柄,hInstance是由主函数传递
wc.lpfnWndProc = WinProc;//窗口处理过程的窗口函数。
wc.lpszClassName =(LPSTR)cName;//窗口类的名字。
wc.lpszMenuName = NULL;//目录名,不设置
wc.style = CS_HREDRAW | CS_VREDRAW; //窗口类的风格
RegisterClass(&wc);//在系统中注册窗口
hWnd = CreateWindow(cName, TEXT("图书馆管理系统"),WS_OVERLAPPEDWINDOW, 300, 100, 800, 550, NULL, NULL, hInstance, NULL) ;
ShowWindow(hWnd,nShowCmd);//显示窗口
UpdateWindow(hWnd);//更新窗口
while(GetMessage(&Msg,NULL,0,0))
{
TranslateMessage(&Msg);//翻译消息
DispatchMessage(&Msg);//分派消息
}
return Msg.message;//程序结束后返回消息
}
LRESULT WINAPI WinProc(HWND hWnd,UINT Msg,WPARAM wParam,LPARAM lParam)
{//处理消息过程
static HWND hwndbutton;
static HWND hwndbutton1;
static HWND inputHwnd ;
switch(Msg)//对消息进行判断
{
case WM_PAINT:
hDC=BeginPaint(hWnd,&paint);//获取设备上下文
rect.left=40;//"文件路径"文本框坐标的定义
rect.top=10;
rect.right=120;
rect.bottom=70;
rect1.left=30;//"书籍信息"文本框定义
rect1.top=50;
rect1.right=700;
rect1.bottom=90;
//在窗口中输出文本
DrawText(hDC,"文件路径: ",-1,&rect,DT_SINGLELINE|DT_CENTER|DT_VCENTER);
DrawText(hDC,"ISBN: 书名: 作者: 出版社: 出版日期: 价格:",-1,&rect1,DT_SINGLELINE|DT_CENTER|DT_VCENTER);
EndPaint(hWnd,&paint);
return 0;
case WM_CREATE:
//创建按钮和文本框
inputHwnd = CreateWindow( TEXT("edit"), NULL,WS_CHILD | WS_VISIBLE | WS_BORDER |ES_LEFT | ES_MULTILINE ,120, 30, 300, 20,
hWnd, (HMENU)88, ((LPCREATESTRUCT) lParam) -> hInstance,NULL ) ;//文件路径框
hwndbutton=CreateWindow(TEXT("BUTTON"),TEXT("导入"),WS_CHILD|WS_VISIBLE,400,450,100,20,
hWnd,(HMENU)1,((LPCREATESTRUCT) lParam) -> hInstance,NULL);
hwndbutton=CreateWindow(TEXT("BUTTON"),TEXT("保存"),WS_CHILD|WS_VISIBLE,520,450,100,20,
hWnd,(HMENU)2,((LPCREATESTRUCT) lParam) -> hInstance,NULL);
hwndbutton=CreateWindow(TEXT("BUTTON"),TEXT("1.新建图书信息"),WS_CHILD|WS_VISIBLE,50,400,120,20,
hWnd,(HMENU)0,((LPCREATESTRUCT) lParam) -> hInstance,NULL);
hwndbutton=CreateWindow(TEXT("BUTTON"),TEXT("2.ISBN查询图书"),WS_CHILD|WS_VISIBLE,190,400,120,20,
hWnd,(HMENU)3,((LPCREATESTRUCT) lParam) -> hInstance,NULL);
hwndbutton=CreateWindow(TEXT("BUTTON"),TEXT("3.作者作品查询"),WS_CHILD|WS_VISIBLE,330,400,120,20,
hWnd,(HMENU)4,((LPCREATESTRUCT) lParam) -> hInstance,NULL);
hwndbutton=CreateWindow(TEXT("BUTTON"),TEXT("4.ISBN修改图书"),WS_CHILD|WS_VISIBLE,470,400,120,20,
hWnd,(HMENU)5,((LPCREATESTRUCT) lParam) -> hInstance,NULL);
hwndbutton=CreateWindow(TEXT("BUTTON"),TEXT("5.删除图书"),WS_CHILD|WS_VISIBLE,610,400,120,20,
hWnd,(HMENU)6,((LPCREATESTRUCT) lParam) -> hInstance,NULL);
hwndbutton=CreateWindow(TEXT("BUTTON"),TEXT("退出"),WS_CHILD|WS_VISIBLE,640,450,100,20,
hWnd,(HMENU)7,((LPCREATESTRUCT) lParam) -> hInstance,NULL);
inputHwnd = CreateWindow( TEXT("edit"), NULL,WS_CHILD | WS_VISIBLE | WS_BORDER |ES_LEFT | ES_MULTILINE , 45, 85, 100, 30, hWnd, (HMENU)9
, ((LPCREATESTRUCT) lParam) -> hInstance,NULL );//ISBN框
inputHwnd = CreateWindow( TEXT("edit"), NULL,WS_CHILD | WS_VISIBLE | WS_BORDER |ES_LEFT | ES_MULTILINE , 165, 85, 100, 30, hWnd, (HMENU)11,
((LPCREATESTRUCT) lParam) -> hInstance,NULL ) ;//书名框
inputHwnd = CreateWindow( TEXT("edit"), NULL,WS_CHILD | WS_VISIBLE | WS_BORDER |ES_LEFT | ES_MULTILINE , 285, 85, 100, 30, hWnd, (HMENU)12,
((LPCREATESTRUCT) lParam) -> hInstance,NULL ) ;//作者框
inputHwnd = CreateWindow( TEXT("edit"), NULL,WS_CHILD | WS_VISIBLE | WS_BORDER |ES_LEFT | ES_MULTILINE , 405, 85, 100, 30, hWnd, (HMENU)13,
((LPCREATESTRUCT) lParam) -> hInstance,NULL ) ;//出版社框
inputHwnd = CreateWindow( TEXT("edit"), "年",WS_CHILD | WS_VISIBLE | WS_BORDER |ES_LEFT | ES_MULTILINE , 525, 85, 40, 30, hWnd, (HMENU)14,
((LPCREATESTRUCT) lParam) -> hInstance,NULL ) ;//出版日期年框
inputHwnd = CreateWindow( TEXT("edit"), "月",WS_CHILD | WS_VISIBLE | WS_BORDER |ES_LEFT | ES_MULTILINE , 570, 85, 24, 30, hWnd, (HMENU)20,
((LPCREATESTRUCT) lParam) -> hInstance,NULL ) ;//出版日期月框
inputHwnd = CreateWindow( TEXT("edit"), "日",WS_CHILD | WS_VISIBLE | WS_BORDER |ES_LEFT | ES_MULTILINE , 595, 85, 24, 30, hWnd, (HMENU)21,
((LPCREATESTRUCT) lParam) -> hInstance,NULL ) ;//出版日期日框
inputHwnd = CreateWindow( TEXT("edit"), NULL,WS_CHILD | WS_VISIBLE | WS_BORDER |ES_LEFT | ES_MULTILINE , 645, 85, 100, 30, hWnd, (HMENU)15,
((LPCREATESTRUCT) lParam) -> hInstance,NULL ) ;//价格框
inputHwnd = CreateWindow( TEXT("edit"), NULL,WS_CHILD | WS_VISIBLE | WS_BORDER |ES_LEFT | ES_MULTILINE , 45, 130, 700, 200,
hWnd, (HMENU)10, ((LPCREATESTRUCT) lParam) -> hInstance,NULL ) ;//查询结果信息框
inputHwnd = CreateWindow( TEXT("edit"), NULL,WS_CHILD | WS_VISIBLE |ES_CENTER | ES_MULTILINE , 100, 350,600, 30,
hWnd, (HMENU)77, ((LPCREATESTRUCT) lParam) -> hInstance,NULL ) ;//提示框
return 0 ;
case WM_COMMAND:
{
switch(LOWORD(wParam)){
case 0://初始化链表
head.next=NULL;
tail=&head;
SetDlgItemText(hWnd,77,"请在对应的信息框中输入信息,再点击“导入”");
break;
case 1://将图书信息导入到链表中
p=(BOOK *)malloc(sizeof(BOOK));
GetDlgItemText(hWnd,9,p->ISBN,sizeof(p->ISBN)/sizeof(char));
GetDlgItemText(hWnd,11,p->book_name,sizeof(p->book_name)/sizeof(char));
GetDlgItemText(hWnd,12,p->writer,sizeof(p->writer)/sizeof(char));
GetDlgItemText(hWnd,13,p->publish,sizeof(p->publish)/sizeof(char));
GetDlgItemText(hWnd,14,p->year,sizeof(p->year)/sizeof(char));
GetDlgItemText(hWnd,20,p->mouth,sizeof(p->mouth)/sizeof(char));
GetDlgItemText(hWnd,21,p->day,sizeof(p->day)/sizeof(char));
GetDlgItemText(hWnd,15,p->price,sizeof(p->price)/sizeof(char));
tail->next =p;
p->next =NULL;
tail=p;
SetDlgItemText(hWnd,77,"导入成功");
clean();
break;
case 2://将链表数据保存到文件中
p=head.next;
GetDlgItemText(hWnd,88,filename,sizeof(filename)/sizeof(char));
file();
while(p!=NULL){
fprintf(fp,"%s %s %s %s %s %s\n",p->ISBN,p->book_name,p->writer,p->publish,p->year,p->mouth,p->day,p->price);
p=p->next;
}
fclose(fp);
MessageBox(NULL,TEXT("已成功保存到文件"),TEXT("提示"),MB_OK);
clean();
break;
case 3: //搜索ISBN,从文件中查找并显示图书数据
file();
findISBN();
sprintf(temp,"ISBN: %s 书名:《%s》 作者:%s 出版社:%s 出版日期: %s年%s月%s日 价格:%s元 ",
p->ISBN,p->book_name,p->writer,p->publish,p->year,p->mouth,p->day,p->price);
SetDlgItemText(hWnd,10,temp);
fclose(fp);
break;
case 4://从文件中搜索作者所有图书
char writer[12];
GetDlgItemText(hWnd,12,writer,sizeof(writer)/sizeof(char));
file();
ZeroMemory(temp,sizeof(temp));
p=head.next;
while(p!=NULL){
if(strcmp(p->writer,writer)==0)
{
sprintf(temp1,"ISBN:%s 书名:《%s》 作者:%s 出版社:%s 出版日期: %s年%s月%s日 价格:%s元 \r\n",
p->ISBN,p->book_name,p->writer,p->publish,p->year,p->mouth,p->day,p->price);
strcat(temp,temp1);
}
p=p->next;
}
SetDlgItemText(hWnd,10,temp);
fclose(fp);
clean();
break;
case 5://修改所找的图书信息
file();
findISBN();
GetDlgItemText(hWnd,9,p->ISBN,sizeof(p->ISBN)/sizeof(char));
GetDlgItemText(hWnd,11,p->book_name,sizeof(p->book_name)/sizeof(char));
GetDlgItemText(hWnd,13,p->publish,sizeof(p->publish)/sizeof(char));
GetDlgItemText(hWnd,12,p->writer,sizeof(writer)/sizeof(char));
GetDlgItemText(hWnd,14,p->year,sizeof(p->year)/sizeof(char));
GetDlgItemText(hWnd,20,p->mouth,sizeof(p->mouth)/sizeof(char));
GetDlgItemText(hWnd,21,p->day,sizeof(p->day)/sizeof(char));
GetDlgItemText(hWnd,15,p->price,sizeof(p->price)/sizeof(char));
MessageBox(NULL,TEXT("修改成功"),TEXT("提示"),MB_OK);
clean();
break;
case 6://删除所找的图书
BOOK *pre;
n=1;
pre=&head;
GetDlgItemText(hWnd,9,ISBN,sizeof(ISBN)/sizeof(char));
GetDlgItemText(hWnd,88,filename,sizeof(filename)/sizeof(char));
file();
while(!feof(fp)){
p=(BOOK *)malloc(sizeof(BOOK));
i=fscanf(fp,"%s %s %s %s %s %s\n",p->ISBN,p->book_name,p->writer,p->publish,p->year,p->mouth,p->day,p->price);
if(i<6)break;
}
p=head.next;
while(p!=NULL){
if(strcmp(p->ISBN,ISBN)==0)//显示查询的书籍的信息
break;
p=p->next;
n++;
}
pre=&head;
for(i=1;i<=n-1;i++){
pre=pre->next;
}
p=pre->next ;
fflush(stdin);
pre->next=p->next;
free(p);
fclose(fp);
clean();
MessageBox(NULL,TEXT("删除成功"),TEXT("提示"),MB_OK);
break;
case 7:
exit(0);
}
return 0;
}
case WM_DESTROY://如果是点击关闭窗口时的消息
PostQuitMessage(0);//退出消息队列
return 0;//返回0,结束函数
}
//如果是其余的消息,调用默认消息处理函数,将消息该函数处理并将返回值返回
return DefWindowProc(hWnd,Msg,wParam,lParam);
}
void clean(){//刷新窗口
SetDlgItemText(hWnd,9,"");
SetDlgItemText(hWnd,11,"");
SetDlgItemText(hWnd,12,"");
SetDlgItemText(hWnd,13,"");
SetDlgItemText(hWnd,14,"年");
SetDlgItemText(hWnd,15,"");
SetDlgItemText(hWnd,20,"");
SetDlgItemText(hWnd,21,"月");
SetDlgItemText(hWnd,77,"日");
}
BOOK* findISBN(){//定位要查询的ISBN,并返回p的地址]
int i;int n=0;
GetDlgItemText(hWnd,9,ISBN,sizeof(ISBN)/sizeof(char));
GetDlgItemText(hWnd,88,filename,sizeof(filename)/sizeof(char));
while(!feof(fp)){
p=(BOOK *)malloc(sizeof(BOOK));
i=fscanf(fp,"%s%s%s%s%s%s",p->ISBN,p->book_name,p->writer,p->publish,p->year,p->mouth,p->day,p->price);
if(i<6)break;
}
p=head.next;
while(p!=NULL){
if(strcmp(p->ISBN,ISBN)==0)
break;
else
p=p->next;
}
if(p==NULL){
MessageBox(NULL,TEXT("不存在此图书!请重新输入"),TEXT("错误"),MB_OK);
return 0;
}
return p;
}
FILE* file(){// 读写文件
GetDlgItemText(hWnd,88,filename,sizeof(filename)/sizeof(char));
if((fp=fopen(filename,"r+"))==NULL){
MessageBox(NULL,TEXT("文件打开失败"),TEXT("错误"),MB_OK);
return 0;
}
else
return fp;//返回文件的地址
}