Algoritma dan Struktur Data
Tugas 11 Programing Basic Algoritma
Assalamu'alaikum Wr. Wb.
Nama : Aufa Munadil Al - Khoir
NIMa : 3420210024
Prodi : Teknik Informatika
Fakultas : Sains dan Teknologi
Lampiran Tugas 11 Algoritma & Struktur Data
Example 1
Script Ex 1
//Program Penggunaan Struct - F
#include <stdio.h> // Nama : Aufa Munadil Al - Khoir
#include "iostream.h" // NIM : 3420210024
#include <conio.h> // Prodi : Teknik Informatika
#include "iomanip.h"
void garis(int a) {
int i;
printf ("\n");
for (i=1; i<=a; i++) { printf ("="); }
printf ("\n\n");
}
void main() {
struct data {
char nama[25];
char nim[15];
int nilai;
};
data mahasiswa;
garis(50);
cout << "\t Masukkan Nama : "; cin >> mahasiswa.nama;
cout << "\t NIM : "; cin >> mahasiswa.nim;
cout << "\t Nilai Total : "; cin >> mahasiswa.nilai;
garis(50);
cout << "\t Data Mahasiswa" << endl;
cout << "\t Nama Mahasiswa : " << mahasiswa.nama << endl;
cout << "\t NIM : " << mahasiswa.nim << endl;
cout << "\t Nilai Total : " << mahasiswa.nilai << endl;
garis(50);
cout << setw(51) << "Modify by: " << endl;
cout << setw(51) << "A.M F.S " << endl;
getch();
}
Example 2
Script Ex 2
#include <stdio.h>
#include "iostream.h" // Nama : Aufa Munadil Al - Khoir
#include <conio.h> // NIM : 3420210024
#include "iomanip.h" // Prodi : Teknik Informatika
#define out cout
#define in cin
void line() {
out << "\t===========================================================" << endl;
}
void title() {
line();
out << "\t\t Penggunaan Struktur Dengan Array" << endl;
line();
}
char hasil(int nilai);
void main() {
/*
Sebuah struct adalah sekelompok elemen data yang dikelompokkan bersama dibawah satu nama.
Unsur-unsur data, yang dikenal sebagai anggota, dapat memiliki berbagai jenis tipe data dan panjang yang berbeda.
*/
title();
struct {
char nim[11];
char nama[25];
char ket[30];
int nilai;
} mhs[5];
int i, jml;
out << "\t Masukkan Jumlah Data : "; in >> jml;
for (i=1; i<=jml; i++) {
out << "\t Data Ke - " << i << endl;
out << "\t Masukkan NIM : "; in >> mhs[i].nim;
out << "\t Masukkan Nama : "; in >> mhs[i].nama;
out << "\t Masukkan Nilai : "; in >> mhs[i].nilai;
out << endl;
if (mhs[i].nilai >= 75) {
strcpy (mhs[i].ket, "Lulus");
} else {
strcpy (mhs[i].ket, "Tidak Lulus");
}
}
clrscr();
title();
out << "\t No Nama NIM Nilai Keterangan" << endl;
line();
for (i=1; i<=jml; i++) {
out << "\t " << setw (3) << setiosflags (ios::left) << i;
out << setw (25) << mhs[i].nama;
out << setw (11) << mhs[i].nim;
out << setw (7) << mhs[i].nilai;
out << mhs[i].ket;
out << endl;
}
line();
out << "\t\t\t\t\t\t " << "Modify by:" << endl;
out << "\t\t\t\t\t\t " << " A.M F.S" << endl;
getch();
}
Example 3
Script Ex 3
#include <stdio.h>
#include "iostream.h"
#include <conio.h>
#include "iomanip.h"
#define exit cout // Nama : Aufa Munadil Al - Khoir
#define back cin // NIM : 3420210024
#define newl endl // Prodi : Teknik Informatika
void line() {
exit << "\t =========================================================" << newl;
}
void title() {
line();
exit << "\t\t\t PENITIPAN MOBIL" << newl;
exit << "\t\t\t "; putchar(3); exit << " AZYAF CAR'S "; putchar(3); exit << newl;
line();
}
float tobay(float total, float subtotal);
void main() {
char ns[25];
char np[25];
int i, jml;
start:
clrscr();
title();
exit << "\t Nama Staff : "; back >> ns;
exit << "\t Nama Pemilik : "; back >> np;
exit << "\t Jumlah Mobil : "; back >> jml;
clrscr();
title();
puts ("\t Data Ke - ");
puts ("\t Kode Mobil [S/J/H] : ");
puts ("\t Kode Service [1/2] : ");
puts ("\t Jumlah Mobil : ");
struct {
char cdm;
char cds;
int jmlm;
} masukkan[5];
char jenis[5][15],service[5][15];
float price[5], subtotal[5];
float total=0;
for (i=1; i<=jml; i++) {
gotoxy (32,6); clreol();
gotoxy (32,7); clreol();
gotoxy (32,8); clreol();
gotoxy (20,5); exit << i;
gotoxy (32,6); back >> masukkan[i].cdm;
gotoxy (32,7); back >> masukkan[i].cds;
gotoxy (32,8); back >> masukkan[i].jmlm;
if (masukkan[i].cdm == 'S' || masukkan[i].cdm == 's') {
strcpy (jenis[i], "SPORT");
if (masukkan[i].cds == '1') {
price[i] = 900000;
strcpy (service[i], "Body Repair");
}
else if (masukkan[i].cds == '2') {
price[i] = 950000;
strcpy (service[i], "Tune Repair");
}
else {
price[i] = 0;
strcpy (service[i], " - ");
}
}
else if (masukkan[i].cdm == 'J' || masukkan[i].cdm == 'j') {
strcpy (jenis[i], "JEEP");
if (masukkan[i].cds == '1') {
price[i] = 750000;
strcpy (service[i], "Body Repair");
}
else if (masukkan[i].cds == '2') {
price[i] = 800000;
strcpy (service[i], "Tune Repair");
}
else {
price[i] = 0;
strcpy (service[i], " - ");
}
}
else if (masukkan[i].cdm == 'H' || masukkan[i].cdm == 'h') {
strcpy (jenis[i], "HATCHBACK");
if (masukkan[i].cds == '1') {
price[i] = 550000;
strcpy (service[i], "Body Repair");
}
else if (masukkan[i].cds == '2') {
price[i] = 650000;
strcpy (service[i], "Tune Repair");
}
else {
price[i] = 0;
strcpy (service[i], " - ");
}
}
else {
strcpy (jenis[i], " - ");
if (masukkan[i].cds == '1') {
price[i] = 0;
strcpy (service[i], " - ");
}
else if (masukkan[i].cds == '2') {
price[i] = 0;
strcpy (service[i], " - ");
}
else {
price[i] = 0;
strcpy (service[i], " - ");
}
}
// Hitung Subtotal
subtotal[i] = masukkan[i].jmlm * price[i];
// Hitung Total
total = tobay (total, subtotal[i]);
}
clrscr();
title();
printf ("\t Nama Staff : %s\n",ns);
printf ("\t Nama Pemilik : %s\n",np);
line();
exit << "\t No Jenis Jenis Biaya Jumlah Subtotal" << newl;
exit << "\t Mobil Service Service Mobil" << newl;
line();
for (i=1; i<=jml; i++) {
exit << "\t " << setw (4) << setiosflags(ios::left) << i;
exit << setw (10) << jenis[i];
exit << setw (13) << service[i];
exit << setw (12) << price[i];
exit << setw (10) << masukkan[i].jmlm;
exit << setprecision (10) << subtotal[i];
exit << newl;
}
line();
float pay, refund;
exit << "\t\t\t\t Total Bayar : "<< setprecision (10) << total << newl;
exit << "\t\t\t\t Bayar : "; back >> pay;
refund = pay - total;
exit << "\t\t\t\t Kembalian : "<< setprecision (10) << refund << newl;
goto finish;
finish:
char again;
exit << "\t\t\t Ingin Input Data lagi [Y/N] : "; back >> again;
if (again == 'Y' || again == 'y') {goto start; } else {
line();
exit << "\t\t\t\t\t\t\t" << "Modify by: " << endl;
exit << "\t\t\t\t\t\t\t" << " A.M F.S " << endl;
getch();
}
}
float tobay (float total, float subtotal) {
return total + subtotal;
}
Example 4
Script Ex 4
//Contoh Struktur Lanjutan
#include <iostream.h> // Nama : Aufa Munadil Al - Khoir
#include "string.h" // NIM : 3420210024
#include <conio.h> // Prodi : Teknik Informatika
struct Movie {
char title [25];
char year [5];
} Me, You;
void PrintMovie (Movie who);
void main() {
strcpy (Me.title, "KKN DI DESA PENARI");
strcpy (Me.year, "2022");
cout << "\t ===================================== \n";
cout << "\t You title Movie Is : "; cin >> You.title;
cout << "\t Meput Year : "; cin >> You.year;
cout << endl;
cout << "\t Movie Favorite Me Is : \n";
PrintMovie (Me);
cout << endl;
cout << "\t Movie Favorite You Is : \n";
PrintMovie (You);
cout << "\t ===================================== \n";
cout << "\t\t\t\t " << "Modify by: " << endl;
cout << "\t\t\t\t " << " A.M F.S " << endl;
getch();
}
void PrintMovie (Movie who) {
cout << "\t " << who.title;
cout << "\t " << "(" << who.year << ")" << endl;
}
Terimakasih
Wassalamu'alaikum Wr. Wb.
Tidak ada komentar:
Posting Komentar