Menu:
1.Input di awal.
2.Input di akhir.
3.Hapus Di Awal
4.Hapus Di Akhir
5.Jumlah.
6.rata-rata.
7.Tampilkan Semua Data
Keterangan
Daftar Pulsa
Daftar Pulsa
Telkomsel
PULSA -----------------> HARGA
5.000 -----------------> 6.000
10.000 ----------------> 11.000
20.000 ----------------> 21.000
25.000 ----------------> 26.000
50.000-----------------> 50.000
100.000----------------> 100.000
Indosat
PULSA -----------------> HARGA
2.000 ------------------>3.000
5.000 ----------------> 6.000
10.000 ----------------> 11.000
25.000 ----------------> 26.000
50.000-----------------> 50.000
100.000----------------> 100.000
Source:
#include <iostream>
#include <string.h>
using namespace std;
struct data{
char nama[20];
char kode[5];
int harga;
data* next;
};
typedef data* pointer;
typedef pointer List;
void createElement(pointer& pBaru){
pBaru=new data;
cout<<"masukkan data"<<endl;
cout<<"nama = ";cin>>pBaru->nama;
cout<<"kode = ";cin>>pBaru->kode;
if(strcmp(pBaru->kode,"TK5")==0)
pBaru->harga=6000;
else if(strcmp(pBaru->kode,"TK10")==0)
pBaru->harga=11000;
else if(strcmp(pBaru->kode,"TK20")==0)
pBaru->harga=21000;
else if(strcmp(pBaru->kode,"TK25")==0)
pBaru->harga=26000;
else if(strcmp(pBaru->kode,"TK50")==0)
pBaru->harga=50000;
else if(strcmp(pBaru->kode,"TK100")==0)
pBaru->harga=100000;
else if(strcmp(pBaru->kode,"IS2")==0)
pBaru->harga=3000;
else if(strcmp(pBaru->kode,"IS5")==0)
pBaru->harga=6000;
else if(strcmp(pBaru->kode,"IS10")==0)
pBaru->harga=11000;
else if(strcmp(pBaru->kode,"IS25")==0)
pBaru->harga=26000;
else if(strcmp(pBaru->kode,"IS50")==0)
pBaru->harga=50000;
else if(strcmp(pBaru->kode,"IS100")==0)
pBaru->harga=100000;
else
pBaru->harga=0;
pBaru->next=NULL;
}
void insertFirst(List& First, pointer pBaru){
if(First==NULL)
First=pBaru;
else{
pBaru->next=First;
First=pBaru;
}
}
void insertLast(List& First, pointer pBaru){
pointer Last;
if(First==NULL)
First=pBaru;
else{
Last=First;
while(Last->next!=NULL){
Last=Last->next;
}
Last->next=pBaru;
}
}
void DeleteLast(List& First,pointer& pHapus){
pointer Last,PrecLast;
if(First==NULL)
{
pHapus = NULL;
cout<<"List Kosong"<<endl;
}
else if(First->next==NULL){
pHapus=First;
First=NULL;
}
else{
Last = First->next;
PrecLast = First;
while(Last->next!=NULL){
PrecLast = Last;
Last = Last -> next;
}
pHapus = Last;
PrecLast->next=NULL;
}
}
void DeleteFirst(List& First,pointer& pHapus){
if(First==NULL)
{
pHapus = NULL;
cout<<"List Kosong"<<endl;
}
else if(First->next==NULL){
pHapus=First;
First=NULL;
}
else{
pHapus=First;
First=First->next;
pHapus->next=NULL;
}
}
void traversal(List First){
pointer pBantu;
int i=1;
pBantu=First;
cout<<"No.\tNama\t\tKode\tHarga\n";
while(pBantu!=NULL){
cout<<i<<"\t"<<pBantu->nama<<"\t\t"<<pBantu->kode<<"\tRp "<<pBantu->harga<<endl;
pBantu=pBantu->next;
i++;
}
}
void jumlah2(List First, int& jumlah, int& i){
pointer pBantu;
pBantu=First;
i=0;
jumlah=0;
while(pBantu!=NULL){
jumlah+=pBantu->harga;
pBantu=pBantu->next;
i++;
}
}
int main(){
List First=0;
pointer p,pHapus;
int pilih,jumlah,i;
char pilih2;
do{
system("cls");
cout<<"Program Penghitung Harga Pulsa"<<endl;
cout<<"Menu : "<<endl<<"1. Insert First"<<endl<<"2. Insert Last"<<endl<<"3. Delete First"<<endl<<"4. Delete Last"<<endl<<"5. Jumlah"<<endl<<"6. Rata rata"<<endl<<"7. Tampilkan"<<endl<<"Pilih 1-7:";
cout<<"pilihan(1-7):";cin>>pilih;
switch(pilih){
case 1:
{
createElement(p);
insertFirst(First,p);
break;
}
case 2:
{
createElement(p);
insertLast(First,p);
break;
}
case 3:
{
DeleteFirst(First,pHapus);
traversal(First);
break;
}
case 4:
{
DeleteLast(First,pHapus);
traversal(First);
break;
}
case 5:
{
jumlah2(First,jumlah,i);
cout<<"Jumlah = Rp "<<jumlah;
break;
}
case 6:
{
jumlah2(First,jumlah,i);
cout<<"Rata-rata = Rp "<<jumlah/i;
break;
}
case 7:
{
traversal(First);
break;
}
}
cout<<endl<<"Input Lagi?";cin>>pilih2;
}while(pilih2=='y'||pilih2=='Y');
return 0;
}
Tidak ada komentar:
Posting Komentar