📄 m_02.cpp
字号:
putchar('*');
gotoxy(16,8);
for(x=0;x<k;++x)
putchar('*');
gotoxy(16,10);
printf("%s",userInfo[3]);
gotoxy(16,12);
printf("%s",userInfo[4]);
gotoxy(16,14);
printf("%s",userInfo[5]);
if(select==1)
TextArea(16,4,i,select,8,2,userInfo[0]);
else if(select==2)
TextArea(16,6,j,select,1,3,userInfo[1]);
else if(select==3)
TextArea(16,8,k,select,2,4,userInfo[2]);
else if(select==4)
TextArea(16,10,i1,select,3,5,userInfo[3]);
else if(select==5)
TextArea(16,12,j1,select,4,6,userInfo[4]);
else if(select==6)
TextArea(16,14,k1,select,5,7,userInfo[5]);
else if(select==7)
{
SetConsoleTextAttribute(hCon,31);
gotoxy(4,18);
printf(" 确认 ");
while((c=getch())!=9&&c!=13&&c!=224)
;
if(c==13)
{
if(RegOK(userInfo[0],userInfo[1],userInfo[2]))
{
FILE * fp; //写入文件
fp=fopen("user.data","ab");
stUser user;
strcpy(user.szName,userInfo[0]);
strcpy(user.szPW,userInfo[1]);
strcpy(user.szSex,userInfo[4]);
strcpy(user.szAge,userInfo[5]);
strcpy(user.szClass,userInfo[6]);
fwrite(&user,sizeof(stUser),1,fp);
fclose(fp);
break;
}
}
else if(c==224)
{
c=getch();
if(c==72||c==75)
select=6;
if(c==77||c==80)
select=8;
}
else
select=8;
}
else if(select==8)
{
SetConsoleTextAttribute(hCon,31);
gotoxy(20,18);
printf(" 取消 ");
while((c=getch())!=9&&c!=13&&c!=224)
;
if(c==13)
{
break;
}
else if(c==224)
{
c=getch();
if(c==72||c==75)
select=7;
if(c==77||c==80)
select=1;
}
else
select=1;
}
}
}
void LoginFrm()
{
SetConsoleTitle("登录窗口");
system("mode concols=41 lines=12 ");
system("color 30");
SetConsoleTextAttribute(hCon,112);
for(int i=1;i<11;++i)
{
gotoxy(4,i);
printf(" ");
}
gotoxy(10,3);
printf("用户名:");
gotoxy(10,5);
printf(" 密码:");
SetConsoleTextAttribute(hCon,128);
gotoxy(7,8);
printf(" 登录 ");
gotoxy(17,8);
printf(" 退出 ");
gotoxy(27,8);
printf(" 注册 ");
gotoxy(18,3);
SetConsoleTextAttribute(hCon,240);
printf(" ");
gotoxy(18,5);
printf(" ");
}
void ExitFrm2()
{
exit(0);
}
int CheckLogin(char *name,char *pw)
{
if(strlen(name)==0||strlen(pw)==0)
{
SetConsoleTitle("错误");
system("mode concols=41 lines=12 ");
system("color 5F");
printf("\n\n\n 出错啦,用户名和密码不能为空!!\n\n");
printf(" 请返回重新输入...");
getch();
return 0;
}
FILE *fp;
fp=fopen("user.data","rb");
if(fp!=NULL)
{
stUser user;
fread(&user,sizeof(stUser),1,fp);
while(!feof(fp))
{
if(strcmp(user.szName,name)==0&&strcmp(user.szPW,pw)==0)
{
SetConsoleTitle("欢迎");
system("mode concols=41 lines=12 ");
system("color 5F");
printf("\n\n\n 确认完毕,欢迎%s的到来!\n\n",name);
printf(" 程序载入中");
for(int i=0;i<4;++i)
{
Sleep(800);
printf(".");
}
fclose(fp);
return 1;
}
fread(&user,sizeof(stUser),1,fp);
}
fclose(fp);
}
SetConsoleTitle("错误");
system("mode concols=41 lines=12 ");
system("color 5F");
printf("\n\n\n 出错啦,用户名或密码输入错误!!\n\n");
printf(" 请返回重新输入...");
getch();
return 0;
}
void LoginInput()
{
static char userName[20]="";
char passWord[20]="";
int select=1,i=0,j=0,k=0,c;
while (1)
{
LoginFrm();
gotoxy(18,3);
printf("%s",userName);
gotoxy(18,5);
for(k=0;k<j;++k) //隐藏密码
putchar('*');
if(select==1) //输入焦点为1时
TextArea(18,3,i,select,5,2,userName);
else if(select==2)
TextArea(18,5,j,select,1,3,passWord);
else if(select==3)
{
SetConsoleTextAttribute(hCon,31);
gotoxy(7,8);
printf(" 登录 ");
while((c=getch())!=9&&c!=13&&c!=224)
;
if(c==13)
{
if(CheckLogin(userName,passWord))
{
LoginName=userName;
return;
}
}
else if(c==224)
{
c=getch();
if(c==72||c==75)
select=2;
if(c==77||c==80)
select=4;
}
else
select=4;
}
else if(select==4)
Button(17,8,3,5,select,"退出",ExitFrm2);
else if(select==5)
Button(27,8,4,1,select,"注册",RegInput);
}
}
int Button2(int x,int y,int pre,int next,int &select,char *title,char *time)
{
int c;
SetConsoleTextAttribute(hCon,31);
gotoxy(x,y);
printf(" ");
gotoxy(x,y);
printf("%2d.%-24s(%s)",y-4,title,time);
while((c=getch())!=9&&c!=13&&c!=224&&c!=27&&c!=0)
;
if(c==13)
{
return select;
}
else if(c==224)
{
c=getch();
if(c==72||c==75)
select=pre;
if(c==77||c==80)
select=next;
}
else if(c==27)
exit(0);
else if(c==0)
{
c=getch();
if(c==59)
{
memset(szInput,'\0',sizeof(szInput)); //新记录
topIndex=0;
return 0;
}
else if(c==60)
return -2;
else if(c==61)
return -3;
}
else
select=next;
return -1;
}
void AlterTitle(int select)
{
SetConsoleTextAttribute(hCon,92);
for(int i=8;i<16;++i)
{
gotoxy(5,i);
printf(" ");
}
gotoxy(7,9);
printf("原标题:%s",topNine[topIndex-select].szTitle);
gotoxy(7,11);
printf("新标题:");
gotoxy(7,12);
SetConsoleTextAttribute(hCon,240);
printf(" ");
gotoxy(7,12);
char title[30];
gets(title);
FILE *fp;
fp=fopen("record.data","rb+");
stRecord st1;
int size=sizeof(stRecord);
while(!feof(fp))
{
fread(&st1,size,1,fp);
if(strcmp(st1.szTime,topNine[topIndex-select].szTime)==0)
{
strcpy(topNine[topIndex-select].szTitle,title);
fseek(fp,-size,SEEK_CUR);
fwrite(&topNine[topIndex-select],size,1,fp);
break;
}
}
fclose(fp);
SetConsoleTextAttribute(hCon,94);
gotoxy(7,14);
printf(" 修改完成!! ");
getch();
}
void DeleteRecord(int select)
{
SetConsoleTextAttribute(hCon,92);
for(int i=8;i<15;++i)
{
gotoxy(5,i);
printf(" ");
}
gotoxy(7,9);
printf("警告:");
gotoxy(7,11);
printf("你确定把第%d条记录删除掉吗?(Y/N)",select);
int c;
while((c=getch())!='y'&&c!='Y'&&c!='n'&&c!='N'&&c!=27)
;
if(c=='n'||c=='N'||c==27)
return;
FILE *fp1,*fp2;
rename("record.data","temp.data");
fp1=fopen("temp.data","rb");
fp2=fopen("record.data","ab");
stRecord st1;
fread(&st1,sizeof(stRecord),1,fp1);
while(!feof(fp1))
{
if (strcmp(st1.szTime,topNine[topIndex-select].szTime)!=0)
fwrite(&st1,sizeof(stRecord),1,fp2);
fread(&st1,sizeof(stRecord),1,fp1);
}
fclose(fp1);
fclose(fp2);
remove("temp.data");
SetConsoleTextAttribute(hCon,94);
gotoxy(7,13);
printf(" 记录已删除!");
getch();
}
void personFrm()
{
char title[20];
strcpy(title,LoginName);
strcat(title,"的记事本");
SetConsoleTitle(title);
system("mode concols=58 lines=29 ");
system("COLOR 70");
SetConsoleTextAttribute(hCon,1920);
for(int i=5;i<25;++i)
{
gotoxy(5,i);
if(i-4<topIndex)
printf(" ");
else
printf(" 空 ");
}
int j;
for(i=topIndex-1,j=5;i>=0;--i,++j)
{
gotoxy(5,j);
printf("%2d.%-24s(%s)",j-4,topNine[i].szTitle,topNine[i].szTime);
}
SetConsoleTextAttribute(hCon,112);
gotoxy(5,1);
printf(" 欢迎%s的到来...你目前有%d条记录.",LoginName,topIndex);
gotoxy(5,4);
printf("最近的20条记录: 创建日期:");
SetConsoleTextAttribute(hCon,128);
gotoxy(0,27);
printf(" 新记录-F1 删除记录-F2 修改标题-F3 退出-Esc ");
}
int PerInput(int select)
{
int c;
int n;
while (1)
{
personFrm();
gotoxy(9,5);
if(topIndex==0)
{
while((c=getch())!=27&&c!=0)
;
if(c==27)
exit(0);
c=getch();
{
if(c==59)
return 0;
}
}
for(int i=1,j=topIndex;i<=topIndex;++i,--j)
{
if (select==i)
{
if((n=(Button2(5,i+4,i==1?topIndex:i-1,i==topIndex?1:i+1,select,topNine[j-1].szTitle,topNine[j-1].szTime)))==-1)
break;
if(n==0)return 0;
if(n==-2)
{
DeleteRecord(select);
return -select;
}
if(n==-3)
{
AlterTitle(select);
return -select;
}
return select;
}
}
}
}
int FindTop() //查询最前面的9条记录
{
FILE *fp;
fp=fopen("record.data","rb");
stRecord record;
if(fp==0)
return 0;
fread(&record,sizeof(stRecord),1,fp);
for(topIndex=0;!feof(fp)&&topIndex<20;)
{
if(strcmp(record.szWriter,LoginName)==0)
topNine[topIndex++]=record;
fread(&record,sizeof(stRecord),1,fp);
}
fclose(fp);
return topIndex;
}
int main(int argc, char* argv[])
{
int select=1;
LoginInput();
while (1)
{
FindTop();
if(select==0) //新建记录时复位
++select;
topSelect=PerInput(select);
if(topSelect>=0)
{
if(topSelect>0)
szText=topNine[topIndex-topSelect].szRecord; //反转输出
else
szText=szInput;
Input(topSelect);
select=topSelect; //保留选择的记录号
}
else
{
select=-topSelect;
if(select>=topIndex)
--select;
}
}
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -