Upload
others
View
8
Download
0
Embed Size (px)
Citation preview
�
�
�
�
�
�
�
�
�
�
�
�
�
�
�
�
�
�
�
�
�
�
�
�
�
�
�
�
�
�
�
�
�
� ���
�
�
�
�
�
� Authorized translation from the English language edition of�� Learning C++
By Tom Swan� Published by Sams� Copyright by Tom Swan
�
�
� �������������������������� �������� ���
�
�
� ������ ���������� 52108����863���� 03-6773898����
� http://www.focus.co.il������������ [email protected]�������������
� 03-9021902�������������������������������������������
�
�
� ����������� ������ �������������������������������������
� 1997�
�
��� ��� ������� ��������� �� ���� ��� �� � �������� ����� ����� ������� ��
�������������������������������������������������������������
� ���������� ����������������������
�
�
�
� ��������������������� ��������������������
� ������������������������������������ ���������S���� ������ ����������
� �����������������������
�
���������������� �������������������������������������
���������������������������������������������������������
���������� �����������������������������������������
����������������� ����������������������������������
� �������� ����������������������������������������
�
�
� �����������
�
� ��.......................................................................................................������ �� ............................................................................................................... ������� �� ...................................................................................................���������� �� .............................................................................................������� ������� �� ................................................................................................... ������������ �� ..............................................................................................������� ��������� �� ............................................................................ ZORTECH����C������������� �� .........................................................................................���������������� �� ...............................................................................................VDE������������ �� .......................................................................................................�������������
� ��...................................................................................... C�������������
� �� ......................................................................................... C������������������ � ............................................................................................................������� �� ................................................................................... ��� ������������������ �� .........................................................................................������������������� �� ............................................................................................................. ����� �� ...........................................................................����������������������������� .....................................................................................................� ������ ......................................................................................................���� ������ ..........................................................................................................����� � � ....................................................................................................������������ � ..............................................................................................................������� ........................................................................�������������������������� � .........................................................................������������������������� � ...........................................................................................��������������� � .................................................................������������������������������� � ............................................................................................................ �� ������ � ....................................................................................................... �� ������
�
� �� ............................................................................................... �������������� � �� .......................................................................................................������� � �� ..............................................................................................���������������� �� ............................................................................................... ���������������� �� .......................................................................................................��������� � ..............................���������������������������������������������� ���� �� ................................................................................................................������� �� ...........................................................................................�������������� ��� �� ...............................................................................................����� �������� �� ................................................................................................�������������� �� ...............................................................................................�������������� � .............................................................................................. ��������������� �� ....................................................................... ���������������������������� �� ...........................................................................................................������ ��� �� ...........................................................................................������������� ��� �� ....................................................................��������������������������� ��� �� ........................................................................................................... �������� � ...................................................................................................��������������
� ��..................................................... ��� ������ ���������� ��������� �� ............................................................................................. ������������ ��� �� .....................................................................................................����������� �� ..............................................................................������������if/else������ �� ...............................................................................������������� ������������� �� ............................................................................. ��������������������� ��� �� .........................................������������������������������������� �������� ��� �� .....................................................................................................���������� � � ........................................................................���������������������������� �� ....................................................................................................... ����������� �� ...........................................................................................��������������Exit� � ..............................................................................................�������������� �� ......................................................................................while��� ��������� � �� ........................................................................... if/else������������������� � .................................................................................................switch�� �� � .................................................................................... switch��� �������� � � .............................................................................................do/while��� �� � � ........................................................for��������C++������������ � ��� ��� � � ............................................................................ for��������������������� �� ............................................................................................��� ������������
�
�
� ��� ...................................................................................����� ��������break� ��� ...............................................................................��������������continue� ��� ..............................................................................................goto�������� ��� .................................................................. RAM��������������������������� ��� ............................................................................................... ����������� ��� ................................................................................................������������ �� .........................................................................................................�������� ��� .........................................................................................................������ ��� ..................................................................................................... ���������� � ............................................................................................... ����������� �� .......................................................................................��������������� �� ................................................................................�������������������� �� .................................................................................��������������������� �� ........................................................................................... �������������� �� .............................................................................................��������������� ��� ...............................................................................����������������������� ��� .................................................................................................��������������
� �� ...........................................................����� ��������������������� ��� ................................................................................................���� ��������� ��� .....................................................................................����������� ������� ��� ....................................................................................... ��������������� ��� ....................................................................................��������������������� � ��� ............................................................................................������������� ��� ........................................................................................... ��������������� �� ...................................................................................................����������� ��� .............................................................................................�������������� ��� ....................................................................................�������������������� � ��� .................................................................................................����� ������� �� ..................................................................... ������ �������������� �������� ��� ..................................................................................������������������� ��� .....................................................................������������������������������ ��� .................................................................................................INLINE�������� � ��� ........................................................................................................�� ������� � � .................................................................................................��������������
� �� ..................................................................................... ���� ������� � � .............................................................................................���������������� � � ...................................................................������������������������������ � � .....................................................................................�� ����������������
�
� � � ........................................................................... Void��������NULL������� � � ............................................................................... ������������� �������� ��� .................................................................................��������������������� ��� ..............................................................................�������������������� ��� ................................................................................�������������������� �� ...............................................................................................������������������ �� .............................................................................................������������� ��� ................................................................................................ �������������� ��� .................................................................... �������������������������� ��� .....................................................................................������������������� �� ..............................................................................................������������� �� .........................................................................malloc�������������������� ��� ..........................................................................������ ��������������������� ��� .............................................................................................������� ��������� �� ............................................................................................... �������������� ��� ...............................................................................................������������ ��� ....................................................................................... ���������������� ��� ................................................................... ���������������������������� � ��� .................................................................................................������������ � ��� ................................................................................. ������������������ � �� ................................................................................................������������ ��� .............................................................................................������������� ��� ............................................................................................. ��������� �� ��� .................................................................................. ���� ������������������ ��� ............................................................................................���������������� ��� .........................................................................................���� ����������� �� .................................................................................................��������������
� ��� .............................................................................����������������� ��� ............................................................................................................����� ��� ................................................................................................����������� ��� ............................................................................. inline����������������� � ��� ..................................................................................���������� ����������� ��� ..................................................................................................������������ ��� ..............................................................................�������������������� ��� ............................................................................................������������� � ..........................................................................................������������������ � .......................................................................................... ������������������ � ................................................................................................ PERSON�����
�
�
� � ...................................................................................person��������������� � ...................................................................................perscollection������ � ...................................................perscollection���person��������������� �� �� ..................................................................................................FLOOR������ � .......................................................................................floor���������� � ............................................................................................ELEVATOR������ .................................................................................. elevator���������� � .............................................................................................���������������� �� ...............................................................................................������������ �� ............................................................................................. BUILDING������ �� ................................................................................. �� ��������������� � � ..................................................................................building���������� �� .................................................................................��������������������� �� .................................................................................................��������������
� ��� .......................................... ��������� ����������������� ������ �� ...........................................................................................WINTOOL��������� �� .....................................................................................WINTOOL���������� �� ............................................................................WINTOOL.H������������� �� ................................................................................. ������������������ �� .................................................................................�������������������� �� .....................................................................��� ����� �������������������� � �� ........................................................................................���� ����� ����� � �� .......................................................................................���������������� � ................................................................... WINTOOL.CPP���������������� �� ...................................................................................������������������� � � .............................................................................. OOP����OOP�������������� �� ..................................................................................................KEY������� �� ...........................................................................................KEY������������ �� ........................................................................................ KEY������������� � ...............................................................................................���������� ��� � � ....................................................................................ERROR������������� � � .................................................................................................��������������
� � .............................................. �������� ����������������� ������ �� ................................................................................................ ���������� �� �� ......................................................................................����������������� ��� .............................................................................................. ��� ��������
�
� ��� ....................................................................................ITEM.H������������� ��� .................................................................................................����������� ��� ............................................................ protected���public��private���������� ��� .........................................................................................................���� � ��� .......................................................................................item����������� ��� .........................................................................................ITEM.CPP������� ��� ..........................................................................................�������������this� ��� ..............................................................................................LISTS���������� ��� .....................................................................................LIST.H������������� ��� .........................................................................................list����������� ��� ..........................................................................................LIST.CPP������� �� ...........................................................................................��������������� �� .............................................................................STRITEM.H������������� � .................................................................................................������������ �� ................................................................................... strItem����������� �� .........................................................................................strItem.cpp������� �� ...............................................................................��������������������� �� ....................................................................................... WINDOWS���������� �� .............................................................................WINDOW.H������������� ��� ...................................................................................������������������� � ��� ........................................................................................����������������� ��� ...............................................................................................������������ ��� ............................................................................................... ������������ � ��� ..........................................................................�������������������������� �� ..................................................................................WINDOW.CPP������� ��� ............................................................................................��������������� ��� .......................................................................... SELECTOR.H������������� ��� .................................................................................................����������� ��� .................................................................................. selector����������� �� ............................................................................... SELECTOR.CPP������� ��� ............................................................................................����� ���������� ��� .........................................................................COMMAND.H������������� ��� ............................................................................... command����������� �� ................................................................................................................������ �� ............................................................................................................ �������
�
�
� �� ...........................................................................��������� ������� ��� .......................................................DOS����������� ������������������������� � ��� .......................................................................................................����������� ��� ............................................................������������������������������������� ��� .......................................................................................... ����������������� �� .....................................................................................................������� � ��� .....................................................................��������� ����������� ��������� ��� ......................................................................................... ����������������� ��� ...........................................................................�� ���������������������� ��� ...................................................................... �� ������������������������ ��� .............................................................................................��������������� � � ......................................................................................................������������� � � ...........................................................��������������������������������������� � .........................................................................����������������������������� � .............................................................................�������������������������� � � .............................................................................������������������������� � � ..................................................................................������������������������ �� .................................................................................������������������������ ��� ................................................................................................���� ����������� ��� ................................................................................ ��������� ������������ ��� ................................................................................� �������� ������������ ��� ...................................................................................................��� ������� ��� ..................................................................................��� ��������������� � ��� ....................................................................................... ��� ���������������� �� ............................................................................................���� ����������� �� ......................................................................................DIR.H������������� ��� ...........................................................................Dir������������������� ��� ........................................................................................... DIR.CPP������� �� ................................................................................................���� ������� �� ............................................................. OOP��������������������������READ� �� ...................................................................................READ�������������� �� ...................................................................................... READ.CPP��������� �� .................................................................................................��������������
� �� .............................................................C++� ��������������������� ��� ........................................................................................... �������������������� �� ............................................................................................... ������������ ��� ......................................................................................�����������������
�
� ��� ..................................................................................�������������������� � ��� ..................................................................................�������������������� � ��� .................................................................................................������� ������ ��� ........................................................................... �������������� �������� � ��� .....................................................................................overload�� ������ ��� ...................................................................................�������������� ������ ��� ..................................................................... ������������������ ������ ��� .............................................................................................. ������ �������� ��� .......................................................................��������� ���������� ������ �� ...............................................................................�������������� �������� ��� ...............................................................����������� ��������������� ��� ��� ..............................................................................������������������������� �� ......................................................���������������������� �������������� �� .......................................................................................���������� �������� ��� ................................................................................���������� �������� ��� ............................................................................................������ ������� ��� .....................................................................................������������������ �� .................................................................................................������������ ��� .............................................................................................������ ������� ��� ..............................................................����������������operator=�������� ��� ....................................................................... ���������������� �������� ��� ............................................................................................ ������������������� ��� ................................................................................................. this�������� ��� ...................................................................................................new������ �� ................................................................................................ delete������ ��� ...................................................................................................������������ ��� ...........................................................................................�� ������������ �� ...........................................................................................��������������� ��� ................................................................................................................������ ��� ............................................................................................�����I/O������� ��� ...............................................................................................�������������� ��� .............................................................������� ����������������������������� � ��� .............................................................���������������������������� � ................................................................................������������ ��������� � ................................................................................. ��������������������� � � .................................................................................... ���C���������������� � � ...................................................................................... C++��C������������ � � ...................................................................... C����������������������� � � .................................................................................................��������������
�
�
� �� .....................................................C������������������������
� � � .........................................................................................����������������� ��� ..................................................................................................������� �����
� ��� ..........................................................................������������������� ��� .............................................................������������������ ����� ��� ....................................................................................... ����������� ��� ................................................................... ������������������ ���� ��� ...............................................................................................................���� � �� ...............................................................................................................���� � ��� ...............................................................................................................��� � � � ...............................................................................................................���� � � � ...............................................................................................................���� � � � ...............................................................................................................���� � �� ...............................................................................................................���� � �� ...............................................................................................................���� � ��� ...............................................................................................................����
� ��� .......................................................................................������������
�
� �����
������ ����� � ���� ��� ������ �� ��� ����� ������ ������� ��� ��� ���� ������� ���
����������������� �������� �C++����������������� ������� ����� ������ ��� ������������������ ���� ��������������������� ���������� ��������� ��������������������
� ���������������������������
����������������C++����������������������������������������������������������� �C++�� �������� ���������������� ���������� ��������������������������������������������������������friends�������������������������������� ����������� ���������C++������������������� ����������������������������������������������������������������������C++������������������������� �������������������� �����
� �C++�������������������������������������������������������� �
���� ������� ���������������������������������������������������������������
��������� ������� ���� �� � �� �Zortech� ���� �� �������Walter Bright� ���� �� ��������������������������������������MS_DOS��������������C++����������������������C++����������� ���������������� �C++����������������AT&T�
� �C++������������ �����������������������������������
��������������������������� ���������������������������� �����������������
���������������������������� ���������������������������������������������C++� �C++��������������������������
������ ���� ����� ���� �� ���� � �� ���� ���� ��� �� ���� � ������ ������� �����
�������������������������������������������������INSTALL����� ����������
�� �� �� ���� �� ������� �Zortech� �C++� ����� ������� ��� ������ �� �� ���
� ������
� ������� ������ ����������� ������� ���� � ���� ������ ������� ���� � �������� ������ ������
����������������������������������������������������������������������������
� �������������������������� �������
� �������������� �� �C++���� ����������������� � ��
� ���������� � ��PC������� ������PS/2��AT��XT��IBM PC� ���
� � ����� ���������RAM��512K����384K� ���
� �������������������� �������� ���
� �� ������������� ������ ���
� ���������� ���
� ����������� ������DOS 3.0��������������������������DOS 2.0� ���
� ������������������� ��������������� ���
� �� �������������������������������������������������������������������������������������� ���
� ������������ ����������������������������VDE������
���������������������� ���������������������������Zortech C++����������� ���
����������������� ������������������ ����������������������������������
���������� ����������� ������� ������������ ������������������������������
�������������������������������������������������������������Zortech C++� ������
� Zortech� ��C++��������������������� ��������� ����������������������Zortech C++�������������������������������������� ������������������������������ ������������ �����
� �� �����������������������������������������������������������
� ������������������INSTALL������������� ����������������������� ������������������������
� DOS��������� ������������
��������������������������� ������ �C:������������ ������������ ��������������������Zortech C++���������������������������������������������������������� ��DIR������������ ������������������������ ���������������������� ��
������������������������������������������������������������ �������� �C>� ������ �������������� ������������������������ ��������� � ����������������
� �� � �������������� �� ����
�
��������� � �� �CPFILES.ZIP� ��� ������ ����� ����� �������� ������� ����� ������������� .CPP� ������� ���� ������ ������ �\LCPP� ��� ������ �C� ����� ������� ������
���������������������������������������������������������\LCPP\SOURCE������� ���������������������������������\LCPP\LIB
����� ������ ��� ������AUTOEXEC.BAT� ����������������� ����� ������� ���� ���� �AUTOEXEC.~AK���
� ����AUTOEXEC.BAT���������������������
� �PATH����� �������C\LCPP�������������� ����
� �������������������PATH������������������� ����������
� ������������������� �������������� ���
� �SET����������������� ����
������� ������ ������� ������ ���� ��
���� ������ ���
� �� ������ ������������������AUTOEXEC.BAT������������������
� VDE���������������� �������� ����� ������� ����VDE������ �������� ��������� ���VDE������� �DOS��EDIT���������������������������������� ������������������������
�� �� ����� �vinst� ������� ��� �����C:\LCPP� ������ ����� ����������� ���� ������� ���� ����S� �� � � ��� � ��� ������� ��� ������ ���� ����� �� ������� ����������������� ���������� ���������������������������������configuration����������C:\LCPP\SOURCE\C01���������������������������������������DOS��� ���
� �������������������������� ����������
������� ����� ���� � � ����� ��� ����� ����� ����� ���� WELCOME.CPP� ������ ������ ������� �����ASCII� ��������������� �����������VDE������� /n���������VDE�������������������������������������������/n����������������������������� ����VINST.COM����������������������� �������� �������������� ���
����������������������������nondocument����������� �����������������������VDE.DOC� ������ ������� �������� ��� ���� �VDE�� ������� �������� ��� ���
������������������������������������������������������������VINST.DOC�
� ���������������������� �������������������������������������
��������������
� �������������� �� �C++���� ����������������� � �
������� �������������VDE��DOC���� ��� �������READ����� ��������������
������ ������������READ������ ���� ���� ��������������READ�� �
� �!������������ ����������� ������������ ������ ��
���������Y���� �������������������������<Ctrl>-KQ������VDE���������������� ������������������������� ���� �N���� � ��� ���������������� ���DOS�������������� ���������������� �<Ctrl>-KS����� �DOS���� ��������������
� �<Ctrl>-KX������DOS���� �������
� ��������������������������������������� ������������������������������������������������
� ��������������������������������������������������������������
������� ����� ����� ������� LIB�� �INCLUDE� �PATH� �������� ���� �DOS�� ���
������ ��� � ��� DOS� � � ���� ���� ��� set� ���� �������� ����� ���� ����������� �������� ��� ���� �PATH�� C:\LCPP� ������� ��� ����� ���� ��������
� ���������������������� ����������
���� ���� ������������� ������ ������� �� ����� ���� cd \lcpp\source\c01� ���� ���
���������� ������� ����� ���� ����� ����������� ��� �� �� ������ ��� ������ ���
� �C:\LCPP\LIB������ ���������������������������� ��������
����� ������ �WELCOME.CPP� �������� ��� ���� ���� ���� ztc welcome� ���� ���
� ���������������������������������������
������� ���!��"������� ���
#����$�"%%�&���"��� ��
���'����!��"����� �� �����(
)*+,-��� �������
�������� ������� ������ ����� �WELCOME.EXE� ������ ������ ����� ����� ����� ���
� ���������������������welcome����
���������� ��� ���� ���� ����� ��������� ������ ��� ���� ���� ������ ztc� ������� ���� ���
� ��������������������������������������� ��������������������������������
����� �Command line error; unable to open input file�� ����� �������������� ���
����������Zortech��C�����������ZTC1���� �����������������������CPP�������������������� �C�������������������ZTC� ������ ����� ����� ��� ����������
� ������������������������������������������������������
� � � �������������� �� ����
�
��������������������������� ��������� �WELCOME����������������������� ��������� � �� ����� ���� ������� ������ ���� ���� �� ������� ������� ���� ����� ���� ����
� ������
� �����������������������cd \lcpp\source\c05����� ���
����������������������������� �ZZ.BAT��������������� ����zz����� �DOS�� ���
������ ��� ����� ����� ����� ���� ��� ���� ��������� � ������ ������ �� ������
� ���� ���������������
�<Esc>���� ����������������������elevsim��������� ��DOS��� ������������ ���
� ��������
��C:\LCPP\SOURCE\C04��POPUP.CPP�������������������������������������<Spacebar>� �� � ��POPUP� ��� ���� ���� � �� �ztc popup� ������� �������� ����ZZ.BAT� ������ ��� ����� ��� ���� ���� ��������� ���� �� ��� �� �� ���� ������ ��������� ���� ��� ����� ���� ���� �WINTOOL� �������� ��� ���� ���� C:\LCPP\LIB��� � �WINTOOL������� �� ��������������� ����� ����WINTOOL�����
� �DOS���� �����<Esc>��
�n�����C:\LCPP\SOURCE\C0n��� � �����CD��� ��������������������������
������ ���� �C:\LCPP\LIB��� ����� ��� "� � � #� ������� � � � ��������� ������ ����� ���
�����READ��� ������� ���VDE��������C:\LCPP\ANSWERS������������ ���������
���������� ������ �read������������������ ���������������� ��� ����������� ��
��� ���� �������� ��������� � �� ����������� ��������� � �����������������
� ������ ����� ����� ����� ����
�
�
� �����
�����������
� �����������
������������������������� ���������� ��������������� ���������������������
��� ���� �� ����� �������� �������������� ��� � ���� ������� ������ ������������� ���
� �� �������
������� ����� � �� �� ������� ������ ������� �� ���� ����� �� �� ������� �������� �����
�������������������������������������������C++���������������������� �������������������������������������������������������������������������� ��������� ��
� ��������������������������������������������������������
���� ��� ������� ��� ����C++������������������� ���������������� ������ ������ ����������������������� ������� ����������������� ��������������� ���� ���
���������������������������� ����������������������������������� ���������������
����������������������������������� ������������������ ����������������
������������������������������ ��������� ������������������ �Pascal����C�����C++�������������� � ������ ��������� ������ �������������� �������� �����������
� �����������
�������������� ���������������������������������������� �������������������
� ������������������������������������������� �������������������������������
������� ������������ � �� ������������������������������� ����� ���������� �������
� ���������� ��� ������� ������������������������������
� ���������������� �C++������ ����� ������������ � ��
���� ������ ����� ������ ������� �C++������ ����������������������������������������������� �� �� �������������������������������������������������������������
������������������������������������������������ ������������������������
����������������������������� ��������������������� ���� ����������� �����
� ���������������������������������������������������������������������
������������� �������������������������������������������������������
���������������������������������������������������������������� ������
�����������������������������������������������������! ��" ��#�I/O��������������� ������$������������$��������������C++��������������� ���������������������
� ������������������ ����������������������������������������C++������������������� ���� ������������������������������������������������������������������������������� ����� �
� �����
���������������������������C++��������������� ����������������������������������������������� ���� ���� ��� ���� �������� �������� ���������� ������ ��������� ������
� �����������������������
� ������������������ ���������������������C++����relational operators���������� �� ��������������� �� ����������count���������������������������� ����������������������������������count�����������������������������������������������������
� count < 10�������� �evaluates�� ��� ��� ���� ���� ���� ���� �� ������ ������ ��� ������� ������ ������������������������������� ������ ����������������������� � �� ������������
�������������������;��������������������count�������� ���� �������������������� ������������������ ������ ��������������������������count < 10�������� ����������������������������� � ������������������������� �������� ��� ���������������� ����������
� ��������������� ����������������������������������������������
� �����������������������
� �������� ������ ����
� �� ������� (a<b)� ��� ����������������� (a<=b)� �� ����� �� (a>b)
� ��� ��������������� �� (a>=b)� ��� ������� (a==b)� ��� ���������� (a!=b)
� �� � ���������������� ���������������������������������
�
�
�
�
������������������%������������������� �����������������C++��������$��������
� ��������������������������������������� ����
���������� ���������� � ����������� � ������������������ ��� ��� ��������� ��������
������������������ ������������������������������� �����������������������������
� ������������������������������������������������� ������������ �������������
��������������(a=b)�������� ��������������������b���a�����������������(a==b)�������������� ������� ������� ��� ��� ���� ����� ������ ��������� �� ���� ������ a��� b� ������ ������� ������� ������ ��� ����� � ���� ����� ����� ����� �C���� C++��� �������
� ��������������������������������������������������������
��� �������������������������MILES.CPP��� ����� ��������������������� ������������������ ��������������������������������������������������������������
���������������������� �����������������������������������������������������
����������������������������������MILES������������� ��������������������������������� ������������������������������������������������� ������������������
���������������������������������� ����������� �������������������������� ������ �
� �����������
� MILES.CPP���������
1: // miles.cpp -- Miles to Kilometers converter 2: 3: #include <stream.hpp> 4: 5: #define MILES_PER_KM 0.6214 6: #define KMS_PER_MILE 1.6093 7: 8: main() 9: {10: int choice;11: float km, miles;12:13: cout << "\nMiles and Kilometers Converter\n";14: cout << "------------------------------\n";15: cout << "1 : Miles to kilometers\n";16: cout << "2 : Kilometers to miles\n";17: cout << "3 : Quit\n\n";18: cout << "Enter 1, 2, or 3: ";
������
� ���������������� �C++������ ����� ������������ � ��
19: cin >> choice;20:21: if (choice == 1) {22: cout << "Miles? ";23: cin >> miles;24: km = miles * KMS_PER_MILE;25: cout << miles << " miles = " << km << " kilometers\n";26: }27: else if (choice == 2) {28: cout << "Kilometers? ";29: cin >> km;30: miles = km * MILES_PER_KM;31: cout << km << " kilometers = " << miles << " miles\n";32: }33: else if (choice != 3)34: cout << "Illegal choice!";35: }
� ������������if/else������������� ������������������������ ������� ������������MILES.CPP������������������������������������ C++��������������������������if/else����������������������������������������������� ��������������if���������������������� ���������������count������������������������� � �� ����� �� ���������
� ��������������������
������������� ����������������������������������
����������������������count���� �������������������������������� ��count������ ������������������������(count > 100)�������� ������������������ ��������
�������� ��� ��������� �� � �������� ������� ��������������� ��������� if� ����� ���������� ���������������� ����������������������������������
�������������
���������������������������������
������������������������������ ����������������C++���;�������� ��������������������� ������������������������������������������������������ �����������
� ���������������� ����������
���������� ������� ��� ���� � ��� ����� ����� ����� ��� ��������� if� ����� ������ � ��������� �block����������compound statement���� ����������������� � ��������
� ����������� ����������������� ���������������������������������������������� ��
� �� � ���������������� ���������������������������������
�
�
�
�
������������������������������������ ������������������ �������� ���������
������ �������� �������������������������������������� �� ������������������ ��
� ���������������������count�������������������������
�������������� �����������������������������������������������������������������������������
�
������ � ������� ��������������������� ������������������ ������������ if��������������������� ����������������� �������������������������������������
� ������������������ ������������������������������������������ �����������������
����������������������������������������������������������������������������� �
� �������������������� ������������
���� ������� ��� ��� ��� ����� ���� ����� ���� ���� ������ ���� ���������� ��������� �����
����� �� ���� ���� ��������� �� ������������� ������� ��������������������������
����� ��������������������������������;����������������������������������� �������������� ������������������������������������ ���� � ����� ������� ����������
�������������������������������������� ����������������������� ���������������������
������������������������������������������� �����������������������������
� ����������� �������������������������������� �
���
�
� ������������������� ������������������� �������������������������������
� ���������������������������������������
���������� ������� � �� ��� �� ������������ ���� �MILES.CPP� �� ����� ����������������������������������choice����������������������������������� ������if������choice���� ���������������������������� ����������������������������������������
� ������������������������������� ����������������������
� ������������else������ else��if�������������������� �������������������������if��������������� �������if������������������������������������������������ ����� ����������������������������������������������� ���������count���������������
� ���������� ����������������������
���������������������������������
����
��������� ! "������������#�����������������
� ���������������� �C++������ ����� ������������ � ��
���������� �������������������������else���if������������������������������� ������ �������������������������������� ����������
������������� ��
$$����%�������&���������&'��������������
��������
$$����%�������&���������&'����������������
�
������������ � MILES.CPP���������������� ��if��������������������else������� �����������������������������
���������������
$$����%�������&��������������������
���������������������( ��
$$����%�������&�������������������(
���������������������) ��
$$����%�������&�������������������)
��������
$$����%�������&�������������������������*�����
�
���� ���� if��� ������ ������� ����� ������� ��� ��� ������ ��� ����� ������� else��� ���� ��������������������������������MILES.CPP���������������� ����������
����������������+��)
��������,���#���������+��
������������������������ ��� ����choice���� ������������������� �������choice������������������������������������������� ������� ����������������������������
�������������� ������� ��������������� ������������������������������� ���������
� ����������������������������������������������������if/else������������� �
� ������������������������������������������������������������������������������������� ���������� ������
������������������ ���� ������������������������������ ���������������������� ��
������������� �� �������������������� ��������������������������������������
���������������������� � ���������������������������count�����������������������AND� ��������������������������� ������� �������������������������������
� �&&������������� �--�������������
���������������������������������
� �� � ���������������� ���������������������������������
�
�
�
�
����� ����� � ������������������� ��� ���� ��� �� ������ �������� ��������������
� ��1 <= count <= 100��������������������������������������������
�������������� �--�������������
���������������������������������
� if������������������������������������� ���������������
�� �� ���� �����������������count���������������((1 <= count) && (count <= 100))������������������������������������������������������������C++��������������������������������������������������������AND���������������������������������
� ����������������������������
short-circuit�� ������ ��� ���� ��� ��� ����� ����� ������� C++� ������ ���
������� ��������������������������������������������������expression evaluation
��%��������count��������������� �������������������������������C++�������
��������������������((1 <= count) && (count <= 100))������� ����&�����&�������
��������������������������� $���� �(1 <= count)� $����������� �� �����������
� ������������������������������������������������ ���������� ��
����� ��� ����� � �� ���C++��� ������� ��logical OR�� �����OR��� ���� ���� ����� ������������������������ ����������������������������� ��������������������������������
� �������������������� ���������
�������������� �..�������������
����������������������������
����� �� ��� ���� count������� ���������������� ������� ����� ������� ��������������� ���������������������������������������������������������������� ������
�� ��������������������� �������������������������������������������������� ��������
� ����������������
��������������� �--������������� �..����������/�
�������������/��������0�1��������2��������
������������� ��� ���������� ������������count������������������������������ ���������� ������� ����� ���� ���� ����� ����� �� �� �� �� �� �������� ��� ����� ��� ��� ��
�� ��� ������ ���� ������� ������ ����� ������ ������ ���� ���� ��� ���� ������ �������
� ����������������������� ���������if�����������
�������������� �--�������������
$$�3��%����������������*��������%��������
�����������������/�
$$�3��%��������������/�
� ���������������� �C++������ ����� ������������ � ��
������� ����������� ������������ ������������������������� ������� ��� ����� �
����� ������ ������� ��� ���� �������� ��� ���� ������ ��� ������ ������� �� �� � �����
������������ ������ ����������������������������������� �����������������
������ �������� � �� ���������������������������������������� �� � ������������
������������������������������������������� ����������������������������������
��������������������������� ����������������������������� ���������������
� ���������� ��������������
� GAS.CPP���������
1: // gas.cpp -- Gas consumption calculator 2: 3: #include <stream.hpp> 4: #include <stdlib.h> 5: 6: main() 7: { 8: float start, end, gallons, miles, consumption; 9:10: cout << "Enter starting odometer reading: ";11: cin >> start;12: cout << "Enter ending odometer reading: ";13: cin >> end;14: if (start > end) {15: cout << "Odometer readings are reversed\n";16: exit(1);17: }18: cout << "Enter gallons purchased: ";19: cin >> gallons;20: miles = 1 + (end - start);21: consumption = miles / gallons;22: cout << "Consumption = " << consumption << " miles per gallon\n";23:24: cout << "Your vehicle's fuel use is ";25: if (consumption < 20.0)26: cout << "poor\n";27: else if ((consumption >= 20.0) && (consumption < 25.0))28: cout << "good\n";29: else30: cout << "excellent\n";31: }
� �� � ���������������� ���������������������������������
�
�
�
�
�if��������������������� �����������������������������GAS.CPP����������������������� �������������������end���������� ��start������ ���� ����������������������� ������������exit(1)���������� ���������������������� ���������������������������DOS������������������������������� ��exit������� ����� �����������DOS�������������� ���������������������������������� ������������������������������������������ ���������� �� ���������������������������������errorlevel���
� ��������������������
�STDLIB.H����������������������������� �exit������������������ ����� ���
�������� ��� $��� �� �� ������ ����� exit� ������� ��� �'� ����� ����GAS.CPP��� ���
� �C++��
���������� ����������������������� ������������������������������������if����������������������� ����������������������������� ������������������������
������������� � ������ ������� ����AND� ��������������������������� ���������
������� ��� ������� �������������������� ������else����� ��� ������������ ������� �������� ���� ��� ���� ������� �������� �������� ��� ������� ������ ����� ��� ��
� ��������� �������������������������������� ��GAS.CPP�
������������������������������������ ��������GAS.CPP��� ��������������������������������������� ���������GAS.CPP� ������������������ ��������� � ��������������� ��������������������������������������������������������������
�����������������������������������������������������������if/else�������������������������������� ������������������������������������������������������
� �����
� ������������������������������� �������������������C++������������������������������������������������NOT���������� ��������������� ���������������������� ������������������NOT��������������������� ������������������������������������������������������
� ���������������������������������������
����+�����������
�������������������������������
������� ������� ������ �������� ��� ��� � �� ������� ������� (!(count < 100))� ���������������������������������not�������������������� ����������������(count < 100)�������� ���� ��� ���� ���� count���� ��� ������ ����� ���� ����������� ������� ����
� ���������������������������������(count >= 100)�������
� ���������������� �C++������ ����� ������������ � ��
������������������� ������������������������
� ������C������� C++�������������������� ��������������������������������������������� ������� ���������������������������������������������������������������������
����������������������������������������������������� ����������������������������
�������������������������C++� ��������������������������������������������������� ���������������� �����������������
������ �� ���������������������� ����������������������������������������������
������������ ��������������������������������������������������������������
��������������������������������� �� �������������������������������������������������
�����B����������� ������������;�����������A����������������� ������������������
� ���������;������
���������������������������������� �������������������� ���������������
��� �� ���������� ����� ��� ����� ��� ��� �� ���� �������� �� ����� �������� �������
����������������������� � ��������������C++����bitwise operators�������������������������������������������������������� ���������������������������������
� ���������������������������������� ������������������
� �����������������������������
� �������� �����
� �� AND������������������������
� �� �����OR������������������������
� (inclusive OR)
� �� (exclusive OR)�������OR������������������������
� ��� ���������������
� ��� ����������������
� �� ������������
������� $��� ������ ���� ���� ��������� ������ �� ������ ������ ��� ����� ���
��� ��������������� ������������������������������������ ������������� ��������
�$���������������������������$��������� ��������������������C++������
� ���������������� �����
� �� � ���������������� ���������������������������������
�
�
�
�
����������������������� �����������������������������������������������������
������ ������������ �����������OR�����������������OR��������AND����������������
������������������������������������ ��TXOR.CPP����TOR.CPP��TAND.CPP���������������������������������������������� �������������������������������
�����XOR�;��������Test�������������������������T������������������������������������������������������������������ �exclusive OR��������OR������������
���� ���� ������ ��������� ������� ���������������������������������� ���������
���� ����� ���� ��� ��� ���� ���� ������� ����� ����� ������ <Enter>� ��� ����� TAND.CPP����� ��������������������������������� ����� �� ���<Enter>
� TAND.CPP���������
1: // tand.cpp -- Test program for bitwise AND 2: 3: #include <stream.hpp> 4: 5: main() 6: { 7: unsigned int v1, v2, v3; 8: 9: cout << "Enter values to AND: (ex: 1234 15) ";10: cin >> v1 >> v2;11:12: v3 = v1 & v2;13:14: cout << form(" %5u %#06x %016b\n", v1, v1, v1);15: cout << form("AND %5u %#06x %016b\n", v2, v2, v2);16: cout << "=====================================\n";17: cout << form(" %5u %#06x %016b\n", v3, v3, v3);18: }
������AND�������������� �� �����������������������TAND.CPP����������������� v2���v1����������������������unsigned int������������������ ���� ��������������������������������� v3� ��������������������������������������������� ����� ������������������ ��������������������� � ��form����������� �������
� AND�����������������������������������������
� ���������������� �C++������ ����� ������������ � ��
� AND� ���������������������������������������
A & B == C
0 & 0 == 00 & 1 == 01 & 0 == 01 & 1 == 1
������ �� ����� �������� B��� A� ������ �� ������ �� ������� ������ � �� ���� ���
��������������������������� �������������������������������������������AND��������
� ����������������� ����� ������������������TAND.CPP�������� �����
������������������������������������AND�����������������������������������
� ��������������������������TAND������������� �� �����������������������������
� �������������������� �mask�������������� ��������������������������
��(� �&�)�2 ����������������
456 ���7 �&���� ����������������
������������������������������������������������
���) �&���2 ����������������
��� ������ �������� ���� ������� ��� ������� ������ ������� ������������������� �����
���������������������������� �����������������������������������������������������
� �������������������������������� ��������������
������OR�������������� �� ������������ ���TOR.CPP��� ����� �������������� �bitwise inclusive OR�
� TOR.CPP���������
1: // tor.cpp -- Test program for bitwise inclusive OR 2: 3: #include <stream.hpp> 4: 5: main() 6: { 7: unsigned int v1, v2, v3; 8: 9: cout << "Enter values to OR inclusively: (ex: 1234 15) ";10: cin >> v1 >> v2;11:
������
� �� � ���������������� ���������������������������������
�
�
�
�
12: v3 = v1 | v2;13:14: cout << form(" %5u %#06x %016b\n", v1, v1, v1);15: cout << form("OR %5u %#06x %016b\n", v2, v2, v2);16: cout << "=====================================\n";17: cout << form(" %5u %#06x %016b\n", v3, v3, v3);18: }
�����������������������v2���v1�unsigned int���������� ��������TOR.CPP��������� ����� �� �������� ��� �� ����������������OR������������������������
��������������������������������������������������������������������������� OR� ����������������������������������������������� �����
� OR� ���������������������������������������
A | B == C
0 | 0 == 00 | 1 == 11 | 0 == 11 | 1 == 1
���� ������������������������� ���������������������OR������������������������
��������������������� �� ����������������������������������������������
����������TOR������� ������������ �����������������������������������������
� �������������� ����
�7( �&��89 ����������������
! ��7 �&���7 ����������������
������������������������������������������������
�7: �&��82 ����������������
���������������������� ����������� �������������� �����������������������������
������������������������������������� ������������������ ����� ����������������
� �������������������
������OR������������������������������ ������� ���TXOR.CPP��� ����� �
� TXOR.CPP���������
1: // txor.cpp -- Test program for bitwise exclusive OR 2: 3: #include <stream.hpp> 4:
������
� ���������������� �C++������ ����� ������������ � ��
5: main() 6: { 7: unsigned int v1, v2, v3; 8: 9: cout << "Enter values to OR exclusively: (ex: 1234 15) ";10: cin >> v1 >> v2;11:12: v3 = v1 ^ v2;13:14: cout << form(" %5u %#06x %016b\n", v1, v1, v1);15: cout << form("XOR %5u %#06x %016b\n", v2, v2, v2);16: cout << "=====================================\n";17: cout << form(" %5u %#06x %016b\n", v3, v3, v3);18: }
����������������� ���������������OR�������������� �� ����� �������� ��TXOR������� �������� ��� ��� ��� ������� � �� ����� ������� ������� ������ v2��� v1� ������������������������ ������������������� ���������������������������������
� ������OR� ���������������������������������������
A ^ B == C
0 ^ 0 == 00 ^ 1 == 11 ^ 0 == 11 ^ 1 == 0
����������� �������������� ���������������������&���������� ��������� ������� ��
�������������������������� � �������� �� �������������������� � ������������
� ��� ������������������������TAND�������� ���������� �� ��������������������
� ����������������
;7��� �&���8 ����������������
456 ����7 �&���� ����������������
������������������������������������������������
����8 �&���8 ����������������
������� ������ ���������� ������� ������ ����� ��� ������ �� ��� ����� ���� ��� ��
������������������������������ ����������������������������������������������������
������������������������������������������� �������������� �����������������������
�������������� ������ ���������������������������������������������������AND� ��������������������������������������
� �� � ���������������� ���������������������������������
�
�
�
�
������������������ ���������� �����AND��������������������������������������
����������������������� ������ ��������������TAND�������� ����� �� �� ����� ��
�����������������������������������������������������������������������������������
� ���������������������������
������������������������� ��������������������������OR������������������������
���������������� �������������������������������������������������������������
������� �TAND�������������������������������TOR�������� ������� ����� ���
� ����
;7��� �&���8 ����������������
! ����7 �&���� ����������������
������������������������������������������������
;7��: �&���� ����������������
������������������������������������������������������������������������������
���� ����� ������ ������� �� � ������� ������� ������ ����� �������� �������� �� �����
��������������������TOR�������� ����� �� �����������������������������
��������������������������������������������������������������������� ����������
� ��������������������
� ��������������� ���������� ������ ��OR��������������������������������� �����
�������� ��� �� ������������ ������ �� �� ������������������XOR����������������
����������������������������������XOR�������������������� ������������������
�TXOR������������������XOR�������� ����������� �� ������������������������
����������������������������� ���������������������������������������������
� ������ ��������TOR���TAND����������������������������������������� �������
;7��� �&���8 ����������������
<! ����7 �&���� ����������������
������������������������������������������������
;;889 �&���= ����������������
��������������������������������������XOR���������������������� �������������
�������������������� ��������������������������������������������������������������
����������������������� � ������������������������������������������������������
��������� ����� ���� ������������������������TXOR������������������������
� ������������������
;;889 �&���= ����������������
<! ����7 �&���� ����������������
������������������������������������������������
;7��� �&���8 ����������������
� ���������������� �C++������ ����� ������������ � ��
������������ XOR��������������������������� ������� ����������������������������
� �������������������������������������������������������
������������������������������ ���������������������������������������������
���������������XOR������������������������� ������������ ����������������������
������������� ����������������������������������������������������������������������
�����������������������������������������������������������������������������XOR� ��������������������
�����������������������������XOR�����������������������XOR����������
��������������������������������������������� ���������������������������������
����������������������������������������������������������������� ��������������
������������������������ ����������������������������XOR���� ������������
� �����������������C++���������������������������������������������������������� ������������������������������������������������������������������������� �� �
� ���������� ������� ����������������������
������ ������������������������ �������������������������������� ���������������
��������������������������������������������� shl���shr����� � ����������������������������������������������������������������������������� ����������������
� �����������������������������������������������������������������������
����v1����������� ������(v1 << 2)� �������������C++���������������������������������� ��������� ���������������v1� ����������� ���� �� (v1 >> 1)��� ����������������������������������������������������������������������������������������
� ������������������������������������������������������
��������������I/O���������������������� �����$��������$�������������������
������ �� $��� ����� �� ��� ������� ����� ������� ������ �������� ��� �� ����� ��
�����������������������������������������������C++�����������������������
��� ������������� I/O� ��������������� ������������� ����� ����������������
� �������
��� ����������� ��� ����� ��� �TSHR.CPP��� TSHL.CPP� ������� ��� ������� ��� ���TOR.CPP� �TAND.CPP��� ����� ����� ���� ������� ����� ������� ����� �����
� ������������������TXOR.CPP��
� �� � ���������������� ���������������������������������
�
�
�
�
� ���������������������������� �����������������������������TSHL���������� �
��������� ����� ����������������������������������� ����������������TSHL�������
�������� ������ ���� ���� ������ ���� ���� ���� ����� ���� ������ ������ ����� ��� ���
�������������������������������������� ������������������������������������������
����������������������� ������ ����������������������������������������������
� ������������������������������������������������ �������������
������� �� ���������������������������������������������������������������� ������
����������������������� ������� � ���������������������� ��������������TSHR���
� �������������������������������������
��������������������������� �����������C++��������������������������������������������������� �� ���������� ������������� ����������������������������������� ���
������ �� ���NOT�������������������� ����� � ������ �� �������������������� ����
������������������������������������������count����������������~count��������count���������������������������������������������������������� ��������������������� �� ���� ���������������������� ��� ������������������������������� ������
��������� ������������������������������������������� ��� ���TCOMP.CPP��� �� ���
� TCOMP.CPP���������
1: // tcomp.cpp -- Test program for bitwise complement 2: 3: #include <stream.hpp> 4: 5: main() 6: { 7: unsigned int v1, v2; 8: 9: cout << "Enter value to complement: ";10: cin >> v1;11:12: v2 = ~v1;13:14: cout << form(" %5u %#06x %016b\n", v1, v1, v1);15: cout << "=====================================\n";16: cout << form("COMP %5u %#06x %016b\n", v2, v2, v2);17: }
� ���������������� �C++������ ����� ������������ � ��
� �����������������������������������������������������������������������������
� ���������������������count���������������������������� ������� �������������-�>�
���������������������������� count = count & 65534������������������������������� ���� C����C++��������������������������������������������������count & ~1������������� ���������������������������������������������������� ����� ���� ���
� ����������������� ����� �����������
� ������������ �������������������������� count = count & ~1� �� ��������� ����������� ���������� ����������� ������� ��� ����� ���� ��� �� ������ ����� ��������� ��� ������ �count� �� ����������� ����� ����� ��� ������� ������ ������ ���� ���� � �������� ��� ������ count�� combined�� ������ ����� ��� �� ��� ������������ ��� ������ C++� ������ �����
� ��assignment
����������������������� ���������������������������������������������������������
� ����������������count = count & ~1������������ �� ���������������������
�����-��>��
�������count�����������������������������������������������������������C++���������������� �������� ���������� ��������������������������� ���������� ���
� ������������������������������������������
�����?����� $$�������������?����
�����@��(� $$�������������@�(�
�����@������� $$�������������@������
�����A���=� $$�������������A��=�
������������������������� ������� ������������������������������������������ ��
������������������������������� ��������������������� ���� ����������� ����������
�������� ���� ������ ������ ���� ��� ����� ���� �������� ��������� ����� ���� �����
������ ���� ����� ���� ������ ����� ��� ������ ��� �� ����� ��������� ������� ����
����������������������������������������������������������������������������� ���
����������������������������������� ������������������������������������������
� ����������������� ������ ���������� ���� ������������
��������� ��� ������� ���������� C++�������������������������������������� �������� ������� �� �������� ���������� ����� � ���� ��������� �������� ���� ������ ����
������������������������������������� ������������� �������������������������
� ���������� ���������������������������������������� ��� ���� ������� �������������
� �� � ���������������� ���������������������������������
�
�
�
�
�������� ��� ����� ������ ��� ������� ���� ����� ������� ������ ������� ����� ��� ������
������ �������������������������������������������������� ����������������������
� ��� ����������������������������� �������������
� �������������������������������main���������������������������������C++������������������� ���������� ��������� � �������� �������� ������������������ ��� �����
������������������������������� ���������������������������������� ����������
�������� ����� ������������������program flow statements������������� �� ��������� �control structures��� ����������
������������� ������������������������ ��������������������������������
� ������������������������������� ��������� ������� �� ����� ���������������������
��������������������� ������������� ��������������������������������������������
����������������������� ���������������� ����� ��� ����������������� ������������
� ����� ������� ������ ������� ���� �������� �������� ������ ����� ������ ���� ��
��������������������� �����������������������������������������������������������
� �������������������� �
� ������������Exit����������������������� main����������������������������������������������������
� ���������������������� ��������������������DOS��������� �
� GAS.CPP��� ����� �������� ��� �exit� ���� �� ���������������������������� �� ���������������������������������DOS����������������������������������������������������� ����� ��� ������� ������ ������ �exit�������� ���� errorlevel� �� ��� �� � �
� STDLIB.H
��������������������������exit� ������� ����������������YESNO.CPP��� ����� ��� ����� ������DOS����������������������������������YESNO� �����������������
������ ��� ���� ���� ������ ��� ��� ��� �� ����� ��������� ������� ����� ������
���� ������������� ������������������YESNO.CPP����� �� ������������������������ ��������������������
� YESNO.CPP��������
1: // yesno.cpp -- Return ERRORLEVEL=0 (No), 1 (Yes) 2: 3: #include <stream.hpp> 4: #include <stdlib.h>
������
� ���������������� �C++������ ����� ������������ � ��
5: #include <ctype.h> 6: 7: main() 8: { 9: char answer;10:11: cout << "Type Y for yes, N for no: ";12: cin >> answer;13: answer = toupper(answer);14: if (answer == 'Y')15: exit(1);16: else17: exit(0);18: cout << "This statement never executes!\n";19: }��������������answer���toupper(answer)���������������YESNO.CPP������������������������ ����������� ����������������������������������������������char������ � ’Y’���answer���������������’y’�������answer��������������������������� ����������������������� �����������������������������if�����������������
� ���
��������������������� ��������������������������������������������������
��������������������������������� ������������������X������������������� �
������ ������������������������������������������������������if/else�������������������� exit� ���������������� ������ ������ ��������� ������� exit����� ����
� �������������������������
���������������� �����������C++�����macro�� � ��������toupper� ������������������� ������ ������ �� toupper��� ��������� ������ ������ ���� �� �������� ����� ��������������������������������������toupper��������� �� �������������������� �
� �����������YESNO.CPP������CTYPE.H
�������� ������������������ �CTYPE.H��������� ����������� �tolower� ���� � ������ ������ �������������������������tolower���toupper���������
��''������ � $$�����4�
��''����B� � $$�����B�
���1����C� � $$�������
��''����)� � $$�����)�
� �� � ���������������� ���������������������������������
�
�
�
�
��������������� ��������������������������C����C++��� toupper� ���� !��#� ������ ���
�������������C++���������������!��#�����������������������������$���������������
� �C++��������������������������� ����
�YESNO.EXE��� YESNO.CPP�������� ������� ���TESTYN.BAT��� ���� �
���������������������� � ��TESTYN����� ��������������������TESTYN.BAT��
����Y� �����Type Y for yes, N for no:����� ���������������� DOS���������������������������������������������� ��������� ���������������� <Enter>���������N
� �������errorlevel������ ��� �����������������������������������������
� TESTYN.BAT��������
1: echo off 2: rem 3: rem * Test YESNO.CPP 4: rem 5: echo Continue program? 6: yesno 7: if errorlevel == 1 goto YES 8: echo You answered no! 9: goto end10: YES11: echo You answered yes!12: END
��if errorlevel == 1������������������ ��(�)��������*������errorlevel����������
��DOS�����������������if (errorlevel >= 1)�C++��� ������������������������
� �C++���>=����������������������������
� �������������� �������������if���������������������YESNO.CPP��������������� ��������������������������������������������exit������� ������������� �������������������� �������� �exit(1)��� exit(0)� ���� �������� ������ ������ DOS��� ����� ����� ���������������������������������������������������������������������������������
� ����
� ���������������� �C++������ ����� ������������ � ��
��� ������� ��� ����� ��� ������ (toupper(answer) == ’Y’)� ������� ������� ����� ����������������������������������������������������������’Y’����toupper(answer)������������������������������� �exit��� �DOS��������� ���������������� ��������������exit(0)�������������’Y’�������answer������ ���� ��� ����� ���������������
� �exit(1)
� �exit������������������������������� ���������������������������������������
�&�����''������1�� �����D� �
���������������������������������������������������� �������� ������� ������������ �
�����������������������answer���������toupper������� ���������������������������������������������������������������� ’Y’�������������������������������� ��� �
� exit�������������������������������
��������������������������������C++��� ������������������ ��������������������������� ������ ���� ���� ���� ���� ��� ��� ����� ����� ���� ���� ���� ����� �����
� ������������������������������ �����������������������������������������
� ������������ �����������������������������������������������
�� ��exit������������������������������������������������YESNO2.CPP��� ���� ����� ���� �YESNO2.CPP� �� �� ��� ����� �������� YESNO� ������� ��� ����
���������������������� YESNO.EXE���YESNO2.EXE��� ��������YESNO.EXE� ������������������������������������������� ��TESTYN.BAT�������
� YESNO2.CPP���������
1: // yesno2.cpp -- Shortened version 2: 3: #include <stream.hpp> 4: #include <stdlib.h> 5: #include <ctype.h> 6: 7: main() 8: { 9: char answer;10:11: cout << "Type Y for yes, N for no: ";12: cin >> answer;13: exit((toupper(answer) == 'Y'));14: }
� �� � ���������������� ���������������������������������
�
�
�
�
� while�����������������������������������������������������������������������������������filter�� �������������� ������������������ � ����� ��� ����������������������������� �����
������ ������ ���� ����� ���� ���� ��� ����� ��� ���� ��� �� ��DOS��� ��� ������������������������������������������������� ��������������������� ����������
� �������������� �
����������� ���������� ���������������������������������������������������������
���������� ������ � ������������ ������������������� �������������� �� ���������
��� �������������������������������������������������� ���������������������� ���
�SORT� ��������� ���� ���� ����� �� ������ ������ ����� ���� ����� DOS��� ��������������������������������� ��������������� ���� ����������������DOS���FIND��
� ������������������� ���������������������������������
2���.����2��:/(�/8���.����
�������������������������������������������������� ������������������������
��MYNAMES.TXT��������������������� ��������� �������������������������
� ��������������������%E��%���&������2�&
��������� ����� ���� ������SORT���MYNAMES.TXT� �� ��������� ������ ��� ���
� SORTED.TXT���� ���������������������
� �������������������������������������C++��������������������������������������� �while���������������������������������������� �������� �������������� �� ������������������ �������������������������������while������� while
� �����������������������������while����������int�����������������count���������
1����������������
�����������??���������
������� ��� �� ���� ����� ��� ������ ����� ��� ����� ��� ��� ��� ������ ������ ��
�������������������������� ���������� main������������������������int count;������������ � ���������������������������������� �������count����������������while��������� ���������������� ��� ����������������� (count <= 10)������ ������ ���������������������������� �������������� ������������������������������
������������������������������� �����while�������������������������������������� ���������������������while�������������������������� ��������� �
1�������&'������� ��
�$$����%�������&�����1������&'����������������
� ���������������� �C++������ ����� ������������ � ��
������ � �� ��� ���������������������� ����� ����������������� (expression)����������������������������������������� ����������������������������while������������putchar���getchar�������������������C++���������� ��� ����������������
� �������������������������������������������STDIO.H��������������
������������while������������������������� ����������FILTER.CPP��� ������ �������������������������������������������� ��� ��������� ��� ��� ��������
��� ����������������DOS���������� ������������������� ������������������������������ �������������������������������������������������������XCOPY���COPY���� �������� ������ ������� ������ �� ������� ��� ��� ��� ���� FILTER.CPP� �
� DOS���������� ��������������
����� ����������������� ����FILTER� ����� ��������������������������� ���
�$������������� ��������������������DOS���������������������������������
����������������������������������&����&�����������������������������������
�$������������������������$�����DOS�������<Ctrl>-Z���������� ��������� ��������
� �DOS�������
� FILTER.CPP����������
1: // f ilter.cpp -- Filter shell (copies input to output) 2: 3: #include <stdio.h> 4: 5: main() 6: { 7: char c; 8: 9: while ((c = getchar()) != EOF)10: putchar(c);11: }
�putchar(c)����������while������� FILTER.CPP������������������������������������� ������ � �� �������� ((c = getchar()) != EOF)� ������� ��� ��������������������������������������������������������������������������������������������
� �c = getchar()���������
�������������������������� ��� ������������ ������������ ��getchar��������������getchar()������������������������ ������� �������������� �c����char����������� � ������������������������������������������ ��������������������������
(c =� ������� ��������� ���� C++��� ������� ����� ����� �� ����� �������� �� ��� �����
� �� � ���������������� ���������������������������������
�
�
�
�
������(c = getchar())���������������������� c��������������������������getchar())�������FILTER��� ���� ������������������������������������� ������c�����������������������������������EOF����������������������������������c�������������������������������������getchar����������������EOF� STDIO.H�����������������
� ��� �������������
������� ��� �������� � �������������� �BASIC���Pascal� ���������������� ���
�����������$������ ���������������Basic���a = b���Pascal���a := b� ��������������
������������������������������������������������(a = b)��� ����!C���#�C++��
� �b�������������������������������������������������� ���������a���b
�������� �������������������� ����������������������FILTER���������while���������������putchar(c)��������� ���������while�������������������������������
� ��� ������������EOF������ ������
filter <�� �������� �����DOS������������������������ ���FILTER���������������
���������� ���FILTER.CPP� �� ���������������� ���� ���� ������ ���� filter.cpp���� ���� ������ FILTER.CPP� ��� ������� � �� ����� ���� �� �� �� ���� ������ ������������� ���� �� filter < filter.cpp > temp.txt�� �������� �����TEMP.TXT
� filter < filter.cpp > prn
������������������������������������� ��������������$��������������������
�$���������� ����������� $������������������������������ ���� �������������
� �C++���������������������� ����$����������������
� if/else����������������������������������������� if/else�������������������������������� ����� �������� ����LINENUM.CPP��� ������ ����������������������������������while��������
� ����������������������������� �����
� LINENUM.CPP����������
1: // linenum.cpp -- Counts number of lines in a text file 2: 3: #include <stream.hpp> 4: #include <stdio.h> 5: 6: #define NEWLINE '\n' 7:
������
� ���������������� �C++������ ����� ������������ � ��
8: main() 9: {10: char c;11: int linenum = 1;12:13: cout << dec(linenum, 4) << ": ";14: while ((c = getchar()) != EOF) {15: if (c == NEWLINE) {16: linenum++;17: cout << NEWLINE << dec(linenum, 4) << ": ";18: } else19: putchar(c);20: }21: }�� �� linenum < linenum.cpp� ���� � ���� ���� �LINENUM.CPP� �� �� ���� ����������� ������� ��� �� ����� ���� ���� ����� LINENUM.CPP��� ���� ���� �� ����linenum < linenum.cpp > temp.txt�� ��������������� ������������� ��������������TEMP.TXT���������������������LINENUM.CPP���������������������� �
� ��������������������������DOS���TYPE�� ��������������������������������
������� ��������������������������������������������������������LINENUM����������������������������� FILTER������������������������������������while���������������������������������������������if/else��������� ��������������������� ���if�������������� ������NEWLINE���������������� c���������getchar�� ���������c���� ������������������������ �� ���������� ������ �linenum� �� ����
����������������������� ��putchar�������else�������NEWLINE�����������
� � ���������������������������� �������� ��� ���
������������� ������������������������LINENUM������������������������������
��������������������������������������������������������������$�������
������� $� ���� �NEWLINE��� �������� ������ ������ ���� ��� ������ ���� �����
����������������C++����������������������������������������$�����$����� ���� �
� �������������� ������LINENUM����$���
��������������� ����� �while��������������WORD.CPP��� ������ ������������ ����C++����� ����������������� ������������ ���������������������������������
� ����� �������
� �� � ���������������� ���������������������������������
�
�
�
�
� WORDS.CPP����������
1: // words.cpp -- Count words in standard input 2: 3: #include <stream.hpp> 4: #include <ctype.h> 5: 6: main() 7: { 8: char c; 9: int words, chars, lines, insideWord;10:11: words = chars = lines = insideWord = 0;12:13: while ((c = getchar()) != EOF) {14: chars++;15: if (c == '\n') {16: lines++;17: chars++;18: }19: if (insideWord)20: insideWord = !isspace(c);21: else {22: insideWord = !isspace(c);23: if (insideWord)24: words++;25: }26: }27:28: cout << chars << " total character(s)\n";29: cout << words << " word(s)\n";30: cout << lines << " line(s)\n";31: }
words <� ���� DOS��� � ���� ���� ������� ��� � �� �WORDS��� ����� ��� ��� WORDS.CPP������������������������������������������� ������ ���� words.cpp������������ ������ � ��WORDS.CPP�����������ASCII��������������������
� ���������
� ���������������� �C++������ ����� ������������ � ���
���������������� ������������� ��������������;����������������WORDS.CPP�������������� ��������������������������������������white space������� �� ������ ���������isspace�������������� � ������������������������������������ ��������� ���islower� �isdigit� �isalpha� ���� ���� � ������ �������� CTYPE.H��� ����� �����
� �������������isupper������� ���������������������������������������� ��WORDS.CPP���������
� �������������c����b��a�int����������������������� �����������0�����������
� ������������ ��������������������������0���������
�������������������������������������� �����������������������������������������
� �������������������������
� ����� ������� ������ ���� ����� ���� ����� ������� ����� WORDS.CPP������������������������ ����������� �flag�������� ��int��������insideWord������
� ������������������������������������� ���� ����������������������������������
�insideWord��������������������while����������������������������������������������� � insideWord��� !isspace(c)� ������������������ ������� ������� ���������������������������������������������������������������insideWord�����������������������������������insideWord���� �NOT�������������������������������������������������� ������������� ���������������insideWord����������
� words����������������������������������if����������
�� ��������(�)�������������������WORDS.CPP�����������������������������
� �((����(%������������� ������
� switch��������������� ���������������������������������������������������������������
������������������������� ���������������������������������������� �� if/else� ������������������������������ c����char�����������������’C’���’B’��’A’
����������4� $$����%��������4��
���������������F� $$����%��������F��
����������������� $$����%�����������
����$$����%��������������������������
� ��� � ���������������� ���������������������������������
�
�
�
�
�� ����������������������������������������� �������������������� �������������� �����
������C++��������������������������if/else������������ �������� �������������������������� �������� ������������������������ ���� �������� �������������switch������������������������������������������������� ���’C’���’B’��’A’����cases���� ����if/else������switch��������������� ����������������������������c����selector�� ���
� �� ���
�1����� ��
������4�"
$$����%���� ������4��
0���G�
������F�"
$$����%���� ������F��
0���G�
��������"
$$����%���� ���������
0���G�
2�����"
$$����%���� ������������������������
�
������������ ���������������������� �����������������switch�������������������������������� ��� ��������� ��� ����� ����� �����������������������case��� ������� ��� �������� ������� ������� ���� ���������������������� ����������� ���������������������
�������������������case���������������������c�������������������switch��������������� ��������������������case������������������ ���� ������������������������������� ������������ ������������������������ ������� ���� ������������������case���������������������break������� case���������������������������������break��� ������������ switch������������ ��������������case�����������������������default:�case��������������������� �switch������ ���������������case��� �����
� ����������������
������������������������������������������������C++��������������������������� ��������������������������������� switch�������cases�����
�1������ ��
������4�"��
���%���
���%���
0���G�
�
������F�"��������
� ���������������� �C++������ ����� ������������ � ���
���%���
0���G�
�
2�����"��
���%���
�
�
� �������� ������������������� ��������������������������������������������
�1������ ��������4�"
���%������%���0���G�
������F�"���%���0���G�
2�����"���%���
�
������ case����������������������������break�������������������������� ����������� C++����C����������� �����������������switch�����������������C�������
������ ����� break� ��� ���� ��� ����� �� ��� ���� ��� ������ break��� ������ ��� ���������������������������������case�����������case���������
�1������ ��
������4�"��
���%����
���%��(� $$� ������F�"��
���%��)�
0���G�
2�����"��
���%��;�
�
�case ’A’���������������� ��������������statement2���statement1��’A’�������c������ �� ’A’�����c�����������������statement3��statement2������break��������������switch������ ����������� ����� �������� ������ ������������� ������������������������������������� ���case��������������������������breaks������ ������������������� ����� ������� ���� ������� �������������������������cases���� ������������������������������������������������������������������������������������
� break���case����
� ��� � ���������������� ���������������������������������
�
�
�
�
� ������������������ ����������������������� ���case���������������� ��1����� ��
������4�"
������F�"
��������"
���%����
���%��(�
���%��)�
0���G�
������6�"
���%��;�
�
���������������������� �� ’C’�����’B’��’A’�������c������ ���������������������� ��� ����������if������������������������������������case������������
����������4� �..�������F� �..��������� ��
���%����
���%��(�
���%��)�
�����������������6�
���%��;�
� ������������������������������������������� �����������
������ break� ������ ���� ���� ������� ���� ��� switch� ����� default:� ������ ������������������switch������cases��������������������������������������default:�����cases������� ������������ ���������������������������������������������
� default:�������������������������������
� switch����������������������������������������������������������������������������switch�������������������� � �������������������������������������������������������
�������������������� �� �������������������������������������������switch����� ������������������� �����switch����� ������������� ����������������� ����
� �����������������������������
��������������������������������MENU.CPP��� ������ ����������� ������� ���� ������������������������������� �����Quit��� ��������S(ort��������D(elete��������A(dd������������������� ���������� ����������������������������������������� ���
��������������������MENU����������������� ������ ����� ��������� ��� ������
� �����������������������������
� ���������������� �C++������ ����� ������������ � ���
� MENU.CPP����������
1: // menu.cpp -- Using switch to create a simple program menu 2: 3: #include <stream.hpp> 4: #include <stdlib.h> 5: #include <ctype.h> 6: 7: #define INUSE 1 8: 9: main()10: {11: char choice;12:13: while (INUSE) {14: cout << "\nMenu: A(dd D(elete S(ort Q(uit: ";15: cin >> choice;16: switch (toupper(choice)) {17: case 'A':18: cout << "You selected Add\n";19: break;20: case 'D':21: cout << "You selected Delete\n";22: break;23: case 'S':24: cout << "You selected Sort\n";25: break;26: case 'Q':27: exit(0);28: default:29: cout << "\nIllegal choice. Try again!\n";30: }31: }32: }
���������������� INUSE�������������������������#define��������������������while����������������������� ����� �����������������while (INUSE)������������������case�������� ��������������� ��������������������� �������������������
� Q(uit�� ����������������������������������������exit(0)���������’Q’
� ��� � ���������������� ���������������������������������
�
�
�
�
���������� ������������� ��������������switch��������cases�������������� ��������������������break� ���������� ����������������������� �����������������������������������������MENU.CPP�����������������break���������� �� case������������ ������ � ��D� ����� ���� ����� ����� ��������� ����� � �� ����� ������ ���
������break��� ����������������S(ort����D(elete��������������� ����������D(elete� ’S’������case������������’D’������case�������
� do/while���������������������� �������������� ������������������������������while����������
� ���������������� ���������������������� �� do/while������������
2���
��������������������
����??�
��1���������������� �
�������������������������� ����������������������while������������������� ��������������do/while������������ �������������������������������������������������������� while���do������������������������ �����while����������do������������������� ������������������������������������ ������� ������������������������� ����
���������������� ����������� ����� �� ����� ������������������������������������
� ��������� ��
2���
��������������������
1������??���������� �
������������������������� �do/while����������������������������������� ������ ������������������������� ��������������������
��������������while����������������do/while��������while������������������� ������������������ ����������� �do/while������� ���������� ���������� ������������������������ ���������������������������������������������������������������
����� �����while������� ���� �������count���� ����� �� ���������������������� � �������� ���������
1��������������� ��
��������H������*����&���������
����??�
�
� ���������������� �C++������ ����� ������������ � ���
�count�������� ��������������������������������������do/while��������������� ���� ����������������������������
2���
��������H�����&�����������%�����
����??�
��1���������������� �
������������������������������������������������������������count���������������������������������� ��(count <= 10)���������������������������������������
� ����������
��������� ��������� �do/while�����������������������while�������� ������ �������� ��� �������������� �������� ����� ��������� ������ ������� ����� �� � ���
�����while���������������������������� ����������������������������������������������� ����������������� �������������� ��������������������� ��������������
� do/while����������������������������������
�������������������������������� �������do/while����������HEAD.CPP��� ������ ������������������� ������������������������ � ��� ����������������������������
������ ��� �DOS���TYPE�� ������������ ��������������������������������������
� ��������
� HEAD.CPP����������
1: // head.cpp -- Write first 10 lines of input to output 2: 3: #include <stream.hpp> 4: #include <stdlib.h> 5: 6: #define MAXLINE 10 7: #define NEWLINE '\n' 8: 9: main()10: {11: char c;12: int linenum = 1;13:14: do {15: if ((c = getchar()) == EOF) exit(0);
������
� ��� � ���������������� ���������������������������������
�
�
�
�
16: if (c == NEWLINE) {17: linenum++;18: cout << NEWLINE;19: } else20: putchar(c);21: } while (linenum <= MAXLINE);22: }
�������������������head < head.cpp�����DOS�� ���������HEAD�������������
� ������������������������������������������ �� ��������������������������
���������� ������������������������������������ ������������������������
� �����HEAD�
� MAXLINE���������������linenum�� ��������������������������������������
����� �����������������getchar�� ����������������������������������������if����������������� ��������if������� �������������������������������EOF��������������������������linenum�������������� ���������������������������NEWLINE
� ��� ������������������� ������������������������������������ � �����
��� ��� ������ � �� ��� do/while��� while� �������� ����� ��� ����� ������ ���� ���� ���������������������������������������� �� while�����������HEAD.CPP
� ��������� ��������� �����������������������������������
� for������� �C++ ���������������������������������������������������������������������������������� ����������
�for���������� ���������������������������������������� ������for������������� ��������������������
� for������������ ���
� �������������������������������� ���
� ������������������� ���
� ����������������������������������������������������for��������������%�����&'�����������&'�������( ��
$$����%���� ����&����
�
� ������������������������������������������for��������������������������������������������������� �� ����������������������������������statement������������expression1������������������ ����������������� �����������������������������������������expression2����� ��������������� ���for������������������
� �����������������������������������
� ���������������� �C++������ ����� ������������ � ���
��� ���������������while������������������������������������������� ����������� �����
���%���
1�������&'�������� ��
$$����%���� ����&����
�&'�������(�
�
������������������������������������������expression2����expression1��statement���������������� ��������������������� ��for������������� �� ��������������������
� ����������������count����int�������� ���
�������������������������������??
��������������������
� while�����������������������������������
1���������������� ��
��������������������
����??�
�
������������ �������������������� ��for���������������������������������������������� ���������while��������������������������������������������� ����for���������� ������ ���� �������� ���� ��������� ����� �� ���� ������ ������� for��� ������������������������������ ���������� ��for������������ � ����������������������while
� �����������������������������
������������������ASCII.CPP��� ������ �� �������for������������������� �������� ���� �����������������ASCII
� ASCII.CPP����������
1: // ascii.cpp -- Display ASCII chart 2: 3: #include <stream.hpp> 4: 5: main() 6: { 7: unsigned char c; 8: 9: cout << '\n';
������
� ��� � ���������������� ���������������������������������
�
�
�
�
10: for (c = 32; (c < 128); c++) {11: if ((c % 32) == 0) cout << '\n';12: cout << c;13: }14: cout << '\n';15: }
�unsigned char�����������������������������������ASCII.CPP���for�������������������������ASCII��������������������chars���������� ����������������c���� ����� ������������� �������������unsigned chars� �� � ���������������������������� ������� ���� ���� ������ ������������ ���for������� �������c������������������� ���������������������� ����� ���������� �for���������������������������������������������������� c++��������������������������������������for����������������
� for����������control variable��� ������������
������� ���� �������������������������char����int������������������������������ ����������� ��������������� ������������������ ������������������������������ �������
� ����
�����
����������������������??
�������2����I�9 �
� �for���������������������������������� ���������������
�������������������������??
�������2����I�9 �
�C++����� ��������������������������������������������������� ��i����������������������������������������������������i���������������������������������������� �������������� �������� ������� ������� ;���������������� i� ���������������� for�������� ������ �� � ��� ������ ����� ���� ���� ����� ����������� ����� ��������������� �
��������������������������������������������������������� �����������������
� ������������������
� for������������������������������������� � ����������������� for���������������������� ���� ������������������� ������� � ���� ��� �i� �int������������� � �� ����� ������������ ��� ������������
� �����������������������������int���������������� ����������J�
����������I�J���8���������8 �--�������J ���??I�J//
������������������������J������J�
� ���������������� �C++������ ����� ������������ � ���
�while��������������������������������������������������for�������������������� ���������
����I�J�
������
J���8�
1������������8 �--�������J ��
�������������������������J������J�
�??�
J//�
�
� while�������� ��������������������� ����� ���������������for��������������� ��������� ���������� �������������� ��� ��������� �for��������������������������� ������� � ������� ����������������������������� ;������ ��� � ������� ��������������������for�������� ������������������������������������ ���� ���������������������������� ���������� ������ ������������������������������ ������������
� ����������������������������������
� ����������������������� ������� ��������� ����c++� ���� ���������������������� for����������� ���������� ����(c < 128)����������� ���������� �������������� �������������������������� ��������������������� ������ ������������� �������������� ��� ������
� ���������for������������������ �� ���������������for�����������������
������� �� $$�K��'������*���
�������� ��������������� ������������������ ���������� ��������������������
����� ������ ������� �������� ����� ����� ������ ���� � ��������� ����� ������ ���
���� ������������� ������������� ������������������������� ������������� ���
����� ������ ����� ������� ������ ���� ����� ���� �� �null statement�� �� � ����� ���� ����� ������������������ �������������������� �������������������������� ���
� � ����������
����������������������do-forever���������������������� ���������������������������� ��������������������������� � �� �������� ���� ���� ������������ ������ ���
�������������� �������������������������������������������������������������
����� ���� ���� ��� ���� ������ ������������� �������� � �������������������������
�������� ��������������������������������������������� ����������������������
� ������������������������
� ��� � ���������������� ���������������������������������
�
�
�
�
�!��������!��break������������ �������������for�����do/while��while����������������������������
� ��� ���BREAKER.CPP��� ������ �������break����������������
� BREAKER.CPP����������
1: // breaker.cpp -- Demonstrate break statement 2: 3: #include <stream.hpp> 4: 5: main() 6: { 7: int count; 8: 9: cout << "\n\nfor loop:\n";10: for (count = 1; count <= 10; count++) {11: if (count > 5) break;12: cout << count << '\n';13: }14:15: cout << "\n\nwhile loop:\n";16: count = 1;17: while (count <= 10) {18: if (count > 5) break;19: cout << count << '\n';20: count++;21: }22:23: cout << "\n\ndo/while loop:\n";24: count = 1;25: do {26: if (count > 5) break;27: cout << count << '\n';28: count++;29: } while (count <= 10);30:31: }
� ���������������� �C++������ ����� ������������ � ���
������������������������while�����������������������for�������������BREAKER� count� �int������������������ ���� �������� ������ ���������������do/while��� ������ ����� ����� count�� ���� ������� ��� ������break� ���� ������ ���� ��������� ���� �� �� ��� ����� ����������� ���� ��� ��� ���� ������ ������� ��� ������
� �������
������������������� ���������������for��������������������������������break����� ���� �������������������������������������������
������� ��
����������L���"�4�22�6������C���"���
������������
�����������''��������� �
���������������4�
��������D���������2�422����
��������������������6�
��������D���������2�6��������
��������������������C�
0���G�
�
�for� ����������� ���� ��� �MENU.CPP� �� ��� ��� ���while� ������� ��� � ��� ����char�������������������������������������� ������������������������ ��������������choice���� ’D’���’A’�����������������������if/else���������� ���choice��������� ������� ������ ����� ����� ���������� ������� ��� ������break� ���� �’Q’��
� for�������������������������������������
� �������������continue����������������������continue���� ������������������ ����������������break�� ��������� ���������� ���CONTINUE.CPP��� ������ �� ��������������������������
� ����������
� CONTINUE.CPP���������
1: // continue.cpp -- Demonstrate continue 2: 3: #include <stream.hpp> 4: 5: main() 6: { 7: int count;
������
� ��� � ���������������� ���������������������������������
�
�
�
�
8: 9: cout << "\nStarting for loop with continue...\n";10: for (count = 1; count <= 10; count++) {11: if (count > 5) continue;12: cout << count << '\n';13: }14: cout << "After for loop, count = " << count;15:16: cout << "\n\nStarting for loop with break...\n";17: for (count = 1; count <= 10; count++) {18: if (count > 5) break;19: cout << count << '\n';20: }21: cout << "After for loop, count = " << count;22: }
����� �������� ����������������������������for�����������������CONTINUE�for��� ������ ����� ��� ���� ���� ������ ������ ����� � ��� ��� �� ��� �� �� ��� � ���������������for�������������� ������� ��count����continue����������������
� ������������������ �������� break
������������ ����������� ������������for���������������������������������������������������� ������������������������������ �������count���������������������break����� �����������count++� �������������������������continue����
� � ��������������������������������������������
��������������� ������������������������ ��������������for��������continue����� ������������������
��������%�����&'�����������&'�������( ��
�����&'�������) ���������
$$����%���� ����&����
�
�������� ������� expression2�� ���� ����� continue� ���� ����� ���� expression3� ��� ������� � ������������������ ����������������������������� �������������� �����
�������������������������������������do/while����while������continue����� ������� � ���������
� ���������������� �C++������ ����� ������������ � ���
� goto ������������� ����������������������������� goto���������������������������������������������������� ����������������� ���������������������� ��������������
��goto������������� ������ ���������� ��������������� ������������ �goto� ���������� ��� � �� ������� ��� ������ ������� ��� ������� ���� ���� �� �� �� �� ����
� �������������������������������gotos������������
��������������������������� �����������������������������goto������������������ ��� ����� ��� goto� ���� ���� ����� �� ���� �������� ������ goto� �� ������� �������������� ������������������� ������������������ ���������������������������
�gotos��������� ������� �� ������������������������������ ��goto���������� ������������������� �����������������
������������
H!M"
��������������������
����??�
�������������� �#���H!M�
���������������;������������������������������goto���������������������������������������������������������������� ������������while���for�������� ��������������� ������������������������������������������������goto�������� �������������
� ������� ����������������������������������������������������
����������������� ������ if� ��������� ������� ����������� �����goto� ��������� �������ifs����������������������������������������������������
������� ���������� ��
$$�*����������%�����������������'�1��������� ��
$$�%�������%����������1��������'�����2�������H N�
#���O43H�<,H��
��O43H�<,H"$$����%�������&����������1��#����#��
������������� ���� ��������for�������������������if�������������������goto��������������������� ����������ifs�������������������� ����������������break����
� ������������if���������������break���goto��
� ��� � ���������������� ���������������������������������
�
�
�
�
����� ����case�������������� ���case���������switch��������������goto���������� ��������������������� ��������������������������goto���������������������
� ��������������������������� ��������������������������
�1������ ���43�4"������4�"
$$����%���� ������4��#����43���
������F�"$$����%���� ������F��#����43�4�
�43��"��������"
$$����%���� ���������0���G�
�
�������� � ��������������� �������� ������case��������������� �’A’�������c����� �����������������������case ’B’��������������’B’�������c���� case ’C’����� case�����������������’C’�������c���� ���� ����case ’C’���case ’A’������������
� �������’C’
case���case ’A’:������case����������������CASEC:���CASEA:������������������������������������������������ switch������������������������������������ ’B’:�����gotos��� L99:� ��� �AQ120x:� �BANANA:� ��������� �������������� ����� ���
� switch��������������������������������������������������������������
������ ����� ���� ������������� ������ ������ ������ ������������������������� ���
�������������������� �switch� � �����������������gotos����������������������
� ������������������ ��������������� �� �������������
� RAM ����������������� �������������������������������������� ����������������� ����������� �������� ����� ������ ���
����������������������������������������������������������������������������C++������ �� �������� ����� �������� ��������� �������� ��� �� ������ ����� ���� ����� ��� ���
� ���������������������������� ������ ��������� ������������� ����������
�� �������������������� �������������������C++��������������������������������������������������� ����������������������������������������������������
���� ������� ������ ��� ��� ����� ���� ����� ������� ������ �������� ������� ����������
��� ��� ������ ������� ������ ��� ������ ���� ���� ��������� ����� ��� �� �������
� ��������������������������������������� ��������
� ���������������� �C++������ ����� ������������ � ���
� ������������������������������� ������ ��������������������� ������ ���������������������
��������������������������� �������������������� �������������������� ���struct� ���������������
���� ��� ��� �������� ����� ����������� ���� struct� ������������������ ���� �������������� ���������������������� �� �� �����������������������������
��������� �� �����������������������������������������������������������������
� �������������� �������
�������%��P�Q���L�����
�����1��#������;(�7�
�����'�����P�Q����9�)/777/�(�(��
�������%�(P�Q����L����
�����1��#�(����=7���
�����'����(P�Q����(�8/777/�(�(��
�������%�)P�Q����F�00���
�����1��#�)����)7���
�����'����)P�Q����������
�������� ������ �������� ������ ������ ���� ������ ������ ������ ����� ��� ��� � ���
��������� ���������������������������������������������������������� �������
� ���������������������������� ����������
�������������������������������������� ��������������������������������������
����������������� ��������������������������������������������������������� ���
� �struct���������������������������������������������������� �� ����
�����%�%0����
�������%�P)�Q�
�����1��#��
�����'����P�(Q�
��
���� ����� ������������� ������ �member���� �������������������������������� ���������������������phone�����float���������weight�����������������������name��������������������������������������������������������� �������� ����������
������ ������� ������������������struct���������������� �����������������������������������������;� ������������������������������struct���������������� ���������� ��� ������� ���� ��� ��� ������ ����� �� �� ����� �� ������ ���� � ���� ������
� �����������������struct���������������������
� ��� � ���������������� ���������������������������������
�
�
�
�
������������������������������������������������struct���������������������� ��������������������������
%�%0������L�%0���
���� �������� member� �����������oneMember���� ����������� ������������� ������� ������������� ���������� �struct������������������������� ���������
� ���������� ���� �������������������������������� ����������
�������%'��3�����������O���������,��������3���#P9Q���������������#��K��#�
��
�����������struct������������������ ������� ����� ��������������sampleStruct� � ������������������������ ���
��%'��3������%'��R���
�������� ������ ������ ��� �� �� ����� ��� ���� ������ ����� ��� ���� ��� ���
������� ��������� ������� ����������������sampleVar���������������sampleStruct� �� �������������� �������� ������������������ ������
�
� �$�������������������������struct��������������������������"#�����
�������������������������������� �����struct����������� ����������� ���
������������ ��������� �����������������������C++��� $��������� ��� ������ ���������������!(� �������padding�����������������#����������������������������� ��� $�� ������� ���� ��� ������� �������� ��� ��������������� ��� $���� ����
������������������������������������������������� ��� +������ ���������80x86�������� ���������������������������sizeof��� ������������������������������
� �structs����������������
� � �������� ��� � � sampleStuct�������� ��� � ����� ���
� ���������������� �C++������ ����� ������������ � ���
�������������m1�������������member�����struct���������������������������������� � �������
�����%�%0����
���
��%��
������������ ������������� ;member�����������������m1� ������ ��������������������������� ����� �� ���� ���������������������������������� ������ ���
� ��������������struct���������������������� ���struct�����������
�����
�������
�����%�%0���
����� ������� �� �� ��� ��� ������ ����� ���� onemember� ���� �� �� ����� ������� float���int��������� ���������������������
����������� ����struct����������������������������������������������������������������� ������member����������������� ����� ������������������ ���������� ���
� ����������������struct����� ����� ��������������������m1�����
%�%0���%�� $$�6������*����0���%�����E'��%�%0��
%����%�����H�%�� $$�4���#������#�����%�
%��1��#�����77� $$�4���#��*�������1��#�
%��'��������9�(/777/�(�(�� $$�4���#������#���'����
������� ������ ��� �� �� ���� ��� ������� member� ������� ���� ���� m1� ����������� � � ������� ������������������������������ ������� �member��� ����������� �������������������� ���� �dot notation���������������������� � fully�� ��� ������ �� ���� ���� m1.phone��� �m1.weight� �m1.name� ��������
�������������������� ���������� ��������� ��������������� ����������qualified����� ��� �� ������ ����� ��� ����� �� ������� ����� ���� m1.weight� ������� ����������m1�� ������m1 = ”Tom”���������������� int count���� �����count�����������������������������������m1.name������������� ��������������� member�������
� �����������������
���������struct���������� ������������������������������������������������������������������������������������������ � ��������� ���������������� ������
� ��������� ��
%�%0���%�������4����I����I��=:=/777/�(�(����
� ��� � ���������������� ���������������������������������
�
�
�
�
���������� ������������ ����������������� ���������� ����� ����������������� ���
�� ������� �� ���� ������������������������������������������������ ������ ������
������ ���� ������������� ���� phone� � ��� �member���weight��� name� ��� ������ �����
%�%0���%(������S��E�I���(���
����������������������������� ������������ ����struct��������������������������� �����������������������������������������������������������������������������
���������������� ������������������������������������������������������ ��������
�struct������� ��� ���������������� ������������������ ���������������� ����������������������COMP1.CPP��� ������COMPDB.H��� ������� ��� computer������ �� ����������������� ������� ������� �� ������������������� ��������
� �ztc comp1������������
� COMPDB.H���������
1: // compdb.h -- Computer data base header file 2: 3: #define NEWLINE '\n' 4: 5: enum Boolean {FALSE, TRUE}; 6: 7: struct computer { 8: char *model; // Computer's model name 9: char *cpu; // Type of CPU10: char *display; // Type of installed display11: float speed; // Speed in mHZ12: int numflop; // Number of floppy drives13: int hdcapacity; // Hard drive capacity in megabytes14: float memcapacity; // Memory capacity in megabytes15: enum Boolean modem; // Whether a modem is installed16: };
� COMP1.CPP����������
1: // comp1.cpp -- Computer database demonstration #1 using struct 2: 3: #include <stream.hpp> 4: #include "compdb.h" 5:
������
� ���������������� �C++������ ����� ������������ � ���
6: main() 7: { 8: computer mysystem; 9:10: mysystem.model = "ALR 386/2";11: mysystem.cpu = "80386";12: mysystem.display = "MDA";13: mysystem.speed = 16.0;14: mysystem.numflop = 1;15: mysystem.hdcapacity = 40;16: mysystem.memcapacity = 2.0;17: mysystem.modem = FALSE;18:19: cout << "\nMy old system:\n\n";20: cout << "Model " << (mysystem.model) << NEWLINE;21: cout << "CPU " << (mysystem.cpu) << NEWLINE;22: cout << "Display " << (mysystem.display) << NEWLINE;23: cout << "Speed Mhz " << (mysystem.speed) << NEWLINE;24: cout << "No. floppies " << (mysystem.numflop) << NEWLINE;25: cout << "HD Capacity" << (mysystem.hdcapacity)<< " mb" << NEWLINE;
26: cout << "Memory " << (mysystem.memcapacity) << " mb" << NEWLINE;
27: cout << "Modem ";28:29: if (mysystem.modem)30: cout << "True\n";31: else32: cout << "False\n";33: }
� ��������������������������� ���������������������������������������� ��� �
���� ������ ��� struct��� ����� �������� ����� ��� ������� ������ ����� ���� � �� ��� ��������� ������������������������������������ ������������������������� ����
� ����� ��������������������#include�����������������������������������computer������� �������������������� ������������� �������������� � ��������������
� ������������ ������������computer����� �������������������
�������� $� ���� ������� ������� char *model� ����� ������ COMPDB.H� ���
������ ���� ���� ��� ��� ����� ���� ���� ���� �'� ����� $���� ����� ��pointers�� �������
����� � ������ $������ ������ ���� �model� ���� ���� ������ �������� ������� ��� �����
� ���������������������������
� ��� � ���������������� ���������������������������������
�
�
�
�
����������������COMP1.CPP���������� include ”compdb.h”��������������� ������� � �� ���� ���� ��� ��� ��������� ��������� ���� ������� ������� ������
���������������������������������������� �������������������� STREAM.CPP����������������� �������� ������� ����������� ������������������ �������� ���
������ ���������� ����������������������������� ��� �����������������������
� ��������������������� ������������������������
�������� �������� .HPP� ��� .H����������������� �������������������������� ���
����� ������ ������������������ �������� �.CPP��� ��������������������� ��
������ ������� ������� ��� �� ��� ������ ������� ������ ���� ������ ������ ���
� �!��������������#�INCLUDE
���������������� computer��������mysystem������������ ���COMP1�������������� �� ����� ��� ���� computer� struct��� ��� ������� ��� ������� ���� ���� ������������������� � ������������������mysystem���� �����������������������
��� �� ��� �������� � �� � ��� ���������� ���� ��� ������ ������� ��� ������� ��� �� �
� ������� ���
����� ���� ���� � ��� ����� ����� COMP1��� ������� ���� ����� � ���� ������ ���������� �� ������� ���� ���� ����� �������� � ����� ����� �� �������� �������
����� ���� struct��� ����� ������� � ���� ������� ����������������� ������������ �������� ��� ����� ���� �������� ������ �� �� �� ��� �������� ����� ����� ���
������������������������������� ��� ������� ��������������� ����������������
� ��������
��� ���� ��� ��������� �!������� ������ ���� ��#� ����� ������� ������� �������� ���
��������� ���� ��� ������ ��� ���� ��� ���� �� ��� ���� ����� ��� C++��� ������ �$������
�������� �������� ����� ����� ��� �������� ����� ���� ���� ������ ��� �������� ������
� �struct������������������������
������� �������������������COMP1.CPP������ ��COMP2.CPP��� ������ ������������������������������������������� mysystem������������� ����� �������
���� ������� �� ���� ����� COMP1.CPP��� ���� ���� ��� ��� ��� �����
�computer������������������������������������������ ����� �������COMPDB.H������������������ ���������� ������������������������������������� ��������
� struct����������
� ���������������� �C++������ ����� ������������ � ���
� COMP2.CPP����������
1: // comp2.cpp -- Computer data base demonstration #2 using struct 2: 3: #include <stream.hpp> 4: #include "compdb.h" 5: 6: main() 7: { 8: computer mysystem = { 9: "Everex 386/25", // model10: "80386", // cpu11: "VGA", // display12: 25.0, // speed13: 2, // numflop14: 150, // hdcapacity15: 8.0, // memcapacity16: TRUE // modem17: };18:19: cout << "\nMy new system:\n\n";20: cout << "Model " << (mysystem.model) << NEWLINE;21: cout << "CPU " << (mysystem.cpu) << NEWLINE;22: cout << "Display " << (mysystem.display) << NEWLINE;23: cout << "Speed Mhz " << (mysystem.speed) << NEWLINE;24: cout << "No. floppies " << (mysystem.numflop) << NEWLINE;25: cout << "HD Capacity" << (mysystem.hdcapacity) << " mb" << NEWLINE;
26: cout << "Memory " << (mysystem.memcapacity) << " mb" << NEWLINE;
27: cout << "Modem ";28:29: if (mysystem.modem)30: cout << "True\n";31: else32: cout << "False\n";33: }
� ������������������������������������������������������� � ������� ���������������� ��� struct������������� ��������������������������������� ����� � ��� ����� ������� ������
����� ���� ��� �� �� ��� ��� ������� ������ �������� �� ����� ����� �� ��� � ��
� � ���struct��������computer��������to nest������������������� ��������� ��
� ��� � ���������������� ���������������������������������
�
�
�
�
�����'������������@��%�������@�22��������#����%'������%'�
��
�address���� ������name���� ������������������person������� �������������������������������������������� ������ comp����� ��������age���� ��������������� ������ ��� struct��� �computer� ������� ���� ������� � �� ���� � ��
� ��������������������������������������oneperson������������� �� COMPDB.H��
'���������'������
���'��������%�����T���#������'�������22��������7;)�U�����2�4*������'�������#����)(�
�������������������� ������ ���������������������comp�� �������������������������� ����� ��������������oneperson���������������������������computer���������������� ������������������ ���������������� � ������������ ������� �
� �computer���'��������%'�%�2�� ���4H�����������'��������%'��'� ���9�(9=�����'��������%'�2��'��E ���V���������
����������������� ��� ����� ��� ������������ ��� ������ ��� � ��� �� � ������� �����
��������� � ��C:\ZDEMO\EXAMPLES� ������������� �� ��������� ����� �� ����������������� �������������� ������ ���� � ������ ��������������������� ��������
������ ��� ����������������������� � ������������������������� ��������struct���� �� ����� �� � ������� � ������������������ �������������� ���� �����������
� �������� ������ ��������������������������������� ���������
� �������������� ��������� ������������������� ��� � �� �������� ����������������������� ����
����������������������� ��������������������� ������������������������������������
� �������������������������� � ������������������������������
���������struct� ��������������������� ������������� �� ���������� ��� ������������������������������������������� �����������������������������
��� ���������union�������������������������������������� �������� �������������������� ������ ��������������� ���������� ���������������� �����������������
� �������������������������� ���
� ���������������� �C++������ ����� ������������ � ���
������ ���������������� ��������������������������������������������������������
�������������������������������������������������������������������������
����char������� ��������������float�� ��������� ������������������������� �� ����� ���������������� ����� ������������ ��������C++����� ������������������� long������ ��������������������������� ���������� ��������������� �������������
�������� ��� ������ ������� ������ ������ ����� ���� ����� �������� �������� �����
� �������
��������%'��N������
������O����
���#��K��#�
�
��������������������������aLong���aFloat��� �� �sampleUnion������������������ aLong�������������� �aFloat������������������������ ������� �� ������� ���������� ��� ���� ������������ ��������������� ������������������ ������
� ������� ���������� �������
�
� �������������������������������$������������"������
� ����������������UNION.CPP��� ������ ������ �� ������������������������������� � ������������� ���������������������������������� �����������������������
� UNION.CPP����������
1: // union.cpp -- Description 2: 3: #include <stream.hpp> 4: 5: main() 6: { 7: union charint {
������
� ������� ���
� ��� � ���������������� ���������������������������������
�
�
�
�
8: char c; 9: int i;10: } ci;11:12: int i;13:14: for (i = 65; i < 91; i++) {15: ci.i = i; // Assign i to ci.i16: cout << ci.c; // Display c field in ci17: }18: }
���������� ��������������������������������������������UNION� ���������������
������� ����� ����� int i��� char c� ��� � �� ����� charint� ��� ����� ��� �������������� ���� � � ��� ��� � �� ������� struct� ��� �������� ������� ���� ��� ���� �union������� �� ��������������������������������C�����������������������������struct�������������������������� charint��������ci��������� �������������������������
� ���������� �� ����
�������������*���
�������struct����������������������������������� ����� ������������������������������������for��������������UNION.CPP���������������� �� ��������������ci.i���i���������������������������������� ���������� ������������i��int��������� �������� � �� struct��� ������� �������� ������ ��� � ��� ����� ���������� ���� c��char��������������� ������ ���� ���������������������������� ci���i�� �� �������������� ����������������c����������������������������������������������
c�����������������
����������������� ���������� �������� ���������� ��������������������������
�� ��� �� �� �� ��������������� �������� �� ���������������������� ���� �����
� �������������������c���������������������������i�����Z� ��A��������������ASCII
� ������������� ������� ������� ������ �������� ��� ���������������������� ��� ����������� ���
���������������������� �������������������� �������� �������������������������
��������������������������� ������������������������ ����������������������������
������������ ������������������������������������������� ������������������ ��
���������� ����������� ������������������ ��������������������������������������
����������������������������� ����������������������������� �����������������
� ������������������
� ���������������� ���������� ����� ������������ � ���
���� ������������������������������ ������ ���������������� ���������������
�������������� float����������������������������������� �� ����������������������� ������������� ����������������struct����int��������������������
������������������������������ ����������� ����� ���������������������������
������� ��������������� � ������ ������ ���������������������������������������
�������� ����������������������������� ��� ������������������������������������� �
�� ������������������������� ���������� ��������������������������� ����������
� �����������������
�����$��������������������������������������$������������������������������
������������������������������������������ �������� �� ������������ ��������
� �'���������������������������������������
��������������������� ������ �� ��������������������� ��������������������� �
� �array���������
� �������������������
1: // array.cpp -- Demonstrate arrays 2: 3: #include <stream.hpp> 4: 5: main() 6: { 7: int array[100]; // Define array of 100 integers 8: int index; // Index for accessing array elements 9:10: // Fill array with values from 0 to 99:11:12: for (index = 0; index <= 99; index++)13: array[index] = index;14:15: // Display array contents forwards and back:16:17: cout << "\nArray values from [0] to [99]:\n";18: for (index = 0; index <= 99; index++)19: cout << dec(array[index], 8);20:21: cout << "\nArray values from [99] down to [0]:\n";22: for (index = 99; index >= 0; index--)23: cout << dec(array[index], 8);24: }
� ��� � ���������������� ���������������������������������
�
�
�
�
���������������������������������� ��� ���� C++������������ ���� ������������������������������������������������ �������������������������������������������
� �C++���������EP���Q�
���� ������ ���������������������������������������� ���� �������������������
� �� ����������������������� ������������������������������
�
� �������������� %%���������"�"$�����
�� ������������������������������������������������ ������������������ ���������������
������������������������ ������� �� ��������������������������������������������
� ������������������
�����������EP)Q�
���������������������������� �int���������������������������������������� ������������������������������������������� cout���int���������������������
����������� ������������������ ��������� ��������������������array[3]��� ��int���������������������� ���������������� ������������C++��������������������
� �������������array��������������������EP�Q��������������������EP�Q��������������������EP(Q��������������������EP)Q��������������������EP;Q���������
� �for��������������������������������������������������������������������;���??
�����������EP�Q���������
��������� ���
� ����������
� ���������������� ���������� ����� ������������ � ���
����������� ����������������������������� ������ � �� ������� ������� ������
��������� ��������������������������������������� �� ������������������� ���
���������������������� �� ��������������i��int������������ �� �������������� ������������� ��������������������� � ��������
�����������EP��?�GQ�
��� �����������������������������������������������������������������������
� ��������������
����������for������������ ���������������������ARRAY.CPP��������������������index���������������������������������� � ������������� ��index��int��������int��������������������������������������������������������� �������������������������������������������������������������������������������� �����������
������������������������ ������������������������������ for��������� �������index� ������ ��
��������������� ������� �� �������struct���������������������������������������������� ����� ���COMPDB.H��������������������computer����������������
� �����
��%'�����E��%�P7�Q�
��������struct����������������� ��������������systems����������� ���������� ����������������computer����������������������������� �� computer
�E��%�P7Q���%�����M3$(��
���������� ����������systems�����������int����������������������������������������� �����������������������������������������C++� ������� ���������������������� ������������ ���struct���������������� ����computer���������������
� �name��� ������� ������������������������������������
������� ������ ��������� ���������struct���������� ����� ����������� ������������������������� ��������� ������ ������ ��������� ���������� �� ������������������������
���������������������� �����������unions��structs����float���int���������������� ����������������������
� ���������������� ������� � ��� ������ � �� ������������� ���� ���� ����� ���� �� ����� �����
������� �� �� ������ ��� ����� �GRADE.CPP� �� ��� ��� �� ����� ������ ������������� ��������������������������������� ������ ��������������������� ������
���������������������������������������������� ��������������������������
� ����������������
� ��� � ���������������� ���������������������������������
�
�
�
�
� �����������������
1: // grade.cpp -- Grade average calculator 2: 3: #include <stream.hpp> 4: #include <stdlib.h> 5: 6: #define MAXGRADES 18 7: 8: double grades[MAXGRADES]; // Array of grade values 9:10: main()11: {12: int i, numgrades;13: double total, average;14:15: // Prompt user for grades, and store values in array:16:17: for (numgrades = 0; numgrades < MAXGRADES; numgrades++) {18: cout << "Enter grade #" << (numgrades + 1) << "(-1 to quit): ";19: cin >> grades[numgrades];20: if (grades[numgrades] < 0) break;21: }22: if (numgrades <= 0) exit(0); // No grades entered23:24: // Display the values stored in the array:25:26: cout << "\n\nGrades:\n\n";27: for (i = 0; i < numgrades; i++)28: cout << dec(i + 1, 2) << ':'29: << form("%8.2f", grades[i]) << '\n';30:31: // Total the scores:32:33: total = 0.0;34: for (i = 0; i < numgrades; i++)35: total += grades[i];36:37: // Compute and display the average:38:
� ���������������� ���������� ����� ������������ � ���
39: average = total / numgrades;40: cout << "\nTotal scores = " << form("%8.2f", total);41: cout << "\nNumber of grades = " << dec(numgrades, 5);42: cout << "\nAverage grade = " << form("%8.2f", average);43: }
������� �������������� ����� ����������������������������������� ��� ����
����������������� ������������������������������������ �����������������
�� ����������������� �������������������������������������� ����������������
�������� ���������������� ���� �������������������������������� ���������
� ������������������������������������������������
������� �� ������ ������ �� ���� ������� ���� ����� �� ���� ������ �� ���
��� ������� ��������� ������� ����� ���� ��� �� ������ ��� ������ �������������
������ ���� �� ��� ������� ����� ���� �������������� ������� ������ �����
���� ��� ����� ��� ��� ��� �� ����� ���� ��� �� ����� �������� ������
���� ; ���������� ����� ��� ���� ����� ������ ����� ����� �������������
� �����������������������������������������
������� ��� ������ ������� ����� ������ ������� ��� ��������������� ������ �����
������������������ ������������������������������ ������������������� �������
������� ��������������������������������� ��������������� �� ����������
� ������������������
������������� �������������������������� �,-�������� $������������������
����� ����� ��� ������� $���� ��� ����� � ��� �&�� ��� ������ ���� �$��� �������
� ���� ������� ������ ������������� ����
� ������������������� ������ ��� ������� ������� ������ ���� ������ ������� ����� �� ��� ���� ���
��� �������� ��������� ������ ��� �������� ���� ������ ��� �������������� ��� ��������
�������������� ���������������� ��������������������������� �������������������
� �������������������EP��Q�%���� �
�����
����������������8���?? ��������������EP�����������Q�������������EP�Q�
�
� ��� � ���������������� ���������������������������������
�
�
�
�
������������� ������� �������������������������������������������������������������
� ����������������������������������������������������� ��� ����������������
� ���������������������������������������������������������
������������������������� !""�����������������������������������������������
����� ���� ����������� �������� ����� �������� ��� ������ ��� �� ���� ������� �� ��
� ������������������������������������������� ����������������������������
��������������������� �� ����������������������������������������������������
� �������������������
�����������������8���?? ����EP�Q�����
����������������� ������������������������� �����������������������������������
����� �� ����������������������������� ��������������������������������������������
����� � �� ���������������� ����� � �� ���������������������� �������������� �����
������������������������������
����������������������������������������������������������������������������
�������� �������� ����� ������� ������ ������� ��� ������ !""� ������ �� ��� ����
������������������� �������������������������������������������� ������� �� �����
� ���������������������� ������������
�������2�1�P��Q������I�8I�9I�:I�=I�7I�;I�)I�(I����
���������8����������// �������������2�1�P�����������Q�������������2�1�P�Q�
� ������� ����������������������������������������#����������$����������������������������
��������� ������������������������������������������� �������������� ��� ���
��������� ��������������� ����� ���������������������������������� ����������������
� ���
��������������� ����������������������� ��� ��������������������������������
��!""��� �������������������������� ����� ������������������������������������
� �� ������������������������ ��������������2��'��EP(7QP9�Q�
������������ ������#��� ���� ��������������������������� ����������� ��������
������������� ������������������%����������������������������������� ������ ��
� ���������� ���� ����������������������������������������������������������������%���P=QP9Q�
� ���������������� ���������� ����� ������������ � ���
�������������������� ����������������������������������������������������
������������������������������������������������������������������������
� �� ������
�
������������������������������������������������������������ �� � �����"�"%�����
� ����� �������������������
��������������� � ���������� �������� ��������������������� ������������������
��������������������������������������&�����������'��������� ���������������
�� ������� �� ���� ����������������������� ��������������������������� �� ����
� ����������������������������������������������������������������������
�������%���P)QP7Q�
��������������� ������������������������������� ���� �!""��� ������ ��� ������ ���
� ������������������������������������������� �������������� ��� �������������
� ���������������������������������������������������������������� �
����������1��������1����=����1??
������������������������:�����??
�������2���%���P��1QP���QI�9 �
������������
�
�� �� �������������� ���������������������������������������������������������
� � �� ��������������������������������
� ��� � ���������������� ���������������������������������
�
�
�
�
��� �� �� �������� �� ���� �� �� �� ������ ��� �� ��� ���� ������� ��� ������� ������
����������������������������� ������������� �������� �������������� ��������
����� ���������� ������� ��������� ������������������������������������ ������
� �������� ����������������������������������������������������������
�������������������� � �������������������������������������� ���������� ������ �
� ���������������������������� ���������������������������� �� ������� ��������
���������������������� �������� ����������������������� ���������������������
������������������������������ ������������ ������������������������������������ ���
� ���������������
� ��� ���������������
1: // meeting.cpp -- Multidimension array demonstration 2: 3: #include <stream.hpp> 4: 5: #define NUMEMPLOYEES 4 6: #define NUMHOURS 10 7: #define STARTHOUR 8 8: 9: #define JOE 010: #define SAM 111: #define PAULA 212: #define MARY 313:14: int schedule[NUMHOURS][NUMEMPLOYEES];15:16: main()17: {18: int empnum, hour;19:20: // Enter employee appointments:21:22: for (;;) {23: cout << "Employee number? (-1 to quit): ";24: cin >> empnum;25: if (empnum < 0) break;26: if (empnum > NUMEMPLOYEES) {27: cout << "Employee number is out of range\n";28: continue;29: }
������
� ���������������� ���������� ����� ������������ � ���
30: for (;;) {31: cout << "Hour of appointment? (8-17, -1 to quit): ";32: cin >> hour;33: if (hour < 0) break;34: if ((STARTHOUR <= hour) && (hour < STARTHOUR +
NUMHOURS))35: schedule[hour - STARTHOUR][empnum] = 1;36: else37: cout << "Hour is out of range\n";38: }39: }40:41: // Display schedule:42:43: cout << "\nSchedule:\n";44: cout << " Joe Sam Paula Mary\n";45: for (hour = 0; hour < NUMHOURS; hour++) {46: cout << dec(hour + STARTHOUR, 2) << ":00";47: for (empnum = JOE; empnum < NUMEMPLOYEES; empnum++) {48: cout << '\t';49: if (schedule[hour][empnum] != 0)50: cout << 'X';51: }52: cout << '\n';53: }54:55: // Calculate hours when all employees can meet:56:57: cout << "\nPossible meeting times:\n";58: for (hour = 0; hour < NUMHOURS; hour++) {59: int k = 0;60: for (empnum = 0; empnum < NUMEMPLOYEES; empnum++) {61: k += schedule[hour][empnum];62: }63: if (k == 0)64: cout << dec(hour + STARTHOUR, 2) << ":00\n";65: }66: }
� ���������%������� ����� �������������������()*��������������������������
�������� ��������������������������������������%��������������������������
� �� ����������������������������������������������� � �� ���
� ��� � ���������������� ���������������������������������
�
�
�
�
��� �������������������������������������������������������������������������
� �� ������ ����� ��� �������� ������ ���� ������� ��� ��������� ����� ���+� ����
������������������ ������������������������������������������������� �����
� �������������������������� ������������������������ ������������������
����������������������� ���%��������������������� ����������������������
� �%������%�������#������������������� ������� ��������������������� �� ����
������� ������������������������ ���� ������������� ����������������������������
� ������������������������������������
�� ��������������������������������������������������������������������������
����������������������������� ���������� � �� �������� ����� ���������������� ���
�������� ��������������������������������������������� ������������� +������
� �������
����� ��� ��� ������ ���� ��� ����� ��� ����()*�,!--� ��� ����� ���� ���
��������� ����������� �������� ��*.�/0.������(��(/0.�������������
����� �� �������� ����� ��� ��� ��������� ���� ���� ������ !""��� �������
�������� ���������������� ��� �� � �������������������������������������� ���
���������������������������������������������������������������������������
� ���� ��������������������� �������������������������� ���������������� ������
������ ������ ������ ����� ����� ���()*�,!--� ������ �������� ����� ���� ���
����������������������� �������� ������������������������������������������
� ��������������������������������������������������������������)
� ������������������������ ����������������������������������������������� ������������������������
��������� ������� ������������� ������������ �� ������������������������ ������
� ������������������������������������
���������#P9�Q�
������������������������������������������������������������ ����������������
���������������������������������������������������������������������!""��� �%�
� 1��)!���-�����������������������
����������� ����� ����������!""� ��������������������������� ���������� �����
� ������ ��������������������������������� ����������� �%�
���������M����2��PQ����T���#��U�����#����
� ���������������� ���������� ����� ������������ � ���
�����!""���������������������� �������������������������������%������������ �����
������ ��� ����� � �� ��� ������ ������ �������� ������� ���� ������� ����� ��� ���
� ����-������������������������
�����-�������� ������������������������� �������������� �������������� ���������
������������� �� �������������������������������������������� ������������������ ���
� ��������������������������������������
�����%�������7(�
� ���� ����������������������������������������������������
���������*'PQ����W����42�%���
����23�%�����2������������������������������� �������� ��� ���������� ������
�������� ������� ���� ������ ��� ����� ����4#� ��%���� ����� ����� ������� �������� ������
���������������� ������������������������� ���������������������� �������� ��������
� �������������
����������������������������� ����������������������������!""������� �����������
� ���������������������������������������������������������������� �%���
�����������������������������23�%�����2�������������� �����������������������
�!""� ���������������������� ����������������������������� ��� ����������������
�������� ������ ��� ������� ����� �������� ������ ��� ������ ����������������� ���
������� �������������������������������������������������������������%�
�������-���������������4#������� ����������������������������� �����������
�������������������� ������ ����������!""� ��������������������������������
������������������������������������������ � ��������������������� � �������
� ����������� ������������������������������������������������������
�������%��������*.55���������������!""����������������� �������������������
��������$���� ����������������������������������$�������������������������
� ������� !""���������������
� ������������������������� ������������������� � ������%�������������������������������������������
������������������������������� �� ������ �������� �����������������������������
������������������������������������������ ������������� ��� ���������� ������������
� ����
�����G0E'�P=Q�����C�I��U�I����I�� �I��H�I��D���
� ��� � ���������������� ���������������������������������
�
�
�
�
���� �������� ���� ��� ������������������������ ������������������������������
��������*.55��������������������������!""�������������������� �������� �����
� �������������� +���#����676���������������������������
�����G0E'�P=Q����CU� HD��
���������������� ���������������������������������������������������������� �
� ��������
� �����������������%�� 8������ ����� ��� ��� �������� ������� ��� ������ �� ���� ���� ��� ����
������� �������� ������� ��� ������� ������ ��� ����� ��� ������ !0�-�1,/��
� �������������������������������������� ����%�������������������������!""��
������ �������������������������!""� ��� ����������������������� ���������
� ������������������������������������������
�E��%�%�2������<H��������
� ���� ������ ��� � �� �������� ��������� ������� �������� ��� ���� ������� ��� � ��
����� ����������������� ������������ ����� ����������������������������!""
�������������������2 (�!����2�������������������������������������������
� ������������������������������������������������� ������������
����������� �����%���������9�� �������������%�� 8������ ������������ ���� ���� �
� ����������� ���������� ����������������!""������������� ���
�������EP��Q�
��������������������������������������������������������� ��� ����� �������
������������ ���������� �������������� ��������� � ���������� �����������
������� ������ ��� ������� ����� ���� �������� �� � ����� ���� ��� ������� �����
��������������������� �������������������������������� �������� ���� ������� �����
� �����
�����������EP(Q�
� ���������������������������������������
���@����EM��
��������������������� �����������������������������������!""��������������
� �������������������� ��������������������������� �
�����������EM�P�Q�
� ���������������� ���������� ����� ������������ � ���
���������������������������������������������������������� ���� �����������
����������� ����� �������������8���-����������� ���� ���������������
���������� ������������� ����������������� ��������������������� ���-��
�� ��� ���� ���������������� ������� ��� ������������ � ���������� �������������
� ��������������������������������� ��������
�&������&� �� ��� ��� �� ��� ����� ��� ��� ��������� ������ ������ ��� ���� ���� ���
�������� ������ ��������� ������� $��� ���� ��� $���� ������� ������ ������ $�����
�������� ���� �# �� $%��� �# �� %�&'���� ������� ��������� ��� ����� ����� ��� ��������
�� ����������� $�����������%������������������������������ ��������������
������������������������������$�������������������������������������������
������'���������������������������������������$�����������������������������
����� �������� ������� ��� ��� ���� �� ����� ����� ��������� ��������� �����
����������������������������������������(�����������������������������������
� �������)��*�+������������������������������
� �������������������������� �� ������������������������������������� �������������������������
������������������ �������������������� ���������������������� ������������������
������������������ �������������������� � ���������� ������������������ ��������
� ������������������������ ������������������������������� ���
�����'�����������#��2��#�� $$�������88����#��2���&� $$���%���I�����%�������#��2�����2���� $$��������7
��
� ������������������������������������� ����� �����������������������������
��������������������������������������������������� ����������������������:������ �
������ ��������� ������� �������������������� ��� ������������������������ �����
��������������������������� ���������������� ��������������������������������
� �������������������������������
���������������������� ��������������������������������������������� �
�������� ��������������� ������������������������������������������������������
����� ����������������������� �������������������� ������������������������
� ������������'�������
����#��2��#��"�:� $$��������(:����#��2���&�"���� $$���%���I�����%�������#��2�����2����"�;� $$��������7����#��2�"�;� $$�5�����2
��
� ��� � ���������������� ���������������������������������
�
�
�
�
��������������������� ����������� ����� �������������� ����� �������������
������������������ �� ������� ���������������������������������������������������
������������� ������������������������������������������ ���� ��������
��� ���� ��� ����������������������� ������������������������������������������
���� � ����� �������� �������� ������������������ ������������� �%��������� �:�������
� �������������������� �����������������������
� +� ��� ������ ����� ������� ����� ��� ������� ������ �� ���� ��� ������ ���� ���
��������������������������������$�����,�������������������������������������
� �����$����������������������������������������,����������������������������
�� ������������������� �� �������#��������������� �������� �������� �
�������������� ��������������� ����������������� �����������������������������
�� �� ������������������������������������ ������������������������� � �� ����
����������������������� ����� ��������� �� ������������������������� ���������
� �*�������� ������������
��������21�����
����#��2������"��� $$��������
����#��2���*���"�;� $$��������7
����#��2�"��� $$�5�����2
����#��2���������"�(� $$�������)
����#��2�"�)� $$�5�����2
����#��2����*��"��� $$��������
����#��2�"�;� $$�5�����2
��
����������������������������� � ��������������������������������� �������������
����� ������ �������� �������� ������� �� ���� ����� ���� ������ ����� �� ������ ��������
�����������������������������������������������������������������������������
�������������������������� ���������� ����������� ���������������������� ����
� ��������������������������������� ��������������� �����������������
���������� � �� ����� �� �������������� ���������� ��������������� �����
� ����������������#��������� �����������������
'������'�
���
����������4#� �������'��#��
��������������2��� �������'�����2����
� ���������������� ���������� ����� ������������ � ���
������ ������ ���������������������������� �!""��� ���������������������� ������
����� ����� ������ ������ �� � ����� ���� ��� ��� ������� ����������� ����� ����
����������������������������������!""� ��������������������������� �����
� ������������������ �������������������������������������������� ���������������
��� ����������������������������� � ���������������������� �����������������
� �� ������������������������������������������
� ���������������������������(������� ����������������������������������������
�������� �������� � ������������� ������� ��������� ��7.)-,!--� �� ������ �
��������� ������������ ����������� �������������������� ���������������
�������� 1)0�,/�����������������������������;���;�<��#��������������� ����
��������������������������������� ������������������������ ��������������� ��������
���������������� ���������������������� ������������������������
� -!����������������
1: // equip.cpp -- Display list of computer's equipment 2: 3: #include <stream.hpp> 4: #include <bios.h> 5: 6: struct equipment { 7: unsigned hasdiskette : 1; 8: unsigned : 1; // Not used 9: unsigned planar : 2;10: unsigned videomode : 2;11: unsigned numfloppy : 2;12: unsigned : 1; // Not used13: unsigned numserial : 3;14: unsigned gameadaptor : 1;15: unsigned : 1; // Not used16: unsigned numprinters : 2;17: };18:19: union twotypes {20: equipment eq; // The bit field structure21: int k; // Same bytes as an integer
������
� ��� � ���������������� ���������������������������������
�
�
�
�
22: };23:24: main()25: {26: twotypes t;27:28: t.k = _bios_equiplist(); // Get list as integer29:30: cout << "\nNumber of printers " << t.eq.numprinters;31: cout << "\nGame adaptor installed (1) " << t.eq.gameadaptor;32: cout << "\nNumber of serial ports " << t.eq.numserial;33: cout << "\nNumber of diskette drives ";34: if (t.eq.hasdiskette)35: cout << t.eq.numfloppy + 1;36: else37: cout << 0;38: cout << "\nInitial video mode (2) " << t.eq.videomode;39: cout << "\nPlanar RAM size (3) " << t.eq.planar;40:41: cout << "\n\n(1): 0=FALSE, 1=TRUE";42: cout << "\n(2): 1=40x25 color, 2=80x25 color, 3=monochrome";43: cout << "\n(3): 3=64K on XTs\n";44: }
������������� ��������������<��#������������������ ������������7.)-,!--
��������������� � �*������������������������������������������ �����������
������������� ����� ��������������� ��������� ���������������������� ���������
��������������������� � ����������� ������������� ������� � ���������������
� ��������������� ���������������������
������ �������=���#������������������������� ������������������ �����������
��<��#����������� �����������+����<��#�������������<������������������������
���������� �������� ��������������������������������������������������� ����
� ������ ������� �
�������������������� ��������� �=���#����������������� ���������� ���������
� ��������������������������+�� �������������������,+���;���;�<��#�����������
������<��#������� ������ �� ��� ������ ������ ��<�� �������������� ��� ���� �
� ������������������������������ ��������
� ���������������� ���������� ����� ������������ � ���
���,�<�������� ������������������������������,+������������������������
�������� ��������������������� ������������������������ ��������������� �� �����
� ��,�<���������� �����������,+��������������������������������������
����� �������� ������ ����� �������� ����� � $����� ������� ������� ��� ����� ���
����������������������!%&'()#������������./0��������������������������-!��
������ ������ ����� ��� ��� �*�).�+��� ����� ��� ������ ����� ����� �$��� �������
��������������������������������������$����������������������������������
�$� ����������������������������������������������������������������������
������������������������������������,������������� ��������������������������
��� �������(���������������������������� � ���������������������������
�������� ���������������������������������������� ������ ��������������������
������ ����� ������ ���� ������� ������ ������ �������� ������� ����� ������� ��� ���
� ����������������
� ����������������������������������������� ��������������� ���� �������������������� ������� �� ���� ����
������������������ ����������������������� � ���>���������� �>���� �!""��
� ���������������������������������������������������������� �� �����������������
��������3�X�������������������X�������� �
������� ������������������������������������>���?����@� �����������������!""
��������������� �� ��������������� ������������� ���>������������������������
�������>������������������������������������������������������� �������
� �������������������������������������������
����������� ���������������� �>����������� ������� ����)A�0B,!--��� ������ �
������������������� ��������������� !""��������������������������������� ��
����� �������� ���� ��������� ������������������������������� ��������������� ��
����������,%##������������������������������� ����������������������������
� ��!""������ ����������� ���������,%����
� ��1��2��33����������
1: // sizeof.cpp -- Determining the sizes of variables 2: 3: #include <stream.hpp> 4: 5: main()
������
� ��� � ���������������� ���������������������������������
�
�
�
�
6: { 7: char c; 8: short s; 9: int i;10: long l;11: float f;12: double d;13: long double ld;14:15: cout << "Size of char = " << sizeof(c) << " byte(s)\n";16: cout << "Size of short = " << sizeof(s) << " byte(s)\n";17: cout << "Size of int = " << sizeof(i) << " byte(s)\n";18: cout << "Size of long = " << sizeof(l) << " byte(s)\n";19: cout << "Size of float = " << sizeof(f) << " byte(s)\n";20: cout << "Size of double = " << sizeof(d) << " byte(s)\n";21: cout << "Size of long double = " << sizeof(ld) << " byte(s)\n";22: }
�������������� � ���)A�0B������������� ��� �����>��������������������������
��������������� ������ ������� �������������������� ����������������� ��
�������������������������������������������� ���������� ��� �>���?���@� ������
� ��������������
������������������� ������������������ ������������� ���>����������������������
���� ����������� � �� ��� ����� �� �>����������������� ���������� ���� �������
������������������������������� ����� ���!0�-�1,/������#������������
� ���������������� ������������� ���������
B�����2�����%'20���
��������3�X���������%'���������������X������%'��� �
� ��� ������������� ����������������������������������#���������������
�������EP���Q�
��������3�X���������E����������X��������E �
������ ����������������������������������������������� ������� ���>���������
������>��������������������������������� ������� ���������� �������!""������
���������� ���������>���������� ������������������� ����������� �������������� �
� �������� ������������� ��
� ���������������� ���������� ����� ������������ � ���
��������������������� �������!""��������� ��������������������������������� �
��������������������������������������������=%�������������������������������������
����� ������� ������ ��� ��� ��� �������� ��� ������ ������ ������� �������� ������
��������������� � ���������������� �����������������������������������?@�
� ������������������������� ����
���������������������� ���� �������?@������������������� ����������������
����?@� ������� ����� ������� ������ ������ ����� ��� ��� ����� ����� ������� ��� � ����
� ����������������� ��������� ���� �������������� ���������� ���� ������������
�������������������������� ����������� ��� �� �� ����������������������������
�����������������������������������������������?@����� ������������������������
�!""��� ��������������������� ������ ������ � �� ��������� ��������� �������� �� ���
� ��������� ������������������������������ �������
� ��������������� ��������������� �������������!""������������������������������ ����
� ������������ ����������� ����
� ������������� ���������������������������������������������������� ����
� ���������������������������� ����������������������������������������
������ ��������� ���� ����� ���� ������� ����� =%������ ���� ��� ���� ����
� ������������
�������������������������/9��������// ��
����������R�����������������������������
����������//��
�
���������������������������������������������������������������������� ����
� ���������������
����������������������� ������������������������������ ��������������� ����
� ������� 0��������������� ��������������
������������������������������0�������������������������������������� ���
� �������4�����������
��������� ������� ��� ������� �� ��� ��� ��� C0���,!--� �� ������ ���� ����
� ��������������
������� =%���� ����� ����� � �� �� ��� ��� ��� /���,!--� ��� ��� ���� ����
� ��D=%�����
� ��� � ���������������� ���������������������������������
�
�
�
�
� ���� ��������������������������������������������� � ����
� ���������������������������������������������������� �����
��������������������� ���������������������D=%������=%�������������������� �����
� ��������������������������������������=���%�����������
� ����������������������������������� � � ����
� ����������������������������������� ������
���� ��� ����������� �� ��� ������������� ����� ���������� ��� �� ����� � �� ������
� �������������
� �������� ������� � �� ����� ���������������� �������� ������ � �������� ���� � ����
� ��1�!�����B������������������������1!��B�
�� ���������� �������� ������������� ��������������������������������� ����
� ����������������� ���>���������� ������������
�
�
�
�
�
�
�
�
�
�
�
�
�
