您的位置:首页 > 编程语言 > C语言/C++

C语言小区居民查询系统

2010-10-30 01:01 363 查看
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<conio.h>

typedef struct member//成员结构体
{
char name[30];//姓名

char born[10];//出生日

int relation;//与户主关系
int sex;//姓别
struct member *next;
}mem;

typedef struct house//住房结构体
{
char addres[50];//小区名
char num[20];//小区门牌号,唯一

mem *mhead;//家庭成员结构体头指针
mem *mi;//家庭成员结构体尾指针
struct house *next;
}hou;

hou *hhead=NULL;//全局户主结构体头指针
hou *hi=NULL;//全局户主结构体尾指针

void initial()
{

FILE *fp;
fp=fopen("house.txt","r");
if(fp)//1 if
{
fseek(fp,0,SEEK_END);
int n;
n=ftell(fp)/sizeof(hou);//计算用户结构体个数

rewind(fp);//复位

hou *p;
int i=0;
for(;i<n;i++)//1 for
{
p=(hou*)calloc(1,sizeof(hou));//或者(hou*)malloc(sizeof(hou))
fread(p,sizeof(hou),1,fp);
p->mhead=p->mi=NULL;
p->next=NULL;
if(hhead==NULL)hhead=hi=p;
else { hi->next=p; hi=p; }

//下面初始化成员信息
FILE *fp1;
int n1;
fp1=fopen(p->num,"r");// 家庭门牌号是唯一的,所以用以作 文件名
if(fp1)//2 if
{
fseek(fp1,0,2);

n1=ftell(fp1)/sizeof(mem);//计算用户结构体个数

rewind(fp1);//复位

mem *p1;
int j=0;
for(;j<n1;j++)
{//2 for
p1=(mem*)calloc(1,sizeof(mem));//或者(res*)malloc(sizeof(res))
fread(p1,sizeof(mem),1,fp1);
p1->next=NULL;
if(p->mhead==NULL)p->mhead=p->mi=p1;
else {p->mi->next=p1; p->mi=p1; }

}//2 for
// printf("/n/n初始化%s小区%s号%d个家庭成员信息成功!",p->addres,p->num,n1);

fclose(fp1);

}//2 if

// else printf("/n/n提示:%s小区%s号楼未售出!",p->addres,p->num);
//初始化成员信息

}//1 for

// printf("/n/n初始化%d个小区居民信息成功!",n);
fclose(fp);

}//1 if

else printf("/n/n/n数据库无该文件信息!");

}

void newh()
{

hou *p;
p=(hou *)malloc(sizeof(hou));
printf("/n/n/n█创建新单元█/n");
printf("/n请输入新单元小区名:");
scanf("%s",p->addres);
loop:

printf("/n请输入新单元门牌号:");
scanf("%s",p->num);

//检验是否重复
hou *p1=hhead;
while(p1)
{
if(strcmp(p->num,p1->num)==0)
{ printf("/n错误,该门牌号已经存在!");

char c;
printf("/n/n您想 重新输入(A)----退出新建(N),请选择!/n");
c=getch();
if(c=='A'||c=='a')goto loop; else return;
}

p1=p1->next;

}//检验是否重复

p->next=NULL;
p->mhead=p->mi=NULL;

if(hhead==NULL)hhead=hi=p;
else {hi->next=p;hi=p;}

printf("/n |-----------------------|");
printf("/n █新单元楼信息█  ");
printf("/n |-----------------------|");
printf("/n  小区名称:%s ",p->addres);
printf("/n |-----------------------|");
printf("/n  门牌号码:%s ",p->num);
printf("/n |-----------------------|");

printf("/n新单元楼创建成功/n/n");

}

