Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 46 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
46
Dung lượng
1,19 MB
Nội dung
Appendix H Library Functions Function Type Purpose include File abs (1) int Return the absolute value of i. stdlib. h acos (d) double Return the arc cosine of d. math. h asin (d) double Return the arc sine of d. math. h atan (d) double Return the arc tangent of d. math.h atan2(dl,d2) double Return the arc tangent of dl /d2. math. h atof (s) double Convert string s to a double-precision quantity. stdlib. h atoi ( s) int Convert string s to an integer. stdlib. h atol (s) long Convert string s to a long integer. stdlib. h calloc(ul,u2) void* Allocate memory for an array having ul malloc. h, or elements, each of length u2 bytes. Return a stdlib. h pointer to the beginning of the allocated space. ceil (d) double Return a value rounded up to the next higher integer. math. h cos (d) double Return the cosine of d. math. h cosh (d) double Return the hyperbolic cosine of d. math. h difftirne(ll,12) double Return the time difference 11 - 12, where 11 and time. h 12 represent elapsed times beyond a designated base time (see the time function). exit ( u) void Close all files and buffers, and terminate the program. stdlib. h (Value of U is assigned by function, to indicate termination status.) double Raise e to the power d (e = 2.71 82818 - - is the base math. h of the natural (Naperian) system of logarithms). f abs (d) double Return the absolute value of d. math. h f close ( f ) int Close file f. Return 0 if file is successfully closed. stdio. h feof (f) int Determine if an end-of-file condition has been reached. stdio. h If so, return a nonzero value; otherwise, return 0. f getc ( f ) int Enter a single character from file f. stdio. h fgets(s,i,f) char* Enter string s , containing i characters, from file f. stdio. h floor (d) double Return a value rounded down to the next lower integer. math. h fmod(dl,d2) double Return the remainder of dl /d2 (with same sign as dl). math.h f open (sl , s2) file* Open a file named sl of type s2. Return a pointer to the file. stdio. h fprintf(f, ) int Send data items to file f (remaining arguments are stdio. h complicated - see Appendix G). fputc(c,f) int Send a single character to file f. stdio. h fputs(s,f) int Send string s to file f. stdio. h 487 488 LIBRARY FUNCTIONS [APP. H Function Type Purpose inc 1 ude File fread(s,il,i2,f) int Enter i2 data items, each of size il bytes, from stdio. h file f to string s. free ( P 1 void Free a block of allocated memory whose beginning is malloc. h, or indicated by p. stdlib. h fscanf(f, ) int Enter data items from file f (remaining arguments are stdio. h complicated - see Appendix G) fseek(f ,l,i) int Move the pointer for file f a distance 1 bytes from stdio. h location i (i may represent the beginning of the file, the current pointer position, or the end of the file). f tell( f ) long int Return the current pointer position within file f. stdio. h fwrite(s,il,i2,f) int Send i2 data items, each of size il bytes, from stdio. h string s to file f. int Enter a single character from file f. stdio. h int Enter a single character from the standard input device. stdio. h char* Enter string s from the standard input device. stdio. h int Determine if argument is alphanumeric. Return a ctype. h nonzero value if true; 0 otherwise. isalpha(c) int Determine if argument is alphabetic. Return a ctype. h nonzero value if true; 0 otherwise. isascii(c) int Determine if argument is an ASCII character. Return a ctype. h nonzero value if true; 0 otherwise. iscntrl(c) int Determine if argument is an ASCII control character. ctype. h Return a nonzero value if true; 0 otherwise. isdigit(c) int Determine if argument is a decimal digit. Return a ctype. h nonzero value if true; 0 otherwise. isgraph(c) int Determine if argument is a graphic ASCII character ctype. h (hex 0x21 -0x7e; octal 041 -1 76). Return a nonzero value if true; 0 otherwise. islower (c) int Determine if argument is lowercase. Return a ctype. h nonzero value if true; 0 otherwise. isodigit(c) int Determine if argument is an octal digit. Return a ctype. h nonzero value if true; 0 otherwise. isprint(c) int Determine if argument is a printing ASCII character ctype. h (hex Ox20-0x7e; octal 040-1 76). Return a nonzero value if true; 0 otherwise. ispunct(c) int Determine if argument is a punctuation character. ctype. h Return a nonzero value if true; 0 otherwise. isspace(c) int Determine if argument is a whitespace character. ctype. h Return a nonzero value if true; 0 otherwise. isupper (c) int Determine if argument is uppercase. ctype. h Return a nonzero value if true; 0 otherwise. isxdigit(c) int Determine if argument is a hexadecimal digit. ctype. h Return a nonzero value if true; 0 otherwise. long int Return the absolute value of 1. math.h double Return the natural logarithm of d. math.h 489 APP. HI LIBRARY FUNCTIONS Function Type Purpose include File log1 0 (d ) double Return the logarithm (base 10) of d. math. h malloc ( u) void* Allocate U bytes of memory. Return a pointer malloc. h, or to the beginning of the allocated space. stdlib. h pow(dl,d2) double Return dl raised to the d2 power. math. h printf( ) int Send data items to the standard output device stdio. h (arguments are complicated - see Appendix G). int Send a single character to file f. stdio. h int Send a single character to the standard output device. stdio. h int Send string s to the standard output device. stdio. h int Return a random positive integer. stdlib. h void Move the pointer to the beginning of file f. stdio. h int Enter data items from the standard input device stdio. h (arguments are complicated - see Appendix G). sin (d) double Return the sine of d. math. h sinh (d) double Return the hyperbolic sine of d. math.h sqrt(d) double Return the square root of d. math. h srand ( u) void Initialize the random number generator. stdlib. h st rcmp (sl , s2) int Compare two strings lexicographically. Return a string. h negative value if sl < s2; 0 if sl and s2 are identical; and a positive value if sl s2. strcmpi(sl,s2) int Compare two strings lexicographically, without regard string. h to case. Return a negative value if sl < s2; 0 if sl and s2 are identical; and a positive value if sl > s2 strcpy(s1 ,s2) char* Copy string s2 to string sl . string. h st rlen (s) int Return the number of characters in a string. string. h strset(s,c) char* Set all characters within s to c (excluding the string. h terminating null character \O). system( s) int Pass command s to the operating system. stdlib. h Return 0 if the command is successfully executed; otherwise, return a nonzero value, typically -1. tan (d) double Return the tangent of d. math. h tanh(d) double Return the hyperbolic tangent of d. math. h time (p) long int Return the number of seconds elapsed time . h beyond a designated base time. toascii(c) int Convert value of argument to ASCII. ctype. h tolower(c) int Convert letter to lowercase. ctype. h, or stdlib. h toupper(c) int Convert letter to uppercase. ctype. h, or stdlib. h Notes: Type refers to the data type of the quantity that is returned by the function. An asterisk (*) denotes a pointer. c denotes a character-type argument d denotes a double-precision argument 490 LIBRARY FUNCTIONS [APP. H f denotes a file argument i denotes an integer argument 1 denotes a long integer argument p denotes a pointer argument s denotes a string argument U denotes an unsigned integer argument Most commercial C compilers are accompanied by many more library functions. Consult the C reference manual for your particular compiler for more detailed information on each of the above functions, and for a listing of additional functions. Answers to Selected Problems Chapter 1 1.31 (a) This program prints the message Welcome to the Wonderful World of Computing I. The program does not contain any variables, The line containing printf is an output statement. There are no assignment or input statements. This program also prints the message Welcome to the Wonderful World of Computing!. The program does not contain any variables. (MESSAGE is a symbolic constant, not a variable.) The line containing printf is an output statement. There are no assignment or input statements. This program calculates the area of a triangle from its base and height. The variables are base, height and area. The alternating printf - scanf statements provide interactive input. The final printf statement is an output statement. The statement that begins with area = is an assignment statement. (6) This program calculates net (after tax) salary, given the gross salary and the tax rate (which is expressed as a constant 14%). The variables are gross, tax and net. The initial printf - scanf statements provide interactive input. The final two printf statements are output statements. Note that the statements containing tax = and net = are assignment statements. This program uses a function to determine the smaller of two integer quantities. The variables are a, b and min. The alternating pairs of printf - scanf statements provide interactive input. The final printf statement is an output statement. The statement min = smaller (a, b) references the function, which is called smaller. This function contains an if - else statement that returns the smaller of the two quantities to the main portion of the program. v) This program processes n pairs of integer quantities, and determines the smaller of each pair. A for loop is used to process the multiple pairs of integer quantities. In all other respects, this program is similar to that shown in part (e). (9) This program processes an unspecified number of pairs of integer quantities, and determines the smaller of each pair. The computation continues until a pair of zeros are entered into the computer. A while loop is used to process the multiple pairs of integer quantities. In all other respects, this program is similar to that shown in part v). This program processes an unspecified number of pairs of integer quantities, and determines the smaller of each pair. The original values and the corresponding minimum values are stored in the arrays a, b and min. Each array can store as many as 100 integer values. After all of the data have been entered and all of the minimum values have been determined, the number of data sets is “tagged” with the assignment statement n = - - i; then a for loop is used to display the data. In all other respects, this program is similar to that shown in part (9). Valid An identifier must begin with a letter. Valid return is a reserved word. An identifier must begin with a letter. Valid Blank spaces are not allowed. Valid Dash (minus sign) is not allowed. An identifier must begin with a letter or an underscore. Distinct (c) Identical (e) Distinct Distinct (6) Distinct v) Distinct Valid (real) Illegal character ( , ) Valid (real) 49 1 492 ANSWERS TO SELECTED PROBLEMS Valid (real) Valid (decimal integer) Valid (long integer) Valid (real) Illegal character (blank space) Valid (octal constant) Illegal characters (C , 0, F), if intended as an octal constant. If intended as a hexadecimal constant, an X or an x must be included (Le., OX1 8CDF). Valid (hexadecimal long integer) Illegal character (h) Valid Valid Valid Escape sequences must be written with a backward slash. Valid Valid Valid Valid (null-character escape sequence). A character constant cannot consist of multiple characters. Valid (octal escape sequence). Note that octal 52 is equivalent to decimal 42. In the ASCII character set, this value represents an asterisk (*). A string constant must be enclosed in double quotation marks. Valid Trailing quotation mark is missing. Valid Valid Valid Quotation marks and (optionally) the apostrophe within the string must be expressed as escape sequences; i.e., "The professor said , \ "Please don\ I t sleep in class\ It 2.44 (a) int P, q; float x, y, z; (6) char current, last; unsigned count; char a, b, c; float error; float rootl, root2; (e) char first, last; long counter; char message[80]; short flag; int index; unsigned cust-no; double gross, tax, net; 2.45 (a) float a = -8.2, b = 0.005; int x = 129, y = 87, z = -22; (c) long big = 123456789L; double c = 0.3333333333; char cl = Owl, c2 = I&'; char eol = '\,I; double dl = 2.88e-8, d2 = -8.4e5; (d) char message[] = "ERROR"; int U = 0711, v = Oxffff; Subtract the value of b from the value of a. Add the values of b and c, then multiply the sum by the value of a. Add the values of b and c and multiply the sum by the value of a. Then assign the result to d. Determine whether or not the value of a is greater than or equal to the value of b. The result will be either true or false, represented by the value 1 (true) or 0 (false). Divide the value of a by 5, and determine whether or not the remainder is equal to zero. The result will be either true or false. v) Divide the value of b by the value of c, and determine whether or not the value of a is less than the quotient. The result will be either true or false. Decrement the value of a; i.e., decrease the value of a by 1. Expression statement Control statement containing a compound statement. (The compound statement is enclosed in braces.) 493 ANSWERS TO SELECTED PROBLEMS Control statement Compound statement containing expression statements and a control statement. Compound statement containing an expression statement and a control statement. The control statement itself contains two compound statements. #define FACTOR -18 (d) #define NAME "Sharon' #define ERROR 0.0001 (e) #define EOLN '\n' #define BEGIN { U> #define COST "$19.95' #define END } 6 45 2 2 -1 3 -4 0 (because b / c iszero) -1 -16 7.1 49 2.51429 The remainder operation is not defined for floating-point operands. -5,17647 -2.68571 20.53333 1.67619 69 79 51 3 98 6 100 63 159 2703 integer float (some versions of C will convert to double-precision) double-precision long integer float (or double-precision) integer long integer integer long integer 14 18 -466.6667 -13 9 9 4 1.005 (i) -1.01 494 ANSWERS TO SELECTED PROBLEMS 0 0 1 0 1 1 0 1 0.0 1 1 1 0 0 0 1 1 0 k= 13 2 = -0.005 1=5 k=O k = 99 2 = 1.0 b = 100, a = 100 (Note that 100 is the encoded value for ' d I in the ASCII character set.) j=l,i=l k = 0, z = 0.0 z = 0.005, k = 0 [compare with (i) above] i= 10 y = -0.015 x = 0.010 i=l 1=3 i= 11 k=8 k=5 z = 0.005 2 = 0.0 a= 'c1 1=3 Return the absolute value of the integer expression (i - 2 * j ) . Return the absolute value of the floating-point expression ( x + y ) . Determine if the character represented by c is a printing ASCII character. Determine if the character represented by c is a decimal digit. Convert the character represented by c to uppercase. Round the value of x up to the next higher integer. Round the value of (x + y ) down to the next lower integer. Determine if the character represented by c is lowercase. Determine if the character represented by j is uppercase. Return the value ex. Return the natural logarithm of x. Return the square root of the expression (x*x + y*y ) . Determine if the value of the expression ( 10 * j ) can be interpreted as an alphanumeric character. Determine if the value of the expression ( 10 * j ) can be interpreted as an alphabetic character. Determine if the value of the expression ( 10 * j ) can be interpreted as an ASCII character. Convert the value of the expression ( 10 * j ) to an ASCII character. Divide the value of x by the value of y, and return the remainder with the same sign as x. Convert the ASCII character whose numerical code is 65 to lowercase. ANSWERS TO SELECTED PROBLEMS 495 (s) Determine the difference between the value of x and the value of y, then raise this difference to the 3.0 power. (f) Evaluate the expression (x - y) and return its sine. (U) Return the number of characters in the string 'I he 110 " . (v) Return the position of the first occurrence of the letter e in the string hello " . 2 0.005 1 0 ID' 1 .o 0.0 0.0 -1 .o 1 0 1.0050 13 -5.2983 17 0.005 0.011180 1 0 1 '2' 0.005 'a' 3.375e-6 0.014999 5 1 (0 indicates first position) 1.002472 Chapter 4 4.50 (a) a = getchar(); b = getchar(); c = getchar(); (b) putchar (a) ; putchar(b); putchar(c); 4.51 (a) scanf ( "%c%c%c" &a, &b, &c) ; or scanf ("%c %c %c", &a, &b, &c); (b) printf(n%c%c%cn,a, b, c); or printf("%c %c %c', a, b, c); 4.52 (a) for (count = 0; count < 60; ++count) text[count] = getchar(); (b) for (count = 0; count < 60; ++count) putchar(text[count]); (Note: count is assumed to be an integer variable.) 4.53 for (count = 0; (text[count] = getchar()) I= '\nu; ++count) , 4.54 scanf ( "%[ A \n] 'I, text) ; The method used in Prob. 4.53 indicates the number of characters that have been read. 4.55 (a) scanf('%d %d %d', &i, &j , &k); (6) scanf ("%d %o %x' , &i, &j, &k); (c) scanf ("%x %x %on, &i, &j, &k) ; 496 ANSWERS TO SELECTED PROBLEMS &I, 4.56 scanf ('%6d %6d %6d*, &i, Bk); &I, scanf("%8d %80 %8x', &i, &k); scanf(*%7x %7x %70", 81, &j, &k); 4.57 a will be assigned a long decimal integer with a maximum field-width of 12; b will be assigned a short decimal integer with a maximum field-width of 5; c and d will be assigned double-precision quantities with maximum field-widths of 15. a will be assigned a long hexadecimal integer with a maximum field-width of 10; b will be assigned a short octal integer with a maximum field-width of 6; c will be assigned a short unsigned integer with a maximum field-width of 6; and d will be assigned a long unsigned integer with a maximum field-width of 14. a will be assigned a long decimal integer with a maximum field-width of 12; b will be assigned a short decimal integer whose maximum field-width is unspecified; c and d will be assigned floating-point quantities with maximum field-widths of 15. a will be assigned a decimal integer with a maximum field-width of 8; another decimal integer will then be read into the computer but not assigned; c and d will then be assigned double-precision quantities with maximum field-widths of 12. 4.58 scanf ("%d %d %e %le" &i, &j, &x, &dx); or scanf("%d %d %f %lf", &i, &j, &x, &dx); scanf('%d %Id %d %f %U", &i, &ix, &I, &x, &U); scanf('%d %U %c", &i, &c); &U, scanf ('%c %f %If %hd", &c, &x, &dx, as) ; or scanf ( "%c %e %le %hd" , &c, &x, &dx, as) ; 4.59 scanf("%4d %4d %8e %151e", &i, &j, &x, &dx); &j, or scanf("%4d %4d %8f %151fn, &i, &x, &dx); scanf("5d %121d %5d %10f %5u", &i, &ix, &j, &x, &U); scanf ("%6d %6u %c", &i, &c); &U, scanf ( "%c %9f %161f %6hd" &c, &x, &dx, as) ; or scanf("%c %9e %161e %6hd", &c, &x, &dx, &s); 4.60 scanf ( "%s" text) ; 4.61 scanf("%[ abcdefghijklmnopqrstuvwxyz\n]", text); 4.62 scanf("%[ ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890$ J", text); 4.63 scanf ("%[ ^*I ", text) ; 4.64 $*@ (no spaces separating the characters) $ * @ (one or more blank spaces between the characters) $ * @ (one or more whitespace characters between the input characters) $ * @ (one or more blank spaces between the characters. Other whitespace characters may also appear with the blank spaces.) $*@ (no spaces separating the characters) 4.65 12 -8 0.011 -2.2e6 12 -8 0.011 -2.2e6 12 -8 0.011 -2.2e6 12 -8 0.011 -2.2e6 Note that the specified field-widths cannot be exceeded, and that one or more blank spaces must separate the successive numerical quantities. The most convenient representation of the floating-point values is as shown, irrespective of the particular conversion characters in each scanf function. 4.66 printf ('%d %d %do', i, j, k); printf("%d %d", (i + j), (1 - k)); printf("%f %d", sqrt(i + j), abs(i - k)); 4.67 printf("%3d %3d %3d", i, j, k); printf("%5d %5d*, (i + j), (i - k)); printf("%9f %7d", sqrt(i + j), abs(i - k)); 4.68 printf("%f %f %f", x, y, z); printf("%f %f", (x + y), (x - z)); printf("%f %f", sqrt(x + y), fabs(x - z)); [...]... corresponding to a: 101 0 0 010 1100 0011 (a) 5d3c 0101 1101 0011 1100 (b) 2202 0 010 0 010 0000 0 010 (c) 9dc5 100 1 1101 1100 0101 (4 b f c 7 101 1 1111 1100 0111 (e) 80cl 100 0 0000 1100 0001 v) 623a 0 110 0 010 0011 101 0 (g) e2fb 1 110 0 010 1111 101 1 (h) 1458 0001 0100 0101 100 0 (i) 5860 0101 100 0 0 110 0000 0') 0 0000 0000 0000 0000 (valid for any value of a) (k) f fff 1111 1111 1111 1111 (valid for any value of a) 517... a) 517 518 ANSWERS TO SELECTED PROBLEMS (m) ffff a000 (n) c100 (0) aOc3 5bc3 3a00 5b3c fbc3 fbOO fbff (I) (p) (4) (r) (s) (t) (U) 13.40 (a) (b) (c) 13.41 (a) (6) 13.42 ( a ) (b) 1111 101 0 1100 101 0 0101 0011 0101 1111 1111 1111 1111 1111 1111 (validforanyvalueofa) 0000 0000 0000 0001 0000 0000 0000 1100 0011 101 1 101 0 101 1 101 1 101 1 101 1 1100 0011 0000 0000 0011 1100 1100 0011 0000 0000 1111 1111 a... ] Address of x [ 2 ] (6) (e) 12 (i.e., 10 + 2) 30 (this is the value of x [ 21) 10 Address of t a b l e [ 01 [ 01 Address of row 1 (the second row) of t a b l e Address of t a b l e [ 1] [ 01 Address of t a b l e [ 1 ] [ 1] Address of t a b l e [ 01 [ 1] 2.2 (i.e., 1 2 + 1) 1.2 2.1 2.2 Address of c o l o r [ 01 (the beginning of the first string) Address of c o l o r [ 21 (the beginning of the third... b) { double c; c = funct2(a, b ) ; 1 s t a t i c double funct2(double a, double b ) { 1 4 6 9 13 18 100 196 80 184 60 164 104 116 136 136 100 101 102 106 124 200 6 11 16 21 26 6 11 16 21 26 9 25 57 121 249 40 136 20 100 This program will return the number of characters within a line of text entered from the keyboard The terminating newline character will not be included in the sum name is a... requires five bits, and 8 requires four bits) U U,v and w are each 7 bits wide Two words of memory will be required U and v will fit into one word, but w will be forced to the beginning of the next word U, v and w are each 7 bits wide Two words will be required U will be placed within the first word, followed by 9 empty bits v and w will fit into the second word, separated by 2 empty bits U, v and w are each... O O ] ) { 1 9.3 1 20 (sum of the array elements whose values are even) 25 (sum of the even array elements) Will not run (automatic arrays cannot be initialized) 25 (sum of the external array elements whose values are odd) 1 (smallest value) 1 5 9 (smallest value within each row) 9 10 11 12 (largest value within each column) 0 2 2 4 4 6 6 8 8 10 10 12 (if the value of an element is odd, reduce its... elements of a five-element integer array Calculate the sum of the last two elements of the five-element integer array sum=90 ANSWERS TO SELECTED PROBLEMS 51 1 A pointer to an integer quantity A pointer to an integer quantity The address of the element of p whose value is the largest (p is actually a five-element integer array) Determine the largest value of the elements of p max=50 Address of x [ 0... f unct by value Hence the reassignment within f unct is not recognized within main Note that only the first value is meaningful in the first line of output, and only the second value is meaningful in the last line (6) -2621 4 -0.300000 0 0.500000 The union variable U is again passed to f u n c t by value Hence the reassignment within f u n c t is not recognized within main The first value in each line... one-dimensional array of pointers to the strings " n o r t h , south" , "east and "west " p is a pointer to a group of contiguous, two-dimensional, long-integer arrays; equivalent to p [ ] [ 101 [ 20) ; p is a two-dimensional array of pointers to long-integer quantities (equivalent to a three-dimensional array of long integers) sample is a function that accepts an argument which is a function and returns a... l x = 38 Chapter 7 7.32 7.33 accepts an integer argument and returns an integer quantity accepts two arguments and returns a double-precision quantity The first argument is a double-precision quantity, and the second is an integer f accepts three arguments and returns nothing The first function is a long integer, the second is a short integer and the third is an unsigned integer f does not accept any . Subtract the value of b from the value of a. Add the values of b and c, then multiply the sum by the value of a. Add the values of b and c and multiply the sum by the value of a. Then. 164 40 136 20 100 104 116 136 136 100 101 102 106 124 200 6 11 16 21 26 6 11 16 21 26 9 25 57 121 249 This program will return the number of characters within a line of text entered from. unspecified number of pairs of integer quantities, and determines the smaller of each pair. The original values and the corresponding minimum values are stored in the arrays a, b and min. Each