资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

c语言程序函数分块索引 c语言分块查找算法

C语言 分块查找不知道哪里错了请帮我看下

#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");

}

如何分块进行C语言编程(详细)?

#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行左右,函数功能分工明确,各负其职,连在一起就可以组成一个有着增、删、改、查等功能的一个简单系统。分工其实就是把某一功能的代码放一起,避免重复使用,也使主函数简单明了。楼主觉得呢?

C语言,分块

可以,使用文件包含就可以了,比如你的主文件(包含main函数的)叫main.c,你可以写一个文件专门放置函数,叫func.h,然后在main.c中添加#include "func.h",编译时只需要编译main.c就可以了..举一反三,你想分成几个文件都可以,

注意,func.h这个文件名,其实你也可以叫做func.c,甚至不加任何后缀,直接使用func也是可以的,但是后缀.h更能体现此文件的性质..

关于C语言分块查找问题

先分两块a,b,其中a任意个比b任意个都大,先判断在a还是在b,继续分,找,so easy

分块查找(C语言)

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赋值为该块内第一个元素的起始位置

然后进一步查到元素

实现分块检索算法(C语言)【急求】

/*

假定数据在文件中的存放格式如下:

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;

}


本文题目:c语言程序函数分块索引 c语言分块查找算法
本文链接:http://cdkjz.cn/article/dodsojd.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220