Anda di halaman 1dari 10

TUGAS 5

Tugas_5.cpp

#include <iostream>
#include <cstdlib>
using namespace std;

bool found;

void InsertElement(int arah,int data);


bool SearchByBoolean(int data[],int find);
int SearchByAddress(int data[],int find);

int datas[30];
int dirrection;
int jumlahdata=30;
int cari;

int main()
{
int x=0;
while(x<4){
cout<<"\n\nMenu :\n";
cout<<"\t0. Check Data\n";
cout<<"\t1. Buat Data\n";
cout<<"\t2. SearchByBoolean\n";
cout<<"\t3. SearchByAddress\n";
cout<<"\t4. Exit\n";
cout<<"Masukan Pilihan :";
cin>>x;

switch(x)
{
case 0: {system("cls");
cout<<"Data : ";
for(int b=1;b<=jumlahdata;b++)
{
cout<<datas[b]<<" ";
}
}break;
case 1: {cout<<"Arah(1=Maju , 0=Mundur) :";
cin>>dirrection;
cout<<"Jumlah Data(max.30) :";
cin>>jumlahdata;
system("cls");
InsertElement(dirrection,jumlahdata);
}break;
case 2: {cout<<"Cari Angka :";
cin>>cari;
if(SearchByBoolean(datas,cari)==true)
{
cout<<"Angka : "<<cari<<"
Ditemukan\n\n";
}
else
{
cout<<"Angka : "<<cari<<" Tidak
Ditemukan\n\n";
}
system("pause");
system("cls");
}break;
case 3: {cout<<"Cari Alamat dari Angka :";
cin>>cari;
if(SearchByAddress(datas,cari)<=jumlahdata)
{
cout<<"Angka : "<<cari<<" Ditemukan di
Alamat ke-"<<SearchByAddress(datas,cari)<<"\n\n";
}
else
{
cout<<"Angka : "<<cari<<" Tidak
Ditemukan di Alamat manapun~\n\n";
}
system("pause");
system("cls");
}break;
}
}
}

void InsertElement(int arah,int data)


{
if(arah==1)
{
for(int a=1;a<=data;a++)
{
datas[a]=a;
}
}
else if(arah==0)
{
for(int a=data;a>=1;a--)
{
datas[data-a+1]=a;
}
}
else cout<<"Data Kosong\n";

cout<<"Data : ";
for(int b=1;b<=data;b++)
{
cout<<datas[b]<<" ";
}
}

bool SearchByBoolean(int data[],int find)


{
int a=1;
found=false;
while(found==false && a<=jumlahdata)
{
if(data[a]==find)
{
found=true;
}
else a++;
}
return found;
}

int SearchByAddress(int data[],int find)


{
int a;
for(a=1;a<=jumlahdata;a++)
{
if(data[a]==find)
{
break;
}
}
return a;
}
TUGAS 6.1

header6.h

#ifndef HEADER61_H
#define HEADER61_H

#include <iostream>
#include <list>

using namespace std;

static bool found;

bool SearchByBoolean(list<int> data,int find);


list<int>::iterator SearchByAddress(list<int> data,int find);

static list<int> datas;


static list<int>::iterator poin;
static int cari;

#endif

header6.cpp

#include "header6.h"

bool SearchByBoolean(list<int> data,int find)


{
poin=data.begin();
found=false;
while(!found && poin!=data.end())
{
if((*poin)==find)
{
found=true;
}
else poin++;
}
return found;
}

list<int>::iterator SearchByAddress(list<int> data,int find)


{
poin=data.begin();
found=false;
while(!found && poin!=data.end())
{
if((*poin)==find)
{
found=true;
}
else poin++;
}
return poin;
}

Tugas6_1.cpp

#include "header6.h"
#include <iostream>
#include <list>
#include <cstdlib>
using namespace std;

