Sabtu, 21 April 2012

Structures Data Using C + +

STRUCTURE
The structure is a collection of data elements that are combined into a single unit of data. Each data element is called a field or structural elements. The field can have different data types or, even if those fields in a single entity but can still be accessed individually.

Declaration of structure

struct name_struktur
{
type1 element1;
type2 element2; member / element of the structure
element3 type3;
.
.
Name_object} / / identifier that is used to call the structure
------------ Or ---------------
struct nama_struktur
{
type1 element1;
element2 type2;
element3 type3;
.
.};

struct nama_struktur nama_object;

Examples of structures with different data types:
struct student
{
nim char [10];
char name [25];
majors char [2];
float GPA;
MHS};


Examples of structures with the same data type:
struct date
{
int date;
int month;
int year;
Date};

Can be written --- or ---
struct date
{
int day, month, year;
Date};


Example program 1:

/ / Program inventory data
# Include
# Include
struct {stock
char name [50];
int Number;
Stoks};
main () {
cout << "Enter the item name ="; cin >> stoks.nama;
cout << "Enter the number of items ="; cin >> stoks.jml;
cout << ""; cout << "------------------------- \ n"; cout << ""; cout << "Output" << stoks.nama << "="; cout << stoks.jml; getch ();} Can also be written: / / Program inventory data # Include
# Include
struct {stock
char name [50];
int Number;
};
struct stoks stock;
main () {
cout << "Enter the item name ="; cin >> stoks.nama;
cout << "Enter the number of items ="; cin >> stoks.jml;
cout << ""; cout << "------------------------- \ n"; cout << ""; cout << "Output" << stoks.nama << "="; cout << stoks.jml; getch ();}


Example program 2: 

/ / Program to access the structure elements # Include
# Include
struct data_tgl
{Int date, month, year;};
struct a friend
{Char name [20];
j_kel char [1];
struct data_tgl date;};
struct a friend info;
main ()
{
/ / Input data
cout << "Enter your name:"; cin >> info.nama;
cout << "Your Gender ="; cin >> info.j_kel;
cout << "Your birth date ="; cin >> info. tanggal.tgl;
cout << "Your birth month ="; cin >> info. tanggal.bln;
cout << "The year you were born:"; cin >> info. tanggal.thn;
cout << "";
cout << "------------------------- \ n";
cout << "";
/ / Output data
cout << "Name:" << info.nama;
cout << "\ nKelamin:" << info.j_kel;
cout << "\ nTanggal born:" << info.tanggal.tgl << "-" << info.tanggal.bln << "-" << info.tanggal.thn;
getch ();}





source :  STMIK AMIKOM YOGYAKARTA

0 komentar

Posting Komentar

Statistik