Pada kesempatan kali ini saya akan memposting program Struct.
1. Source Code
#include<stdio.h>
#include<conio.h>
#include<iostream>
using namespace std;
main()
{
struct dtmhs
{
char nim[5];
char nama[15];
};
struct dtnil
{
float nil1;
float nil2;
};
struct
{
struct dtmhs mhs;
struct dtnil nil;
} nilai;
//clrscr();
//-> masukan data
cout<<"masukan NIM = "; cin>>nilai.mhs.nim;
cout<<"masukan Nama = "; cin>>nilai.mhs.nama;
cout<<"masukan Nilai UTS = "; cin>>nilai.nil.nil1;
cout<<"masukan Nilai UAS = "; cin>>nilai.nil.nil2;
cout<<endl;
//-> menampilkan hasil masukan
cout<<"NIM = "<<nilai.mhs.nim<<endl;
cout<<"Nama = "<<nilai.mhs.nama<<endl;
cout<<"Nilai UTS = "<<nilai.nil.nil1<<endl;
cout<<"Nilai UAS = "<<nilai.nil.nil2<<endl;
cout<<endl;
getch();
return(0);
}
Dev C++:
Output:
2. Souce Code:
#include <iostream>
#include <stdio.h>
#include <conio.h>
using namespace std;
main(){
cout<<"Menghitung gaji perhari\n\n";
struct jam
{int jam,lembur,nilai;
}jm;
cout<<"masukkan jam kerja : ";cin>>jm.jam;
if(jm.jam<=7)
jm.nilai= jm.jam*500;
else if(jm.jam>7)
{jm.lembur=jm.jam-7;
jm.nilai= (7*500)+(jm.lembur*15*500);
}
cout<<"gaji = "<<jm.nilai;
return 0 ;}
#include <stdio.h>
#include <conio.h>
using namespace std;
main(){
cout<<"Menghitung gaji perhari\n\n";
struct jam
{int jam,lembur,nilai;
}jm;
cout<<"masukkan jam kerja : ";cin>>jm.jam;
if(jm.jam<=7)
jm.nilai= jm.jam*500;
else if(jm.jam>7)
{jm.lembur=jm.jam-7;
jm.nilai= (7*500)+(jm.lembur*15*500);
}
cout<<"gaji = "<<jm.nilai;
return 0 ;}
Dev C++:
Output:
3. Source Code:
#include <iostream>
#include <conio.h>
#include <string>
using namespace std;
struct data
{
string nim[12],nama[12];
int i, menu;
};
data biodata;
void input()
{
cout <<"Masukkan banyak data : ";cin>>biodata.i;
for(int a=1;a<=biodata.i;a++)
{
cout <<"Masukkan NIM : ";cin>>biodata.nim[a];
cout <<"Masukkan Nama : ";cin>>biodata.nama[a];
}
}
void tampil(){
cout <<endl<<"Data yang sudah masuk : "<<endl;
for(int a=1;a<=biodata.i;a++)
{
cout <<"NIM : "<<biodata.nim[a]<<endl;
cout <<"Nama : "<<biodata.nama[a]<<endl<<endl;
}
getch();
}
int main()
{
do{
//clrscr();
cout<<"1. Input"<<endl;
cout<<"2. Tampil"<<endl;
cout<<"3. Keluar"<<endl;
cout<<"Menu : ";cin>>biodata.menu;
switch(biodata.menu){
case 1: input();break;
case 2: tampil();break;
}
}while(biodata.menu!=3);
return 0;
}
#include <conio.h>
#include <string>
using namespace std;
struct data
{
string nim[12],nama[12];
int i, menu;
};
data biodata;
void input()
{
cout <<"Masukkan banyak data : ";cin>>biodata.i;
for(int a=1;a<=biodata.i;a++)
{
cout <<"Masukkan NIM : ";cin>>biodata.nim[a];
cout <<"Masukkan Nama : ";cin>>biodata.nama[a];
}
}
void tampil(){
cout <<endl<<"Data yang sudah masuk : "<<endl;
for(int a=1;a<=biodata.i;a++)
{
cout <<"NIM : "<<biodata.nim[a]<<endl;
cout <<"Nama : "<<biodata.nama[a]<<endl<<endl;
}
getch();
}
int main()
{
do{
//clrscr();
cout<<"1. Input"<<endl;
cout<<"2. Tampil"<<endl;
cout<<"3. Keluar"<<endl;
cout<<"Menu : ";cin>>biodata.menu;
switch(biodata.menu){
case 1: input();break;
case 2: tampil();break;
}
}while(biodata.menu!=3);
return 0;
}
Dev C++:
lanjutan....
Output:
Wassalamu'alaikum wr.wb
SEKIAN DAN SEMOGA BERMANFAAT