/* Chapter 10 - Program 7 - ANYFILE.C */
#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE *fp1;
char oneword[100], filename[25];
char *c;
printf("Enter filename -> ");
scanf("%s", filename); /* read the desired filename */
fp1 = fopen(filename, "r");
if (fp1 == NULL)
{
printf("File failed to open\n");
exit (EXIT_FAILURE);
}
do
{
c = fgets(oneword, 100, fp1); /* get one line from the file */
if (c != NULL)
printf("%s", oneword); /* display it on the monitor */
} while (c != NULL); /* repeat until NULL */
fclose(fp1);
return EXIT_SUCCESS;
}
/* Chapter 3 - Program 9 - DUMBCONV.C*/
#include <stdio.h>
int main()
{
int x1, x2, x3;
printf("Centigrade to Fahrenheit temperature table\n\n");
for(x1 = -2 ; x1 <= 12 ; x1 = x1 + 1)
{
x3 = 10 * x1;
x2 = 32 + (x3 * 9) / 5;
printf(" C =%4d F =%4d ", x3, x2);
if (x3 == 0)
printf(" Freezing point of water");
if (x3 == 100)
printf(" Boiling point of water");
printf("\n");
}
return 0;
}
/* Result of execution
Centigrade to Fahrenheit temperature table
C = -20 F = -4
C = -10 F = 14
C = 0 F = 32 Freezing point of water
C = 10 F = 50
C = 20 F = 68
C = 30 F = 86
C = 40 F = 104
C = 50 F = 122
C = 60 F = 140
C = 70 F = 158
C = 80 F = 176
C = 90 F = 194
C = 100 F = 212 Boiling point of water
C = 110 F = 230
C = 120 F = 248
*/
/* Chapter 5 - Program 7 - FLOATSQ2.C */
#include <stdio.h> /* Prototypes for standard Input/Outputs */
float sqr(float inval);
float glsqr(void);
float z; /* This is a global variable */
int main()
{
int index;
float x, y;
for (index = 0 ; index <= 7 ; index++)
{
x = index; /* convert int to float */
y = sqr(x); /* square x to a floating point variable */
printf("The square of %d is %10.4f\n", index, y);
}
for (index = 0 ; index <= 7 ; index++)
{
z = index;
y = glsqr();
printf("The square of %d is %10.4f\n", index, y);
}
return 0;
}
float sqr(float inval) /* square a float, return a float */
{
float square;
square = inval * inval;
return(square);
}
float glsqr(void) /* square a float, return a float */
{
return(z * z);
}
/* Result of execution
The square of 0 is 0.0000
The square of 1 is 1.0000
The square of 2 is 4.0000
The square of 3 is 9.0000
The square of 4 is 16.0000
The square of 5 is 25.0000
The square of 6 is 36.0000
The square of 7 is 49.0000
The square of 0 is 0.0000
The square of 1 is 1.0000
The square of 2 is 4.0000
The square of 3 is 9.0000
The square of 4 is 16.0000
The square of 5 is 25.0000
The square of 6 is 36.0000
The square of 7 is 49.0000
*/
/* Result of execution
(The file selected is listed on the monitor)
*/
/* Chapter 9 - Program 3 - BETTERIN.C */
#include <stdio.h>
#include <conio.h>
#define CR 13 /* this defines CR to be 13 */
#define LF 10 /* this defines LF to be 10 */
int main()
{
int c;
printf("Input any characters, hit X to stop.\n");
do
{
c = _getch(); /* get a character */
putchar(c); /* display the hit key */
if (c == CR) putchar(LF); /* if it is a carriage return */
/* put out a linefeed too */
} while (c != 'X');
printf("\nEnd of program.\n");
return 0;
}
/* Result of execution
Input any characters, hit X to stop.
(The output depends on what characters you enter.)
End of program.
*/
/* Chapter 13 - Program 3 - BITOPS.C */
#include <stdio.h>
int main()
{
char mask;
char number[6];
char and, or, xor, inv, index;
number[0] = 0X00;
number[1] = 0X11;
number[2] = 0X22;
number[3] = 0X44;
number[4] = 0X88;
number[5] = 0XFF;
printf(" nmbr mask and or xor inv\n");
mask = 0X0F;
for (index = 0 ; index <= 5 ; index++)
{
and = mask & number[index];
or = mask | number[index];
xor = mask ^ number[index];
inv = ~number[index];
printf("%5x %5x %5x %5x %5x %5x\n",
number[index], mask, and, or, xor, inv);
}
printf("\n");
mask = 0X22;
for (index = 0 ; index <= 5 ; index++)
{
and = mask & number[index];
or = mask | number[index];
xor = mask ^ number[index];
inv = ~number[index];
printf("%5x %5x %5x %5x %5x %5x\n",
number[index], mask, and, or, xor, inv);
}
return 0;
}
/* Result of execution
nmbr mask and or xor inv
0 f 0 f f ffff
11 f 1 1f 1e ffee
22 f 2 2f 2d ffdd
44 f 4 4f 4b ffbb
ff88 f 8 ff8f ff87 77
ffff f f ffff fff0 0
0 22 0 22 22 ffff
11 22 0 33 33 ffee
22 22 22 22 0 ffdd
44 22 0 66 66 ffbb
ff88 22 0 ffaa ffaa 77
ffff 22 22 ffff ffdd 0
*/
/* Chapter 6 - Program 5 - DEBUGEX.C */
#include <stdio.h>
#define MY_DEBUG
int main()
{
int index;
for (index = 0 ; index < 6 ; index++)
{
printf("Index is now %d", index);
printf(" and we can process the data");
printf("\n");
#ifdef MY_DEBUG
printf("The processor is not debugged yet! *************\n");
#else
for (count = 1 ; count < index * 5 ; counter++)
{
value = (see page 16 of the documentation)
limit = (ask Bill about this calculation)
Linda has a data table for the worst case analysis
printf("count = %d, value = %d, limit = %d\n,
count, value, limitt);
}
#endif
}
return 0;
}
/* Result of execution
(As written with MY_DEBUG defined)
Index is now 0 and we can process the data
The processor is not debugged yet! *************
Index is now 1 and we can process the data
The processor is not debugged yet! *************
Index is now 2 and we can process the data
The processor is not debugged yet! *************
Index is now 3 and we can process the data
The processor is not debugged yet! *************
Index is now 4 and we can process the data
The processor is not debugged yet! *************
Index is now 5 and we can process the data
The processor is not debugged yet! *************
(Removing line 3, or commenting it out)
(This program will not compile due to errors.)
*/
No comments:
Post a Comment