������ �� ���������
(Arrays of Strings)
�������� �� ��������� �� �������� ���������, ����� �� �������� �� ��������� �� �����, ������, ����� �� ������� � ����� �������� �� �������.
1. ��������� �� ����� �� ���������. ��� ���� �������� � ����� �� �������, �� �������� ������� �� ��������� ������������ ����� �� ����� �� �������. ��� ������ �� ������� 10 ����� �� �������� 20 �������, �� ��������� �������� ���������.
char names[10][20]; // array of 10 strings
�������� ��������, �� ����� �� ����������� (� ������ 10) �� ���� � ������� ����� � ������� ������ �����. ��� ������� ������, �������� � ���� � ����� j, �� �������� � names[j]: �� ����������, �� ������� ������ ��� ����� 0, �.�. ���� � ����� j �� � ���������� j+1 ������. �������� ��� ��������� ���������� �� ���� 10 ����� � ���������� (��� ��-����� �� �����, ��� ���������� ������� ������ ��� ���� �������):
for
(j = 0; j < 10; j++)
{
cout << �Enter name (or press Enter to exit loop): �;
cin.get(names[j], 10);
if (strlen(names[j]) == 0) // if user presses [Enter],
break;
// exit from loop
}
2. �������������� �� ����� �� ���������. ��� ���� �� �������������� ������ ��������� ��� � �������, ������ �� ���������. ��� ���� ������ �� ������������� �� ������� �� ����� �� ���������:
const
int MAX = 10; // maximum length of day name, +1
const
int DPW = 7; // days per week
//
array of day names
const
char day_name[DPW][MAX] = { "Sunday", "Monday", "Tuesday",
"Wednesday", "Thursday", "Friday",
"Saturday"};
����������� ��������� �� ������� ��� ��������� � �� ��������� � ������ �����. �������� �������� �� const � �����������, ����� � �������, ������ ���� ����� �� ����� �� ��������� �� �� �������� � ���� �� ����������.
3. ������ �� ����, ����� ������ � ����� �� ���������. ���� ������ � ���� �� ������� �� ������ [1]. �������� ��������, �� ����������� MAX � ����� �� �����, �.�. � ������� ��-������ �� ��������� �� ���-������� ��� ("Wednesday"), �� �� ��� ����� �� �������� ������, '\0', � ������ -- �� ������� ������ ����� ���� ��������.
�� �� �� �� ������� ��� ����� ���� ����� ����������� day_name, �� � ���������� ���� ������ ���������� (external variable), ����� � �������� ��� ����� ���� ����� �� ���� ��������. ������ ����� ���� �� �� �������� � ���� ������������ �� day_name � �����, �� ���� �������� ���������� (static variable) - � ���� ��������� �� �� ��������� � ���������� ������. ��� � ������� ��� �� ����������.
//
weekdays.cpp
//
creates a class of days of the week objects
#include
<iostream.h>
#include
<string.h> // for stricmp()
#include
<conio.h> // for getch()
const
int MAX = 10; // maximum length of day name, +1
const
int DPW = 7; // days per week
//
array of day names
const
char day_name[DPW][MAX] = { "Sunday", "Monday", "Tuesday",
"Wednesday","Thursday", "Friday",
"Saturday"};
class
weekday
// class of day names
{
private:
int day_number;
// Sunday = 0, etc.
public:
void inday()
// user inputs day name
{
char tempday[MAX];
// holds user input
int gotit = 0;
// match found? (0 = false)
int j;
// loop variable
while( !gotit )
// cycle until user enters
{
// a correct name
cout << "Enter day of week (e.g., Friday): ";
cin >> tempday;
for (j = 0; j < DPW; j++) // compare
user input
{
// with list of names
if (stricmp(tempday, day_name[j]) == 0)
{
// if there's a match,
gotit = 1;
// set flag,
break;
// break out of for loop
}
} // end for
day_number = j;
// record what day it was
} // end while
} // end inday()
void outday()
// display the day name
{
cout << day_name[day_number];
}
void outnumber()
// display the day number
{
cout << (day_number + 1);
}
void add(int days)
// add days to
{
// this weekday
day_number += days;
// add days
day_number %= DPW;
// ensure not > 7
}
};
// end weekdays class
////////////////////////////////////////////////////////////////
void
main()
{
weekday wd;
// make a weekday object
cout << "What day is it?" << endl;
wd.inday();
// user gives it a value
cout << "You entered ";
wd.outday();
// display its day name
cout << "\nThat's day number ";
wd.outnumber();
// display its day number
wd.add(10);
// add 10 days to it
cout << "\nTen days later is ";
wd.outday();
// display its day name
// added to stop the output onto the monitor
getch();
}
// main()
� ����� ��� ���� ����������, day_number, ����� ������� ������ �� ���� (�������� ��������, �� ������ � � ����� ����!) � ������ ������� �� �����: inday(), outday(), outnumber() � add(int days). ������� ������� ����� �� �������� �� ����� �� ����, ����� �� ������ �� �����������, ������� ���������� ����� �� ���� �� ������ � ���������� �� ������� �����, ������� ������� ������� �� ����, ���� �������� �� ������� �� ������, � ���������� "������" ����� ��� ������ �� ����, �.�. �� �������� ��������� ��� ��� �� ��������� �� ���� ���� ���� ����� ���.
� �������� ��������, main(), �� ������� ����� �� ����� weekday, ������ �� �� ����������� ����� �� ���� �� ���������, ���������� ������� ����� � ���� ���� �������� ����� �� ���� ��� � ������ ��������� ��� ��� � ���� ����� ��� (wd.add(10);) � ������� ����� �� ���� ��� �� ������.
4. ����� ���� ��������� � ������� � ���������� �� �. 3. ��� ��������� �� ����� inday() �� �������� ������������� ������� stricmp(tempday, day_name[j]), ����� �������� ����� ������� tempday � day_name[j] � ����� 0, ��� �� �� �������. �� ������� �� ��������� strcmp, �� �� ������� �� ��� �� �� ����� ������� ����� ������ � ����� ����� (ignores case) � �� ��� ����� ������� "Monday" � "monday" �� �������.
���� ��� ������ � ��� ��������� add(int days), ������ ���� ���������� ����������� �������� �� ����������� (Arithmetic Assignment Operator) +=, ������ ������� �� ���� ��� ������ �� ����. �.�. day_number += days; � ������������ �� day_number = day_number + days;, � day_number %= DPW; - �� day_number = day_number%DPW;: �� ����������, �� % ������ �������� �� ������ �� ������ �� ���� �� ������� �� ����.
� C++ ��� ��� ������� ������ ���������:
a
+= b; // same as a = a + b
a
-= b; // same as a = a - b
a
*= b; // same as a = a * b
a
/= b; // same as a = a / b
a
%= b; // same as a = a % b
���������� �� ���������� �� ���������� � �������� (��� ������� "����������"):
What
day is it?
Enter
day of week (e.g., Friday): monday
You
entered Monday
That's
day number 2
Ten
days later is Thursday
������������� ��������� �� ����������� ������ ���� ��-��������� � ��-���, ������ �� ��������� ������ �� ������������� �� C++.
� ��������� �������� �� ���������� �� �� ��������� � ��� ��� ���� ��������� �� ����� -- ��������� ��� (enum) � ���������� ��� (bool).
�����: ����. �������� �����������
[ ���� � �������� �� ���� 22 �� ������ 2008 �. �� �������� "������" www.kosnos.com ]
Keywords: �++,
OOP programming , C++ , Classes , Inheritance , Reusability , Creating
New Data Types , Polymorphism and Overloading
������� ����: ����
, �����, ������� ����������� ������������ , ������������ switch if else
?