Integrated exercises I
(総合演習 I)
Computing Practice I
7th lecture, June 1, 2017
http://www.sw.it.aoyama.ac.jp/2017/CP1/lecture7.html
Martin J. Dürst
© 2005-17 Martin
J. Dürst 青山学院大学
Today's Schedule
- Written test (30 min)
- About last week's exercises
- The C preprocessor
- About today's exercises
総合復習テスト
- 30 分の筆記試験
- ログオフ済み
- 机に場所を作る
(キーボードの長辺を下、倒れないように立たせるなど)
- 授業開始まで教科書、資料、筆箱、財布などを鞄に入れ、鞄を椅子の下に
- テスト終了後その場で待つ
Applications of C: Ruby
- Programming language putting the pleasure of the programmer first
- First publication: 1993
- Creator: Yukihiro Matsumoto (Matz; まつもと ゆきひろ)
- Main application areas: Web programming (Ruby on Rails), scripting
- Overall about 840K lines of code
(main directory: 92 .c
files, 37 .h
files)
- Other implementations (Ex.: JRuby: Ruby implemented in Java)
Results of Previous Exercises
|
06A1 |
06A2 |
06B1 |
06C1 |
06C2 |
06C3 |
100 points |
98 |
96 |
90 |
58 |
26 |
9 |
60 points |
1 |
3 |
9 |
40 |
65 |
65 |
errors |
- |
- |
- |
- |
7 |
11 |
not submitted |
- |
- |
- |
1 |
1 |
14 |
- 06C1 not submitted ⇒ No real program submitted
- 06C2 not submitted ⇒ One more chance today
Previous Exercises: How to Define Structures
- Define structures for frequently appearing actual objects
- Fit structure definition to the essence of the objects
- Choose appropriate names for the structure and its members
- The goal of the structure is to group related data items
- Think about structures, functions, and usage separately
- Separate functions for input/output from functions for calculation
- This applies not only to programming, but also to
requirements analysis and design
Previous Exercises: Examples of Structures
- Bad examples:
typedef struct { double x1, x2, y1, y2; } Point;
typedef struct { double x, y; } Complex;
- Good examples (points in 2D have x and y
coordinates; complex numbers have real and immaginary parts)
typedef struct { double x, y; } Point;
typedef struct { double real, immaginary; } Complex;
- Type names:
Good examples: Complex
, COMPLEX
,
complex_t
, complex_num
, ...
Bad examples: base
, CAL
, calc
, Comps
, Decimal
, INPUT
, Number
, POINT
, ...
Previous Exercises: How to Obtain Information
- Listen well to explanations at the beginning
- Read the handouts carefully
- Carefully read and understand the exercise description
Example: Create a function to add two complex numbers.
- How to use the Q&A forum
- There are still reluctant students
- Questions about English text are okay
- Questions about contents of exercise are okay
- Questions about C or programming in general are okay
- Questions about tests are okay
- Questions about individual programming problems are okay
- Better ask earlier than later
Preprocessor
- Executed before the actual compiler
- Can be called as separate program (
cpp
)
- Running only the preprocessor:
gcc -E 06A1.c -o 06A1.i
→
06A1.i
- Main functions:
- Inclusion of (header) files (
#include
)
- Definition and expansion of macros (
#define
)
- Conditional compilation
(
#ifdef
/#ifndef
/#endif
)
Header Files
- The extension for C header files is
.h
- Compiled by inclusion into
.c
files
- Main functions:
- Share information common to multiple
.c
files
- Define common types
- Declare functions
- Define macros
- [declare global variables]
Extensions
.h
: C header file
.c
: C file
- (
.i
: C file after preprocessor, before compilation)
.o
: C file after compilation (before linking)
.exe
: Windows executable file
Typical Example of Header File Use
library.h
: Declaration of types and functions in the
library
library.c
: Define the functions in the library
(#include
s library.h
)
using.c
: Program using the library (#include
s
library.h
)
Two Different Kinds of #include
#include
for standard libraries (textbook p. 201):
#include <stdio.h>
#include
for 'self-made' libraries:
#include "my_library.h"
Definition of Macros
(textbook pp. 218, 237)
- Basic syntax:
#define COUNT 20
Purpose: Easy change of program
Example: for (i=0; i<COUNT; i++)
- Extended syntax:
#define max(a,b) ((a)>(b)?(a):(b))
Purpose: Type-independent inline pseudo-functions
Example: int i = max(d,e); double z = max(x,y);
Caution: In definition, use lots of parentheses; be careful about side
effects
(char c = max(getchar(), 'n');
)
- Definition of macros on command line:
gcc -DCOUNT=30 ...
Conditional Compilation
#ifdef HOGE
// HOGE が #define で定義されたら残る部分
#endif
#ifndef HOGE
// HOGE が #define
で定義されなかったら残る部分
#endif
Example of Conditional Compilation
Avoid repeated #include
of the same header file:
In header file:
#define MY_LIBRARY_HEADER
When including:
#ifndef MY_LIBRARY_HEADER
#include "my_library.h"
#endif
演習問題の概要
- 07A1: 複素数のライブラリの使用
- 07B1: 複素数のライブラリの作成
- 07C1: 行列計算のライブラリの作成と使用
(
#ifndef
, #define
; インデントに注意)
- 07C2: 日付のライブラリの作成 (第一部)
- 07C3: 日付のライブラリの作成
(第二部、発展問題、月曜締切、部分点)
(問題ごとにテスト用のファイル、コンパイル用のファイルがダウンロード可能)
(問題ごとに用意されたファイルと情報源、要求されたファイルの関係を把握)
次回までの準備
- 今日の復習
- 残りの演習問題を宿題として完成、提出
- 配布資料 (「ビジュアルラーニング C++ 入門」、さかお
まい著、X-media 社、pp. 70-112) をよく読む。
Glossary
- requirements analysis
- 要求分析
- design
- 設計
- header file
- ヘッダファイル
- preprocessor
- プリプロセッサ
- linking
- リンク (の作業)
- expansion
- 展開
- macro
- マクロ
- conditional compilation
- 条件付きコンパイル
- extension
- 拡張子