Integrated exercises I
(総合演習 I)
Computing Practice I
7th lecture, May 26, 2016
http://www.sw.it.aoyama.ac.jp/2016/CP1/lecture7.html
Martin J. Dürst

© 2005-16 Martin
J. Dürst 青山学院大学
Today's Schedule
  - Written test (30 min)
- About last week's exercises 
- The C preprocessor
- About today's exercises
 
総合復習テスト
  - 30 分の筆記試験
- ログオフ済み
- 机に場所を作る
    (キーボードの長辺を下にパソコンとディスプレーの間に挟むなど)
- 授業開始まで教科書、資料、筆箱、財布などを鞄に入れ、鞄を椅子の下に
- テスト終了後その場で待つ
 
C 言語の応用: Ruby
  - プログラミングの楽しさを主な目的としたプログラム言語
- 最初の公開は 1993 年
- 作者はまつもと ゆきひろ (Matz)
- 主な応用分野はウェブサイト (Ruby on Rails)
  とスクリプト
- 全体で約 84万行
- 他の実装も (例: JRuby: Java による Ruby の実装)
 
前回の演習結果
  
  
  
  
  
  
  
  
    
      |  | 06A1 | 06A2 | 06B1 | 06C1 | 06C2 | 06C3 | 
    
      | 100点 | 86 | 82 | 78 | 45 | 15 | 3 | 
    
      | 60点 | 3 | 7 | 11 | 43 | 63 | 36 | 
    
      | エラー | - | - | - | 1 | 10 | 11 | 
    
      | 未提出 | - | - | - | - | 1 | 39 | 
  
  - 06C1 を提出してない ⇒
    本格的なプログラムを提出してない
- 06C2 を提出してない ⇒
    今日違った形でもう一回チャンス
 
前回の演習: 構造体の定義
  - 構造体はプログラムによく出てくる対象物のために定義
- 構造体の定義を表現する対処の本質に合わせる
- 構造体の型名とメンバ名は対象物に合わせる
- 構造体は関連あるデータをまとめるのが目的
- 構造体や関数そのものと使うプログラムを別物と考える
- 入出力関数と計算関数を分離
- 入出力と計算はできるだけ別の関数に
- これらの点はプログラミングだけではなく、
 要求分析、設計などにも大事
 
前回の演習: 構造体の具体例
  - 悪い例 (演習課題にはたまたま二つの点が出現): 
    typedef struct  {  double x1, x2, y1, y2;  }  Point;
typedef struct  {  double x, y; }  Complex;
- 良い例 (二次元の点には x 座標と y 座標のみ): 
    typedef struct  {  double x, y;  }  Point;
- 複素数も同様: 
    typedef struct  {  double real, immaginary;  }  Complex;
- 型名:
 良い例:Complex,COMPLEX,complex_t,complex_numなど
 悪い例:base,CAL,calc,Comps,Decimal,INPUT,Number,POINT
 
前回の演習: 麺棒プログラムではなく、配列と初期化
 
前回の演習: 情報収集
  - 演習前の説明をよく聞く
- 配布資料をよく読む
- 問題文をしっかり読んで理解
 例: Create a function to add two complex numbers.
- Q&A フォーラムの活用 
    
      - いまだ遠慮する学生が存在
- 英文についての質問でもよい
- 個別問題だと思われる場合でもよい
 
 
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 .cfiles
- Main functions: 
    
      - Share information common to multiple .cfiles
- 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
    (- #includes- library.h)
- using.c: Program using the library (- #includes- library.h)
 
Two Different Kinds of #include
  - #includefor standard libraries (教科書 p. 201):
 - #include <stdio.h>
- #includefor 'self-made' libraries:
 - #include "my_library.h"
 
Definition of Macros
(教科書 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
  - header file
- ヘッダファイル
- preprocessor
- プリプロセッサ
- linking
- リンク (の作業)
- expansion
- 展開
- macro
- マクロ
- conditional compilation
- 条件付きコンパイル
- extension
- 拡張子