C语言接口的定义与实现

以下是sort.h
#ifndef _sort_h
#define _sort_h
void SortIntegerArray(int array[], int n);
#endif
以下是sort.c
#include <stdio.h>
#include "sort.h"
static int FindSmallestInteger(int array[], int low, int high);
static void SwapIntegerElements(int array[], int p1, int p2);
void SortIntegerArray(int array[], int n)
{
int lh, rh;
for (lh = 0; lh < n; lh++) {
rh = FindSmallestInteger(array, lh, n-1);
SwapIntegerElements(array, lh, rh);
}
}
static int FindSmallestInteger(int array[], int low, int high)
{
int i, spos;
spos = low;
for (i = low; i <= high; i++) {
if (array[i] < array[spos]) spos = i;
}
return (spos);
}
static void SwapIntegerElements(int array[], int p1, int p2)
{
int tmp;
tmp = array[p1];
array[p1] = array[p2];
array[p2] = tmp;
}
以下是main.c
#include <stdio.h>
#include "sort.h"
#include "time.h"
main()
{
double start,finish,elapsed;
int a[]={1,22,111,234,22,33,44,0,989,678};
int n=10;
start=(double) clock()/CLOCKS_PER_SEC;
SortIntegerArray(a,n);
for(int i=0;i<10;i++)
printf("%3d\t",a[i]);
printf("\n");
finish=(double) clock()/CLOCKS_PER_SEC;
elapsed=finish-start;
printf("%f",elapsed);
return 0;
}
怎么改才能正确啊?谢谢,不能传文件,所以只有这样一个一个写了,麻烦回答一下,谢谢!!!
一楼,main()函数我最后return 0;了,这没问题呀
上面这程序是有问题的,麻烦指点。

一个模块有两部分组成:接口和实现。接口指明模块要做什么,它声明了使用该模块的代码可用的标识符、类型和例程,实现指明模块是如何完成其接口声明的目标的,一个给定的模块通常只有一个接口,但是可能会有许多种实现能够提供接口所指定的功能。每个实现可能使用不同的算法和数据结构,但是它们都必须符合接口所给出的使用说明。客户调用程序是使用某个模块的一段代码,客户调用程序导入接口,而实现导出接口。由于多个客户调用程序是共享接口和实现的,因此使用实现的目标代码避免了不必要的代码重复,同时也有助于避免错误,因为接口和实现只需一次编写和调试就可多次使用

实现
一个实现导出一个接口,它定义了必要的变量和函数以提供接口所规定的功能,在C语言中,一个实现是由一个或多个.c文件提供的,一个实现必须提供其导出的接口所指定的功能。实现应包含接口的.h文件,以保证它的定义和接口的声明时一致的。

Arith_min和Arith_max返回其整型参数中的最小值和最大值:

int Arith_max(int x, int y) {
return x > y ? x : y;
}
int Arith_min(int x, int y) {
return x > y ? y : x;
}
Arith_div返回y除以x得到的商,Arith_mod返回相应的余数。当x与y同号的时候,Arith_div(x,y)等价于x/y,Arith_mod(x,y)等价于x%y

当x与y的符号不同的时候,C的内嵌操作的返回值就取决于具体的实现:

eg.如果-13/5=2,-13%5=-3,如果-13/5=-3,-13%5=2

标准库函数总是向零取整,因此div(-13,2)=-2,Arith_div和Arith_mod的语义同样定义好了:它们总是趋近数轴的左侧取整,因此Arith_div(-13,5)=-3,Arith_div(x,y)是不超过实数z的最大整数,其中z满足z*y=x。

Arith_mod(x,y)被定义为x-y*Arith_div(x,y)。因此Arith_mod(-13,5)=-13-5*(-3)=2

函数Arith_ceiling和Arith_floor遵循类似的约定,Arith_ceiling(x,y)返回不小于实数商x/y的最小整数

Arith_floor(x,y)返回不超过实数商x/y的最大整数

