大家看看,我找了很久还是不知道怎么回事?
#include <windows.h> #include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <conio.h>
#define m 1000
#define SIZE 3
#define R 1
HANDLE hConsole;
struct yonghu { /*个人注册信息*/
char name[10];
char sex[4];
char age[4];
char cla[14];
char Tel[14];
char qins[5];
}student;
struct deng_lu { /*个人登录信息*/
char zhang_hao[20];
char mi_ma[6];
}yong_hu, stud;
void gotoxy(int x, int y)
{
COORD coord;
coord.X = x;
coord.Y = y;
SetConsoleCursorPosition(hConsole, coord);
}
struct stud1ent_type
{
char name[10];
char sex[4];
char age[4];
char cla[14];
char Tel[14];
char qins[5];
}stud1[SIZE]={{"T1","男","19","ASD","0735","225"},{"T2","男","20","ASD","0735","227"},{"T3","男","20","ASD","0735","227"}};
void shan_chu() /*删除界面*/
{
FILE *fp,*ip;
char newname[10];
long n1,n2, n3;
int n4,n5;
void gong_neng_qu();
system("cls");
printf("********************************************************************************\n\n\n");
printf("\t\t\t 删除界面\t\t\t\n\n");
printf("\t\t\t|-----------------------------------|\n");
printf("\t\t\t| |\n");
printf("\t\t\t| |\n");
printf("\t\t\t| |\n");
printf("\t\t\t| 请输入要删除的姓名: |\n");
printf("\t\t\t| |\n");
printf("\t\t\t| Tips: |\n");
printf("\t\t\t| 一旦删除,就不能恢复! |\n");
printf("\t\t\t| |\n");
printf("\t\t\t|-----------------------------------|\n");
gotoxy(46, 16);
if((fp = fopen("student2.txt","rb")) == NULL) {
printf("不能打开这个文件\n");
return;
}
if((ip = fopen("student4.txt","wb")) == NULL) {
printf("不能打开这个文件\n");
return;
}
scanf("%s",newname);
n1 = ftell(fp);
fseek(fp, 0L, SEEK_END);
n2 = ftell(fp);
fseek(fp, n1, SEEK_SET);
n3 = n2/sizeof(student);
n4 = 0;
for(n5=0;n5<n3;n5++) {
fread((char*)&student,sizeof(student),1,fp);
if((strcmp(student.name, newname))== 0) {
student.Tel[14]='1';
n4=1;
}
fwrite((char *)&student,sizeof(student),1,ip);
}
fclose(fp);
fclose(ip);
if(n4==0) {
unlink("student4.txt");
printf("无记录,按任意键进入功能区!\n");
getch();
gong_neng_qu();
}
fp=fopen("student2.txt","wb");
ip=fopen("student4.txt","rb");
n1 = ftell(ip);
fseek(ip, 0L, SEEK_END);
n2 = ftell(ip);
fseek(ip, n1, SEEK_SET);
n3 = n2/sizeof(student);
for(n5=0;n5<n3;n5++) {
fread((char*)&student,sizeof(student),1,ip);
if(student.Tel == 0) {
fwrite((char *)&student,sizeof(student),1,fp);
}
}
fclose(fp);
fclose(ip);
printf("已经删除");
unlink("student4.txt");
getch();
gong_neng_qu();
}
//**********************************************************************************************************
void biaoti()
{
printf("\t $=================================================$\n");
printf("\t <=$======> 欢迎进入宿舍管理系统---查询功能! <======$=>\n");
printf("\t $=================================================$\n\n");
}
int cha_xun()
{
int f1(),f2(),f3(),cc;
void gong_neng_qu(),tui_chu();
char ch;int flag;
system("cls");
cc=0;
biaoti();
printf("======> 查询 主菜单如下:\n\n");
pos1:
printf("\t $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $\n");
printf("\t $ $ $ $\n");
printf("\t $ 1.单一条件查询 2.复合条件查询 $\n");
printf("\t $ $\n");
printf("\t $ 3.Run a game via Nintendo DS Emulator! $\n");
printf("\t $ $\n");
printf("\t $ 9.返回上一级程序 0.退出 $\n");
printf("\t $ $ $ $\n");
printf("\t $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $\n");
printf("\n\n======> 请选择:");
if (cc==0) {getchar();cc++;}
ch=getchar();
getchar();
if (ch=='1') {system("cls");biaoti();flag=f1();goto pos1;}
else if (ch=='2') {system("cls");biaoti();flag=f2();goto pos1;}
else if (ch=='3') {system("cls");biaoti();flag=f3();goto pos1;}
else if (ch=='9') {system("cls");gong_neng_qu();}
else if (ch=='0') {system("pause");tui_chu();}
else {system("cls");printf("\n======>\t\t输入错误,请重新输入!\n");goto pos1;}
if (flag==99 || flag==77 || flag==88) {goto pos1;}
return 0;
}
int f1()
{
void Functions(int n);
char c;
tar1:printf("======> 单一查询 菜单如下: \n");
printf("\t $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $\n");
printf("\t $ $ $ $\n");
printf("\t $ 1.姓名 2.性别 $\n");
printf("\t $ 3.年龄 4.学部班级 $\n");
printf("\t $ 5.电话 6.寝室号 $\n");
printf("\t $ 7.清理屏幕 0.返回 $\n");
printf("\t $ $ $ $\n");
printf("\t $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $\n");
printf("======> 请选择:");c=getchar();getchar();
if (c=='1') {Functions(1);goto tar1;}
else if (c=='2') {Functions(2);goto tar1;}
else if (c=='3') {Functions(3);goto tar1;}
else if (c=='4') {Functions(4);goto tar1;}
else if (c=='5') {Functions(5);goto tar1;}
else if (c=='6') {Functions(6);goto tar1;}
else if (c=='7') {system("cls");biaoti();goto tar1;}
else if (c=='0') return 99;
else {printf("======> 输入错误!请重新输入!\n\n\n");goto tar1;}
}
void Functions(int n)
{
int i,COUNT,rs;
char flag;
char name[10],GET[10];
char sex[4];
char age[4];
char cla[14];
char Tel[14];
char qins[5];
char target[14];
struct stud1ent_type *p=stud1;
system("cls");
biaoti();
if (n==1) {*target=*name;strcpy(GET,"姓名");}
else if (n==2) {*target=*sex;strcpy(GET,"性别");}
else if (n==3) {*target=*age;strcpy(GET,"年龄");}
else if (n==4) {*target=*cla;strcpy(GET,"学部班级");}
else if (n==5) {*target=*Tel;strcpy(GET,"电话号码");}
else if (n==6) {*target=*qins;strcpy(GET,"寝室号");}
loc1:printf("\n======> 请输入%s:",GET);
scanf("%s",target);
getchar();
printf("**********************************************************************\n");
printf(" 编号\t 姓名\t 性别\t 年龄\t 学部班级\t联系电话 寝室号\n");
for (i=0,COUNT=0;i<SIZE;i++)
{
if (n==1) rs=strcmp(target,stud1[i].name);
else if (n==2) rs=strcmp(target,stud1[i].sex);
else if (n==3) rs=strcmp(target,stud1[i].age);
else if (n==4) rs=strcmp(target,stud1[i].cla);
else if (n==5) rs=strcmp(target,stud1[i].Tel);
else if (n==6) rs=strcmp(target,stud1[i].qins);
if (rs==0)
{
COUNT++;
printf(" %-6d %-7s %-7s %-7s %-13s %-15s %-10s\n",COUNT,stud1[i].name,stud1[i].sex,stud1[i].age,stud1[i].cla,stud1[i].Tel,stud1[i].qins);
}
}
printf("\n\n\t\t---该宿舍%s为%s的学生有 %d 个---\n",GET,target,COUNT);
printf("**********************************************************************\n");
if (COUNT==0) printf("======> 对不起! 所查询的信息为空.\n");
printf("======> 还要继续查询吗? (输入y继续,否则返回主菜单:) ");
flag=getchar();getchar();
if (flag=='Y' || flag=='y') goto loc1;
}
int f2() //符合条件查询,目前只有综合年龄和寝室号查询
{
int i,COUNT,rs,rd;char room[5],age[4],flag;
printf("======> 目前符合条件查询功能只有综合年龄和寝室号查询!\n");
system("pause");
loc1:printf("\n======> 请输入寝室号:");
scanf("%s",room);
getchar();
printf("\n======> 请输入年龄:");
scanf("%s",age);
getchar();
printf("**********************************************************************\n");
printf(" 编号\t 姓名\t 性别\t 年龄\t 学部班级\t联系电话 寝室号\n");
for (i=0,COUNT=0;i<SIZE;i++)
{
rs=strcmp(room,stud1[i].qins);
if (rs==0)
{
rd=strcmp(age,stud1[i].age);
if (rd==0)
{
COUNT++;
printf(" %-6d %-7s %-7s %-7s %-13s %-15s %-10s\n",COUNT,stud1[i].name,stud1[i].sex,stud1[i].age,stud1[i].cla,stud1[i].Tel,stud1[i].qins);
}
}
}
printf("**********************************************************************\n");
if (COUNT==0) printf("======> 对不起! 所查询的信息为空.\n");
printf("======> 还要继续查询吗? (输入y继续,否则结束:) ");
flag=getchar();getchar();
system("cls");biaoti();
if (flag=='Y' || flag=='y') goto loc1;
else return 88;
}
int f3()
{
printf("======> 抱歉,这玩意我还不会写,有空你去Google Code借鉴一下代码吧= =|||\n");
system("pause");
return 77;
}
//kkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
int jian_ce_3(char *xin_xi) /*用户名检测*/
{
int i;
FILE *ip;
ip = fopen("denglu.txt", "rb+");
for (i = 0; i < m; i++) {
if(i)
{
fseek(ip, i * sizeof(struct deng_lu) + 2, 0);
fread(&stud, sizeof(struct deng_lu), 1, ip);
}
else
fread(&stud, sizeof(struct deng_lu), 1, ip);
if(strcmp(stud.zhang_hao, xin_xi) == 0) {
fclose(ip);
return (1);
}
continue;
}
fclose(ip);
return (0);
}
void zhu_ce() /*注册界面*/
{
int c, f;
FILE *ip, *fp;
void deng_lu();
system("CLS");
system("CLS");
printf("*******************************************************************************\n\n\n");
printf("校园寝室管理系统\n\n\n");
printf("\t\t\t 注册界面\t\t\t\n\n");
printf("\t\t\t|-----------------------------------|\n");
printf("\t\t\t| |\n");
printf("\t\t\t| 姓 名 : |\n");
printf("\t\t\t| 性 别 : |\n");
printf("\t\t\t| 年 龄 : |\n");
printf("\t\t\t|专业班级: |\n");
printf("\t\t\t|电话号码: |\n");
printf("\t\t\t|寝 室 号: |\n");
printf("\t\t\t|用 户 名: |\n");
printf("\t\t\t| 密 码 : |\n");
printf("\t\t\t|-----------------------------------|\n");
gotoxy(35, 10);
scanf("%s", student.name);
gotoxy(35, 11);
scanf("%s", student.sex);
gotoxy(35, 12);
scanf("%s", student.age);
gotoxy(35, 13);
scanf("%s", student.cla);
gotoxy(35, 14);
scanf("%s", student.Tel);
gotoxy(35, 15);
scanf("%s", student.qins);
gotoxy(35, 16);
scanf("%s", yong_hu.zhang_hao);
f = jian_ce_3(yong_hu.zhang_hao);
if(f) {
printf("\n\n\n\n用户名已经存在,请重新输入用户名!\n");
gotoxy(35, 16);
scanf("%s", yong_hu.zhang_hao);
}
gotoxy(35, 17);
for (c = 0; c < 6; c++) {
yong_hu.mi_ma[c] = getch();
printf("*");
}
if((ip = fopen("student2.txt","ab+")) == NULL) {
printf("不能打开这个文件\n");
return;
}
if(fwrite(&student,sizeof(struct yonghu),1,ip) != 1)
printf("信息写入错误!");
fprintf(ip,"\n");
fclose(ip);
if((fp = fopen("denglu.txt","ab+")) == NULL) {
printf("不能打开这个文件\n");
return;
}
if(fwrite(&yong_hu, sizeof(struct deng_lu), 1, fp) != 1)
printf("信息写入错误!");
fprintf(fp,"\n");
printf("\n\n\t\t\t\t注册成功!\n\n\n");
printf("\t\t\t\t请按任意键进入登陆界面\n");
getch();
deng_lu();
fclose(fp);
}
int jian_ce_1(char* xin_xi) /*登录账号检测*/
{
int i;
FILE *ip;
ip = fopen("denglu.txt", "rb+");
for (i = 0; i < m; i++) {
if(i)
{
fseek(ip, i * sizeof(struct deng_lu) + 2, 0);
fread(&stud, sizeof(struct deng_lu), 1, ip);
}
else
fread(&stud, sizeof(struct deng_lu), 1, ip);
if(strcmp(stud.zhang_hao, xin_xi) == 0) {
fclose(ip);
return (1);
}
continue;
}
fclose(ip);
return (0);
}
int jian_ce_2(char* xin_xi) /*登录密码检测*/
{
int i;
FILE *ip;
ip = fopen("denglu.txt", "rb+");
for (i = 0; i < m; i++) {
if(i)
{
fseek(ip, i * sizeof(struct deng_lu) + 2, 0);
fread(&stud, sizeof(struct deng_lu), 1, ip);
}
else
fread(&stud, sizeof(struct deng_lu), 1, ip);
if(strcmp(stud.mi_ma, xin_xi) == 0) {
fclose(ip);
return (1);
}
continue;
}
fclose(ip);
return (0);
}
void rnd_f()
{
}
void tui_chu()
{
void zhujiemian();
system("cls");
printf("\n\n\n\n\t\t\t********欢迎下次使用!*******\n\n");
getch();
zhujiemian();
}
void gong_neng_qu()
{
int num_2;
system("CLS");
printf("*******************************************************************************\n\n\n");
printf("校园寝室管理系统\n\n\n");
printf("\t\t\t 主菜单\t\t\t\n\n");
printf("\t\t\t|-------------------------|\n");
printf("\t\t\t| |\n");
printf("\t\t\t| 1.个人信息查询 |\n");
printf("\t\t\t| 2.信 息 删 除 |\n");
printf("\t\t\t| 3.自 动 分 寝 |\n");
printf("\t\t\t| 4. 退 出 |\n");
printf("\t\t\t| |\n");
printf("\t\t\t|-------------------------|\n\n\n");
printf(" 请按编号选择要进入的功能区:\n");
gotoxy(51, 18);
scanf("%d", &num_2);
switch(num_2) {
case 1 : cha_xun(); break;
case 2 : shan_chu(); break;
case 3 : rnd_f();break;
case 4 : tui_chu();
default : {
printf("\n\n\t\t\t 请重新选择,请按enter键!\n\n");
gotoxy(43, 21);
getch();
gong_neng_qu();
}
}
}
void deng_lu() /*登录界面*/
{
int p, q, i = 0;
char new_zhang_hao[18];
char new_mi_ma[7]={0},buf;
int jian_ce_1(char* xin_xi) ;
system("CLS");
printf("校园寝室管理系统\n\n\n");
printf("\n\n\t\t\t 登录界面\t\t\t\n\n");
printf("\t\t\t|---------------------------|\n");
printf("\t\t\t| |\n");
printf("\t\t\t| 用户名: |\n");
printf("\t\t\t| |\n");
printf("\t\t\t| 密 码 : |\n");
printf("\t\t\t| |\n");
printf("\t\t\t| |\n");
printf("\t\t\t| |\n");
printf("\t\t\t|---------------------------|\n");
gotoxy(34, 9);
scanf("%s", new_zhang_hao);
p = jian_ce_1(new_zhang_hao);
if (!p)
{
system("CLS");
printf("\n\n\n\n\t不存在这个账号:请先注册(按任意键进入注册界面)!\n");
getch();
zhu_ce();
while(1);
}
else {
gotoxy(34, 11);
while(buf = getch())
{
if(buf == '\r') //回车
break;
else if(buf != '\b') //退格
{
if(i != 6)
new_mi_ma[i++] = buf;
else
{
gotoxy(34, 11);
system("CLS");
printf("密码最多为6位\n");
printf("登录失败,请重新输入!\n");
while(1);
}
}
else
{
if(i == 0)
continue;
else{
new_mi_ma[--i] = 0;
putchar( buf );
putchar( ' ' );
putchar( buf );
continue;
}
}
putchar('*');
}
q = jian_ce_2(new_mi_ma);
if(!q) {
gotoxy(28, 18);
system("CLS");
printf("\n\n\n\n\n\n\t\t\t\t\t密码错误:请重新输入:\n");
while(1);
}
else {
gotoxy(34, 11);
system("CLS");
printf("\n\n\n\n\t\t*************登录成功*************\n");
gong_neng_qu();
while(1);
}
}
}
zhu_jie_mian() /*主界面*/
{
int num;
system("CLS");
printf("校园寝室管理系统\n\n\n");
printf("\t\t\t 主界面\t\t\t\n\n");
printf("\t\t\t|---------------------------|\n");
printf("\t\t\t| 1-登录 |\n");
printf("\t\t\t| |\n");
printf("\t\t\t| |\n");
printf("\t\t\t| 2-注册 |\n");
printf("\t\t\t| |\n");
printf("\t\t\t| |\n");
printf("\t\t\t| 3-退出 |\n");
printf("\t\t\t|---------------------------|\n\n\n");
printf("\t\t 请按编号选择要进入的界面:\n\n\n");
gotoxy(51, 16);
scanf("%d", &num);
switch(num) {
case 1 : deng_lu();break; /*登录*/
case 2 : zhu_ce(); break; /*注册*/
case 3 : tui_chu();break; /*退出*/
default: printf("\n\n\t\t\t 请重新选择,请按enter键!\n\n");
gotoxy(43, 19);
getch();
zhu_jie_mian();
}
return 1;
}
void zhujiemian()
{
int i = 0;
char num[6], mima[6],buf;
system("CLS");
printf("*******************************************************************************\n\n\n");
printf("\t\t\t 校园寝室管理系统\t\t\t\n\n");
printf("\t\t\t|---------------------------|\n");
printf("\t\t\t| |\n");
printf("\t\t\t| 管理员: |\n");
printf("\t\t\t| |\n");
printf("\t\t\t| 密 码 : |\n");
printf("\t\t\t| |\n");
printf("\t\t\t| |\n");
printf("\t\t\t| |\n");
printf("\t\t\t|---------------------------|\n");
gotoxy(34, 7);
scanf("%s", num);
gotoxy(34, 9);
while(buf = getch())
{
if(buf == '\r') //回车
break;
else if(buf != '\b') //退格
{
if(i != 6)
mima[i++] = buf;
else
{
gotoxy(34, 15);
printf("密码最多为6位\n");
printf("登录失败,请重新输入!\n");
return;
}
}
else
{
if(i == 0)
continue;
else{
mima[--i] = 0;
putchar( buf );
putchar( ' ' );
putchar( buf );
continue;
}
}
putchar('*');
}
if (num[0] == 'l' && num[1] == 'i' && num[2] == 'n' && num[3] == 'u' && num[4] == 's' && mima[0] == '4' &&
mima[1] == '5' && mima[2] == '2' && mima[3] == '3' && mima[4] == '2' && mima[5] == '3' ) {
gotoxy(34, 15);
printf("登录成功\n");
getch();
while(zhu_jie_mian());
}
else {
gotoxy(28, 15);
printf("登录失败,请重新输入!\n");
getch();
zhujiemian();
}
}
int main() /*主函数*/
{
hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
zhujiemian();
}
//登录账号:linus
密码:452323
问题:1.我那个删除界面 输入要删除的名字后,其他记录全删除了
2.我输入的用户名和密码 如果我多注册几个信息后,它只识别第一条注册的用户名和密码