int main()
{
int d;
int x=0;
reset:
cout<<"Data Pilihan(1=Maju , 0=Mundur) : ";
cin>>d;
system("cls");
if(d==1)
{
for(int a=1;a<=30;a++)
{
datas.push_back(a);
}
}
else if(d==0)
{
for(int a=1;a<=30;a++)
{
datas.push_front(a);
}
}
else cout<<"Data Kosong\n";
while(x<4){
cout<<"\n\nMenu :\n";
cout<<"\t0. Check Data\n";
cout<<"\t1. SearchByBoolean\n";
cout<<"\t2. SearchByAddress\n";
cout<<"\t3. Reset Data\n";
cout<<"\t4. Exit\n";
cout<<"Masukan Pilihan :";
cin>>x;

switch(x)
{
case 0: {system("cls");
cout<<"Data : ";

for(poin=datas.begin();poin!=datas.end();poin++)
{
cout<<(*poin)<<" ";
}
}break;
case 1: {cout<<"Cari Angka :";
cin>>cari;
if(SearchByBoolean(datas,cari)==true)
{
cout<<"Angka : "<<(*poin)<<"
Ditemukan\n\n";
}
else
{
cout<<"Angka : "<<cari<<" Tidak
Ditemukan\n\n";
}
system("pause");
system("cls");
}break;
case 2: {cout<<"Cari Alamat dari Angka :";
cin>>cari;
if(*SearchByAddress(datas,cari)<=30)
{
cout<<"Angka :
"<<*SearchByAddress(datas,cari)<<" Ditemukan\n\n";
}
else
{
cout<<"Angka : "<<cari<<" Tidak
Ditemukan di Alamat manapun~\n\n";
}
system("pause");
system("cls");
}break;
case 3: for(int b=1;b<=30;b++)
{datas.pop_front();
}
goto reset; break;
}
}
return 0;
}
TUGAS6.2

header6.h

#ifndef HEADER6_H
#define HEADER6_H

#include <iostream>

using namespace std;

static bool found;

bool SearchByBoolean(int data[],int find);


int SearchByAddress(int data[],int find);

static int datas[30];


static int cari;

#endif

header6.cpp

#include "header6.h"

bool SearchByBoolean(int data[],int find)


{
int a=1;
found=false;
while(found==false && a<=30)
{
if(data[a]==find)
{
found=true;
}
else a++;
}
return found;
}

int SearchByAddress(int data[],int find)


{
int a;
for(a=1;a<=30;a++)
{
if(data[a]==find)
{
break;
}
}
return a;
}

Tugas6_2.cpp

#include "header6.h"
#include <iostream>
#include <cstdlib>
using namespace std;

int main()
{
int d;
int x=0;
reset:
cout<<"Data Pilihan(1=Maju , 0=Mundur) : ";
cin>>d;
system("cls");
if(d==1)
{
for(int a=1;a<=30;a++)
{
datas[a]=a;
}
}
else if(d==0)
{
for(int a=30;a>=1;a--)
{
datas[30-a+1]=a;
}
}
else cout<<"Data Kosong\n";
while(x<4){
cout<<"\n\nMenu :\n";
cout<<"\t0. Check Data\n";
cout<<"\t1. SearchByBoolean\n";
cout<<"\t2. SearchByAddress\n";
cout<<"\t3. Reset Data\n";
cout<<"\t4. Exit\n";
cout<<"Masukan Pilihan :";
cin>>x;
switch(x)
{
case 0: {system("cls");
cout<<"Data : ";
for(int b=1;b<=30;b++)
{
cout<<datas[b]<<" ";
}
}break;
case 1: {cout<<"Cari Angka :";
cin>>cari;
if(SearchByBoolean(datas,cari)==true)
{
cout<<"Angka : "<<cari<<"
Ditemukan\n\n";
}
else
{
cout<<"Angka : "<<cari<<" Tidak
Ditemukan\n\n";
}
system("pause");
system("cls");
}break;
case 2: {cout<<"Cari Alamat dari Angka :";
cin>>cari;
if(SearchByAddress(datas,cari)<=30)
{
cout<<"Angka : "<<cari<<" Ditemukan di
Alamat ke-"<<SearchByAddress(datas,cari)<<"\n\n";
}
else
{
cout<<"Angka : "<<cari<<" Tidak
Ditemukan di Alamat manapun~\n\n";
}
system("pause");
system("cls");
}break;
case 3: goto reset; break;
}
}
return 0;
}