C/C++ 程式設計規範 試題
阿新 • • 發佈:2019-01-03
一BOOL float 指標變數 與零值比較的if語句
1.寫出BOOL flag 指標變數與零值比較的if語句。
if(flag)
if(!flag)
2 寫出float x與零值比價的if語句
construction float EPSINON = 0.00001;
if((x>=-EPSINON)&&(x<=EPSINON))
3.寫出char*p與零值比價的if語句
if(p==NULL)
if(p!=NULL)
二 簡述以下兩個for迴圈的優缺點
for(i=0;i<N;i++)
{
if(condition)
Dosomething();
else
DoOtherthing();
}
優點程式簡潔
缺點多執行了N-1次邏輯判斷,並且打斷了迴圈流水線作業,使得編譯器不能對迴圈進行優化處理,降低了效率。
if(condition)
{
for(i=0;i<N;i++)
Dosomething();
}
else
{
for(i=0;i<N;i++)
DoOtherthing();
}
優點:迴圈的效率高
缺點:程式不簡潔
編寫strcpy函式
已知函式原型是
char *strcpy(char *strDest,const char *strSrc);
其中strDest是目的字串,strSrc是源字串。
char *strcpy(char *strDest,const char*strSrc)
{
assert((strDest!=NULL) && (strSrc != NULL));
char *address = strDest;
while(*strDest++ = *strSrc++)!='\0')
NULL;
return adderss;
}