现在的位置: 首页 > 综合 > 正文

C语言课设-房产信息统计与分析

2015年10月30日 ⁄ 综合 ⁄ 共 7379字 ⁄ 字号 评论关闭

2014年9月3日 帮别人写的(陈乔琦) 

//头文件
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define MAX 18
#define MAXLIN 100
//房产结构体
typedef struct House{
	char  num[MAX];		//编号
	char  name[MAX];	//小区名称
	char  area[MAX];	//面积
	char  jiage[MAX];		//价格
	char  qingkuang[MAX];		//售出情况
	char  time[MAX];		//售出时间
}house;
//已售房产结构体
typedef struct yishou{
	int num;
	int area;
	int average;
}yi;
//未售房产结构体
typedef struct weishou{
	int num;
	int area;
	int average;
}wei;
//小区价格走势
typedef struct zoushi{
	char name[MAX];
	int jiage;
	int all;
}shi;
//字符串截取函数
void substring(char *s,int i,int n,char *t)					//截取指定位置的 字符串子串
{
 int j=0;
 for(;j<n;j++)
  t[j]=s[i++];
 t[j]=0;
}
//数据读取
void Output(){
	FILE *fp;
	char haha[36]={"NULL"};
	char hehe[36];
	char hehe2[36];
	if((fp=fopen("D:\\fangchan.txt","rt"))==NULL){
		printf("文件打开失败!\n");
		exit(0);
	}
	fgets(haha,36,fp);
	printf("房产信息如下:\n");
	printf("%s",haha);
	while(fgets(hehe,35,fp)!=NULL){		//依次读取每行
		substring(hehe,0,3,hehe2);
		printf("%s\t",hehe2);
		substring(hehe,4,8,hehe2);
		printf("%s\t",hehe2);
		substring(hehe,13,2,hehe2);
		printf("%s\t",hehe2);
		substring(hehe,16,4,hehe2);
		printf("%s\t",hehe2);
		substring(hehe,21,1,hehe2);
		printf("%s\t",hehe2);
		substring(hehe,23,8,hehe2);
		printf("%s\n",hehe2);
	}
	fclose(fp);
	printf("\n房产信息输出完毕!\n\n");
}
//数据录入
void Input(){
	house h;
	FILE *fp;
	char hehe1[5]={"\t"};
	char hehe2[5]={"\n"};
	if((fp=fopen("D:\\fangchan.txt","a+"))==NULL){
		printf("文件打开失败!\n");
		exit(0);
	}
	printf("\n\n-----------------------------房产信息录入模块:----------------------------\n");			//写入录入数据
	printf("请您依次输入信息:\n编号	小区名称	面积	价格(元/米)	售出情况	售出时间\n");
	scanf("%s %s %s %s %s %s",h.num,h.name,h.area,h.jiage,h.qingkuang,h.time);

	fputs(h.num,fp);
	fputs(hehe1,fp);
	fputs(h.name,fp);
	fputs(hehe1,fp);
	fputs(h.area,fp);
	fputs(hehe1,fp);
	fputs(h.jiage,fp);
	fputs(hehe1,fp);
	fputs(h.qingkuang,fp);
	fputs(hehe1,fp);
	fputs(h.time,fp);
	fclose(fp);
	printf("\n学生信息已录入至 D 盘 fangchan.txt 文件中!\n\n");
}
//数据修改
void XiuGai(){
	house h;
	house list[MAXLIN];
    char hehe3[5]={"\t"};
	char hehe4[5]={"\n"};
	printf("请输入您要进行修改的编号:");
	scanf("%s",&h.num);
	printf("该房产信息为:");
	FILE * fp;
	char haha[34]={"NULL"};
	char hehe[35];
	char hehe2[35];
	if((fp=fopen("D:\\fangchan.txt","rt"))==NULL){
		printf("该文件读取失败!");
		exit(0);
	}
	fgets(haha,34,fp);
	printf("\n%s\n",haha);
	while(fgets(hehe,36,fp)!=NULL){
		substring(hehe,0,3,hehe2);
		if(strcmp(hehe2,h.num)==0){
			printf("%s\t",hehe2);
			substring(hehe,4,8,hehe2);
			printf("%s\t",hehe2);
			substring(hehe,13,2,hehe2);
			printf("%s\t",hehe2);
			substring(hehe,16,4,hehe2);
			printf("%s\t",hehe2);
			substring(hehe,21,1,hehe2);
			printf("%s\t",hehe2);
			substring(hehe,23,8,hehe2);
			printf("%s\t",hehe2);
		}
	}
	fclose(fp);
	printf("\n\n");
	printf("请您输入该房产修改之后的信息·····\n");
	printf("请您依次输入该房产的\n小区名称	面积	价格	情况	时间\n");			//写入修改后的数据
	scanf("%s %s %s %s %s",h.name,h.area,h.jiage,h.qingkuang,h.time);
	if((fp=fopen("D:\\fangchan.txt","rt"))==NULL){
		printf("该文件读取失败!");
		exit(0);
	}
	fgets(haha,35,fp);
	printf("\n%s\n",haha);
	int o=0;
	while(fgets(hehe,36,fp)!=NULL){
		substring(hehe,0,3,hehe2);
		strcpy(list[o].num,hehe2);
		if(strcmp(hehe2,h.num)==0){
			strcpy(list[o].name,h.name);
			strcpy(list[o].area,h.area);
			strcpy(list[o].jiage,h.jiage);
			strcpy(list[o].qingkuang,h.qingkuang);
			strcpy(list[o].time,h.time);
		}else{
			substring(hehe,4,8,hehe2);
			strcpy(list[o].name,hehe2);
			substring(hehe,13,2,hehe2);
			strcpy(list[o].area,hehe2);
			substring(hehe,16,4,hehe2);
			strcpy(list[o].jiage,hehe2);
			substring(hehe,21,1,hehe2);
			strcpy(list[o].qingkuang,hehe2);
			substring(hehe,23,8,hehe2);
			strcpy(list[o].time,hehe2);
		}
		o++;
	}
	fclose(fp);
	char haha3[36]={"编号	小区名称	面积	价格	情况	时间"};
	if((fp=fopen("D:\\fangchan.txt","w+"))==NULL)				//打开文件
	{
		printf("can not open the file\n");
		exit(0);
	}
	fputs(haha3,fp);
	fputs(hehe4,fp);
	for(int i=0;i<o;i++){	
		fputs(list[i].num,fp);
		fputs(hehe3,fp);
		fputs(list[i].name,fp);
		fputs(hehe3,fp);
		fputs(list[i].area,fp);
		fputs(hehe3,fp);
		fputs(list[i].jiage,fp);
		fputs(hehe3,fp);
		fputs(list[i].qingkuang,fp);
		fputs(hehe3,fp);
		fputs(list[i].time,fp);
	//	fputs(hehe3,fp);
		fputs(hehe4,fp);
	}
	fclose(fp);
	printf("修改之后的学生信息已存入到了fangchan.txt文件里!\n\n");
	printf("修改之后的:\n");
	for(i=0;i<o;i++){
		printf("%s\t",list[i].num);
		printf("%s\t",list[i].name);
		printf("%s\t",list[i].area);
		printf("%s\t",list[i].jiage);
		printf("%s\t",list[i].qingkuang);
		printf("%s\n",list[i].time);
	}
	printf("\n\n");
}
//数据删除
void Delete(){
	house h;
	house list[MAXLIN];
    char hehe3[5]={"\t"};
	char hehe4[5]={"\n"};
	printf("请输入您要进行删除的编号:");
	scanf("%s",&h.num);
	FILE * fp;
	char haha[34]={"NULL"};
	char hehe[35];
	char hehe2[35];
	if((fp=fopen("D:\\fangchan.txt","rt"))==NULL){
		printf("该文件读取失败!");
		exit(0);
	}
	fgets(haha,35,fp);
	printf("\n%s\n",haha);
	int o=0;
	while(fgets(hehe,36,fp)!=NULL){
		substring(hehe,0,3,hehe2);		
		if(strcmp(hehe2,h.num)==0){
			continue;
		}else{
			strcpy(list[o].num,hehe2);
			substring(hehe,4,8,hehe2);
			strcpy(list[o].name,hehe2);
			substring(hehe,13,2,hehe2);
			strcpy(list[o].area,hehe2);
			substring(hehe,16,4,hehe2);
			strcpy(list[o].jiage,hehe2);
			substring(hehe,21,1,hehe2);
			strcpy(list[o].qingkuang,hehe2);
			substring(hehe,23,8,hehe2);
			strcpy(list[o].time,hehe2);
		}
		o++;
	}
	fclose(fp);
	char haha3[36]={"编号	小区名称	面积	价格	情况	时间"};
	if((fp=fopen("D:\\fangchan.txt","w+"))==NULL)				//打开文件
	{
		printf("can not open the file\n");
		exit(0);
	}
	fputs(haha3,fp);
	fputs(hehe4,fp);
	for(int i=0;i<o;i++){	
		fputs(list[i].num,fp);
		fputs(hehe3,fp);
		fputs(list[i].name,fp);
		fputs(hehe3,fp);
		fputs(list[i].area,fp);
		fputs(hehe3,fp);
		fputs(list[i].jiage,fp);
		fputs(hehe3,fp);
		fputs(list[i].qingkuang,fp);
		fputs(hehe3,fp);
		fputs(list[i].time,fp);
	//	fputs(hehe3,fp);
		fputs(hehe4,fp);
	}
	fclose(fp);
	printf("修改之后的学生信息已存入到了fangchan.txt文件里!\n\n");
	printf("修改之后的:\n");
	for(i=0;i<o;i++){
		printf("%s\t",list[i].num);
		printf("%s\t",list[i].name);
		printf("%s\t",list[i].area);
		printf("%s\t",list[i].jiage);
		printf("%s\t",list[i].qingkuang);
		printf("%s\n",list[i].time);
	}
	printf("\n\n");
}
//数据维护函数
void WeiHu(){    
	int choice;
	while(true){		//数据维护界面
		printf("\n\n***********************************************\n");
		printf("------------读取数据    请输入 1  ------------\n");
		printf("------------录入数据    请输入 2  ------------\n");
		printf("------------修改数据    请输入 3  ------------\n");
		printf("------------删除数据    请输入 4  ------------\n");
		printf("------------退出        请输入 0  ------------\n");
		printf("------------您的选择是:");
		scanf("%d",&choice);
		switch(choice){
		case 1:  Output();
			break;
		case 2:  Input();		//输入
			break;
		case 3:  XiuGai();		//修改
			break;
		case 4:	 Delete();		//删除
			break;
		case 0: printf("谢谢使用数据维护功能!\n\n");		//退出
			return;
		}
	}
}
//统计函数
void TongJi(){
	yi y;
	wei w;
	int a,b,c,d;
	int m,n,mn;
	y.area=0;y.average=0;y.num=0;
	w.area=0;w.average=0;w.num=0;
	FILE *fp;
	char haha[36]={"NULL"};
	char hehe[36];
	char hehe2[36];
	if((fp=fopen("D:\\fangchan.txt","rt"))==NULL){
		printf("文件打开失败!\n");
		exit(0);
	}
	fgets(haha,36,fp);
	while(fgets(hehe,35,fp)!=NULL){
		substring(hehe,0,3,hehe2);
		substring(hehe,4,8,hehe2);
		substring(hehe,13,2,hehe2);
		a=hehe2[0]-'0';
		b=hehe2[1]-'0';
		m=a*10+b;
		substring(hehe,16,4,hehe2);
		a=hehe2[0]-'0';
		b=hehe2[1]-'0';
		c=hehe2[2]-'0';
		d=hehe2[3]-'0';
		n=a*1000+b*100+c*10+d;
		substring(hehe,21,1,hehe2);
		mn=hehe2[0]-'0';
		substring(hehe,23,8,hehe2);
		if(mn==0){
			w.area+=m;
			w.average+=n;
			w.num++;
		}else if(mn==1){
			y.num++;
			y.area+=m;
			y.average+=n;
		}
	}
	fclose(fp);
	printf("\n已售房产信息为:\n数量为:%d\t总面积为:%d\t平均价格为:%d\n\n",y.num,y.area,y.average);
	printf("\n未售房产信息为:\n数量为:%d\t总面积为:%d\t平均价格为:%d\n\n",w.num,w.area,w.average);
}
//分析函数
void FenXi(){
	
}
//退出函数
void TuiChu(){
	printf("\n谢谢您的使用!\n\n");
}
//主函数
int main(){
	int choice;
	while(true){		//用户主界面
		printf("**********欢迎使用房产信息统计与分析系统**********\n");
		printf("----------数据维护    请输入 1   -----------------\n");
		printf("----------统    计    请输入 2   -----------------\n");
		printf("----------价格分析    请输入 3   -----------------\n");
		printf("----------退    出    请输入 0   -----------------\n");
		printf("请输入您的选择:");
		scanf("%d",&choice);		//输入选择
		//异常处理
		if(choice<0||choice>3){
			printf("请输入 0--3 之间的数!\n");
			continue;
		}
		//根据选择进入相应的功能函数
		switch(choice){
		case 1: WeiHu();
			break;
		case 2: TongJi();
			break;
		case 3: FenXi();
			break;
		case 0: TuiChu();
			exit(0);
		}
	}
	return 0;
}

主界面

 

数据维护界面

 

读取数据

 

录入界面

验证 已加入

 

修改界面

 

删除界面

 

数据维护 退出界面

 

 

统计界面

抱歉!评论已关闭.