C語言接口與實現(xiàn)方法實例詳解
本文以實例形式詳細講述了C語言接口與實現(xiàn)方法,對于深入掌握C語言程序設(shè)計有一定的借鑒價值。分享給大家供大家參考。具體分析如下:
一般來說,一個模塊有兩部分組成:接口和實現(xiàn)。接口指明模塊要做什么,它聲明了使用該模塊的代碼可用的標識符、類型和例程,實現(xiàn)指明模塊是如何完成其接口聲明的目標的,一個給定的模塊通常只有一個接口,但是可能會有許多種實現(xiàn)能夠提供接口所指定的功能。每個實現(xiàn)可能使用不同的算法和數(shù)據(jù)結(jié)構(gòu),但是它們都必須符合接口所給出的使用說明。客戶調(diào)用程序是使用某個模塊的一段代碼,客戶調(diào)用程序?qū)虢涌冢鴮崿F(xiàn)導出接口。由于多個客戶調(diào)用程序是共享接口和實現(xiàn)的,因此使用實現(xiàn)的目標代碼避免了不必要的代碼重復,同時也有助于避免錯誤,因為接口和實現(xiàn)只需一次編寫和調(diào)試就可多次使用。
接口
接口只需要指明客戶調(diào)用程序可能使用的標識符即可,應盡可能地隱藏一些無關(guān)的表示細節(jié)和算法,這樣客戶調(diào)用程序可以不必依賴于特定的實現(xiàn)細節(jié)。這種客戶調(diào)用程序和實現(xiàn)之間的依賴--耦合----可能會在實現(xiàn)改變時引起錯誤,當這種依賴性埋藏在一些關(guān)于實現(xiàn)隱藏的或是不明確的假設(shè)中時,這些錯誤可能很難修復,因此一個設(shè)計良好且描述精確的接口應該盡量減少耦合。
C語言對接口和實現(xiàn)的分離只提供最基本的支持,但是簡單的約定能給接口/實現(xiàn)方法論帶來巨大的好處。在C中,接口在頭文件聲明,頭文件聲明了客戶調(diào)用程序可以使用的宏、類型、數(shù)據(jù)結(jié)構(gòu)、變量以及例程。用戶使用C語言的預處理指令#include導入接口。
下面的例子說明了本篇文章的接口中所使用的一些約定、接口:
extern int Arith_max(int x, int y); extern int Arith_min(int x, int y); extern int Arith_div(int x, int y); extern int Arith_mod(int x, int y); extern int Arith_ceiling(int x, int y); extern int Arith_floor (int x, int y);
該接口的名字為Arith,接口頭文件也相應地命名為arith.h,接口的名字以前綴的形式出現(xiàn)在接口的每個標識符中。模塊名不僅提供了合適的前綴,而且還有助于整理客戶調(diào)用程序代碼。
Arith接口還提供了一些標準C函數(shù)庫中沒有但是很有用的函數(shù),并為出發(fā)和取模提供了良好的定義,而標準C中并沒有給出這些操作的定義和只提供基于實現(xiàn)的定義。
實現(xiàn)
一個實現(xiàn)導出一個接口,它定義了必要的變量和函數(shù)以提供接口所規(guī)定的功能,在C語言中,一個實現(xiàn)是由一個或多個.c文件提供的,一個實現(xiàn)必須提供其導出的接口所指定的功能。實現(xiàn)應包含接口的.h文件,以保證它的定義和接口的聲明時一致的。
Arith_min和Arith_max返回其整型參數(shù)中的最小值和最大值:
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返回相應的余數(shù)。當x與y同號的時候,Arith_div(x,y)等價于x/y,Arith_mod(x,y)等價于x%y
當x與y的符號不同的時候,C的內(nèi)嵌操作的返回值就取決于具體的實現(xiàn):
如果-13/5=2,-13%5=-3,如果-13/5=-3,-13%5=2
標準庫函數(shù)總是向零取整,因此div(-13,2)=-2,Arith_div和Arith_mod的語義同樣定義好了:它們總是趨近數(shù)軸的左側(cè)取整,因此Arith_div(-13,5)=-3,Arith_div(x,y)是不超過實數(shù)z的最大整數(shù),其中z滿足z*y=x。
Arith_mod(x,y)被定義為x-y*Arith_div(x,y)。因此Arith_mod(-13,5)=-13-5*(-3)=2
函數(shù)Arith_ceiling和Arith_floor遵循類似的約定,Arith_ceiling(x,y)返回不小于實數(shù)商x/y的最小整數(shù)
Arith_floor(x,y)返回不超過實數(shù)商x/y的最大整數(shù)
完整實現(xiàn)代碼如下:
#include "arith.h" int Arith_max(int x, int y) { return x > y ? x : y; } int Arith_min(int x, int y) { return x > y ? y : x; } int Arith_div(int x, int y) { if (-13/5 == -2 && (x < 0) != (y < 0) && x%y != 0) return x/y - 1; else return x/y; } int Arith_mod(int x, int y) { if (-13/5 == -2 && (x < 0) != (y < 0) && x%y != 0) return x%y + y; else return x%y; } int Arith_floor(int x, int y) { return Arith_div(x, y); } int Arith_ceiling(int x, int y) { return Arith_div(x, y) + (x%y != 0); }
抽象數(shù)據(jù)類型
抽象數(shù)據(jù)類型(abstract data type,ADT)是一個定義了數(shù)據(jù)類型以及基于該類型值提供的各種操作的接口
一個高級類型是抽象的,因為接口隱藏了它的表示細節(jié),以免客戶調(diào)用程序依賴這些細節(jié)。下面是一個抽象數(shù)據(jù)類型(ADT)的規(guī)范化例子--堆棧,它定義了該類型以及五種操作:
#ifndef STACK_INCLUDED #define STACK_INCLUDED #define T Stack_T typedef struct T *T; extern T Stack_new (void); extern int Stack_empty(T stk); extern void Stack_push (T stk, void *x); extern void *Stack_pop (T stk); extern void Stack_free (T *stk); #undef T #endif
實現(xiàn)
包含相關(guān)頭文件:
#include <stddef.h> #include "assert.h" #include "mem.h" #include "stack.h" #define T Stack_T
Stack_T的內(nèi)部是一個結(jié)構(gòu),該結(jié)構(gòu)有個字段指向一個棧內(nèi)指針的鏈表以及一個這些指針的計數(shù):
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)將分配該結(jié)構(gòu)的一個實例,并將其指針賦給p,因此Stack_new中使用它就可以分配一個新的Stack_T
當count=0時,Stack_empty返回1,否則返回0:
int Stack_empty(T stk) { assert(stk); return stk->count == 0; }
assert(stk)實現(xiàn)了可檢查的運行期錯誤,它禁止空指針傳給Stack中的任何函數(shù)。
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是另一個接口中定義的釋放宏指令,它釋放指針參數(shù)所指向的空間,然后將參數(shù)設(shè)為空指針
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); }
完整實現(xiàn)代碼如下:
#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); }
相信本文所述對大家的C程序設(shè)計有一定的借鑒價值。
相關(guān)文章
基于OpenCV和C++ 實現(xiàn)圖片旋轉(zhuǎn)
這篇文章主要介紹了基于OpenCV和C++ 實現(xiàn)圖片旋轉(zhuǎn),幫助大家更好的利用c++處理圖片,感興趣的朋友可以了解下2020-12-12C++ Vector 動態(tài)數(shù)組的實現(xiàn)
這篇文章主要介紹了C++ Vector 動態(tài)數(shù)組的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-01-01