void newm()
{

printf("/n/n█注册住户资料█/n");
loop:
char nn[30];
char c;

printf("/n请输入住单元的门牌号码:");
scanf("%s",nn);

//检验是否有
hou *p=hhead;
int i=1;

while(p)
{
if(strcmp(p->num,nn)==0)
{ i=0;break;}

p=p->next;

}//检验

if(i){
printf("/n错误,该门牌号不存在!");
printf("/n/n您想 重新输入(A)----退出注册(N),请选择!/n");
c=getch();
if(c=='A'||c=='a')goto loop; else return;
}

loop1:

mem *p1;
p1=(mem *)malloc(sizeof(mem));
p1->next=NULL;

printf("/n请输入姓名:");
scanf("%s",p1->name);
printf("/n请输入性别,男(1)女(2)./n请填1或2:");
scanf("%d",&p1->sex);
printf("/n请输入出生日期(形式:年/月/日):");
scanf("%s",&p1->born);
printf("/n请输入输入与户主关系:户主(1)配偶(2)子女(3)/n请填1或2或3:");
scanf("%d",&p1->relation);

if(p->mhead==NULL)p->mhead=p->mi=p1;
else {p->mi->next=p1;p->mi=p1;}

printf("/n/n若还有家庭成员,请按A继续注册,退出请按N!");
printf("/n/n(提示 :注册住户的所有家庭成员资料必须在本次完成填写!)/n");

c=getch();
if(c=='A'||c=='a')goto loop1;

printf("/n注册住户资料成功/n/n");

}

void showh()
{

hou *p=hhead;
if(p==NULL){printf("/n/n小区信息数据库为空!"); return;}

printf("/n /n /n /n     █小区信息数据库█        ");
printf("/n|-------------------------------------------------------------|");
int i=0;
while(p)
{
i++;
printf("/n|[%d ] 小区名称: %s 门牌号码:%s ",i,p->addres,p->num );

if(p->mhead)printf(" 已售!");else printf(" 未售!");

printf("/n|-------------------------------------------------------------|");

p=p->next;
}

printf("/n█单元楼总数: %d ",i);
printf("/n|-------------------------------------------------------------|/n/n/n/n/n/n");

}

void showm()
{

hou *p=hhead;
if(p==NULL){printf("/n/n居民信息数据库为空!"); return;}

printf("/n /n /n /n     █居民信息数据库█        ");
printf("/n|-------------------------------------------------------------|");
int i=0;
while(p)
{
mem *p1=p->mhead;
if(!p1) goto loop;

while(p1)
{
i++;
printf("/n[%d ] /t姓名: %s /t性别: ",i ,p1->name );
p1->sex-1?puts("女"):puts("男");
printf(" /t出生日期: %s ",p1->born );
if(p1->relation==1)
printf(" /t与户主关系:户主 " );
if(p1->relation==2)
printf(" /t与户主关系:配偶 " );
if(p1->relation==3)
printf(" /t与户主关系:子女 " );

printf("/n| /t小区名称: %s /t门牌号码:%s ",p->addres,p->num );
printf("/n|-------------------------------------------------------------|");
p1=p1->next;
}

loop: ;

p=p->next;

}

printf("/n█居民信息总数: %d ",i);
printf("/n|-------------------------------------------------------------|/n/n/n/n/n/n");

}

void lookh()
{

printf("/n/n█查询单元楼█/n");
loop:
char nn[30];
char c;

printf("/n请输要查询单元楼的门牌号码:");
scanf("%s",nn);

//检验是否有
hou *p=hhead;
int i=1;

while(p)
{
if(strcmp(p->num,nn)==0)
{ i=0;break;}

p=p->next;

}//检验

if(i)printf("/n错误,该门牌号不存在!");
else{
printf("/n|-------------------------------------------------------------|");
printf("/n| 小区名称: %s 门牌号码:%s ",p->addres,p->num );

if(p->mhead)printf(" 已售!");else printf(" 未售!");

printf("/n|-------------------------------------------------------------|");

}

printf("/n/n您想 继续查询(A)----退出查询(N),请选择!/n");
c=getch();
if(c=='A'||c=='a')goto loop; else return;
}

