日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

高质量C /C编程指南---附录B :C /C试题和谜底

發布時間:2024/4/14 编程问答 48 豆豆
生活随笔 收集整理的這篇文章主要介紹了 高质量C /C编程指南---附录B :C /C试题和谜底 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

附錄B :C /C試題

?????? 本試題僅用于觀察C /C法度模范員的根基編程身手。內容限于C /C常用語法,不觸及數據構造、算法以及深奧的語法。考試效果能反映出考生的編程質量以及對C /C的見地探問程度,但不克不及反映考生的智力和軟件啟示妙技名堂。

?????? 口試時候90分鐘。請考生詳細答題,切勿蔑視。

?<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />


一、請填寫BOOL , float, 指針變量 與“零值”對比的 if 語句。(10分)


提示:這里“零值”可以是0, 0.0 , FALSE或許“空指針”。譬喻 int 變量 n 與“零值”對比的 if 語句為:

??? if ( n == 0 )

??? if ( n != 0 )

以此類推。

?


請寫出 BOOL? flag 與“零值”對比的 if 語句:

?


?



請寫出 float? x 與“零值”對比的 if 語句:

?


?


?



請寫出 char ?*p 與“零值”對比的 if 語句:

?


?



?


二、以下為Windows NT下的32位C 法度模范,請打定sizeof的值(10分)


?


?????? char? str[] = “Hello” ;

?????? char?? *p = str ;

int???? n = 10;

請打定

sizeof (str ) =???????

??? ?????

sizeof ( p ) =???????

??? ??????

sizeof ( n ) =

void Func ( char str[100])

{

請打定

?sizeof( str ) =???

}

?



void *p = malloc( 100 );

請打定

sizeof ( p ) =

?



?


三、簡答題(25分)


?


1、頭文件中的 ifndef/define/endif 干什么用?

?


?


?


2、#include? <filename.h>?? 和? #include? “filename.h” 有什么區別?

?


?


?


