重慶分公司,新征程啟航
為企業提供網站建設、域名注冊、服務器等服務
為企業提供網站建設、域名注冊、服務器等服務
#includestdio.h
創新互聯是一家企業級云計算解決方案提供商,超15年IDC數據中心運營經驗。主營GPU顯卡服務器,站群服務器,成都服務器托管,海外高防服務器,服務器機柜,動態撥號VPS,海外云手機,海外云服務器,海外服務器租用托管等。
#define N 26
typedef struct
{
char key; //datatype other;
}table;
// table R[N+1];//可以不用全局變量滴
int blksearch(table R[],char K)//分塊查找
{
int low1,mid,high1;
low1=0;
high1=N - 1;//二分查找區間上下界初值
while(low1=high1)
{
mid=(low1+high1)/2;
if(K R[mid].key)
high1=mid-1;
else
if(K R[mid].key)
low1=mid+1;
else
return mid; //如果找到key,立即返回key的位置
}
return -1;// 只要上面的return語句沒執行,則原結構體數組中無key,返回-1
}
//也不曉得你為什么要用結構體數組,一個一般的數組就可以滴
void main()
{
int num, i;
table R[N + 1];
char K;
for(i = 0;iN;i++)
R[i].key='a'+i;
printf("\nplease input the key number:\n");
K=getchar();
num=blksearch(R,K);
if(num!=-1)
printf("第%d個是關鍵字\n",num+1);
else
printf("查找失敗!\n");
}
#include stdio.h
#include stdlib.h
#include string.h
const char filename[]="查詢結果.txt";
FILE *fp;
struct ticket
{
char banci[20]; //班次
char shifadi[20]; //始發地
char zhongdian[20];//終點站
int date; //日期
int rest; //剩余票數
struct ticket *next;
}Node;
//1、創建鏈表
struct ticket *creat(int n)
{
struct ticket *head,*tail,*newnode;
int i;
head=(struct ticket *)malloc(sizeof(Node));
head-next=NULL;
tail=head;
printf("車輛班次、始發地、終點站、日期(月 日 如九月六日0906)、剩余票數\n");
for(i=0;in;i++)
{
newnode=(struct ticket *)malloc(sizeof(Node));
printf("請輸入第%d次的數據:\n",i+1);
scanf("%s",newnode-banci);
scanf("%s",newnode-shifadi);
scanf("%s",newnode-zhongdian);
scanf("%d",newnode-date);
scanf("%d",newnode-rest);
tail-next=newnode;
tail=newnode;
}
tail-next=NULL;
return(head);
}
//4、瀏覽
void print(struct ticket *head)
{
struct ticket *p;
p=head-next;
fp=fopen(filename,"ab+");
printf("班次\t始發地\t終點地\t日期\t剩余票數 \n");
fprintf(fp,"班次\t始發地\t終點地\t日期\t剩余票數 \n");
while(p!=NULL)
{
fprintf(fp,"%s\t%s\t%s\t%d\t%d \n",p-banci,p-shifadi,p-zhongdian,p-date,p-rest);
printf("%s\t%s\t%s\t%d\t%d \n",p-banci,p-shifadi,p-zhongdian,p-date,p-rest);
p=p-next;
}
fclose(fp);
}
//2、增加班次
struct ticket *insert (struct ticket *head)
{
struct ticket *newnode,*p, *q;
printf("輸入增加的車輛班次、始發地、終點站、日期(月 日 如九月六日09 06)、剩余票數\n");
scanf("%s",Node.banci);
scanf("%s",Node.shifadi);
scanf("%s",Node.zhongdian);
scanf("%d",Node.date);
scanf("%d",Node.rest);
newnode=(struct ticket *)malloc(sizeof(Node));
strcpy(newnode-banci,Node.banci);
newnode-date=Node.date;
newnode-rest=Node.rest;
strcpy(newnode-shifadi,Node.shifadi);
strcpy(newnode-zhongdian,Node.zhongdian);
p=head-next;
if (p==NULL)
{
head-next=newnode;
newnode-next=NULL;
}else{
while(p!=NULL)
{
q=p;
p=p-next;
}
q-next=newnode;
newnode-next=NULL;
}
return (head);
}
//6、訂票
struct ticket *book(struct ticket *head,char b[],int n)
{
struct ticket *p;
p=head-next;
if(n==1)
{
while(p!=NULLstrcmp(b,p-banci)!=0){
p=p-next;
}
if(p==NULL)
printf("你所預定的班次不存在");
if(strcmp(b,p-banci)==0)
{
if(p-rest0)
{
p-rest=p-rest-1;
printf("訂票成功");
}
else{
printf("票已售完");
}
}
}
if(n==2)
{
while(p!=NULLstrcmp(b,p-banci)!=0){
p=p-next;
}
if(p==NULL){
printf("你所退定的班次不存在");
}
if(strcmp(b,p-banci)==0)
{
if(p-rest0)
{
p-rest=p-rest+1;
printf("退票成功");
}
}
}
return (head);
}
//3、刪除班次
struct ticket *del(struct ticket *head,char b[])
{
struct ticket *p,*q;
p=head-next;
while(p!=NULLstrcmp(b,p-banci))
{
q=p;
p=p-next;
}
if (p==NULL)
{
printf("未找到你要刪除的班次!~、\n");
}else{
if((p==head-next)(strcmp(b,p-banci)==0))
{
if (p-next==NULL)
{
free(p);
head-next=NULL;
}else{
head-next=p-next;
free(p);
}
printf("刪除成功!~、\n");
}else if((p!=head-next)(strcmp(b,p-banci)==0))
{
if (p-next==NULL)
{
free(p);
q-next=NULL;
}else{
q-next=p-next;
free(p);
}
printf("刪除成功!~、\n");
}
}
return (head);
}
//5、查詢
struct ticket *chaxun1(struct ticket *head,char a[])
{
struct ticket *p;
p=head-next;
printf("班次\t始發地\t終點地\t日期\t剩余票數 \n");
while(p!=NULL)
{
if(strcmp(p-banci,a)==0)
{
printf("%s\t%s\t%s\t%d\t%d \n",p-banci,p-shifadi,p-zhongdian,p-date,p-rest);
break;
}
else
p=p-next;
}
if(p==NULL){
printf("查詢班次不存在\n");
}
return(head);
}
struct ticket *chaxun2(struct ticket *head,char a[])
{
struct ticket *p;
p=head-next;
printf("班次\t始發地\t終點地\t日期\t剩余票數 \n");
while(p!=NULL)
{
if(strcmp(p-shifadi,a)==0)
{
printf("%s\t%s\t%s\t%d\t%d \n",p-banci,p-shifadi,p-zhongdian,p-date,p-rest);
break;
}
else
p=p-next;
}
return(head);
}
struct ticket *chaxun3(struct ticket *head,int m)
{
struct ticket *p;
p=head-next;
printf("班次\t始發地\t終點地\t日期\t剩余票數 \n");
while(p!=NULL)
{
if(p-date==m)
{
printf("%s\t%s\t%s\t%d\t%d \n",p-banci,p-shifadi,p-zhongdian,p-date,p-rest);
break;
}
else
p=p-next;
}
return(head);
}
void main()
{
printf("=============================車票查詢訂購系統===============================\n");
printf("1、讀入車輛班次初始化信息\n");
printf("2、增加班次信息\n");
printf("3、刪除班次信息\n");
printf("4、瀏覽所有班次\n");
printf("5、查詢\n");
printf("6、訂票退票\n");
printf("7、退出\n");
while(1)
{
int i,n,m,x;
char a[20],d[20],e[20];
struct ticket *head;
printf("請輸入要使用的業務前相應的數字:\t");
scanf("%d",i);
if(i==7)
break;
else
switch(i)
{
case 1: printf("輸入錄入的個數:\t");
scanf("%d",n);;
head=creat(n);
break;
case 2:
insert(head);
break;
case 3:
printf("輸入要刪除的班次:\t");
scanf("%s",e);
del(head,e);
break;
case 4:
print(head);
break;
case 5:printf("1、按班次查詢:\t");
printf("2、按始發站查詢:\t");
printf("3、按日期查詢:\t");
scanf("%d",x);;
switch(x)
{
case 1:printf("輸入要查詢班次:\t");
scanf("%s",d);
chaxun1(head,d);break;
case 2:printf("輸入要查詢的始發站(請查詢后輸入有效的始發站):\t");
scanf("%s",d);
chaxun2(head,d);break;
case 3:printf("輸入要查詢的日期(格式0101):\t");
scanf("%d",m);;
chaxun3(head,m);break;
}
break;
case 6:
printf("訂票輸入1,退票輸入2:\t");
scanf("%d",m);;
printf("輸入你要訂或退的班次:\t");
scanf("%s",a);
book(head,a,m);
break;
}
}
}
看了這個C語言版的車票訂購查詢系統吧,想必樓主會非常明白什么叫分塊了,一個三百多行的程序 ,而主函數就占了50行左右,函數功能分工明確,各負其職,連在一起就可以組成一個有著增、刪、改、查等功能的一個簡單系統。分工其實就是把某一功能的代碼放一起,避免重復使用,也使主函數簡單明了。樓主覺得呢?
可以,使用文件包含就可以了,比如你的主文件(包含main函數的)叫main.c,你可以寫一個文件專門放置函數,叫func.h,然后在main.c中添加#include "func.h",編譯時只需要編譯main.c就可以了..舉一反三,你想分成幾個文件都可以,
注意,func.h這個文件名,其實你也可以叫做func.c,甚至不加任何后綴,直接使用func也是可以的,但是后綴.h更能體現此文件的性質..
先分兩塊a,b,其中a任意個比b任意個都大,先判斷在a還是在b,繼續分,找,so easy
i=idx[low1].low是塊中第一個元素的起始位置的值
int blksearch(sqlist r,index idx,find=0,hb;) // bn為塊個數 //
{ int i,;low=1,high1=bn,midl,find=0,hb;
while(low1=high1!find)
{mid=(low1+high1)/2;
if(kidx[mid1].key)high1=mid-1;
else if(kidx[mid1],key)low1=mid1+1;
else{
low=mid1;
find=1;
}
到這里是初步鎖定要查的元素在那個塊,找到大的方向后 在塊里進行進一步的搜索
if(low1bn)//如果low1的值沒有超過塊的總個數
i=idx[low1].low; //i賦值為該塊內第一個元素的起始位置
然后進一步查到元素
/*
假定數據在文件中的存放格式如下:
4, 6 (4表示行數,6表示列數)
10, 30, 20, 15, 25, 5
100, 200, 150, 250, 300, 50
500, 550, 510, 450, 580, 400
1000, 1500, 1200, 2000, 1800, 1300
代碼僅供參考,如發現錯漏之處,可發消息給我。
*/
#include stdio.h
#include stdlib.h
#include malloc.h
/* 從小到大排序數組 */
void ArraySort(int *a, int s)
{
int i, j, k, t;
for (i = 0; i s-1; ++i)
{
k = i;
for (j = i + 1; j s; ++j)
{
if (a[k] a[j])
{
k = j;
}
}
if (k != i)
{
t = a[i];
a[i] = a[k];
a[k] = t;
}
}
}
/* 使用折半查找定位k所在的區塊 */
int Partition(int *a, int s, int c, int k)
{
int bottom = 0;
int top = s - 1;
while (bottom = top)
{
int middle = (bottom + top) / 2;
if (k = a[middle * c] k = a[middle * c + c - 1])
{
return middle;
}
else
{
if (k a[middle * c])
top = middle - 1;
else
bottom = middle + 1;
}
}
return -1;
}
/* 執行分塊查找 */
int PartSearch(int *a, int s, int c, int k)
{
int i, end, ps = s / c;
int pid = Partition(a, ps, c, k);
printf("%d %d %d\n", ps, pid, c);
if (pid != -1)
{
end = pid * c + c;
for (i = pid * c; i end; ++i)
if (k == a[i])
return i;
}
for (i = ps * c; i s; ++i)
{
if (k == a[i])
return i;
}
return -1;
}
int main()
{
int i, j, ls, lc, k, s = 0, t, *ia = NULL;
char choice;
FILE *fp = fopen("data.txt", "r");
if (NULL == fp)
{
printf("Can't open file.\n");
exit(-1);
}
fscanf(fp, "%d, %d", ls, lc);
ia = (int *)malloc(ls * lc * sizeof(int));
if (NULL == ia)
{
printf("Failed to allocate memory.\n");
exit(-1);
}
for (i = 0; i ls; ++i)
{
for (j = 0; j lc-1; ++j)
fscanf(fp, "%d,", ia[s++]);
fscanf(fp, "%d", ia[s++]);
}
ArraySort(ia, s);
for (i = 0; i s; ++i)
{
printf("%d ", ia[i]);
if (0 == (i+1) % 6)
printf("\n");
}
do
{
printf("\nPlease input the data want to find: ");
scanf("%d", k);
getchar();
printf("Searching...\n");
t = PartSearch(ia, s, lc, k);
if (t != -1)
printf("%d is found in array.\n", ia[t]);
else
printf("%d is not found in array.\n", k);
printf("\nPress 'y' to continue,any other key to exit..\n");
choice = getchar();
} while (choice=='y'||choice=='Y');
fclose(fp);
free(ia);
getchar();
return 0;
}