contoh program file eksternal
by: @taufik.id
/*
Nama : TAUFIK AGUNG SANTOSO
NIM : 14116007
KELAS : IF C
SUBJECT : PTIB-C911
*/
#include <iostream>
#include <fstream>
using namespace std;
int main () {
int MA[5][5]; int NBrs1=5, NKol1=5;
int MB[5][5]; int NBrs2=5, NKol2=5;
int MC[5][5]; int NBrsC, NKolC;
int i, j, k,sum, rata;
ofstream out_stream;
ofstream rata_rata;
cout<<"\tMatrik dimensi 5x5\n\n";
//Memasukkan Matrik A
cout<<"\nMasukkan data Matrik A\n";
for(i=0; i<NBrs1; i++){
for (k=0; k<NKol1; k++){
cout<<"Matriks A ["<<i+1<<"]["<<k+1<<"] = ";
cin>>MA[i][k];
}
cout<<endl;
}
//Memasukkan Matrik B
cout<<"\nMasukkan data Matrik B\n";
for(i=0; i<NBrs2; i++){
for (k=0; k<NKol2; k++){
cout<<"Matriks B ["<<i+1<<"]["<<k+1<<"] = ";
cin>>MB[i][k];
}
cout<<endl;
}
//Proses perhitungan PERKALIAN
for(i=0; i<NBrs1; i++){
for (k=0;k<NKol2; k++){
MC[i][k] = 0;
for (j=0; j<NKol1; j++){
MC [i][k] = MA[i][j] * (MB[k][j]+15);
}
}
}
cout<<endl;
//Memampilkan hasil Perkalian Matrik
cout<<"Hasil Perkalian Matrik adalah : ";
for(i=0; i<NBrs1; i++){
cout<<endl;
for (k=0;k<NKol2; k++){
cout<<MC[i][k]<<"\t";
}
}
cout<<endl;
cout<<endl;
//Menyimpan hasil perkalian matriks ke external data
out_stream.open("data.dat");
for(i=0;i<NBrs1; i++){
for (k=0;k<NKol2; k++){
out_stream<<MC[i][k]<<"\t";
}
out_stream<<endl;
}
//Menghitung rata-rata seluruh elemen C
cout<<"Rata - rata seluruh elemen matriks C";
sum = 0;
for (i=0; i<NBrs1; i++){
for (k=0; k<NKol2; k++){
sum = sum + MC[i][k];
}
}
cout<<"Jumlah : "<<sum<<endl;
rata = sum/25;
cout<<"Rata-rata : "<<rata<<endl;
cout<<endl;
cout<<endl;
//Menyimpan nilai rata rata matriks ke external data
rata_rata.open("rata.dat");
for(i=0;i<NBrs1; i++){
for (k=0;k<NKol2; k++){
}
out_stream<<endl;
}
rata_rata<<rata;
//Note
cout<<"Note : "<<endl;
cout<<"Hasil perkalian matrik disimpan dalam 'data.dat'"<<endl;
cout<<"Hasil perkalian matrik disimpan dalam 'rata.dat'"<<endl;
out_stream.close();
rata_rata.close();
return 0;
}
Nama : TAUFIK AGUNG SANTOSO
NIM : 14116007
KELAS : IF C
SUBJECT : PTIB-C911
*/
#include <iostream>
#include <fstream>
using namespace std;
int main () {
int MA[5][5]; int NBrs1=5, NKol1=5;
int MB[5][5]; int NBrs2=5, NKol2=5;
int MC[5][5]; int NBrsC, NKolC;
int i, j, k,sum, rata;
ofstream out_stream;
ofstream rata_rata;
cout<<"\tMatrik dimensi 5x5\n\n";
//Memasukkan Matrik A
cout<<"\nMasukkan data Matrik A\n";
for(i=0; i<NBrs1; i++){
for (k=0; k<NKol1; k++){
cout<<"Matriks A ["<<i+1<<"]["<<k+1<<"] = ";
cin>>MA[i][k];
}
cout<<endl;
}
//Memasukkan Matrik B
cout<<"\nMasukkan data Matrik B\n";
for(i=0; i<NBrs2; i++){
for (k=0; k<NKol2; k++){
cout<<"Matriks B ["<<i+1<<"]["<<k+1<<"] = ";
cin>>MB[i][k];
}
cout<<endl;
}
//Proses perhitungan PERKALIAN
for(i=0; i<NBrs1; i++){
for (k=0;k<NKol2; k++){
MC[i][k] = 0;
for (j=0; j<NKol1; j++){
MC [i][k] = MA[i][j] * (MB[k][j]+15);
}
}
}
cout<<endl;
//Memampilkan hasil Perkalian Matrik
cout<<"Hasil Perkalian Matrik adalah : ";
for(i=0; i<NBrs1; i++){
cout<<endl;
for (k=0;k<NKol2; k++){
cout<<MC[i][k]<<"\t";
}
}
cout<<endl;
cout<<endl;
//Menyimpan hasil perkalian matriks ke external data
out_stream.open("data.dat");
for(i=0;i<NBrs1; i++){
for (k=0;k<NKol2; k++){
out_stream<<MC[i][k]<<"\t";
}
out_stream<<endl;
}
//Menghitung rata-rata seluruh elemen C
cout<<"Rata - rata seluruh elemen matriks C";
sum = 0;
for (i=0; i<NBrs1; i++){
for (k=0; k<NKol2; k++){
sum = sum + MC[i][k];
}
}
cout<<"Jumlah : "<<sum<<endl;
rata = sum/25;
cout<<"Rata-rata : "<<rata<<endl;
cout<<endl;
cout<<endl;
//Menyimpan nilai rata rata matriks ke external data
rata_rata.open("rata.dat");
for(i=0;i<NBrs1; i++){
for (k=0;k<NKol2; k++){
}
out_stream<<endl;
}
rata_rata<<rata;
//Note
cout<<"Note : "<<endl;
cout<<"Hasil perkalian matrik disimpan dalam 'data.dat'"<<endl;
cout<<"Hasil perkalian matrik disimpan dalam 'rata.dat'"<<endl;
out_stream.close();
rata_rata.close();
return 0;
}
Untuk Script yang kompleks bisa copas and ATM kode ini... :D
#include <iostream>
#include <fstream>
using namespace std;
//DEKLARASI SUBPROGRAM
void PilihMode() ;
void IsiBacaSimpanMatriks() ;
void BacaFile() ;
void TutupProgram() ;
void UlangInputMode() ;
//PROGRAM UTAMA
int main() {
//Kamus Lokal
char ulang ;
//Algoritma
PilihMode();
cout << "Apakah Anda ingin mencoba mode lagi? (y/n) : " ;
cin >> ulang ;
if (ulang=='y' || ulang=='Y') {
main();
}
else {
cout << "Terimakasih Anda telah menggunakan layanan kami." << endl << "Created by AjickCyber." << endl ;
}
return 0;
}
//REALISASI SUBPROGRAM
//Prosedur PilihMode
void PilihMode(){
//Kamus Global
int mode ;
//Algoritma Prosedur
cout << "Pilih mode yang diinginkan :" << endl ;
cout << "1. Isi, baca, dan simpan." << endl ;
cout << "2. Baca file." << endl ;
cout << "3. Tutup program" << endl ;
cout << "Mode pilihan : " << endl ;
cin >> mode ;
//Analisis kasus dg Switch
switch (mode){
case 1 :
cout << "Mode 1 : " << endl ;
IsiBacaSimpanMatriks();
break;
case 2 :
cout << "Mode 2 : " << endl ;
BacaFile();
break;
case 3 :
cout << "Mode 3 :" << endl ;
TutupProgram();
break ;
default :
UlangInputMode();
break ;
}
}
//Prosedur IsiBacaSimpanMatriks
void IsiBacaSimpanMatriks(){
int i, j ;
char simpan ;
int Matriks[5][5];
ofstream File;
//Algoritma
// Mengisi matriks
cout << "1. Mangisi data ke dalam matriks dengan ordo 5 x 5." << endl ;
cout << "Masukkan data ke : " << endl ;
for(i=0;i<5;i++) {
for(j=0;j<5;j++){
cout << "Matriks dengan indeks " << i << " , " << j << " = " ;
cin >> Matriks[i][j] ;
}
cout << endl ;
}
// Membaca Matriks
cout << "2. Membaca isi matriks dengan ordo 5 x 5 " << endl ;
for(i=0;i<5;i++) {
for(j=0;j<5;j++){
cout << Matriks[i][j] << "\t" ;
}
cout << endl << endl ;
}
//Menyimpan Data Matrikas
cout << "2. Menyimpan data dari matriks ordo 5 x 5 ke dalam file eksternal " << endl ;
cout << "Apakah Anda ingin menyimpan data yang ada didalam matriks ke file eksterbal ? (y/n) : " << endl;
cin >> simpan ;
if(simpan=='y' || simpan=='Y'){
cout << endl << "File sudah tersimpan di FileMatriks.txt" << endl;
File.open("FileMatriks.txt");
for(i=0;i<5;i++) {
for(j=0;j<5;j++){
File << Matriks[i][j] << "\t" ;
}
File << endl << endl;
}
File.close(); }
else { main(); }
}
//Prosedur BacaFile
void BacaFile(){
//Kamus
ifstream file ;
int isi, sum=0 ;
//Algoritma
cout << "Menampilkan data yang ada didalam file FileMatriks.txt" << endl ;
cout << "Data yang ada didalam file adalah : " << endl ;
file.open("FileMatriks.txt") ;
while(file >> isi ){
cout << isi << "\t" ;
sum++;
if(sum==5){cout << endl << endl ;}
if(sum==10){cout << endl << endl;}
if(sum==15){cout << endl << endl;}
if(sum==20){cout << endl << endl;}
if(sum==25){cout << endl << endl;}
}
}
//Prosedur TutupProgram
void TutupProgram(){
char Close;
cout << "Apakah Anda yakin ingin menutup program ? (y/n) : " ;
cin >> Close;
}
//Prosedur UlangInputMode
void UlangInputMode(){
main();
}
#include <fstream>
using namespace std;
//DEKLARASI SUBPROGRAM
void PilihMode() ;
void IsiBacaSimpanMatriks() ;
void BacaFile() ;
void TutupProgram() ;
void UlangInputMode() ;
//PROGRAM UTAMA
int main() {
//Kamus Lokal
char ulang ;
//Algoritma
PilihMode();
cout << "Apakah Anda ingin mencoba mode lagi? (y/n) : " ;
cin >> ulang ;
if (ulang=='y' || ulang=='Y') {
main();
}
else {
cout << "Terimakasih Anda telah menggunakan layanan kami." << endl << "Created by AjickCyber." << endl ;
}
return 0;
}
//REALISASI SUBPROGRAM
//Prosedur PilihMode
void PilihMode(){
//Kamus Global
int mode ;
//Algoritma Prosedur
cout << "Pilih mode yang diinginkan :" << endl ;
cout << "1. Isi, baca, dan simpan." << endl ;
cout << "2. Baca file." << endl ;
cout << "3. Tutup program" << endl ;
cout << "Mode pilihan : " << endl ;
cin >> mode ;
//Analisis kasus dg Switch
switch (mode){
case 1 :
cout << "Mode 1 : " << endl ;
IsiBacaSimpanMatriks();
break;
case 2 :
cout << "Mode 2 : " << endl ;
BacaFile();
break;
case 3 :
cout << "Mode 3 :" << endl ;
TutupProgram();
break ;
default :
UlangInputMode();
break ;
}
}
//Prosedur IsiBacaSimpanMatriks
void IsiBacaSimpanMatriks(){
int i, j ;
char simpan ;
int Matriks[5][5];
ofstream File;
//Algoritma
// Mengisi matriks
cout << "1. Mangisi data ke dalam matriks dengan ordo 5 x 5." << endl ;
cout << "Masukkan data ke : " << endl ;
for(i=0;i<5;i++) {
for(j=0;j<5;j++){
cout << "Matriks dengan indeks " << i << " , " << j << " = " ;
cin >> Matriks[i][j] ;
}
cout << endl ;
}
// Membaca Matriks
cout << "2. Membaca isi matriks dengan ordo 5 x 5 " << endl ;
for(i=0;i<5;i++) {
for(j=0;j<5;j++){
cout << Matriks[i][j] << "\t" ;
}
cout << endl << endl ;
}
//Menyimpan Data Matrikas
cout << "2. Menyimpan data dari matriks ordo 5 x 5 ke dalam file eksternal " << endl ;
cout << "Apakah Anda ingin menyimpan data yang ada didalam matriks ke file eksterbal ? (y/n) : " << endl;
cin >> simpan ;
if(simpan=='y' || simpan=='Y'){
cout << endl << "File sudah tersimpan di FileMatriks.txt" << endl;
File.open("FileMatriks.txt");
for(i=0;i<5;i++) {
for(j=0;j<5;j++){
File << Matriks[i][j] << "\t" ;
}
File << endl << endl;
}
File.close(); }
else { main(); }
}
//Prosedur BacaFile
void BacaFile(){
//Kamus
ifstream file ;
int isi, sum=0 ;
//Algoritma
cout << "Menampilkan data yang ada didalam file FileMatriks.txt" << endl ;
cout << "Data yang ada didalam file adalah : " << endl ;
file.open("FileMatriks.txt") ;
while(file >> isi ){
cout << isi << "\t" ;
sum++;
if(sum==5){cout << endl << endl ;}
if(sum==10){cout << endl << endl;}
if(sum==15){cout << endl << endl;}
if(sum==20){cout << endl << endl;}
if(sum==25){cout << endl << endl;}
}
}
//Prosedur TutupProgram
void TutupProgram(){
char Close;
cout << "Apakah Anda yakin ingin menutup program ? (y/n) : " ;
cin >> Close;
}
//Prosedur UlangInputMode
void UlangInputMode(){
main();
}
EmoticonEmoticon