home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
C by Discovery (4th Edition)
/
C_By_Discovery_4th_Edition.tar
/
C_By_Discovery_4th_Edition
/
_DISK_
/
ch11
/
math.c
< prev
next >
Wrap
C/C++ Source or Header
|
2005-06-16
|
2KB
|
61 lines
/* math.c
*
* Synopsis - Accepts input of values for x and n and displays
* the calculated values from the pow(), sqrt() and
* tan() functions.
*
* Objective - To illustrate error handling with the
* mathematical functions in the ANSI C library.
*/
/* Include Files */
#include <math.h>
#include <stdio.h>
#include <errno.h> /* Note 1 */
#include <stdlib.h>
/* Constant Definitions */
#define BUF_SIZE 50
/* Function Declarations */
void error( char *s );
/* PRECONDITION: s contains the address of a string
*
* POSTCONDITION: checks the value of errno, and if it is nonzero,
* issues an error message
*/
int main( void )
{
double n, x, y;
char buff[BUF_SIZE];
printf( "Enter a value for n: " );
fgets( buff, BUF_SIZE, stdin );
n = strtod( buff, NULL );
printf( "Enter value for x: " );
fgets( buff, BUF_SIZE, stdin );
x = strtod( buff, NULL );
printf( "x is %5.2f.\n", x );
printf( "n is %5.2f.\n", n );
y = sqrt( x ); /* Note 2 */
error( "sqrt" );
printf( "sqrt(%4.2f) is %5.2f\n",x, y ); /* Note 3 */
y = tan( x ); /* Note 4 */
error( "tan" );
printf( "tan(%4.2f) is %5.2f.\n", x, y );
y = pow( x, n ); /* Note 5 */
error( "pow" );
printf( "pow(%4.2f, %4.2f) is %5.2f.\n", x, n, y );
return 0;
}
/*******************************error()*************************/
void error( char *s )
{
if ( errno ) /* Note 6 */
perror( s );
errno = 0; /* Note 7 */
}