Professional Documents
Culture Documents
Chapter 10
Chapter 10
struct inv_type {
char item[40]; // name of item
double cost; // cost
double retail; // retail price
int on_hand; // amount on hand
int lead_time; // number of days before resupply
};
listing 2
inv_type inv_var;
listing 3
struct inv_type {
char item[40]; // name of item
double cost; // cost
double retail; // retail price
int on_hand; // amount on hand
int lead_time; // number of days before resupply
} inv_varA, inv_varB, inv_varC;
listing 4
struct {
char item[40]; // name of item
double cost; // cost
double retail; // retail price
int on_hand; // amount on hand
int lead_time; // number of days before resupply
} temp;
listing 5
inv_var.cost = 10.39;
listing 6
cout << inv_var.cost;
listing 7
gets(inv_var.item);
listing 8
int t;
listing 9
inv_type invtry[100];
listing 10
cout << invtry[2].on_hand;
listing 11
const int SIZE = 100;
struct inv_type {
char item[40]; // name of item
double cost; // cost
double retail; // retail price
int on_hand; // amount on hand
int lead_time; // number of days before resupply
} invtry[SIZE];
listing 12
// Initialize the inv_type_info array.
void init_list()
{
int t;
listing 13
// Get a menu selection.
int menu()
{
char ch;
listing 14
// Enter items into the list.
void enter()
{
int i;
input(i);
}
listing 15
// Modify an existing item.
void update()
{
int i;
char name[80];
if(i==SIZE) {
cout << "Item not found.\n";
return;
}
listing 16
// Display the list.
void display()
{
int t;
listing 17
/* A simple inventory program that uses an array
of structures. */
#include <iostream>
#include <cctype>
#include <cstring>
#include <cstdlib>
using namespace std;
struct inv_type {
char item[40]; // name of item
double cost; // cost
double retail; // retail price
int on_hand; // amount on hand
int lead_time; // number of days before resupply
} invtry[SIZE];
int main()
{
char choice;
init_list();
for(;;) {
choice = menu();
switch(choice) {
case 'e': enter();
break;
case 'd': display();
break;
case 'u': update();
break;
case 'q': return 0;
}
}
}
input(i);
}
if(i==SIZE) {
cout << "Item not found.\n";
return;
}
cout << "Enter new information.\n";
input(i);
}
listing 18
// Pass a structure to a function.
#include <iostream>
using namespace std;
int main()
{
struct sample arg; // declare arg
arg.a = 1000;
arg.ch = 'X';
f1(arg);
return 0;
}
listing 19
// Demonstrate structure assignments.
#include <iostream>
using namespace std;
struct stype {
int a, b;
};
int main()
{
stype svar1, svar2;
return 0;
}
listing 20
struct stype1 {
int a, b;
};
struct stype2 {
int a, b;
};
stype1 svar1;
stype2 svar2;
listing 21
inv_type *inv_pointer;
listing 22
struct bal {
float balance;
char name[80];
} person;
listing 23
p = &person;
listing 24
p->balance
listing 25
struct tm {
int tm_sec; // seconds, 0-61
int tm_min; // minutes, 0-59
int tm_hour; // hours, 0-23
int tm_mday; // day of the month, 1-31
int tm_mon; // months since Jan, 0-11
int tm_year; // years from 1900
int tm_wday; // days since Sunday, 0-6
int tm_yday; // days since Jan 1, 0-365
int tm_isdst; // Daylight Saving Time indicator
};
listing 26
// This program displays the current system time.
#include <iostream>
#include <ctime>
using namespace std;
int main()
{
struct tm *ptr;
time_t lt;
lt = time('\0');
ptr = localtime(<);
return 0;
}
listing 27
// This program displays the current system time.
#include <iostream>
#include <ctime>
using namespace std;
int main()
{
struct tm *ptr;
time_t lt;
lt = time('\0');
ptr = localtime(<);
cout << asctime(ptr);
return 0;
}
listing 28
// Demonstrate a reference to a structure.
#include <iostream>
using namespace std;
struct mystruct {
int a;
int b;
};
int main()
{
mystruct x, y;
x.a = 10; x.b = 20;
y = f(x);
return 0;
}
listing 29
struct stype {
int nums[10][10]; // 10 x 10 array of ints
float b;
} var;
listing 30
var.nums[3][7]
listing 31
struct addr {
char name[40];
char street[40];
char city[40];
char zip[10];
}
struct emp {
addr address;
float wage;
} worker;
listing 32
worker.address.zip = 98765;
listing 33
struct mystruct {
int a;
char str[80];
mystruct *sptr; // pointer to mystruct objects
};
listing 34
struct C_struct {
int a;
int b;
}
listing 35
struct status_type {
unsigned delta_cts: 1;
unsigned delta_dsr: 1;
unsigned tr_edge: 1;
unsigned delta_rec: 1;
unsigned cts: 1;
unsigned dsr: 1;
unsigned ring: 1;
unsigned rec_line: 1;
} status;
listing 36
status = get_port_status();
listing 37
status.ring = 0;
listing 38
struct status_type {
unsigned : 4;
unsigned cts: 1;
unsigned dsr: 1;
} status;
listing 39
struct emp {
struct addr address;
float pay;
unsigned lay_off: 1; // lay off or active
unsigned hourly: 1: // hourly pay or wage
unsigned deductions: 3: // IRS deductions
};
listing 40
union utype {
short int i;
char ch;
} ;
listing 41
utype u_var;
listing 42
u_var.ch = 'A';
listing 43
// ...
func1(&u_var); // pass func1() a pointer to u_var
// ...
}
listing 44
// Use a union to exchange the bytes within a short integer.
#include <iostream>
using namespace std;
union swap_bytes {
short int num;
char ch[2];
};
int main()
{
swap_bytes sb;
char temp;
return 0;
}
listing 45
// Display the ASCII code in binary for characters.
#include <iostream>
#include <conio.h>
using namespace std;
union bits {
char ch;
struct byte bit;
} ascii ;
int main()
{
do {
cin >> ascii.ch;
cout << ": ";
disp_bits(ascii);
} while(ascii.ch!='q'); // quit if q typed
return 0;
}
listing 46
// Demonstrate an anonymous union.
#include <iostream>
using namespace std;
int main()
{
// this is an anonymous union
union {
short int count;
char ch[2];
};
return 0;
}
listing 47
union x {
char ch;
int i;
double f;
} u_var;