/* File: calc.c
This program is a postfix calculator. Two operands followed by an
operator must be entered. The program prints the result. The program
repeats until end of file.
*/
#include <stdio.h>
typedef enum { FALSE, TRUE } boolean;
char get_operator(void);
boolean operatorp(char c);
main()
{ float oprnd1, oprnd2, result;
char c;
boolean error;
printf("***Postfix Calculator***\n\n");
printf("Type two numbers, followed by an operator: +, -, *, or /\n");
printf("EOF to quit\n");
while (scanf("%f %f", &oprnd1, &oprnd2) == 2) {
c = get_operator();
error = FALSE;
switch(c) {
case '+': result = oprnd1 + oprnd2; break;
case '-': result = oprnd1 - oprnd2; break;
case '*': result = oprnd1 * oprnd2; break;
case '/': if (oprnd2)
result = oprnd1 / oprnd2;
else
error = TRUE;
break;
}
if (error == FALSE)
printf("%f %c %f = %f\n", oprnd1, c, oprnd2, result);
else
printf("Runtime error: %f %c %f\n", oprnd1, c, oprnd2);
} /* end of while loop */
} /* end of program */
/* Gets one of the allowed operator, +, - , *, /. */
char get_operator(void)
{ char c;
while ((c = getchar()) && operatorp(c) != TRUE)
;
return c;
}
/* Function tests if c is one of the operators +, -, *, /. */
boolean operatorp(char c)
{
switch(c) {
case '+':
case '-':
case '*':
case '/': return TRUE;
default: return FALSE;
}
}
/* File: fact.c
Program computes the factorial of integers using function
fact().
*/
#include <stdio.h>
int fact(int n);
main()
{ int n;
printf("***Factorial Program***\n");
printf("Type positive integers, EOF to terminate\n");
while (scanf("%d", &n) != EOF)
if (n <= 0)
printf("%d typed, type a positive integer\n", n);
else
printf("Factorial of %d is %d\n", n, fact(n));
}
/* Function computes factorial of n using a for loop. */
int fact(int n)
{ int i, product;
product = 1;
for (i = 1; i <= n; i = i + 1)
product = product * i;
return product;
}
/* File: fib.c
Program computes and prints Fibonacci numbers less than a
specified limit of 100.
*/
#include <stdio.h>
#define LIM 100
void fib(int lim);
main()
{
printf("***Fibonacci Numbers***\n");
printf("Limit is %d \n", LIM);
fib(LIM);
}
/* Function computes and prints the Fibonacci numbers less than lim. */
void fib(int lim)
{ int i, j, n;
printf("1\n1\n"); /* print the first two fib. numbers */
for (i = 1, j = 1, n = 0; n < lim; i = j, j = n) {
n = i + j; /* compute the next fib. number */
if (n < lim)
printf("%d\n", n); /* print the next fib. number */
}
}
/* File: mathutil.c */
#include <stdio.h>
#include "tfdef.h"
#include "mathutil.h"
/* Tests if square of guess approximately equals x. */
int close(double guess, double x)
{
if (absolute(guess * guess - x) < 0.001)
return TRUE;
else
return FALSE;
}
/* Returns absolute value of x. */
double absolute(double x)
{
if (x < 0)
return -x;
else
return x;
}
/* Returns average of guess and x / guess. */
double improve(double guess, double x)
{
return (guess + x / guess) / 2;
}
/* Uses Newton's method to compute square root within the margin
allowed by error.
*/
double sqroot(double y, double error)
{ double guess = 1.0;
do
guess = improve(guess, y); /* improve guess. */
while (!close2(guess, y, error)); /* while guess not close */
return guess; /* when close enough, return guess.*/
}
/* Tests if square of g equals y within the error limits specified. */
int close2(double g, double y, double error)
{
if (absolute(g * g - y) < error)
return TRUE;
else
return FALSE;
}
/* Function returns long factorial of n. */
long factcomp(int n)
{ int i;
long prod;
prod = 1; /* initialize */
for (i = 1; i <= n; i++) /* loop from 1 to n */
prod *= i; /* compute cumulative product */
return prod; /* return product */
}
double maxdbl(double x, double y)
{
return (x > y ? x : y);
}
/* File: mathutil.h */
/* File contains prototypes for functions defined in mathutil.c */
double improve(double guess, double x);
int close(double guess, double x);
double absolute(double x);
double sqroot(double y, double error);
int close2(double g, double y, double error);
long factcomp(int n);
double maxdbl(double x, double y);
/* File: size.c */
main()
{ int x;
double y;
printf("***Sizeof operator***\n\n");
printf("Size of x is %d bytes\n", sizeof x);
printf("Size of x+y is %d bytes\n\n", sizeof (x+y));
printf("Size of data types in bytes:\n");
printf("Size of int type is %d\n", sizeof(int));
printf("Size of long int is %d\n", sizeof(long int));
printf("Size of short int is %d\n", sizeof(short int));
printf("Size of unsigned int is %d\n", sizeof(unsigned int));
printf("Size of float is %d\n", sizeof(float));
printf("Size of double is %d\n", sizeof(double));
}
/* File: sqroot.c
Other Files: mathutil.c
Header Files: tfdef.h, mathutil.h
Program computes and prints square roots of numbers. Uses Newton's
method to compute square root of x: Start with any guess. Test if
it is acceptable. If not, improve guess by averaging it with x/guess.
*/
#include <stdio.h>
#include "tfdef.h"
#include "mathutil.h"
#define DEBUG
main()
{ int i;
double x, guess;
printf("***Square Root Program: Newton's Method***\n\n");
printf("Type a positive number: ");
do {
scanf("%lf", &x);
if (x <= 0)
printf("%f typed, type a positive number\n", x);
} while (x <= 0);
guess = 1.0;
do {
guess = improve(guess, x); /* improve guess. */
#ifdef DEBUG /* debug stmt */
printf("guess = %f\n", guess); /* Print guess. */
#endif /* end of debug */
} while (!close(guess, x)); /* terminate if guess is close */
/* exit loop if guess is close enough */
printf("Sq.Rt. of %f is %f\n", x, guess); /* Print sq. rt. */
}
/* File: sqrt2.c
Other Files: mathutil.c
Header Files: tfdef.h, mathutil.h
Program computes and prints square roots of numbers until the end of
file. Uses Newton's method to compute the square root of x to within a
specified error margin.
*/
#include <stdio.h>
#include "tfdef.h"
#include "mathutil.h"
main()
{ int i;
float x, root;
printf("***Square Root Program***\n\n");
printf("Type positive numbers, EOF to quit: ");
while (scanf("%f", &x) != EOF) {
if (x <= 0) {
printf("%f typed, type positive numbers \n");
continue;
}
root = (float) sqroot((double) x, 0.001);
printf("Sq.Rt. of %f is %f\n", x, root);
}
}
/* File: tfdef.h */
#define TRUE 1
#define FALSE 0