void lookm()
{

printf("/n/n█查询居民█/n");

char c,m[30];
do{//do的开始。

printf("/n/n/n [居民查询菜单]");
printf("/n|-----------------------------------|");
printf("/n| 请选择查询方式: |");
printf("/n|-----------------------------------|");
printf("/n| A[ 姓 名 查 询 ] |");
printf("/n|-----------------------------------|");
printf("/n| B[ 出生日期查询] |");
printf("/n|-----------------------------------|");
printf("/n| C[ 单 元 号查询] |");
printf("/n|-----------------------------------|");
printf("/n| D[ 退出 ] |");
printf("/n|-----------------------------------|");

c=getch();

switch (c)
{//switch的开始

case 'A':
case'a':
{ //a

int y=1;
hou *p=hhead;
printf("/n/n请输入要查找的姓名/n/n");
scanf("%s",m);

while(p)
{
mem *p1=p->mhead;

while(p1)
{

if(strcmp(p1->name,m)==0)
{ y=0;
printf("/n|-------------------------------------------------------------|");
printf("/n /t姓名: %s /t性别: " ,p1->name );
p1->sex-1?puts("女"):puts("男");
printf(" /t出生日期: %s ",p1->born );
if(p1->relation==1)
printf(" /t与户主关系:户主 " );
if(p1->relation==2)
printf(" /t与户主关系:配偶 " );
if(p1->relation==3)
printf(" /t与户主关系:子女 " );
printf("/n| /t小区名称: %s /t门牌号码:%s ",p->addres,p->num );
printf("/n|-------------------------------------------------------------|");
}

p1=p1->next;

}

p=p->next;
}

if(y)printf("失败,您查询的居民不存在!/n/n");

}//a
break;

case'B':
case'b':
{ //b
int y=1;
hou *p=hhead;
printf("/n/n请输入要查找的出生年月(形式:年/月/日)/n/n");
scanf("%s",m);

while(p)
{
mem *p1=p->mhead;

while(p1)
{

if(strcmp(p1->born,m)==0)
{ y=0;
printf("/n|-------------------------------------------------------------|");
printf("/n /t姓名: %s /t性别: ",p1->name );
p1->sex-1?puts("女"):puts("男");
printf(" /t出生日期: %s ",p1->born );
if(p1->relation==1)
printf(" /t与户主关系:户主 " );
if(p1->relation==2)
printf(" /t与户主关系:配偶 " );
if(p1->relation==3)
printf(" /t与户主关系:子女 " );
printf("/n| /t小区名称: %s /t门牌号码:%s ",p->addres,p->num );
printf("/n|-------------------------------------------------------------|");
}

p1=p1->next;

}
p=p->next;
}

if(y) printf("失败,您查询的年月日不存在!/n/n");

}break;//b

case'C':
case'c':
{//c
int y=1;
hou *p=hhead;
printf("/n/n请输入要查找的门牌号码/n/n");
scanf("%s",m);

while(p)
{ //while 1
if(strcmp(p->num,m)==0)
{ //if
y=0;

if(!p->mhead)printf("/n/n未出售,无居民信息!");
mem *p1=p->mhead;
while(p1)
{

printf("/n|-------------------------------------------------------------|");
printf("/n /t姓名: %s /t性别: ",p1->name );
p1->sex-1?puts("女"):puts("男");
printf(" /t出生日期: %s ",p1->born );
if(p1->relation==1)
printf(" /t与户主关系:户主 " );
if(p1->relation==2)
printf(" /t与户主关系:配偶 " );
if(p1->relation==3)
printf(" /t与户主关系:子女 " );
printf("/n| /t小区名称: %s /t门牌号码:%s ",p->addres,p->num );
printf("/n|-------------------------------------------------------------|");

p1=p1->next;
}

}//if
p=p->next;
}//while 1

if(y) printf("失败,您查询的门牌号不存在!/n/n");

}break;//c

}//switch的结束

}while(c!='D'&&c!='d');
//do 的结束

}

void delh()
{
printf("/n/n█删除单元楼█/n");

hou *p=hhead,*p1=hhead;
if(p==NULL){printf("/n/n小区信息数据库为空!"); return;}
char m[30];

loop:

printf("/n/n请输入要删除的门牌号码/n/n");
scanf("%s",m);

int y=0,i=1;

while(p)
{
y++;

if(strcmp(p->num,m)==0)
{
if(p->mhead!=NULL){
printf("/n错误,该单元已经售出,无法办理删除!");
printf("/n/n您想 重新输入(A)----退出删除(N),请选择!/n");
char c;
c=getch();
if(c=='A'||c=='a')goto loop; else return;
}

i=0;
if(p==hhead)hhead=hhead->next;
else p1->next=p->next ;
printf("删除%s号楼成功!/n/n",p->num);
if(p->mhead)//有居民信息
{FILE *fp;
fp=fopen(p->num,"w");//将原有的居民清除,W的方式是存在则删除,不存在则新建
}
}

p=p->next;
if(y>1)p1=p1->next;

}

if(i) printf("/n/n/n失败,您输入的门牌号不存在!/n/n");

}

