📄 mpbjq.c
字号:
#include <stdio.h>
#include <stdlib.h>
#include<conio.h>
#include<string.h>
#include<dos.h>
#include<malloc.h>
FILE *fp;
int n;
char name1[20],name2[20];
#define NOBORDER 0
#define SINGLELINE 1
#define DOUBLELINE 2
#define CLOSE 0
#define OPEN 1
#define NORMAL 0
#define ANTI 1
/*-------------------------------------------------窗口函数------------------------------------------------------*/
typedef struct
{
int x;
int y;
int width;
int height;
int backcolor;
int textcolor;
int border;
int usex;
int usey;
int usewidth;
int useheight;
int status;
void *buf;
}VIEW;
void View_Init(VIEW*view,int x,int y,int width,int height,int backcolor,int textcolor,int border)
{
view->x=x;
view->y=y;
view->width=width;
view->height=height;
view->backcolor=backcolor;
view->textcolor=textcolor;
view->border=border;
if(border==NOBORDER)
{
view->usex=0;
view->usey=0;
view->usewidth=width;
view->useheight=height;
}
else
{
view->usex=1;
view->usey=1;
view->usewidth=width-2;
view->useheight=height-2;
}
view->status=CLOSE;
view->buf=NULL;
}
int View_Open(VIEW*view)
{
if(view->status==OPEN)
return 1;
view->buf=malloc(view->width*view->height*2);
if(!view->buf)
return 0;
gettext(view->x,view->y,view->x+view->width-1,view->y+view->height-1,view->buf);
view->status=OPEN;
textattr((view->backcolor<<4)+view->textcolor);
if(view->border!=NOBORDER)
{
int i;
char l,r,t,b,lt,rt,lb,rb;
switch(view->border)
{
case SINGLELINE:
l=179;r=179;t=196;b=196;
lt=218;rt=191;lb=192;rb=217;
break;
case DOUBLELINE:
l=186;r=186;t=205;b=205;
lt=201;rt=187;lb=200;rb=188;
break;
}
window(1,1,80,25);
gotoxy(view->x,view->y);
putch(lt);
for(i=0;i<view->width-2;i++)
putch(t);
putch(rt);
gotoxy(view->x,view->y+view->height-1);
putch(lb);
for(i=0;i<view->width-2;i++)
putch(b);
putch(rb);
for(i=0;i<view->height-2;i++)
{
gotoxy(view->x,view->y+i+1);
putch(l);
gotoxy(view->x+view->width-1,view->y+i+1);
putch(r);
}
}
window(view->x+view->usex,view->y+view->usey,view->x+view->usex+view->usewidth-1,view->y+view->usey+view->useheight-1);
clrscr();
return 1;
}
void View_Close(VIEW*view)
{
if(view->status==CLOSE)
return;
puttext(view->x,view->y,view->x+view->width-1,view->y+view->height-1,view->buf);
free(view->buf);
view->status=CLOSE;
}
void View_Clear(VIEW*view)
{
int x0,y0;
char attr;
x0=view->x+view->usex;
y0=view->y+view->usey;
window(x0,y0,x0+view->usewidth-1,y0+view->useheight-1);
attr=(view->backcolor<<4)+view->textcolor;
textattr(attr);
clrscr();
}
View_PutString(VIEW*view,int x,int y,char*string,int anti)
{
int x0,y0;
char attr;
x0=view->x+view->usex;
y0=view->y+view->usey;
window(x0,y0,x0+view->usewidth-1,y0+view->useheight-1);
gotoxy(x+1,y+1);
attr=(view->backcolor<<4)+view->textcolor;
if(anti==ANTI)
attr=(~attr)&0x7f;
textattr(attr);
cputs(string);
}
/*---------------------------------------------------菜单函数-------------------------------------------------------*/
typedef struct
{
VIEW view;
int xnum;
int ynum;
char **items;
int curx;
int cury;
int itemlen;
}MENU;
void Menu_Init(MENU*menu,int x,int y,int xnum,int ynum,char**items,int backcolor,int textcolor,int border)
{
int width,height;
int i,num=xnum*ynum;
/*calculate the item lenght*/
menu->itemlen=0;
for(i=0;i<num;i++)
{
int len=strlen(items[i]);
if(menu->itemlen<len)
menu->itemlen=len;
}
menu->itemlen+=2;
/*calculate the item width*/
if(border==NOBORDER)
{
width=menu->itemlen*xnum;
height=ynum;
}
else
{
width=menu->itemlen*xnum+2;
height=ynum+2;
}
/*initial the window*/
View_Init(&menu->view,x,y,width,height,backcolor,textcolor,border);
menu->xnum=xnum;
menu->ynum=ynum;
menu->items=items;
menu->cury=0;
menu->curx=0;
}
void Menu_ShowCurltem(MENU*menu,int anti)
{
View_PutString(&menu->view,menu->curx*menu->itemlen+1,menu->cury,menu->items[menu->cury*menu->xnum+menu->curx],anti);
}
int Menu_Open(MENU*menu)
{
int i,j;
if(menu->view.status==OPEN)
return 1;
if(!View_Open(&menu->view))
return 0;
for(j=0;j<menu->ynum;j++)
for(i=0;i<menu->xnum;i++)
View_PutString(&menu->view,i*menu->itemlen+1,j,menu->items[j*menu->xnum+i],NORMAL);
Menu_ShowCurltem(menu,ANTI);
return 1;
}
void Menu_Close(MENU*menu)
{
View_Close(&menu->view);
}
int Menu_Drive(MENU*menu)
{
if(menu->view.status==CLOSE)
Menu_Open(menu);
while(1)
{
switch(getch())
{
case 72:
Menu_ShowCurltem(menu,0);
menu->cury--;
if(menu->cury<0)
menu->cury=menu->ynum-1;
Menu_ShowCurltem(menu,1);
break;
case 80:
Menu_ShowCurltem(menu,0);
menu->cury++;
if(menu->cury>=menu->ynum)
menu->cury=0;
Menu_ShowCurltem(menu,1);
break;
case 75:
Menu_ShowCurltem(menu,0);
menu->curx--;
if(menu->curx<0)
menu->curx=menu->xnum-1;
Menu_ShowCurltem(menu,1);
break;
case 77:
Menu_ShowCurltem(menu,0);
menu->curx++;
if(menu->curx>=menu->xnum)
menu->curx=0;
Menu_ShowCurltem(menu,1);
break;
case '\r': /* enter*/
return menu->cury*menu->xnum+menu->curx;
case 0x1b: /*ESC*/
return -1;
}
}
}
/*-----------------------------------------------操作函数----------------------------------------------------*/
MENU menu;
VIEW view_out,view_in;
struct addr
{ char post_num[10]; /*邮编*/
char addr[40]; /*家庭地址*/
};
struct birth
{ char year[5]; /*年份*/
char month[3]; /*月份*/
char day[3]; /*日期*/
};
struct data
{ int number; /*序号*/
char name[20]; /*姓名*/
char sex[2]; /*性别*/
struct birth birth; /*出生年月*/
struct addr addr; /*通信地址*/
char telephone[13]; /*联系电话*/
struct data *next,*pre;
}*std_data,*p1,*p3,*p2,*p4,*p5,*p6,*p7,*p8,*p9,*head,*end;
void data_load()/*读取记录函数*/
{
int j,k;
fp=fopen("mpbjq.txt","rb+t");
head=NULL;
if(fp!=NULL)
{
n=0;
/*读取数据,建立链表*/
for(j=1;fgetc(fp)!=EOF;j++)
{
k=j-1;
fseek(fp,k*sizeof(struct data),0);
p1=(struct data*)malloc(sizeof(struct data));
fread(p1,sizeof(struct data),1,fp);
if(head==NULL)
{
head=p1;
head->pre=NULL;
head->next=NULL;
end=head;
}
else
{
end->next=p1;
p1->pre=end;
end=p1;
}
n++;
end->next=NULL;
}
}
else fp=fopen("mpbjq.txt","wb");
}
void data_showhead()/*从第一条显示记录函数*/
{
int ch1;
p2=head;
if(p2==NULL)
{
View_PutString(&view_in,0,0,"There is no record.",NORMAL);
View_PutString(&view_in,0,1,"Press any key to return to menu...",NORMAL);
getch();
}
while(p2!=NULL)
{
View_Clear(&view_out);
View_PutString(&view_out,0,0,"No:",NORMAL);
gotoxy(0,4);printf("%d",p2->number);
View_PutString(&view_out,0,2,"Name:",NORMAL);
View_PutString(&view_out,5,2,p2->name,NORMAL);
View_PutString(&view_out,0,3,"Sex:",NORMAL);
View_PutString(&view_out,4,3,p2->sex,NORMAL);
View_PutString(&view_out,0,4,"Birthday(y/m/d):",NORMAL);
View_PutString(&view_out,16,4,p2->birth.year,NORMAL);
View_PutString(&view_out,21,4,"/",NORMAL);
View_PutString(&view_out,22,4,p2->birth.month,NORMAL);
View_PutString(&view_out,25,4,"/",NORMAL);
View_PutString(&view_out,26,4,p2->birth.day,NORMAL);
View_PutString(&view_out,0,5,"Postnumber:",NORMAL);
View_PutString(&view_out,11,5,p2->addr.post_num,NORMAL);
View_PutString(&view_out,0,6,"Address:",NORMAL);
View_PutString(&view_out,8,6,p2->addr.addr,NORMAL);
View_PutString(&view_out,0,7,"Tele:",NORMAL);
View_PutString(&view_out,5,7,p2->telephone,NORMAL);
View_PutString(&view_in,0,0,"Press (Page UP/Page Down) to continue...",NORMAL);
View_PutString(&view_in,0,1,"Press (Esc) to return to menu...",NORMAL);
loop:
ch1=bioskey(0);
switch(ch1)
{
case 20736:/*Page Down向下翻页*/
p2=p2->next;
if(p2==NULL)
{
View_Clear(&view_in);
View_PutString(&view_in,0,0,"End of the note.",NORMAL);
View_PutString(&view_in,0,1,"Press any key to return to menu...",NORMAL);
getch();
}
break;
case 18688:/*Page Up向上翻页*/
p2=p2->pre;
if(p2==NULL)
{
View_Clear(&view_in);
View_PutString(&view_in,0,0,"Head of the note.",NORMAL);
View_PutString(&view_in,0,1,"Press any key to return to menu...",NORMAL);
getch();
}
break;
case 283:/*Esc回主选单*/
return;
default:
{
View_Clear(&view_in);
View_PutString(&view_in,0,0,"Press (Page UP/Page Down) to continue...",NORMAL);
View_PutString(&view_in,0,1,"Press (Esc) to return to menu...",NORMAL);
goto loop;
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -