C语言程序纠错
楼盘信息查询系统的程序,在录完信息后,按照户型查询就会崩溃,也不知道是什么问题。
下面是所有代码,貌似问题出在type函数里??
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct house //房屋
{
char num3[100]; //房屋编号
char type[100];//户型
char area[100];//面积
char share[100];//公摊率
char price[100];//价格
char property[100];//对应楼盘
char b_num[100];//对应楼栋编号
char b_unit[100];//对应楼栋单元
struct house *next;
}house;
typedef struct building //楼栋
{
char num2[100]; //楼栋编号
char unit[100]; //单元
char flo[100]; //楼层
char property[100]; //对应楼盘
house *p2;
struct building *next;
}building;
typedef struct property //楼盘
{
char num1[100]; //楼盘编号
char name[100]; //楼盘名称
char addr[100]; //楼盘地址
char pro[100]; //开发商
char com[100]; //物业公司
building *p1;
struct property *next;
}property;
property *head=NULL;
void est1()//输入楼盘
{
char sz1[100]={0};
struct property *ptemp2=head;
struct property *ptemp=(property*)malloc(sizeof(property));
system("cls");
printf("请输入楼盘编号:");
fflush(stdin);
gets(sz1);
while(ptemp2!=NULL)
{
if( strcmp(ptemp2->num1,sz1)==0 )
{
printf("重复,请重新输入\n");
system("pause");
return;
}
ptemp2=ptemp2->next;
}
strcpy(ptemp->num1,sz1);
printf("请输入楼盘名称:");
fflush(stdin);
gets(ptemp->name);
printf("请输入楼盘地址:");
fflush(stdin);
gets(ptemp->addr);
printf("请输入开发商:");
fflush(stdin);
gets(ptemp->pro);
printf("请输入物业公司:");
fflush(stdin);
gets(ptemp->com);
ptemp->next=head;
ptemp->p1=NULL;
head=ptemp;
printf("成功!\n");
system("pause");
}
void build1()//输入楼栋
{
char sz1[100]={0};
struct property *ptemp2=head;
struct building *ptemp = ptemp2->p1;
building *pinfo = (building*)malloc(sizeof(building));
system("cls");
printf("请输入楼盘名称:");
fflush(stdin);
gets(sz1);
while(ptemp2!=NULL)
{
if( strcmp(ptemp2->name,sz1)==0 )
{
char num2[100]={0} ;
char unit[100]={0} ;
char flo[100]={0} ;
printf("请输入楼栋编号:");
fflush(stdin);
gets(num2);
while(ptemp!=NULL)
{
if( strcmp(ptemp->num2,num2)==0 )
{
printf("楼栋已存在,请重新输入!\n");
system("pause");
return;
}
ptemp=ptemp->next;
}
strcpy(pinfo->num2, num2);
printf("请输入单元:");
fflush(stdin);
gets(unit);
printf("请输入楼层:");
fflush(stdin);
gets(flo);
strcpy(pinfo->unit, unit);
strcpy(pinfo->flo, flo);
strcpy(pinfo->property, ptemp2->num1);
pinfo->next=ptemp2->p1;
pinfo->p2=NULL;
ptemp2->p1=pinfo;
printf("成功!\n");
system("pause");
return;
}
ptemp2=ptemp2->next;
}
printf("楼盘不存在,重新输入\n");
system("pause");
}
void house1()//输入房屋
{
char sz1[100] = {0};
char sz2[100] = {0};
char sz3[15] = {0};
struct property *ptemp=head;
struct building *ptemp2=ptemp->p1;
house *pnewuser=(house*)malloc(sizeof(house));
system("cls");
printf("请输入楼盘名称:");
fflush(stdin);
gets(sz1);
while(ptemp != NULL)
{
if( strcmp(ptemp->name,sz1) == 0 )
{
printf("请输入楼栋编号:\n");
fflush(stdin);
gets(sz2);
while(ptemp2 != NULL)
{
if( strcmp(ptemp2->num2,sz2) == 0 )
{
printf("请输入房间编号:\n");
fflush(stdin);
gets(sz3);
strcpy(pnewuser->property, sz1);
strcpy(pnewuser->b_num, sz2);
strcpy(pnewuser->b_unit, ptemp2->unit);
strcpy(pnewuser->num3, sz3);
printf("请输入户型:\n");
fflush(stdin);
gets(pnewuser->type);
printf("请输入面积:\n");
fflush(stdin);
gets(pnewuser->area);
printf("请输入公摊率:\n");
fflush(stdin);
gets(pnewuser->share);
printf("请输入价格:\n");
fflush(stdin);
gets(pnewuser->price);
pnewuser->next = ptemp2->p2;
ptemp2->p2 = pnewuser;
printf("添加成功\n");
system("pause");
return;
}
ptemp2 = ptemp2->next;
}
printf("楼栋编号不存在,添加失败\n");
system("pause");
return;
}
ptemp= ptemp->next;
}
printf("楼盘不存在,重新输入\n");
system("pause");
}
void out_all()//输出所有楼盘
{
struct property *ptemp2 = head;
system("cls");
while(ptemp2 != NULL)
{
{
printf("楼盘编号:%s\n楼盘名称:%s\n楼盘地址:%s\n开发商:%s\n物业公司:%s\n\n",ptemp2->num1,ptemp2->name, ptemp2->addr,ptemp2->pro,ptemp2->com);
}
ptemp2 = ptemp2->next;
}
system("pause");
}
void est2() //修改楼盘
{
char sz1[100]={0};
char sz2[100]={0};
char sz3[100]={0};
char sz4[100]={0};
char sz6[100]={0};
char sz5[100]={0};
struct property *ptemp2=head;
system("cls");
printf("请输入楼盘编号:");
fflush(stdin);
gets(sz1);
while(ptemp2!=NULL)
{
if( strcmp(ptemp2->num1,sz1)==0 )
{
printf("请输入楼盘名称:");
fflush(stdin);
gets(sz2);
strcpy(ptemp2->name,sz2);
printf("请输入楼盘地址:");
fflush(stdin);
gets(sz3);
strcpy(ptemp2->addr,sz3);
printf("请输入开发商:");
fflush(stdin);
gets(sz4);
strcpy(ptemp2->pro,sz4);
printf("请输入物业公司:");
fflush(stdin);
gets(sz5);
strcpy(ptemp2->com,sz5);
printf("修改成功!\n");
system("pause");
return;
}
ptemp2=ptemp2->next;
}
printf("楼盘编号不存在!\n");
system("pause");
}
void build2()//修改楼栋
{
char sz1[100]={0};
struct property *ptemp2=head;
struct building *ptemp=ptemp2->p1;
char buil[100]={0};
char unit[100]={0};
char flo[100]={0};
system("cls");
printf("请输入楼盘名称:");
fflush(stdin);
gets(sz1);
while(ptemp2!=NULL)
{
if( strcmp(ptemp2->name,sz1)==0 )
{
printf("请输入楼栋编号:\n");
fflush(stdin);
gets(buil);
while(ptemp!=NULL)
{
if( strcmp(ptemp->num2,buil)==0 )
{
printf("请输入单元:\n");
fflush(stdin);
gets(unit);
printf("请输入楼层:\n");
fflush(stdin);
gets(flo);
strcpy(ptemp->unit,unit);
strcpy(ptemp->flo,flo);
printf("修改成功\n");
system("pause");
return;
}
ptemp=ptemp->next;
}
printf("修改失败\n");
system("pause");
return;
}
ptemp2= ptemp2->next;
}
printf("楼盘不存在,重新输入\n");
system("pause");
}
void house2()//修改房屋
{
char sz1[100]={0};
struct property *ptemp2=head;
struct building *ptemp=ptemp2->p1;
struct house *ptemp3=ptemp->p2;
char buil[100]={0};
char hou[100]={0};
char type[100]={0};
char area[100]={0};
char share[100]={0};
char price[100]={0};
system("cls");
printf("请输入楼盘名称:");
fflush(stdin);
gets(sz1);
while(ptemp2!=NULL)
{
if(strcmp(ptemp2->name,sz1)==0)
{
printf("请输入楼栋编号:\n");
fflush(stdin);
gets(buil);
while(ptemp!=NULL)
{
if(strcmp(ptemp->num2,buil)==0)
{
printf("请输入房屋编号:\n");
fflush(stdin);
gets(hou);
while(ptemp!=NULL)
{
if(strcmp(ptemp3->num3,hou)==0)
{
printf("请输入户型:\n");
fflush(stdin);
gets(type);
printf("请输入面积:\n");
fflush(stdin);
gets(area);
printf("请输入公摊率:\n");
fflush(stdin);
gets(share);
printf("请输入价格:\n");
fflush(stdin);
gets(price);
strcpy(ptemp3->type,type);
strcpy(ptemp3->area,area);
strcpy(ptemp3->share,share);
strcpy(ptemp3->price,price);
printf("修改成功\n");
system("pause");
return;
}
ptemp3=ptemp3->next;
}
printf("修改失败\n");
system("pause");
return;
}
ptemp=ptemp->next;
}
printf("楼栋不存在,重新输入\n");
system("pause");
return;
}
ptemp2=ptemp2->next;
}
printf("楼盘不存在,重新输入\n");
system("pause");
}
void addr()//按地址查询
{
char sz1[100]={0};
struct property *ptemp=head;
system("cls");
printf("请输入楼盘地址:");
fflush(stdin);
gets(sz1);
while(ptemp!=NULL)
{
if(strcmp(ptemp->addr,sz1)==0 )
{
printf("楼盘编号:%s\n楼盘名称:%s\n楼盘地址:%s\n开发商:%s\n物业公司:%s\n\n",ptemp->num1,ptemp->name,ptemp->addr,ptemp->pro,ptemp->com);
ptemp=ptemp->next;
}
else ptemp=ptemp->next;
}
system("pause");
}
void price()//按价格查询
{
char sz1[100]={0};
char sz2[100]={0};
struct property *ptemp=head;
system("cls");
printf("请输入价格最大值:");
fflush(stdin);
gets(sz1);
printf("请输入价格最小值:");
fflush(stdin);
gets(sz2);
while(ptemp!=NULL)
{
if(strcmp(ptemp->p1->p2->price,sz1)<=0&&strcmp(ptemp->p1->p2->price,sz2)>=0)
{
printf("楼盘编号:%s\n楼盘名称:%s\n地址:%s\n开发商:%s\n物业公司:%s\n楼栋编号:%s\n房屋编号:%s\n价格:%s\n\n",ptemp->num1,ptemp->name, ptemp->addr,ptemp->pro,ptemp->com,ptemp->p1->num2,ptemp->p1->p2->num3,ptemp->p1->p2->price);
ptemp->p1->p2=ptemp->p1->p2->next;
}
else ptemp->p1->p2=ptemp->p1->p2->next;
}
system("pause");
}
void type()//按户型查询
{
char sz1[100]={0};
struct property *ptemp=head;
system("cls");
printf("请输入户型:");
fflush(stdin);
gets(sz1);
while(ptemp!=NULL)
{
if(strcmp(ptemp->p1->p2->type,sz1)==0)
{
printf("楼盘编号:%s\n楼盘名称:%s\n地址:%s\n开发商:%s\n物业公司:%s\n楼栋编号:%s\n单元:%s\n楼层:%s\n房屋编号:%s\n面积:%s\n公摊率:%s\n价格:%s\n\n",ptemp->num1,ptemp->name, ptemp->addr,ptemp->pro,ptemp->com,ptemp->p1->num2,ptemp->p1->unit,ptemp->p1->flo,ptemp->p1->p2->num3,ptemp->p1->p2->area,ptemp->p1->p2->share,ptemp->p1->p2->price);
ptemp->p1->p2=ptemp->p1->p2->next;
}
else ptemp=ptemp->next;
}
system("pause");
}
void bui(building* p2)
{
struct property *ptemp2=head;
while(ptemp2!=NULL)
{
if(strcmp(ptemp2->num1,p2->num2)==0)
{
p2->next=ptemp2->p1;
ptemp2->p1=p2;
break;
}
ptemp2=ptemp2->next;
}
}
void hou(house* p3)
{
struct property *ptemp2 = head;
while(ptemp2!=NULL)
{
struct building *p2 = ptemp2->p1;
while(p2!=NULL)
{
if(strcmp(p2->num2,p3->property)==0&&strcmp(p2->num2,p3->b_num)==0)
{
p3->next=p2->p2;
p2->p2=p3;
break;
}
p2=p2->next;
}
ptemp2=ptemp2->next;
}
}
void save()
{
FILE* file1=fopen("property.txt","w+");
FILE* file2=fopen("building.txt","w+");
FILE* file3=fopen("house.txt","w+");
if(file1!=NULL||file2!=NULL||file3!=NULL)
{
struct property *p1=head;
while(p1!=NULL)
{
struct building *p2=p1->p1;
while(p2!=NULL)
{
struct house* p3=p2->p2;
while (p3!=NULL)
{
fwrite(p3,sizeof(house),1,file3);
fseek(file3, 0,2);
p3=p3->next;
}
fwrite(p2,sizeof(building),1,file2);
fseek(file2,0,2);
p2=p2->next;
}
fwrite(p1,sizeof(property),1,file1);
fseek(file1,0,2);
p1=p1->next;
}
fclose(file1);
fclose(file2);
fclose(file3);
printf("保存成功!\n");
system("pause");
}
}
void read()
{
FILE *file1=fopen("property.txt","r");
FILE *file2=fopen("building.txt","r");
FILE *file3=fopen("house.txt","r");
int lenall=0;
int num1=0;
int num2=0;
int num3=0;
int a=0;
int b=0;
int c=0;
property *p1=(property*)malloc(sizeof(property)*100);
building *p2=(building*)malloc(sizeof(building)*100);
house *p3=(house*)malloc(sizeof(house)*100);
if(file1!=NULL||file2!=NULL||file3!=NULL)
{
while(1)
{
int len=fread((void*)(p1+lenall) ,sizeof(property),1,file1);
if(len!=1)
{
break;
}
lenall+=len;
p1[num1].next=NULL;
p1[num1].p1=NULL;
num1++;
}
lenall=0;
while(1)
{
int len=fread((void*)(p2+lenall) ,sizeof(building),1,file2);
if(len!=1) break;
lenall+=len;
p2[num2].next=NULL;
p2[num2].p2=NULL;
num2++;
}
lenall=0;
while(1)
{
int len=fread((void*)(p3+lenall) ,sizeof(house),1,file3);
if(len!=1) break;
lenall+=len;
p3[num3].next=NULL;
num3 ++;
}
for (a=0;a<num1;a++)
{
head=p1;
if(a>0)
{
p1[a-1].next=&p1[a];
}
}
for(b=0;b<num2;b++) bui(&p2[b]);
for(c=0;c<num3;c++) hou(&p3[c]);
fclose(file1);
fclose(file2);
fclose(file3);
}
}
int menu()
{
int a;
system("cls");
printf("********************欢迎使用楼盘查询系统********************\n\n\n");
printf("************************信息录入与修改**********************\n");
printf(" 1.楼盘信息输入\n");
printf(" 2.楼盘信息更改\n");
printf(" 3.楼栋信息输入\n");
printf(" 4.楼栋信息更改\n");
printf(" 5.房间信息输入\n");
printf(" 6.房间信息更改\n\n\n");
printf("***************************信息查询*************************\n");
printf(" 7.全部楼盘信息查询\n");
printf(" 8.按地址查询楼盘\n");
printf(" 9.按户型查询楼盘\n");
printf(" 10.按价格查询楼盘\n\n\n");
printf("*****************************其他***************************\n");
printf(" 11.保存信息\n");
printf(" 12.退出系统\n\n\n");
printf("请选择功能序号:");
scanf_s("%d",&a);
return a;
}
void main()
{
int b;
read();
while(1)
{
b=menu();
switch(b)
{
case 1:
est1();
break;
case 2:
est2();
break;
case 3:
build1();
break;
case 4:
build2();
break;
case 5:
house1();
break;
case 6:
house2();
break;
case 7:
out_all();
break;
case 8:
addr();
break;
case 9:
type();
break;
case 10:
price();
break;
case 11:
save();
break;
case 12:
exit(0);
break;
}
}
}