void delm()
{
printf("/n/n█删除居民█/n");

hou *p=hhead;
if(p==NULL){printf("/n/n小区信息数据库为空!"); return;}
char m[30];

loop:
printf("/n/n请输入要删除居民的门牌号码/n/n");
scanf("%s",m);

while(p)
{

if(strcmp(p->num,m)==0)
{
if(p->mhead==NULL)
{ printf( "/n该单元未售出,无居民可删除!");
printf("/n/n您想 重新输入(A)----退出删除(N),请选择!/n");
char c;
c=getch();
if(c=='A'||c=='a')goto loop; else return;}
}
p=p->next;

}

int i=1;

p=hhead;

while(p)
{//while 1

if(strcmp(p->num,m)==0)
{//if 1
i=0;
printf("/n/n请输入要删除居民的姓名:/n");
scanf("%s",m);
mem *p1=p->mhead,*p2=p->mhead;
int y1=0,y2=1;

while(p1)
{//while 2
y1++;

if(strcmp(p1->name,m)==0)
{//if 2
y2=0;
if(p1==p->mhead) p->mhead=p->mhead->next;
else p2->next=p1->next ;

printf("删除%s号楼的居民%s成功!/n/n",p->num,p1->name);
}// if 2
p1=p1->next;
if(y1>1)p2=p2->next;

}//while 2
if(y2) printf("/n/n/n失败,您输入的姓名不存在!/n/n");

}//if 1

p=p->next;

}//while 1

if(i) printf("/n/n/n失败,您输入的门牌号不存在!/n/n");

}

void modifyh()
{
printf("/n/n/n█修改单元█/n");

int y=1 ;
char m[30];
loop:
printf("/n/n/n请输入要修改单元门牌号码:");
scanf("%s",m);

//检验是否有
hou *p=hhead;
while(p)
{
if(strcmp(p->num,m)==0)
{ //if
y=0;
loop1:

printf("/n/n请输入新门牌号码/n/n");
scanf("%s",m);

//检验是否重复
hou *p1=hhead;
while(p1)
{
if(strcmp(m,p1->num)==0)
{ printf("/n错误,该门牌号已经存在,请重新输入!");

goto loop1;
}

p1=p1->next;

}//检验是否重复

strcpy(p->num,m);
}//if

p=p->next;
}

if(y)printf("/n错误,该门牌号不存在!");

else printf("/n新单元楼修改成功/n/n");

char c;
printf("/n/n您想 重新输入(A)----退出新建(N),请选择!/n");
c=getch();
if(c=='A'||c=='a')goto loop; else return;

}

void save()
{

FILE *fp;
fp=fopen("house.txt","w");

hou *p=hhead;
int i=0;
while(p)
{
i++;

fwrite(p,sizeof(hou),1,fp);

//保存家族成员
if(p->mhead)
{

FILE *fp1;
fp1=fopen(p->num,"w");
mem *p1=p->mhead;
int n=0;
while(p1)
{
n++;
fwrite(p1,sizeof(mem),1,fp1);

p1=p1->next;
}
//printf("/n/n保存%d个家庭成员信息成功!",n);
}//保存家族成员

p=p->next;

}

//printf("/n/n保存%d个小区居民信息成功!",i);

fclose(fp);

}

void release()
{

printf("/n/n/n [清空菜单]");
printf("/n|---------------------|");
printf("/n| 请选择清空方式: |");
printf("/n|---------------------|");
printf("/n| A[ 全部清空] |");
printf("/n|---------------------|");
printf("/n| B[ 清空居民] |");
printf("/n|---------------------|");
printf("/n| C[ 退 出] |");
printf("/n|---------------------|");
char c;
c=getch();
if(c=='C'||c=='c') return;

hou *p=hhead;
while(p)
{
if(p->mhead)//有居民信息
{FILE *fp;
fp=fopen(p->num,"w");//将原有的居民清除,W的方式是存在则删除,不存在则新建
}
p->mhead=p->mi=NULL;

p=p->next;
}

if(c=='A'||c=='a') hhead=hi=NULL;

printf("/n/n/n 清空信息成功!");

}

