用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - c++代码库

各种查找算法

2012-09-23 作者: 神马举报

[c++]代码库

#include <stdio.h>
typedef int KeyType;
typedef struct
{
	KeyType key;
	int maths;
	int english;
} ElemType;
#define EQ(a,b)  ((a)==(b))
#define LT(a,b)  ((a)< (b))
#define LQ(a,b)  ((a)<=(b))

typedef struct
{
	ElemType *elem;
	int length;
} SSTable;

int Search_Seq ( SSTable ST,KeyType key )
{
	int i;
	ST.elem[0].key=key;
	for ( i=ST.length; !EQ ( ST.elem[i].key,key ); --i );
	return i;
}

int Search_Bin ( SSTable ST,KeyType key )
{
	int low,mid,high;
	low=1;
	high=ST.length;
	while ( low<=high )
	{
		mid= ( low+high ) /2;
		if EQ ( key,ST.elem[mid].key ) return mid;
		else if LT ( key,ST.elem[mid].key ) high=mid -1;
		else  low=mid +1;
	}
}

getdata ( SSTable * t )
{
	FILE *fp;
	int i=1;
	fp=fopen ( "stu.txt","r" );
	fscanf ( fp,"%d",& ( t->length ) );
	while ( i<=t->length )
	{
		fscanf ( fp,"%d %d %d",& ( t->elem[i].key ),
		         & ( t->elem[i].maths ),& ( t->elem[i].english )  );
		i++;
	}
	fclose ( fp );
}

main()
{
	ElemType stu[50];
	SSTable  class;
	int i,j,k;
	long time;
	class.elem=stu;


	getdata ( &class );

	printf ( "This class has %d students.\n",class.length );
	printf ( "Input stuno you want search:\n" );
	scanf ( "%d",&k );

	i=Search_Seq ( class,k );
	j=Search_Bin ( class,k );
	printf ( "Maths   English\n" );
	printf ( "%d       %d\n",class.elem[i].maths,class.elem[i].english );
	printf ( "%d       %d\n",class.elem[j].maths,class.elem[j].english );

	for ( i=1; i<=4; i++ )
	{
		j=stu[i].maths+stu[i].english;
		printf ( "%d\n",j );
	}

}


网友评论    (发表评论)

共1 条评论 1/1页

发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。


扫码下载

加载中,请稍后...

输入口令后可复制整站源码

加载中,请稍后...