// -- C ++ short cut tutorial -- Chapter 10 -- Structure and Union
// -- Chapter 10 -- Structure and Union
// -- 11/24/2005 Thurs.
// -- Computer lab
// -- Liwei
// -- Program #1 uses the Array Structure
# Include <iostream>
# Include <cctype>
# Include <cstring>
# Include <cstdlib>
Using namespace STD;
Const int size = 10;
Struct inv_type {
Char item [40];
Double cost;
Double retail;
Int on_hand;
Int lead_time;
} Invtry [size];
Void enter (), init_list (), display ();
Void Update (), input (int I );
Int menu ();
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;
} // Switch
} //
Cout <Endl <"=========================" <Endl;
// Getchar ();
Return 0;
}
Void init_list ()
{
Int T;
For (t = 0; t <size; t ++)
* Invtry [T]. Item = '/0 ';
}
Int menu ()
{
Char ch;
Cout <Endl;
Do {
Cout <"(e) nter/N ";
Cout <"(d) isplay/N ";
Cout <"(u) pdate/N ";
Cout <"(q) uit/n ";
Cout <"choose one :";
Cin> CH;
} While (! Strchr ("eduq", tolower (CH )));
Return tolower (CH );
}
Void enter ()
{
Int I;
For (I = 0; I <size; I ++)
If (! * Invtry [I]. item)
Break;
If (I = size ){
Cout <"list full./N ";
Return;
}
Input (I );
}
Void input (int I)
{
Cout <"item :";
Cin> invtry [I]. item;
Cout <"cost :";
Cin> invtry [I]. cost;
Cout <"retail price :";
Cin> invtry [I]. Retail;
Cout <"on hand :";
Cin> invtry [I]. on_hand;
Cout <"lead time to resupply (in days ):";
Cin> invtry [I]. lead_time;
}
Void Update ()
{
Int I;
Char name [80];
Cout <"Enter item :";
Cin> name;
For (I = 0; I <size; I ++)
If (! Strcmp (name, invtry [I]. Item ))
Break;
If (I = size ){
Cout <"item not found./N ";
Return;
}
Cout <"Enter new info:/N ";
Input (I );
}
Void display ()
{
Int T;
For (t = 0; t <size; t ++ ){
// If (* invtry [T]. Item ){
Cout <invtry [T]. Item <Endl;
Cout <"cost: $" <invtry [T]. cost;
Cout <"/nretail: $ ";
Cout <invtry [T]. Retail <Endl;
Cout <"on hand:" <invtry [T]. on_hand;
Cout <"/nresupply time :";
Cout <invtry [T]. lead_time <"Days/n ";
///} // If
} //
}
// -- Program #2 pass a structure to the Function
# Include <iostream>
Using namespace STD;
Struct sample {
Int;
Char ch;
};
Void F1 (sample parm );
Int main ()
{
Struct sample ARG;
Arg. A = 1000;
Arg. CH = 'X ';
F1 (ARG );
Cout <Endl <"=========================" <Endl;
// Getchar ();
Return 0;
}
Void F1 (sample parm)
{
Cout <parm. A <"" <parm. ch <Endl;
}
// -- Program #3 Structure assignment
# Include <iostream>
Using namespace STD;
Struct stype {
Int A, B;
};
Int main ()
{
Stype svar1, svar2;
Svar1.a = svar1. B = 10;
Svar2.a = svar2. B = 20;
Cout <"structures before assignment./N ";
Cout <"svar1:" <svar1.a <''<svar1. B;
Cout <Endl;
Cout <"svar2:" <svar2.a <''<svar2. B;
Cout <Endl;
Svar2 = svar1;
Cout <"structures after assignment./N ";
Cout <"svar1:" <svar1.a <''<svar1. B;
Cout <Endl;
Cout <"svar2:" <svar2.a <''<svar2. B;
Cout <Endl;
Cout <Endl <"=========================" <Endl;
// Getchar ();
Return 0;
}
// -- Program #4 system time
# Include <iostream>
# Include <ctime>
Using namespace STD;
Int main ()
{
Struct TM * PTR;
Time_t lt;
Lt = Time ('/0 ');
PTR = localtime (& lt );
Cout <PTR-> tm_hour <':' <PTR-> tm_min;
Cout <':' <PTR-> tm_sec;
Cout <Endl <"=========================" <Endl;
// Getchar ();
Return 0;
}
// -- Program #5 system time
# Include <iostream>
# Include <ctime>
Using namespace STD;
Int main ()
{
Struct TM * PTR;
Time_t lt;
Lt = Time ('/0 ');
PTR = localtime (& lt );
Cout <asctime (PTR );
Cout <Endl <"=========================" <Endl;
// Getchar ();
Return 0;
}
// -- Program #6 usage of structure Reference
# Include <iostream>
Using namespace STD;
Struct mystruct {
Int;
Int B;
};
Mystruct & F (mystruct & var );
Int main ()
{
Mystruct X, Y;
X. A = 10;
X. B = 20;
Cout <"original X. A and X. B :";
Cout <X. A <''<X. B <Endl;
Y = f (x );
Cout <X. A <''<X. B <Endl;
Cout <Y. A <''<Y. B <Endl;
Cout <Endl <"=========================" <Endl;
// Getchar ();
Return 0;
}
Mystruct & F (mystruct & var)
{
Var. A = var. A * var.;
Var. B = var. B/var. B;
Mystruct B = var;
Return var; // note the reference here
}
// -- Program #7 joint usage
# Include <iostream>
Using namespace STD;
Void disp_binary (unsigned U );
Union swap_bytes {
Short int num;
Char ch [2];
};
Int main ()
{
Swap_bytes Sb;
Char temp;
SB. num = 15; // 0000 0000 0000 1111
Cout <"original Bytes :";
Disp_binary (sb. ch [1]);
Cout <"";
Disp_binary (sb. ch [0]);
Cout <Endl;
Temp = sb. ch [0];
SB. ch [0] = sb. ch [1];
SB. ch [1] = temp;
Cout <"exchanged Bytes :";
Disp_binary (sb. ch [1]);
Cout <"";
Disp_binary (sb. ch [0]);
Cout <Endl;
Cout <Endl <"=========================" <Endl;
// Getchar ();
Return 0;
}
Void disp_binary (unsigned U)
{
Register int T;
For (t = 128; t> 0; t = T/2)
If (U & T)
Cout <"1 ";
Else
Cout <"0 ";
}
// -- Program #8: The combined use outputs ASCII in binary format
# Include <iostream>
# Include <conio. h>
Using namespace STD;
Struct byte {
Unsigned A: 1;
Unsigned B: 1;
Unsigned C: 1;
Unsigned D: 1;
Unsigned E: 1;
Unsigned F: 1;
Unsigned G: 1;
Unsigned H: 1;
};
Union bits {
Char ch;
Struct byte bit;
} ASCII;
Void disp_bits (bits B );
Int main ()
{
Do {
Cout <"enter a char :";
Cin> ASCII. ch;
Cout <"/nascii :";
Disp_bits (ASCII );
} While (ASCII. ch! = 'Q ');
Cout <Endl <"=========================" <Endl;
// Getchar ();
Return 0;
}
Void disp_bits (bits B)
{
If (B. Bit. h) cout <"1 ";
Else cout <"0 ";
If (B. Bit. g) cout <"1 ";
Else cout <"0 ";
If (B. Bit. f) cout <"1 ";
Else cout <"0 ";
If (B. Bit. e) cout <"1 ";
Else cout <"0 ";
If (B. Bit. d) cout <"1 ";
Else cout <"0 ";
If (B. Bit. c) cout <"1 ";
Else cout <"0 ";
If (B. Bit. B) cout <"1 ";
Else cout <"0 ";
If (B. Bit. a) cout <"1 ";
Else cout <"0 ";
Cout <Endl;
}
// -- Program #9 anonymous Union
# Include <iostream>
Using namespace STD;
Int main ()
{
Union {
Short int count;
Char ch [2];
};
Ch [0] = 'X ';
Ch [1] = 'y ';
Cout <ch [0] <ch [1] <Endl;
Cout <count <Endl;
Cout <Endl <"=========================" <Endl;
// Getchar ();
Return 0;
}