3、const 有什么用處?(請至多闡明');兩種)

?


?


?


4、在C 法度模范中挪用被 C編譯器編譯后的函數,為什么要加 extern “C”聲明?

?


?


?


?


5、請簡述以下兩個for輪回的優錯誤舛誤

?


// 第一個


for (i=0; i<N; i )


{


if (condition)


??? DoSomething();


else


??? DoOtherthing();


}


// 第二個


if (condition)


{


for (i=0; i<N; i )


??? DoSomething();


}


else


{


??? for (i=0; i<N; i )


??? DoOtherthing();


}



益處:


?


?


錯誤舛誤:


?


?


?


益處:


?


?


錯誤舛誤:


?


?



?


四、有關內存的考慮題(20分)


?


void GetMemory(char *p)


{


p = (char *)malloc(100);


}


void Test(void)

{

char *str = NULL;


GetMemory(str);??


strcpy(str, "hello world");


printf(str);


}


?


叨教運轉Test函數會有什么樣的效果?


答:

?


?


?


?


char *GetMemory(void)


{??


char p[] = "hello world";


return p;


}


void Test(void)


{


char *str = NULL;


str = GetMemory();???


printf(str);


}


?


叨教運轉Test函數會有什么樣的效果?


答:

Void GetMemory2(char **p, int num)


{


*p = (char *)malloc(num);


}

void Test(void)


{


char *str = NULL;


GetMemory(&str, 100);


strcpy(str, "hello");??


printf(str);???


}

叨教運轉Test函數會有什么樣的效果?


答:

?


?


?


?


?


void Test(void)


{


char *str = (char *) malloc(100);


??? strcpy(str, “hello”);


??? free(str);? ???


??? if(str != NULL)


??? {


??? ? strcpy(str, “world”);


printf(str);


}


}

叨教運轉Test函數會有什么樣的效果?


答:

?


?


?


?



?


?


五、編寫strcpy函數(10分)


已知strcpy函數的原型是

?????? char *strcpy(char *strDest, const char *strSrc);

?????? 其中strDest是目標字符串,strSrc是源字符串。

?


(1)不挪用C /C的字符串庫函數,請編寫函數 strcpy

?


?


?


?


?


?


?


(2)strcpy能把strSrc的內容復制到strDest,為什么還要char * 類型的前往值?

?


?


?


?


六、編寫類String的組織函數、析構函數和賦值函數(25分)


已知類String的原型為:

??? >

??? {


??? ? public:


??? ??? String(const char *str = NULL);??? // 淺易組織函數


??? ??? String(const String &other);??? ??? // 拷貝組織函數


??????? ~ String(void);??????????? ??? ??? ??? // 析構函數


??? ??? String & operate =(const String &other);??? // 賦值函數


??? ? private:


??????? char? ??? *m_data;??????? ??? ??? // 用于保存字符串


??? };


?????? 請編寫String的上述4個函數。

?


附錄C :C /C試題的謎底與評分尺度

一、請填寫BOOL , float, 指針變量 與“零值”對比的 if 語句。(10分)

?


請寫出 BOOL? flag 與“零值”對比的 if 語句。(3分)

尺度謎底:


??? if ( flag )


??? if ( !flag )

如下寫法均屬不良作風,不得分。

??? if (flag == TRUE)??


??? if (flag == 1 ) ???


??? if (flag == FALSE) ?


??? ??? if (flag == 0)? ???

請寫出 float? x 與“零值”對比的 if 語句。(4分)

尺度謎底示例:


const float EPSINON = 0.00001;


if ((x >= - EPSINON) && (x <= EPSINON)


弗成將浮點變量用“==”或“!=”與數字對比,應該設法轉化成“>=”或“<=”此類體式格局。

??

?


如下是錯誤的寫法,不得分。

??? if (x == 0.0)??


??? if (x != 0.0)?? ???


???

請寫出 char ?*p 與“零值”對比的 if 語句。(3分)

尺度謎底:


??? if (p == NULL)


??? if (p != NULL)

如下寫法均屬不良作風,不得分。

??? if (p == 0)


??? if (p != 0) ???


??? if (p) ?


??? ??? if (!)? ???

?


二、以下為Windows NT下的32位C 法度模范,請打定sizeof的值(10分)


?


?????? char? str[] = “Hello” ;

?????? char?? *p = str ;

int???? n = 10;

請打定

sizeof (str ) =? 6?? (2分)


??? ?????


sizeof ( p ) =?? 4?? (2分)


??? ??????


sizeof ( n ) =?? 4?? (2分)

void Func ( char str[100])

{

請打定

?sizeof( str ) =?? 4 ????(2分)


}

?



void *p = malloc( 100 );

請打定

sizeof ( p ) =? 4????? (2分)


?



?


三、簡答題(25分)


?


1、頭文件中的 ifndef/define/endif 干什么用?(5分)

答:防御該頭文件被頻頻援用。


?


2、#include? <filename.h>?? 和? #include? “filename.h” 有什么區別?(5分)

答:對付#include? <filename.h> ,編譯器從尺度庫途徑起頭搜索 filename.h


??? 對付#include? “filename.h” ,編譯器從用戶的使命途徑起頭搜索 filename.h


?


3、const 有什么用處?(請至多闡明');兩種)(5分)

答:(1)可以定義 const 常量


(2)const可以修飾函數的參數、前往值,以致函數的定義體。被const修飾的器械都遭到強制保護,可以防止不測的改不雅觀,能前進法度模范的強健性。


?


4、在C 法度模范中挪用被 C編譯器編譯后的函數,為什么要加 extern “C”? (5分)

答:C 言語支持函數重載,C言語不支持函數重載。函數被C 編譯后在庫中的名字與C言語的分歧。假定某個函數的原型為: void foo(int x, int y);


該函數被C編譯器編譯后在庫中的名字為_foo,而C 編譯器則會孕育發生像_foo_int_int之類的名字。


C 提供了C連接交流指定標記extern“C”來處理名字婚配題目。


?


5、請簡述以下兩個for輪回的優錯誤舛誤(5分)


?


for (i=0; i<N; i )


{


if (condition)


??? DoSomething();


else


??? DoOtherthing();


}


if (condition)


{


for (i=0; i<N; i )


??? DoSomething();


}


else


{


??? for (i=0; i<N; i )


??? DoOtherthing();


}



益處:法度模范簡潔


?


錯誤舛誤:多實施了N-1次邏輯鑒定,而且打斷了輪回“流水線”功課,使得編譯器不克不及對輪回終止優化措置懲罰,低落了服從。


益處:輪回的服從高


?


錯誤舛誤:法度模范不簡潔


?


?



?


四、有關內存的考慮題(每小題5分,共20分)


?


void GetMemory(char *p)


{


p = (char *)malloc(100);


}


void Test(void)

{

char *str = NULL;


GetMemory(str);??


strcpy(str, "hello world");


printf(str);


}


?


叨教運轉Test函數會有什么樣的效果?


答:法度模范潰散。


由于GetMemory并不克不及通報靜態內存,


Test函數中的 str不絕都是 NULL。


strcpy(str, "hello world");將使法度模范潰散。

?


char *GetMemory(void)


{??


char p[] = "hello world";


return p;


}


void Test(void)


{


char *str = NULL;


str = GetMemory();???


printf(str);


}


?


叨教運轉Test函數會有什么樣的效果?


答:概略是亂碼。


由于GetMemory前往的是指向“棧內存”的指針,該指針的地點不是 NULL,但其原現的內容已經被肅清,新內容弗成知。

void GetMemory2(char **p, int num)


{


*p = (char *)malloc(num);


}

void Test(void)


{


char *str = NULL;


GetMemory(&str, 100);


strcpy(str, "hello");??


printf(str);???


}

叨教運轉Test函數會有什么樣的效果?


答:


(1)可以輸出hello


(2)內存走漏


?


?


void Test(void)


{


char *str = (char *) malloc(100);


??? strcpy(str, “hello”);


??? free(str);? ???


??? if(str != NULL)


??? {


??? ? strcpy(str, “world”);


printf(str);


}


}

叨教運轉Test函數會有什么樣的效果?


答:篡篡改態內存區的內容,效果難以料想,特別很是危險。


由于free(str);之后,str成為野指針,


if(str != NULL)語句不起傳染打動。


?


?



?


五、編寫strcpy函數(10分)


已知strcpy函數的原型是

?????? char *strcpy(char *strDest, const char *strSrc);

?????? 其中strDest是目標字符串,strSrc是源字符串。

(1)不挪用C /C的字符串庫函數,請編寫函數 strcpy

char *strcpy(char *strDest, const char *strSrc);


{


??? assert((strDest!=NULL) && (strSrc !=NULL));?? // 2分


??? char *address = strDest;?? ?????????? ?????? // 2分


??? while( (*strDest = * strSrc ) != ‘\0’ )? ??? // 2分


?????? NULL ;


??? return address ;? ?????????? ?????????? ??? // 2分


}


?


(2)strcpy能把strSrc的內容復制到strDest,為什么還要char * 類型的前往值?

答:為了完成鏈式表達式。???????????????????? ???????????????????? ?????? // 2分


譬喻?????? int length = strlen( strcpy( strDest, “hello world”) );


?


六、編寫類String的組織函數、析構函數和賦值函數(25分)


已知類String的原型為:

??? >

??? {


??? ? public:


??? ??? String(const char *str = NULL);??? // 淺易組織函數


??? ??? String(const String &other);??? ??? // 拷貝組織函數


??????? ~ String(void);??????????? ??? ??? ??? // 析構函數


??? ??? String & operate =(const String &other);??? // 賦值函數


??? ? private:


??????? char? ??? *m_data;??????? ??? ??? // 用于保存字符串


??? };


?????? 請編寫String的上述4個函數。

尺度謎底:


?


// String的析構函數

?????? String::~String(void)?????????????? // 3分


{


??? delete [] m_data;??? ????????????????????


// 由于m_data是內部數據類型,也可以寫成 delete m_data;


?????? }


?


?????? // String的淺易組織函數????????????

?????? String::String(const char *str)????? // 6分


{


??? if(str==NULL)?????????????????????????


??? {


??? ??? m_data = new char[1];??? // 若能加 NULL 鑒定則更好


??? ??? *m_data = ‘\0’;?????????????????????


??? }?? ??????????????????????????????????????


??? else


??? {


??? ??? int length = strlen(str);??????????


??? ??? m_data = new char[length 1];? // 若能加 NULL 鑒定則更好?????


??? ??? strcpy(m_data, str);???????????????


??? }


}??


// 拷貝組織函數


??? String::String(const String &other)?? // 3分


??? {??


??? int length = strlen(other.m_data);???


??? m_data = new char[length 1];????? // 若能加 NULL 鑒定則更好???


??? strcpy(m_data, other.m_data);????????


}


// 賦值函數


??? String & String::operate =(const String &other)??? // 13分


??? {??


?????? // (1) 檢查自賦值???????????????????? // 4分


??? ??? if(this == &other)


?????? ??? return *this;


???


// (2) 釋放原有的內存資本??????????? // 3分


??? ?? delete [] m_data;


??????


?????? // (3)分派新的內存資本,并復制內容 // 3分


??? int length = strlen(other.m_data);???


??? m_data = new char[length 1];???????? // 若能加 NULL 鑒定則更好


??? ??? strcpy(m_data, other.m_data);


??????


?????? // (4)前往本對象的援用??????????? // 3分


??? ??? return *this;


}??



版權聲明: 原創作品,容許轉載,轉載時請務必以超鏈接體式格局標明文章 原始來由 、作者信息和本聲明。否則將追究執法責任。

轉載于:https://www.cnblogs.com/zgqjymx/archive/2011/03/07/1974593.html

總結

以上是生活随笔為你收集整理的高质量C /C编程指南---附录B :C /C试题和谜底的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。