Program Kompetisi Sepakbola Menggunakan C++
Code :
main.cpp
#include <iostream>
#include "menuawal.h"
using namespace std;
int main(){
int pilih;
Menu menu;
while(1){
system("cls");
menu.showMenu();
cout << "\tMasukkan Pilihan : ";
cin >> pilih;
menu.menuInput(pilih);
if(pilih == 7){
return 0;
}
system("pause");
}
return 0;
}
menuawal.h
#include "kompetisi.h"
using namespace std;
class Menu{
public:
Kompetisi laLiga;
void showMenu(){
cout << "\t==========================================================================================\n";
cout << "\t|>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> PROGRAM KOMPETISI <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<|\n";
cout << "\t------------------------------------------------------------------------------------------\n";
cout << "\t| 1. Input Data Klub |\n";
cout << "\t| 2. Edit Data Klub |\n";
cout << "\t| 3. Cari Data Klub |\n";
cout << "\t| 4. Delete Data Klub |\n";
cout << "\t| 5. Tampilkan Klasemen |\n";
cout << "\t| 6. Urutkan Klasemen |\n";
cout << "\t| 7. Keluar Program |\n";
cout << "\t==========================================================================================\n";
}
int menuInput(int pilih){
Kompetisi laLiga;
switch(pilih){
case 1:
this->menuTambah();
break;
case 2:
this->menuEdit();
break;
case 3:
this->menuSearch();
break;
case 4:
this->menuDelete();
break;
case 5:
this->menuTampil();
break;
case 6:
this->menuSorting();
break;
case 7:
cout << "\tProgram selesai, Terima Kasih telah Menggunakan Program Kami :)\n";
break;
case '\0':
cout << "\tTidak menerima inputan selain nomor\n\n";
cin.clear();
cin.ignore(1, '\n');
break;
default:
cout << "\n\tPilihan Anda Tidak tersedia." << endl;
break;
}
}
void menuTambah(){
cout << "\t==========================================================================================\n";
cout << "\t|>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Menu Tambah <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<|\n";
cout << "\t------------------------------------------------------------------------------------------\n";
laLiga.simpanData();
}
void menuEdit(){
//Kamus Lokal
int dataUpdate;
cout << "\t==========================================================================================\n";
cout << "\t|>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Menu Edit <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<|\n";
cout << "\t------------------------------------------------------------------------------------------\n";
cout<<"\tMasukan ID dari tim yang akan dirubah : ";
cin>>dataUpdate;
laLiga.updateData(dataUpdate);
}
void menuSearch(){
//Kamus Lokal
string namaCari;
cout << "\t==========================================================================================\n";
cout << "\t|>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Menu Cari <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<|\n";
cout << "\t------------------------------------------------------------------------------------------\n";
cout<<"Masukan nama dari tim yang akan dicari : ";
cin>>namaCari;
laLiga.cariData(namaCari);
}
void menuTampil(){
cout << "\t==========================================================================================\n";
cout << "\t|>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Klasemen <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<|\n";
cout << "\t------------------------------------------------------------------------------------------\n";
laLiga.bacaData();
}
void menuSorting(){
cout << "\t==========================================================================================\n";
cout << "\t|>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Klasemen Sorting <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<|\n";
cout << "\t------------------------------------------------------------------------------------------\n";
laLiga.sortingKlasemen();
}
void menuDelete(){
//Kamus Lokal
int dataDelete;
cout << "\t==========================================================================================\n";
cout << "\t|>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Menu Delete <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<|\n";
cout << "\t------------------------------------------------------------------------------------------\n";
cout<<"\tMasukan ID dari tim yang akan dihapus : ";
cin>>dataDelete;
laLiga.deleteData(dataDelete);
}
};
kompetisi.h
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <string>
#include <iomanip>
#include <locale>
using namespace std;
class Kompetisi {
public:
string namaTim;
int id,menang, seri, kalah;
int gol, kebobolan;
void inputData(){
cout<<"\tId Tim : ";
cin>>this->id;
cout<<"\tNama Tim : ";
cin>>this->namaTim;
cout<<"\tJumlah menang : ";
cin>>this->menang;
cout<<"\tJumlah seri : ";
cin>>this->seri;
cout<<"\tJumlah kalah : ";
cin>>this->kalah;
cout<<"\tJumlah Kebobolan : ";
cin>>this->kebobolan;
cout<<"\tJumlah Gol : ";
cin>>this->gol;
}
char namaFile[30] = "kompetisi.dat" ;
void simpanData() {
Kompetisi laLiga;
laLiga.inputData();
//proses simpan objek ke file
ofstream f;
f.open(namaFile, ios::binary|ios::app);
f.write((char*) &laLiga, sizeof(laLiga));
f.close();
}
void bacaData(){
Kompetisi laLiga;
ifstream f;
f.open( namaFile, ios::binary );
cout << "+------+---------------------------+----------+----------+----------+----------+----------+-------------+----------+\n";
cout << "| ID | Nama Tim | Menang | Seri | Kalah | Gol | Kegolan | Selisih Gol | Point |\n";
cout << "+------+---------------------------+----------+----------+----------+----------+----------+-------------+----------+\n";
while (f.read((char*)&laLiga, sizeof(laLiga))) {
laLiga.outputData();
}
cout << "+------+---------------------------+----------+----------+----------+----------+----------+-------------+----------+\n";
f.close();
}
void outputData(){
cout << "| " << setiosflags(ios::left) << setw(4) << this->id << " | ";
cout << setiosflags(ios::left) << setw(25) << this->namaTim << " | ";
cout << setiosflags(ios::left) << setw(8) << this->menang << " | ";
cout << setiosflags(ios::left) << setw(8) << this->seri << " | ";
cout << setiosflags(ios::left) << setw(8) << this->kalah << " | ";
cout << setiosflags(ios::left) << setw(8) << this->gol << " | ";
cout << setiosflags(ios::left) << setw(8) << this->kebobolan << " | ";
cout << setiosflags(ios::left) << setw(11) << this->selisihGol(this->gol,this->kebobolan) << " | ";
cout << setiosflags(ios::left) << setw(8) << this->point(this->menang,this->seri,this->kalah) << " |\n";
}
int point(int menangP, int seriP, int kalahP){
return (menangP*3)+(seriP*1)+(kalahP*0);
}
int selisihGol(int golP, int kebobolanP){
return golP-kebobolanP;
}
void sortingKlasemen(){
Kompetisi laLiga;
string temp;
vector<Kompetisi> myvector;
ifstream f (namaFile,ifstream::in);
int n=0;
while (f.read((char*)&laLiga, sizeof(laLiga))) {
myvector.push_back(laLiga);
n = n + 1;
}
int i, j;
for (i = 1; i < n; i++){
int keyPoint = myvector[i].point(myvector[i].menang,myvector[i].seri,myvector[i].kalah);
int keyGol = myvector[i].selisihGol(myvector[i].gol,myvector[i].kebobolan);
Kompetisi keyObject = myvector[i];
j = i - 1;
while (j >= 0 && ( myvector[j].point(myvector[j].menang,myvector[j].seri,myvector[j].kalah) < keyPoint || ( myvector[j].point(myvector[j].menang,myvector[j].seri,myvector[j].kalah) == keyPoint && myvector[j].selisihGol(myvector[j].gol,myvector[j].kebobolan) < keyGol)) ){
myvector[j + 1] = myvector[j];
j = j - 1;
}
myvector[j + 1] = keyObject;
}
//Tampilkan Tabel
cout << "+------+---------------------------+----------+----------+----------+----------+----------+-------------+----------+\n";
cout << "| ID | Nama Tim | Menang | Seri | Kalah | Gol | Kegolan | Selisih Gol | Point |\n";
cout << "+------+---------------------------+----------+----------+----------+----------+----------+-------------+----------+\n";
for (i = 0; i < n; i++){
cout << "| " << setiosflags(ios::left) << setw(4) << myvector[i].id << " | ";
cout << setiosflags(ios::left) << setw(25) << myvector[i].namaTim << " | ";
cout << setiosflags(ios::left) << setw(8) << myvector[i].menang << " | ";
cout << setiosflags(ios::left) << setw(8) << myvector[i].seri << " | ";
cout << setiosflags(ios::left) << setw(8) << myvector[i].kalah << " | ";
cout << setiosflags(ios::left) << setw(8) << myvector[i].gol << " | ";
cout << setiosflags(ios::left) << setw(8) << myvector[i].kebobolan << " | ";
cout << setiosflags(ios::left) << setw(11) << myvector[i].selisihGol(myvector[i].gol,myvector[i].kebobolan) << " | ";
cout << setiosflags(ios::left) << setw(8) << myvector[i].point(myvector[i].menang,myvector[i].seri,myvector[i].kalah) << " |\n";
}
cout << "+------+---------------------------+----------+----------+----------+----------+----------+-------------+----------+\n";
f.close();
}
void updateData(int idTim){
Kompetisi laLiga;
fstream f;
f.open(namaFile, ios::in|ios::out);
cout << "+------+---------------------------+----------+----------+----------+----------+----------+-------------+----------+\n";
cout << "| ID | Nama Tim | Menang | Seri | Kalah | Gol | Kegolan | Selisih Gol | Point |\n";
cout << "+------+---------------------------+----------+----------+----------+----------+----------+-------------+----------+\n";
while (f.read((char*) &laLiga, sizeof(laLiga))) {
if(laLiga.id == idTim){
laLiga.outputData();
cout << "+------+---------------------------+----------+----------+----------+----------+----------+-------------+----------+\n";
//input data baru
cout << "Silahkan masukkan data baru" << endl;
laLiga.inputData();
int pos = -1 * sizeof(laLiga);
f.seekp(pos, ios::cur);
f.write ( (char*) &laLiga, sizeof(laLiga));
break;
}
}
f.close();
}
void cariData(string namaDicari){
Kompetisi laLiga, dataKetemu;
int ketemu = 0;
ifstream f;
f.open(namaFile, ios::binary);
while(f.read((char*)&laLiga, sizeof(laLiga))){
if(laLiga.namaTim == namaDicari){
dataKetemu = laLiga;
ketemu = 1;
break;
}
}
if (ketemu == 0){
cout << "\nTim " << namaDicari << " tidak ditemukan" << endl;
}else {
cout << "\nTim " << namaDicari << " ditemukan" << endl << endl;
cout << "+------+---------------------------+----------+----------+----------+----------+----------+-------------+----------+\n";
cout << "| ID | Nama Tim | Menang | Seri | Kalah | Gol | Kegolan | Selisih Gol | Point |\n";
cout << "+------+---------------------------+----------+----------+----------+----------+----------+-------------+----------+\n";
dataKetemu.outputData();
cout << "+------+---------------------------+----------+----------+----------+----------+----------+-------------+----------+\n";
}
f.close();
}
void deleteData(int idTim){
Kompetisi laLiga;
ifstream fi;
fi.open(namaFile, ios::binary);
ofstream fo;
fo.open("tmp.dat", ios::out|ios::binary);
int ketemu = 0;
while(fi.read((char*) &laLiga, sizeof(laLiga))) {
if(laLiga.id != idTim){
fo.write((char*) &laLiga, sizeof(laLiga));
}else {
ketemu = 1;
}
}
fi.close();
fo.close();
remove(namaFile);
rename("tmp.dat", namaFile);
if (ketemu == 1){
cout << "Data dengan id " << idTim << " telah dihapus!" << endl;
}else {
cout << "Data dengan id " << idTim << " Tidak ditemukan" << endl;
}
}
};
Komentar
Posting Komentar