/************************************************************************************/
/* 著作権所有者 : /
/* 文件名 :電子地圖管理系統 */
/* 內容 : 對一個二進制地圖文件進行處理,排序,檢索,更新 */
/* 日期:2015-7-31 */
/* */
/* */
/*************************************************************************************/
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<windows.h>
#include<malloc.h>
#include<conio.h>
void UI1(); //基本服務界面
void UI2();//會持續出現的服務選擇界面
void server();//服務信息,提示用戶輸入數字
int rf();
void sort();
void search();
void RD(FILE *p);
void update();
int nsizecount=2;
int n = rf();
struct RoadRecord
{
short ussize;
long ullinkId;
int usdispclass;
int usbrunch;
int usroadnameflag;
char roadname[20];
};
struct Map_t
{
struct RoadRecord road[700000] ;
}*s,*ss;
struct Map_t road_t;
int M_t1 = 0;
int M_t2 = 0;
#define MCGETLONG(data) ( ( ((unsigned long)MCGETSHORT((data)))<<16 )|( ((unsigned long)MCGETSHORT((char *)(data)+2))&0x0000ffff))
#define MCGET3BN(data) ( (((unsigned long )MCGETCHAR((data)))<<16)|(((unsigned long)MCGETSHORT((char *)(data+1))&0x0000ffff))
#define MCGETCHAR(data) (*((char *)(data)))
#define MCGETSHORT(data)((unsigned short)((((unsigned short)(*((char *)(data))))<<8)|(((unsigned short)(*((char *)(data)+1)))&0x00ff)))
#define Length 700000
void UI1(){
printf("/********************---Welcome!---*******************");
printf("\n");
printf("/*** 電子地圖導航系統 ***/");
printf("\n");
printf("/*** team9 陳永勝 李想 孫林桐 ***/");
printf("\n ");
printf("/*** 2015.7.31 ***/");
printf("\n ");
printf("/****************************************************/");
}
void UI2(){
printf("\n");
printf("請選擇服務的種類:");
printf("\n");
printf(" 1.讀取文件(“./data/GTBL.dat”)");
printf("\n");
printf(" 2.排序(快速排序)並輸出排序結果(“./data/SortGTBL.dat”)");
printf("\n");
printf(" 3.檢索");
printf("\n");
printf(" 4.更新");
printf("\n");
printf(" 0.退出");
printf("\n");
printf(" 請選擇");
}
void server(){
int n;
FILE *p;
scanf("%d",&n);
switch(n){
case 1:
rf(); //調用讀取文件函數
printf("文件讀取成功!\n");
printf("Success!\n");
s=(struct Map_t*)malloc(sizeof(struct Map_t)*(nsizecount-2));
ss=(struct Map_t*)malloc(sizeof(struct Map_t)*(nsizecount-2));
p=fopen("d:\\data\\a.txt","r");
M_t1++;
RD(p);
UI2();
server();
break;
case 2:
if(M_t1 == 0){
printf("請先讀取文件,輸入1 \n");
UI2();
server();
}
else if(M_t1 == 1){
M_t2++;
sort();
} //調用排序文件函數
break;
case 3:
if(M_t1 ==0 || M_t2 ==0)
{
printf("先讀取並排序文件,才可以檢索\n");
UI2();
server();
}
else
{
search();
}
//調用搜索文件函數
break;
case 4:
if(M_t1 ==0 || M_t2 ==0)
{
printf("先讀取並排序文件,才可以更新\n");
UI2();
server();
}
else
{
update();
}//調用更新文件函數
break;
case 0:
exit(0); //調用退出函數
break;
default :
printf("輸入錯誤請重新輸入:");
server();
break;
}
}
int rf(){
int m;
char aclinkId[4];
char acroadnamesize[2];
char acRecordinfo[4];
unsigned short ustotalsize;
unsigned long ullinkId;
unsigned short usroadnamesize;
char actotalsize[2];
FILE *p; FILE *pl;
p=fopen("f:\\data\\GTBL.dat","rb+");
pl=fopen("f:\\data\\a.txt","w");
if(p==NULL)
{
printf("文件不存在!\n");
}
else
{
while(fread(actotalsize,sizeof(actotalsize),1,p)==1)
{
fread(aclinkId,sizeof(aclinkId),1,p);
fread(acroadnamesize,sizeof(acroadnamesize),1,p);
fread(acRecordinfo ,sizeof(acRecordinfo),1,p);
ustotalsize=MCGETSHORT(actotalsize);
ullinkId=MCGETLONG(aclinkId);
usroadnamesize=MCGETSHORT(acroadnamesize);
road_t.road[nsizecount].ussize=ustotalsize;
road_t.road[nsizecount].ullinkId=ullinkId;
m=(int)acRecordinfo[3]&255;
road_t.road[nsizecount].usdispclass=m&15;
road_t.road[nsizecount].usbrunch=(m&112)/16;
road_t.road[nsizecount].usroadnameflag=(m&128)/128;
fread(road_t.road[nsizecount].roadname,sizeof(char),ustotalsize-12,p);
fprintf(pl,"#\t");
fprintf(pl,"LinkID=");
fprintf(pl,"%d\t",road_t.road[nsizecount].ullinkId);
fprintf(pl,"flag=");
fprintf(pl,"%d\t",road_t.road[nsizecount].usroadnameflag);
fprintf(pl,"brunch");
fprintf(pl,"%d\t",road_t.road[nsizecount].usbrunch);
fprintf(pl,"dispclass=");
fprintf(pl,"%d\t",road_t.road[nsizecount].usdispclass);
fprintf(pl,"Roadname=1=");
fprintf(pl,"%s\t",road_t.road[nsizecount].roadname+4);
fprintf(pl,"#");
fprintf(pl,"\n");
nsizecount++;
}
fclose(p);
fclose(pl);
// printf("%d",nsizecount);
}
return nsizecount;
}
void update(){
}
void search(){
printf("請選擇檢索的方式:\n");
printf(" 1.指定linkID檢索\n");
printf(" 2.指定交叉Link列表示ClassF番號 檢索\n");
printf(" 3.指定查找岔路數 檢索\n");
printf(" 4.指定道路名稱 檢索\n");
printf(" 0.返回\n");
int n;
int linkID;
int fh;
int cl;
int count1=0;
int count2=0;
char name;
scanf("%d",&n);
int count = 0;
switch(n)
{
case 1:
int low,high;
int key;
int mid;
low = 0;
high = nsizecount - 1;
printf("\n請輸入LinkID:");
scanf("%d",key);
while(low<high)
{
count++;
mid = (low + high)/2;
if(key<road_t.road[mid].ullinkId)
high = mid - 1;
else if(key > road_t.road[mid].ullinkId)
low = mid + 1;
else if(key==road_t.road[mid].ullinkId)
{
printf("\n\n LinkID=%s ",road_t.road[mid].ullinkId);
printf("flag=%s ",road_t.road[mid].usroadnameflag);
printf("brunch=%s",road_t.road[mid].usbrunch);
printf("displace=%s",road_t.road[mid].usdispclass);
printf("Roadname=%s",road_t.road[mid].roadname+4);
break;
}
}
int i;
for(i=0;i<=1000;i++)
{
printf("%d\n",road_t.road[i].ullinkId);
}
break;
case 2:
printf("\t請輸入交叉link列表示class番號:");
scanf("%s",&fh);
for (i=0;i<n-2;i++)
{
if (fh==road_t.road[i].usdispclass)
{
printf("%d",road_t.road[i].usdispclass);
count1++;
}
if (count1==0)
{
printf("\n沒有匹配結果,請重新選擇");
}
}
break;
case 3:
printf("\t請輸入岔路數:");
scanf("%d",&cl);
for (i=0;i<n-2;i++){
if (cl==road_t.road[i].usbrunch)
{
printf("%d",road_t.road[i].usbrunch);
count2++;
}
}
if (count2==0)
{
printf("\n沒有匹配結果,請重新選擇");
break;
}
break;
case 4:
printf("請輸入道路名稱:");
scanf("%s",name);
break;
case 0:
system("cls");
UI2();
server();
break;
default:
printf("輸入錯誤,請重新輸入\n");
search();
}
}
void sort(){
}
void RD(FILE *p)
{
if( M_t1 != 0)
{
int i=0;
printf("\n\t\t\t文件信息----寫入內存中... ...\n");
for(i=0;i<n-2;i++)
{
fscanf(p,"%s",road_t.road[i].ullinkId);
fscanf(p,"%s",road_t.road[i].usroadnameflag);
fscanf(p,"%s",road_t.road[i].usbrunch);
fscanf(p,"%s",road_t.road[i].usdispclass);
fscanf(p,"%s",road_t.road[i].roadname);
}
if(road_t.road[5].ullinkId != NULL)
printf("\n\t\t\t 文件信息----寫入內存成功\n\n");
UI1();
server();
}
else
{
UI1();
server();
}
}
int main()
{
UI1();
UI2();
server();
return 0;
}
讀文件的一個錯誤
有關指針的一個錯誤
一個空指針錯誤
----------------------biu~biu~biu~~~在下問答機器人小D,這是我依靠自己的聰明才智給出的答案,如果不正確,你來咬我啊!