void show()
{

printf("/n |----------------------------------------|");
printf("/n | █主 菜 单 █ |");
printf("/n |----------------------------------------|");
printf("/n | A 小 区 信 息 |");
printf("/n |----------------------------------------|");
printf("/n | B 新 建 单 元 |");
printf("/n |----------------------------------------|");
printf("/n | C 住 户 注 册 |");
printf("/n |----------------------------------------|");
printf("/n | D 信 息 查 询 |");
printf("/n |----------------------------------------|");
printf("/n | E 系 统 管 理 |");
printf("/n |----------------------------------------|");
printf("/n | F 清 空 数 据 |");
printf("/n |----------------------------------------|");
printf("/n | G 退 出 程 序 |");
printf("/n |----------------------------------------|");
printf("/n | 请您选择一个任务 |");
printf("/n |----------------------------------------|/n/n/n");
}

void show1()
{

printf("/n |-------------------------|");
printf("/n | █小 区 信 息 █ |");
printf("/n |-------------------------|");
printf("/n | A 所 有 单 元 信息 |");
printf("/n |-------------------------|");
printf("/n | B 所 有 居 民 信息 |");
printf("/n |-------------------------|");
printf("/n | C 退 出 程 序 |");
printf("/n |-------------------------|");
printf("/n | 请您选择一个任务 |");
printf("/n |-------------------------|/n/n/n");
}

void show2()
{

printf("/n |-------------------------|");
printf("/n | █ 信 息 查 询█ |");
printf("/n |-------------------------|");
printf("/n | A 查 询 单 元 信息 |");
printf("/n |-------------------------|");
printf("/n | B 查 询 居 民 信息 |");
printf("/n |-------------------------|");
printf("/n | C 退 出 程 序 |");
printf("/n |-------------------------|");
printf("/n | 请您选择一个任务 |");
printf("/n |-------------------------|/n/n/n");
}

void show3()
{

printf("/n |-----------------------------|");
printf("/n | █管 理 菜 单█ |");
printf("/n |-----------------------------|");
printf("/n | A 修 改 单 元 |");
printf("/n |-----------------------------|");
printf("/n | B 删 除 单 元 |");
printf("/n |-----------------------------|");
printf("/n | C 删 除 居 民 |");
printf("/n |-----------------------------|");
printf("/n | D 退 出 程 序 |");
printf("/n |-----------------------------|");
printf("/n | 请您选择一个任务 |");
printf("/n |-----------------------------|/n/n/n");
}

void main()
{

printf("/n/n/n/n/n/n/n/n/n/n");
printf("/n/n ★欢迎您使用小区查询系统!★");
initial();

char c;
do {

show();

c=getch();

switch ( c)
{

case 'a':
case 'A':{
char c1;

do{
show1();

c1=getch();

switch ( c1)

{
case 'a':
case 'A':showh();break;

case 'b':
case 'B':showm();break;

}

}while(c1!='c'&&c1!='C');

}break;

case 'b':
case 'B':newh();break;

case 'c':
case 'C':newm();break;

case 'd':
case 'D':
{
char c2;

do{
show2();

c2=getch();

switch ( c2)

{
case 'a':
case 'A':lookh();break;

case 'b':
case 'B':lookm();break;

}

}while(c2!='c'&&c2!='C');

}break;

case 'e':
case 'E':
{
char c3;

do{
show3();

c3=getch();

switch ( c3)

{
case 'a':
case 'A':modifyh();break;

case 'b':
case 'B':delh();break;

case 'c':
case 'C':delm();break;

}

}while(c3!='d'&&c3!='D');

}break;

case 'f':
case 'F': release();break;

}

save();

}while(c!='g'&&c!='G');

save();

printf("/n/n ★ 谢谢您的使用!★");

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: