當前位置:編程學習大全網 - 源碼下載 - c++ 完美 源代碼

c++ 完美 源代碼

說到最完美的代碼,可能C++的比較少,因為C++是類結構的,過程都封裝在類中,真正的代碼之美是體現在C當中的,把這種美封裝到C++中也就有了C++的美了。

說到最美的C,沒有人能超過Dennis M. Ritchie————這個C語言之父,他寫的程序真的是美到不能減壹分不能多壹分

建議看看他寫的 the c programming language.

這裏有段代碼,書中的。

#include <stdio.h>

/* 統計各個數字、空白字符及其他字符分別出現的次數*/

main ( )

{

int c, i, nwhite, nother;

int ndigit[10];

nwhite = nother = 0;

for ( i =0; i < 10; ++i )

ndigit[i] = 0;

while ( ( c = getchar() ) != EOF )

if ( c >= '0' && c <= '9' )

++ndigit[c - '0'];

else if ( c == ' ' ||c == '\n' || c == '\t' )

++nwhite;

else

++nother;

printf( "digits =" );

for ( i =0; i < 10; ++i )

printf( " %d", ndigit[i] );

printf( ", white space = %d, other = %d", nwhite, nother);

}

當把程序自身作為輸入時,輸出為:

digits = 9 3 0 0 0 0 0 0 0 1, white space = 123, other = 345

另外壹個:

妳自己先想想,當妳面對這問題時候,妳如何寫的

問題:將字符串s轉換成整數

/* atoi:將字符串s轉換成整數 */

int atoi( char s[])

{

int i, n;

n = 0;

for ( i = 0; s[i] >= '0' && s[i] <= '9'; ++i )

n = 10 * n + (s[i] - '0');

return n;

}

第三個:知道string.h裏面對於strcat()是怎麽定義的嘛?妳難以想像庫函數就是這麽簡單的:

/* strcat:把字符串t連接到字符串s的後面;s必須有足夠大的空間 */

void strcat(char s[], char t[])

{

int i, j;

i = j = 0;

while (s[i] != '\0') /* 找到s的末尾 */

i++;

while ( (s[i++] = t[j++]) != '\0') /* 拷貝t */

}

  • 上一篇:Ubuntu14.04麒麟怎麽安裝vim
  • 下一篇:Slice 擴容機制解惑
  • copyright 2024編程學習大全網