modul_02

download modul_02

of 16

description

modul pengerjaan dasar pemograman simulasi fisika

Transcript of modul_02

  • P02 | Menulis dan Membaca Berkas Teks

    Modul Praktikum FI2283 Pemrograman dan Simulasi Fisika

    Versi 2013.09.09

    Catatan. Perhatikan berkas (atau file) apa yang harus anda buat dalam setiaptugas. Setiap kali praktikum buat folder baru dengan nama YYYYMMDD-NNdi mana YYYY adalah tahun, MM bulan, DD hari, dan NN nomor praktikum.Untuk hari Senin, 16 September 2013 yang merupakan praktikum minggu ke-dua, setiap pengguna menyimpan berkas-berkasnya dalam folder 20130916-02 dibawah folder Documents (untuk ini tanyakan jelasnya pada asisten praktikumAnda). Bila berkas Anda tidak ditemukan maka nilai praktikum hanya dariangka kehadirannya saja.

    1 Input dan output pada konsol 12

    2 Piping pada program yang menggunakan cin 13

    3 Menulis ke dalam berkas teks 13

    4 Menulis berkas teks dengan argumen program 14

    5 Menuliskan banyak kata tanpa dapat diedit 16

    6 Menuliskan pasangan data x dan y 17

    7 Membaca berkas teks 19

    8 Membaca berkas teks pasangan data x dan y 20

    9 Membaca dan menulis berkas data x dan y 22

    10 Perkenalan dengan gnuplot 24

    11

  • 1 Input dan output pada konsol

    Ketiklah program berikut ini

    /*

    console-io.cpp

    20130907.01

    Sparisoma Viridi

    Input dan output (I/O) in the console

    Compile: g++ console-io.cpp -o console-io

    Run: ./console-io

    */

    #include

    #include

    using namespace std;

    int main(int argc, char *argv[]) {

    char comp[] = "COMP: ";

    string line;

    int NIM;

    cout

  • cout
  • writehello.cpp

    Nama Anda

    yyyy.mm.dd.versi (misalnya: 2013.09.07.1)

    Fungsi program (misalnya: Menuliskan kalimat "Hello

    world!" ke dalam suatu file teks

    Compile: g++ writehello.cpp -o writehello

    Run: ./writehello

    */

    #include

    #include

    using namespace std;

    int main(int argc, char *argv[]) {

    // Filename

    char filename[] = "hello.txt";

    // Output file stream

    ofstream fout;

    // Open an output file stream with certain filename

    fout.open(filename);

    // Write to the output file stream

    fout

  • writehelloarg.cpp

    Sparisoma Viridi

    2013.09.07.1

    Menuliskan kalimat "Hello world!" ke dalam suatu file

    teks di mana nama keluaran diberikan melalui argumen

    program

    Compile: g++ writehelloarg.cpp -o writehelloarg

    Run: ./writehello

    */

    #include

    #include

    #include

    using namespace std;

    int main(int argc, char *argv[]) {

    // Default Filename

    char filename[] = "hello.txt";

    // Change default name if output filename is provided

    if(argc > 1) {

    strcpy(filename, argv[1]);

    }

    // Output file stream

    ofstream fout;

    // Open an output file stream with certain filename

    fout.open(filename);

    // Write to the output file stream

    fout

  • dan

    ./writehelloarg hello2.txt

    5 Menuliskan banyak kata tanpa dapat diedit

    Salin, kompilasi, dan jalankan kode berikut ini seperti petunjuk dalam keteran-gan programnya.

    /*

    writewords.cpp

    20130907.01

    Sparisoma Viridi

    Write some words for some lines, terminate the program

    by providing x charater then press enter

    Compile: g++ writewords.cpp -o writewords

    Run: ./writewords

    */

    #include

    #include

    #include

    #include

    using namespace std;

    int main(int argc, char *argv[]) {

    bool NO_FILENAME = true;

    bool EXIT = false;

    string filename;

    string word;

    string line;

    cout > filename;

    ofstream fout;

    fout.open(filename.c_str());

    cout

  • bool FIRST_TIME = true;

    while(!EXIT) {

    char ch = getchar();

    if(ch == x) {

    EXIT = true;

    } else {

    // Avoid extra blank line in output file

    if(!FIRST_TIME) {

    fout

  • writexy.cpp

    20130908.01

    Sparisoma Viridi

    Write pains of data x and y

    Compile: g++ writexy.cpp -o writexy

    Run: ./writexy [filename]

    */

    #include

    #include

    #include

    #include

    using namespace std;

    int main(int argc, char *argv[]) {

    // Default output filename

    char ofn[] = "data-xy.txt";

    if(argc > 1) {

    // Change output filename if new name provided

    strcpy(ofn, argv[1]);

    }

    // Output file stream

    ofstream fout;

    fout.open(ofn);

    if(fout.is_open()) {

    // Header for readability

    fout

  • // Terminate the program

    return 0;

    }

    Tugas 6. Tuliskan hubungan antara x dan y dalam bentuk y = f(x) dalamberkas laporan.txt. Tuliskan pula nilai dari f(10), f(5), serta f(7). Perik-salah apakah isi dari berkas yang dihasilkan oleh perintah writexy sama sepertiyang dicantumkan dalam kode program? Jelaskan jawaban Anda dalam berkaslaporan.txt.

    7 Membaca berkas teks

    Salin, kompilasi, dan jalankan kode berikut ini seperti petunjuk dalam keteran-gan programnya.

    /*

    readfile.cpp

    20130908.01

    Sparisoma Viridi

    Read a common text file

    Compile: g++ readfile.cpp -o readfile

    Run: ./readfile filename

    */

    #include

    #include

    #include

    #include

    using namespace std;

    int main(int argc, char *argv[]) {

    if(argc < 2) {

    cout

  • fin.close();

    } else {

    cout

  • #include

    #include

    using namespace std;

    int main(int argc, char *argv[]) {

    // Reserve array

    // Next time you should be able to define number of data

    // before reserve the array size

    int N = 11;

    double x[N];

    double y[N];

    if(argc < 2) {

    cout y[i];

    i++;

    }

    }

    fin.close();

    } else {

    cout

  • }}

    return 0;

    }

    Tugas 8. Jalankan perintah readxy dengan cara

    ./readxy data-xy.txt

    lalu laporan hasilnya dalam berkas laporan.txt. Apa perbedaan programreadxy.cpp dengan readfile.cpp? Jelaskan pula dalam laporan.txt.

    9 Membaca dan menulis berkas data x dan y

    Salin, kompilasi, dan jalankan kode berikut ini seperti petunjuk dalam keteran-gan programnya.

    /*

    absxy.cpp

    20130908.01

    Sparisoma Viridi

    Read a common text file for data pairs x and y

    and calculate its absolute value and write the

    results into other file

    Compile: g++ absxy.cpp -o absxy

    Run: ./absxy input output

    */

    #include

    #include

    #include

    #include

    #include

    using namespace std;

    int main(int argc, char *argv[]) {

    // Reserve array

    // Next time you should be able to define number of data

    // before reserve the array size

    int N = 11;

    double x[N];

    22

  • double y[N];

    if(argc < 3) {

    cout y[i];

    i++;

    }

    }

    fin.close();

    } else {

    cout

  • }fout.close();

    } else {

    cout

  • Keluaran praktikum

    1. console-io.cpp, writehello.cpp, hello.txt, hello2.txt, mywords.txt,writexy.cpp, data-xy.txt, readfile.cpp, readxy.cpp, absxy.cpp.

    2. laporan.txt yang berisikan jawaban dari Tugas 1, 2, 3, 4, 5, 6, 7, 8, 9,10.

    This work is licensed under a Creative Commons Attribution-ShareAlike 3.0Unported License. 1

    1Sparisoma Viridi , .., 2013

    25

  • .26