Post on 06-Feb-2018
1
Analisis Top Downinput & Output
Runtunan
Pertemuan 3
IF-UTAMA Ver/Rev : 1/0 III - 2
Overview
o Deskripsi
o Tujuan Instruksional
o Referensi
o Overview Library Header
o Analisa Top Down
o Input & Output
o Runtunan
IF-UTAMA Ver/Rev : 1/0 III - 3
Deskripsi
Pada pertemuan ini akan dipelajari mengenaiAnalisa Top Down, input & Output dalam Bahasa
C/C++, serta runtunan
2
IF-UTAMA Ver/Rev : 1/0 III - 4
Tujuan Instruksional
Mahasiswa diharapkan dapat :
o Menjelaskan analisis Top-Down
o Menjelaskan proses input dan output, serta cara
penulisannya dalam program
o Membedakan proses input dan output
o Menjelaskan proses runtunan/sequence
o Menggunakan analisis Top-Down
o Menggunakan proses input dan output
o Menggunakan proses runtunan/sequence
IF-UTAMA Ver/Rev : 1/0 III - 5
Referensi
1. Deitel, H.M. and Deitel, P.J., “C++ How to Program, 2nd Edition”, Prentice Hall, 1994 (Bab 3
dan 12)
2. Deitel, H.M. and Deitel, P.J., “C How to Program,
4nd Edition”, Prentice Hall, 2004 (bab 5,8,9 dan21)
3. Herianto,“Presentasi Pemrograman
Terstruktur.ppt”,2004
Header File
3
IF-UTAMA Ver/Rev : 1/0 III - 7
Header Files
Common Usages:
o Creates an informal module interface (No relation to Java interfaces)
o Provides documentation
o Each module usually has a header file
o Often include the following:� function prototypes� struct and class declarations� typedefs� global variable declarations� Lots of (high-level) comments
o Abstracts code for optimization (less common)
IF-UTAMA Ver/Rev : 1/0 III - 8
Preprocessor Overview
The preprocessor is a lexical macro engine
run before the compiler sees the code
o Performs lexical transforms of text
� It does not understand C/C++� There is no concept of types
o Transforms are based on lexical substitution
� Think Search and Replace
o Preprocess directives (commands) start with ‘#’
� Each directive goes on its own line
� The # must be the first character on a line.
o No semicolons!!!!!!!!!!!!
� Newlines end a preprocessor directive, no a ‘;’.
IF-UTAMA Ver/Rev : 1/0 III - 9
#define
o #define Tag Substitution
� Replaces all following occurrences of Tag with Substitution
� The Substitution may be the empty string� Does not replace Tag if it is inside quotation marks
o #define Tag(x,y,z) Substitution
� Creates a Function-like macro Tag
� The Substitution may refer to the parameters x, y, or z
� Only items of the form Tag(x,y,z) will be replaced (Just plain Tag will be ignored)
#define MAX_SIZE 80
#define GET_FIELD(a,b) a->b
int ar[MAX_SIZE];
GET_FIELD(point, xCoord);
GET_FIELD(“lalala", f03j?);
int ar[80];
point->xCoord
“lalala”->f03j?
4
IF-UTAMA Ver/Rev : 1/0 III - 10
The #if, and its variants
o #if constant-expression
Include the following block of text if constant-expression is non-zero
o #ifdef Tag
Include the following block if Tag is currently defined (via an earlier #define)
o #ifndef Tag
Include the following block if Tag is currently not defined (via an earlier #define)
o #else
Used to delimit the else clause in a preprocessor if statement
o #elif constant-expression
Used to create an else-if clause in a preprocessor if statement
o #endif
Delimits the end of a preprocessor if statement. There should be exactly one of these for each #if, #ifdef, and #ifndef.
IF-UTAMA Ver/Rev : 1/0 III - 11
#if examples
#ifdef INCLUDE_DEBUG_PRINT
# define DPRINT(msg) \
fprintf(stderr, “DBG: %s”, msg);
#else
# define DPRINT(msg)
#endif
#ifdef WIN32
# define open _open
# define close _close
#endif
The DPRINT macro when INCLUDE_DEBUG_PRINT is defined, evaluates to printing code. Otherwise, it evaluates to the empty string.
Windows calls the open and close functions, _open, and _close respectively. This is annoying, so let’s fix it with preprocessor macros.
IF-UTAMA Ver/Rev : 1/0 III - 12
#include
#include essentially copies and pastes the given file into the current line
o There are two forms of #include
o They differ in the order in which directories are search for thegiven file
o The search order is implementation defined
o Here is the general pattern for different compilers:
#include <filename>
�Searches the “include path,” then the current directory
#include “filename”
�Searches the current directory, then the “include path”
5
IF-UTAMA Ver/Rev : 1/0 III - 13
#undef, #error, #warning
o #undef Tag
Removes definition of a Tag. (undoes a #define)
o #error message
Causes compilation to stop at this line with the given error message. Often this is used with #if blocks to indicate an invalid set of #define macros.
o #warning message
Causes the compiler to output an warning at this line with the given message. Can be used as strong reminders that a file needs work.
o #pragma option
Used to pass an option to the compiler. Almost all #pragma commands are compiler-dependent.
IF-UTAMA Ver/Rev : 1/0 III - 14
#pragma, #line
o #line number
Used to change what line number that the compiler thinks it is on.
This is often seen in:
�Computer Generated Code (from like flex, bison, or something)
�Code outputed by the preprocessor
#line to allows programs to make the compiler’s error messages to correspond to the original source file’s line numbers rather than the generated source file’s line numbers.
Analisis Top Down
6
IF-UTAMA Ver/Rev : 1/0 III - 16
Masalahutama
Sub Masalah A Sub Masalah B Sub Masalah C
Masalah Besar
Sub Masalah A1 Sub Masalah A2
B
A
CA1
A2
Strategi umum dalampenyelesaian masalah besar; kompleks; rumit
Top Down
IF-UTAMA Ver/Rev : 1/0 III - 17
Contoh Top Down
Mahasiswa Dosen Perkuliahan
Sistem Informasi Akademis
Entry data
Hapus data
Laporan data
Entry data
Hapus data
Laporan data
Entry data
Hapus data
Laporan data
IF-UTAMA Ver/Rev : 1/0 III - 18
Implementasi : Metode Modular
……..
Call A
……..
Call B
……..
Call C
……..
…….
Call A1
…….
Call A2
……..
…….
…….
…….
…….
…….
…….
…….
…….
A
B
C
A1
A2
Bagian Utama
Dapat diterapkan secara :
- Internal : sub program, procedure, function
- Eksternal : file unit, header, modul
7
Sequence/Runtunan
IF-UTAMA Ver/Rev : 1/0 III - 20
Simple Sequence
IF-UTAMA Ver/Rev : 1/0 III - 21
Sequence
o Instruksi dikerjakan secara berurutan.
� dari atas ke bawah
Print harga3
harga ← jmlBrg x hrgSat2
input jmlBrg, hrgSat1
Instruksistep
8
IF-UTAMA Ver/Rev : 1/0 III - 22
Contoh Sequence (1)
o Program akan dikerjakan dengan input:
� 2, 1500
Print harga3
harga ← jmlBrg x hrgSat2
input jmlBrg, hrgSat1
Instruksistep
hargahrgSatjmlBrg
OutputVariabelstep
IF-UTAMA Ver/Rev : 1/0 III - 23
Contoh Sequence (2)
o Mulai dengan langkah-1:
� input 2, 1500
Print harga3
harga ← jmlBrg x hrgSat2
input jmlBrg, hrgSat1
Instruksistep
150021
hargahrgSatjmlBrg
OutputVariabelstep
IF-UTAMA Ver/Rev : 1/0 III - 24
Contoh Sequence (3)
o Langkah-2:
� Hitung perkalian, simpan hasilnya di variabel
harga
Print harga3
harga ← jmlBrg x hrgSat2
input jmlBrg, hrgSat1
Instruksistep
3000150022
150021
hargahrgSatjmlBrg
OutputVariabelstep
9
IF-UTAMA Ver/Rev : 1/0 III - 25
Contoh Sequence (4)
o Langkah-3:
� Tampilkan isi variabel harga
Print harga3
harga ← jmlBrg x hrgSat2
input jmlBrg, hrgSat1
Instruksistep
30003000150023
3000150022
150021
hargahrgSatjmlBrg
OutputVariabelstep
Input & Output
IF-UTAMA Ver/Rev : 1/0 III - 27
C-style IO is an acquired taste. Learn to like it.
I/O C-style
Basic functions:
o printf, scanf, fprintf, fscanf, sprintf, sscanf, etc.
o gets, puts, getc, putc, getchar
o read, write, fread, fwrite
We will cover the basics of the “formated” family of
functions (printf, scanf, etc).
10
IF-UTAMA Ver/Rev : 1/0 III - 28
printf(char *format_string, ...);
fprintf(FILE*, char *format_string, ...);
snprintf(char* buf, size_t n, char *format_string, ...);
printf
o In C, all devices are treated like files
o Three standard files are:
� stdin Often the keyboard
� stdout Often the text console
� stderr Often the text console
o printf(....) is fprintf(stdout, ....)
o The format string is a pattern for the output; it describes how to display the arguments to printf.
o Snprintf write to the string “buf”. The variable n specifies the size of the buffer.
o printf returns the number of characters written
IF-UTAMA Ver/Rev : 1/0 III - 29
format string
o Format strings are normal strings with embedded “conversion specifications” which are placeholders for arguments
o Conversion specifications are a ‘%’ and a letter with an optional set of arguments in between the ‘%’ and letter.
o To print a ‘%’, you need to write ‘%%’
Example:
printf(“Here is a number: %d\n”, 10);
%d is the conversion specification for signed integers.
IF-UTAMA Ver/Rev : 1/0 III - 30
Conversion Specifications
Conversion Specifications:
o %d, %i -- signed integer
o %u -- unsigned integer
o %f -- floating point number
o %c -- character
o %s -- string
o %x -- hexadecimal value
o %p -- pointer
Converion specifications tell how to translate
a data value into a string
Options:
o l -- long (32-bit value)
o ll -- long long (64-bit value)
o n -- field width of n digits
o .n -- precision of n digits
o 0 -- fill unused field with 0s
There are many more! Read man pages, or Google it.
11
IF-UTAMA Ver/Rev : 1/0 III - 31
printf quiz!
Figure out the output of the following:
o printf(“%.3f rounded to 2 decimals is %.2f\n”, 2.325, 2.325);
o printf(“%d in hex is: %04x\n”, 24, 24);
o printf(“Quizzes are fun, ja?\n”);
IF-UTAMA Ver/Rev : 1/0 III - 32
scanf(char *format_string, ...);
fscanf(FILE*, char *format_string, ...);
sscanf(char*, char *format_string, ...);
scanf
o scanf(....) is fscanf(stdin, ....)
o All arguments ot scanf must be pointers (or arrays)
o scanf does almost no size checks. It is easy to get a buffer overflow here. Make sure you use a field length specifier with the %s conversion specifer!!!
o scanf returns the number of items read.
IF-UTAMA Ver/Rev : 1/0 III - 33
scanf Examples
int items_read;
Read a number:
int num;
items_read = scanf(“%d”, &num);
Read a character:
char ch;
items_read = scanf(“%c”, &ch);
Read a string of max length, 79 chars:
char buf[80];
buf[79]=‘\0’; // Ensure a terminating NULL.
items_read = scanf(“%79s”, buf);
Read number after pattern of “a:<num>”:
int num;
items_read = scanf(“a:%d”, &num);
always check the return value of
scanf
12
IF-UTAMA Ver/Rev : 1/0 III - 34
C++-style IO is easier for simple stuff
I/O C++-style
Basic classes:
o iostream (cout, cin, cerr)
o ostringstream, istringstream
cout << “Hello World!” << endll;
cout << “Boo! “ << 10 << ‘c’ << endl;
cerr << “Here’s the error stream” << endl;
int n;
cin >> n;
char ch;
cin >> ch;
IF-UTAMA Ver/Rev : 1/0 III - 35
...but harder for complex stuff
I/O C++-style continued...
printf(“%.3f rounded to 2 decimals is %.2f\n”, 2.325, 2.325);
…becomes…
cout << setprecision(3) << 2.325
<< “ rounded to 2 decimals is “
<< setprecision(2) << 2.3.25
<< endl;
IF-UTAMA Ver/Rev : 1/0 III - 36
C and C++ I/O compared
C-style I/O:
• No type safety. What happens with printf(“%d”, ‘c’);?
• Conversion specifications have a high learning curve.
• Almost all the state of the I/O is contained in the function call.
C++ style I/O:
• Manipulators are very verbose/annoying
• Global state gets changed. When you do “cout << 2.4555”, what precision are you set at? You don’t know. It’s worse with threads.
• You get more customizability since C++ I/O is classed based.
NEVER mix C and C++ I/O...until you know what ios::sync_with_stdio() does.