完整实现代码如下:

arith.c
抽象数据类型
抽象数据类型(abstract data type,ADT)是一个定义了数据类型以及基于该类型值提供的各种操作的接口

一个高级类型是抽象的,因为接口隐藏了它的表示细节,以免客户调用程序依赖这些细节。下面是一个抽象数据类型(ADT)的规范化例子--堆栈,它定义了该类型以及五种操作:

stack.h
实现
包含相关头文件:

#include <stddef.h>
#include "assert.h"
#include "mem.h"
#include "stack.h"
#define T Stack_T
Stack_T的内部是一个结构,该结构有个字段指向一个栈内指针的链表以及一个这些指针的计数:

struct T {
int count;
struct elem {
void *x;
struct elem *link;
} *head;
};
Stack_new分配并初始化一个新的T:

T Stack_new(void) {
T stk;
NEW(stk);
stk->count = 0;
stk->head = NULL;
return stk;
}
其中NEW是一个另一个接口中的一个分配宏指令。NEW(p)将分配该结构的一个实例,并将其指针赋给p,因此Stack_new中使用它就可以分配一个新的Stack_T

当count=0时,Stack_empty返回1,否则返回0:

int Stack_empty(T stk) {
assert(stk);
return stk->count == 0;
}
assert(stk)实现了可检查的运行期错误,它禁止空指针传给Stack中的任何函数。

Stack_push和Stack_pop从stk->head所指向的链表的头部添加或移出元素:

void Stack_push(T stk, void *x) {
struct elem *t;
assert(stk);
NEW(t);
t->x = x;
t->link = stk->head;
stk->head = t;
stk->count++;
}
void *Stack_pop(T stk) {
void *x;
struct elem *t;
assert(stk);
assert(stk->count > 0);
t = stk->head;
stk->head = t->link;
stk->count--;
x = t->x;
FREE(t);
return x;
}
FREE是另一个接口中定义的释放宏指令,它释放指针参数所指向的空间,然后将参数设为空指针

void Stack_free(T *stk) {
struct elem *t, *u;
assert(stk && *stk);
for (t = (*stk)->head; t; t = u) {
u = t->link;
FREE(t);
}
FREE(*stk);
}
完整实现代码如下:

#include <stddef.h>
#include "assert.h"
#include "mem.h"
#include "stack.h"
#define T Stack_T
struct T {
int count;
struct elem {
void *x;
struct elem *link;
} *head;
};
T Stack_new(void) {
T stk;
NEW(stk);
stk->count = 0;
stk->head = NULL;
return stk;
}
int Stack_empty(T stk) {
assert(stk);
return stk->count == 0;
}
void Stack_push(T stk, void *x) {
struct elem *t;
assert(stk);
NEW(t);
t->x = x;
t->link = stk->head;
stk->head = t;
stk->count++;
}
void *Stack_pop(T stk) {
void *x;
struct elem *t;
assert(stk);
assert(stk->count > 0);
t = stk->head;
stk->head = t->link;
stk->count--;
x = t->x;
FREE(t);
return x;
}
void Stack_free(T *stk) {
struct elem *t, *u;
assert(stk && *stk);
for (t = (*stk)->head; t; t = u) {
u = t->link;
FREE(t);
}
FREE(*stk);
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2010-04-21
没问题,这是结果:
RTGSTS6[rtgs110]% make
rm -rf Main.o sort.o sort
echo 'this is MAKEOBJECT'
this is MAKEOBJECT
xlc -c Main.c
xlc -c sort.c
echo 'this is 'sort
this is sort
xlc -o sort Main.o sort.o
rm -rf Main.o sort.o
RTGSTS6[rtgs111]% ./sort
0 1 22 22 33 44 111 234 678 989
0.000000
倒是你得main()少了个int main(),这不会是你得错吧

++++++++++++++++++++++++++++++++++++++++

都给你结果了,还问有问题!我都跑出来了,没问题,老大!本回答被提问者采纳

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网