Contoh Program Struct C++

Contoh program C ++ Struck
Boleh copas Cuma Cantumin sumbernya ya!!
// Program : struct2.cpp
#include <iostream.h>
#include <string.h>
#include <stdlib.h>

struct movies_t {
  char title [50];
  int year;
} mine, yours;

void printmovie (movies_t movie);

int main ()
{
  char buffer [50];

  strcpy (mine.title, "2001 A Space Odyssey");
  mine.year = 1968;

  cout << "Masukkan judul film favorit: ";
  cin.getline (yours.title,50);
  cout << "Masukkan tahun: ";
  cin.getline (buffer,50);
  yours.year = atoi (buffer);

  cout << "Judul film favorit yang ada:\n ";
  printmovie (mine);
  cout << "Judul film favorit kamu adalah:\n ";
  printmovie (yours);
  return 0;
}

void printmovie (movies_t movie)
{
  cout << movie.title;
  cout << " (" << movie.year << ")\n";
}



....................................
// Program : struct3.cpp
// array of structures
#include <iostream.h>
#include <stdlib.h>

#define N_MOVIES 5

struct movies_t {
  char title [50];
  int year;
} films [N_MOVIES];

void printmovie (movies_t movie);

int main ()
{
  char buffer [50];
  int n;
  for (n=0; n<N_MOVIES; n++)
  {
     cout << "Masukkan judul film: ";
     cin.getline (films[n].title,50);
     cout << "Masukkan tahun : ";
     cin.getline (buffer,50);
     films[n].year = atoi (buffer);
  }
  cout << "\nFilm yang menjadi favotir kamu:\n";
  for (n=0; n<N_MOVIES; n++)
     printmovie (films[n]);
  return 0;
}

void printmovie (movies_t movie)
{
  cout << movie.title;
  cout << " (" << movie.year << ")\n";
}

........................
// Program : struct4.cpp
#include <iostream.h>
struct time {
    int jam;
    int min;
};

struct rencana {
struct time *awal;    /* penunjuk bagi struktur */
struct time *akhir;
};

struct time jk = { 1,2 };
struct time kl = { 3,4 };
struct rencana kerja = { &jk,&kl };
main()

{

kerja.akhir->min = 37;

cout << kerja.awal->jam << " " <<  kerja.awal->min << "  "

<< kerja.akhir->jam << " " << kerja.akhir->min << endl;

}


1 komentar:

  1. wuaduch apa ini sob, gak ngerti saya yang beginian...

    ReplyDelete

Copyright © 2014 Dunia Naeta All Right Reserved