// -- C ++ shortcut tutorial -- Chapter 9 -- more data types and operators (Part 2)
// -- Chapter 9 -- more data types and operators
// -- 11/17/2005 Thurs.
// -- Computer lab
// -- Liwei
// -- Program #9 converts to uppercase characters
# Include <iostream>
Using namespace STD;
Int main ()
{
Char ch;
Do {
Cin> CH;
Ch & = 223;
// CH = CH & 223; // 223 = 1101 1111
Cout <ch;
} While (Ch! = 'Q ');
Cout <Endl <"=========================" <Endl;
// Getchar ();
Return 0;
}
// -- Program #10 to lowercase characters
# Include <iostream>
Using namespace STD;
Int main ()
{
Char ch;
Do {
Cin> CH;
Ch | = 32;
// CH = CH | 32; // 223 = 1101 1111
Cout <ch;
} While (Ch! = 'Q ');
Cout <Endl <"=========================" <Endl;
// Getchar ();
Return 0;
}
// -- Program #11 binary output
# Include <iostream>
Using namespace STD;
Void disp_binary (unsigned U );
Int main ()
{
Unsigned U;
Cout <"enter a number between 0--255 :";
Cin> U;
Cout <"here is ther number in binary :";
Disp_binary (U );
Cout <"here is the complement of the number :";
Disp_binary (~ U );
Cout <Endl <"=========================" <Endl;
// Getchar ();
Return 0;
}
Void disp_binary (unsigned U)
{
Register int T;
For (t = 128; t> 0; T/= 2 ){
Cout <Endl <"t =" <t <"= ";
If (U & T)
Cout <"1 ";
Else
Cout <"0 ";
}
Cout <Endl;
}
// -- Program #12 shift operation
# Include <iostream>
Using namespace STD;
Void disp_binary (unsigned int U );
Int main ()
{
Unsigned int I = 1, t;
For (t = 0; t <32; t ++ ){
Disp_binary (I );
I = I <1;
}
Cout <Endl;
// I = 4294967295;
For (t = 0; t <32; t ++ ){
I = I> 1;
Disp_binary (I );
}
Cout <Endl <"=========================" <Endl;
// Getchar ();
Return 0;
}
Void disp_binary (unsigned int U)
{
Unsigned int T;
// T = 128 64 32 16 8 4 2 1
For (t = 2147483648; t> 0; T/= 2 ){
// Cout <Endl <"t =" <t <"= ";
If (U & T)
Cout <"1 ";
Else
Cout <"0 ";
}
Cout <Endl;
}
// -- Program #13? Operation
# Include <iostream>
Using namespace STD;
Int div_zero ();
Int main ()
{
Int I, j, result;
Cout <"Enter dividend and divisor :";
Cin> I> J;
Result = J? I/J: div_zero (); // Error
Cout <"Result:" <result;
Cout <Endl <"=========================" <Endl;
// Getchar ();
Return 0;
}
Int div_zero ()
{
Cout <"cannot divide by zero./N ";
Return 0;
}
// -- Program #14, Calculation
# Include <iostream>
Using namespace STD;
Int main ()
{
Int I, J;
J = 10;
I = (J ++, J + 100,999 + J );
Cout <j <<'' <I;
Cout <Endl <"=========================" <Endl;
// Getchar ();
Return 0;
}
// -- Program #15 use of sizeof
# Include <iostream>
Using namespace STD;
Int main ()
{
Char ch;
Int I, Nums [4];
Cout <sizeof ch <'';
Cout <sizeof I <'';
Cout <sizeof (float) <'';
Cout <sizeof (double) <'';
Cout <sizeof Nums;
Cout <Endl <"=========================" <Endl;
// Getchar ();
Return 0;
}
// -- Program #16 use of new Delete
# Include <iostream>
Using namespace STD;
Int main ()
{
Int * P;
P = new int;
* P = 20;
Cout <* P;
Delete P;
Cout <Endl <"=========================" <Endl;
// Getchar ();
Return 0;
}
// -- Program #17 Use of New Delete
# Include <iostream>
Using namespace STD;
Int main ()
{
Int * P;
P = new int (99666666 );
Cout <* P;
Delete P;
Cout <Endl <"=========================" <Endl;
// Getchar ();
Return 0;
}
// -- Program #18 Use of New Delete
# Include <iostream>
Using namespace STD;
Int main ()
{
Double * P;
Int I;
P = new double [10];
For (I = 0; I <10; I ++)
[I] = 100.00 + I;
For (I = 0; I <10; I ++)
Cout <p [I] <'';
Delete [] P;
Cout <Endl <"=========================" <Endl;
// Getchar ();
Return 0;
}
// -- Program #19 use of malloc ()/Free ()
# Include <iostream>
# Include <cstdlib>
Using namespace STD;
Int main ()
{
Int * I;
Double * J;
I = (int *) malloc (sizeof (INT ));
If (! I ){
Cout <"Allocation failure./N ";
Return 1;
}
J = (double *) malloc (sizeof (double ));
If (! J ){
Cout <"Allocation failure./N ";
Return 1;
}
* I = 10;
* J = 100.123;
Cout <* I <''<* J;
Free (I );
Free (j );
Cout <Endl <"=========================" <Endl;
// Getchar ();
Return 0;
}