/*
union.c */
/*
Ye don't get me I'm part o' the union... */
/*
How to do a “union” in C. */
#include
<stdio.h>
#include
<ctype.h>
#include
“conio.h”
/*
We will say for now we can handle up to 100 records. */
#define
MAX_RECORDS 100
struct
resistor_info
{
char component_type;
long value;
int tolerance;
};
struct
transistor_info
{
char component_type;
char type[4];
char number[10];
};
struct
capacitor_info
{
char component_type;
float capacitance;
char
type;
};
/*
This is the union - the names after the “struct ...” don't matter,
so long as they are unique.
*/
union
component_info
{
struct resistor_info resistor_part;
struct transistor_info transistor_part;
struct capacitor_info capacitor_part;
};
typedef
union component_info COMPONENT_INFO;
/*
The above ‑ typedef of “COMPONENT_INFO” allows us to refer to
“union component_info” as just “COMPONENT_INFO”.
*/
/*
Start of program */
void
main()
{
char component_type;
/* We will use this
to count records as they are read. */
int record_count,
record_counter;
/* Declare an array
of records for our components. */
COMPONENT_INFO
component_records[MAX_RECORDS], current_record;
clrscr();
printf (“Union.c -
use of a union of data types.\n”);
record_count = 0;
/* Keep reading
records until the end of the file (“feof”) */
do
{
printf (“Enter Component Type
(1=Resis, 2=Trans, 3=Capac, 4=Quit)>>>“);
component_type =
getche();
/* Newline. */
printf(“\n”);
if
(component_type != '4')
{
/* Set the “tag”
field of our union to be the entered type. */
current_record.resistor_part.component_type = component_type;
switch
(component_type)
{
/*Resistor
specified. */
case '1':
printf (“Enter
Resistance Value (ohms)>>>“);
scanf (“%ld”,¤t_record.resistor_part.value);
printf (“Enter
Tolerance Value (%%)>>>“);
scanf (“%d”,¤t_record.resistor_part.tolerance);
break;
/*Transistor specified. */
case '2':
printf (“Enter
transistor type>>>“);
scanf (“%s”,¤t_record.transistor_part.type);
printf (“Enter
Transistor Number>>>“);
scanf (“%s”,¤t_record.transistor_part.number);
break;
/*Capacitor
specified. */
case '3':
printf (“Enter
Capacitor type (P or E)>>>“);
scanf (“%c”,¤t_record.capacitor_part.type);
printf (“Enter
Capacitance>>>“);
scanf (“%f”,¤t_record.capacitor_part.capacitance);
break;
default:
/* Print
a warning. */
printf (“1
to 3, guv, try again!\n”);
} /* End of “switch”
*/
/* Store record
if valid type was entered. */
if
(component_type >= '1' && component_type <= '3')
{
/* Copy all
the data in our entered structure into the array of
records
and increment the record count. */
component_records[record_count++]
= current_record;
}
/* To stop “scanf”
problems...ho hum. */
fflush
(stdin);
} /* End of “if”
*/
}
while
((component_type != '4') && (record_count < MAX_RECORDS));
printf (“\nAll records
entered. Press 'Return' to see each one.\n\n”);
record_counter = 0;
while (record_counter < record_count)
{
/* Wait for keypress. */
getch();
printf (“Record %d:\n”,record_counter +
1);
switch (component_records[record_counter].resistor_part.component_type)
{
/*Resistor specified. */
case '1':
printf (“Resistance Value is \t”);
printf (“%ld\n”,component_records[record_counter].resistor_part.value);
printf (“Tolerance Value is \t”);
printf (“%d\n”,component_records[record_counter].resistor_part.tolerance);
break;
/*Transistor specified. */
case '2':
printf (“Transistor type is \t”);
printf (“%s\n”,component_records[record_counter].transistor_part.type);
printf (“Transistor Number is \t”);
printf (“%s\n”,component_records[record_counter].transistor_part.number);
break;
/*Capacitor specified. */
case '3':
printf (“Capacitor type is \t”);
printf (“%c\n”,component_records[record_counter].capacitor_part.type);
printf (“Capacitance is \t”);
printf (“%f\n”,component_records[record_counter].capacitor_part.capacitance);
break;
} /* End of switch. */
record_counter ++;
}
printf ( “\n\nFinished.
Press Return.\n”);
getchar();
}
No comments:
Post a Comment