【正文】
f array saving string, get string length */ count = get_strlength(buff)。 /* display string length in LCD */ print_dec(Len=, count)。 } return 0。 } /*FUNC COMMENT************************************************************* * ID : * function name : get_string * function : input string from key matrix * parameter : message:pointer to input waiting message * : buff :pointer to space saving string input * return : none * function used : none * notice : none * History : *FUNC COMMENT END********************************************************/ static void get_string(unsigned char *message, unsigned char *buff) /* define function to input string */ { LCD__puts(message)。 /* display message */ SW__gets(buff)。 /* input string from key matrix*/ LCD__puts(buff)。 /* display string input */ LCD__putchar(39。\n39。)。 /* return */ } /*FUNC COMMENT************************************************************* * ID : * function name : get_strlength * function : refer string using pointer passed as argument, return length * parameter : str:address of string to count length * return : length * function used : none * notice : none * History : *FUNC COMMENT END********************************************************/ static int get_strlength(unsigned char *str) /* define function to get string length */ { int len = 0。 /* define and initialize variable for string length*/ /* refer every character in onedimensional array buff pointed by */ /* pointer str, count up until detect (39。\039。) at end of string */ while(39。\039。 != *str++){ len++。 /* count number of character */ } return len。 /* return length */ } /*FUNC COMMENT************************************************************* * ID : * function name : print_dec * function : display decimal number in LCD * parameter : message:pointer to input waiting message * : dec :decimal number to be displayed * return : none * function used : none * notice : none * History : *FUNC COMMENT END********************************************************/ static void print_dec(unsigned char *message, int dec) /* define function to display decimal number in LCD */ { LCD__puts(message)。 /* display message */ LCD__putdec(dec)。 /*display decimal number in LCD */ LCD__putchar(39。\n39。)。 /* return */ } /***************************************************************************** end of file ******************************************************************************/ 一個編程風(fēng)格良好的例子: 結(jié) 論 1 ? 永遠(yuǎn)不要寫過長的語句,應(yīng)該讓代碼盡可能簡單;永遠(yuǎn)不要把兩條語句寫在一行中,以便于調(diào)試。 include void main() { float num1,num2,oute。 char op,a。 do {coutput two numbers:endl。 cinnum1num2endl。 coutenter its operater:endl。 cinopendl。 if(num2!=0) { switch(op) { case 39。*39。:oute=num1*num2。break。 case 39。/39。:oute=num1/num2。break。 case 39。+39。:oute=num1+num2。break。 case 39。39。:oute=num1num2。break。 default:coutthey can not be work\n。 } coutthe oute isouteendl。 } else couto can not as a divider\n。 coutdo you want to continue(y/n)\n。 cinaendl。 }while(a==39。y39。)。 } 兩個數(shù)的算術(shù)運算 //如果輸入字符 a,則顯示 1,否則程序結(jié)束 include int f() { char c。 cout請輸入 a或其他字符 endl。 cinc。 if(c==39。a39。) return 1。 else 。 } int main() {