/*** bsqrt.c --- sample program by tsaiwn@cs.nctu.edu.tw * ½m²ß: ²©ö¿é¤J + ²©ö¿é¥X + ¨Ï¥Î Library µ{¦¡®w + ²©ö while Loop * Description: ¼g­Óµ{¦¡, Ū¤J x, ¦L¥X®Ú¸¹ x (¥i¥H¥Î Library function) * Note: ­n¤@ª½°µ¨ì ¿é¤J­t¼Æ(¨Ò¦p¿é¤J -1 ) ¤~°±¤î! °O±o­n¦L¥X Bye bye! *** *** ¬Ý¤£À´¥iÁ|¤â½Ð§U±Ð¹L¥h¸ÑÄÀµ¹§AÅ¥ *** ***/ #include #include #include double x; // (ª`·N 1) ÅÜ¼Æ variable; // C++ ¦¡ªºµù¸Ñ comment double getDBL(void); // ¼g¦¨ function ¤ñ¸û¦³¼u©Ê:-) int main( ) { // for( ; ; ) { // Loop forever printf("Give me x: "); x = getDBL( ); // ¼g¦¨¨ç¼Æ (function; ¨ç¦¡) ¬O­Ó«Ü¦nªº²ßºD if( x < 0 ) break; // (ª`·N 3) ¥i¥H§ï¥Î§Oªº¤èªk printf(" sqrt ®Ú¸¹ %f =%f\n ", x, sqrt(x) ); // } // while printf("Bye bye!\n"); return 0; // §iª¾§@·~¨t²Î(OS)ªí¥Ü§Ú­Ì³o¥Dµ{¦¡¥¿±`µ²§ô } double getDBL( ) { // ¥H«á­n§ï¿é¤J¤èªk¥u­n§ï³o function ´N¥i :-) static char buf[999]; double ans; gets(buf); // NOT safe! ans = atof(buf); // required return ans; } /*** Run on Windows/XP cmd windows C:\testc>gcc bsqrt.c C:\testc>a.exe Give me x: 2 sqrt ®Ú¸¹ 2.000000 =1.414214 Give me x: 4 sqrt ®Ú¸¹ 4.000000 =2.000000 Give me x: 3 sqrt ®Ú¸¹ùâ 3.000000 =1.732051 Give me x: 400 sqrt ®Ú¸¹ 400.000000 =20.000000 Give me x: -1 Bye bye! *****************************/ /*** Run on Unix system (FreeBSD) 3:33pm ccbsd2:LAB-01/> gcc bsqrt.c /var/tmp//cc7d8uuS.o(.text+0xab): In function `getDBL': : warning: warning: this program uses gets(), which is unsafe. /var/tmp//cc7d8uuS.o(.text+0x5b): In function `main': : undefined reference to `sqrt' 3:33pm ccbsd2:LAB-01/> ./a.out ./a.out: Command not found. 3:33pm ccbsd2:LAB-01/> gcc bsqrt.c -lm /var/tmp//ccgOfzt7.o(.text+0xab): In function `getDBL': : warning: warning: this program uses gets(), which is unsafe. 3:33pm ccbsd2:LAB-01/> ./a.out warning: this program uses gets(), which is unsafe. Give me x: 3 sqrt ®Ú¸¹ 3.000000 =1.732051 Give me x: 2 sqrt ®Ú¸¹ 2.000000 =1.414214 Give me x: -1 Bye bye! 3:33pm ccbsd2:LAB-01/> ***===================***/