[C言語]複数ファイルのコンパイル&実行

main.c

#include <stdio.h>

int main() {
    double a, b, sum(double, double), product(double, double);
    printf("input two numbers: ");
    scanf("%lf%lf", &a, &b);
    printf("sum=%g\n"; sum(a, b));
    printf("product=%g\n", product(a, b));
    return 0;
}

sub1.c

double sum(double a, double b){
    return a + b;
}

sub2.c

double product(double a, double b){
    return a * b;
}

$ gcc -o sum_product main.c sub1.c sub2.c
$ ./sum_product
input two numbers: 2 3
sum=5
product=6

main.cからはheader情報は特になし
gccで複数ファイルから一つにコンパイルする

[TCP/IP] ソケット

TCPまたUDPで通信を行うにはソケットの作成をOSに依頼する必要がある。プログラムの必要に応じてソケットタイプを指定
int socket(int protocolFamily, int type, int protocol)
アプリケーションでソケットを終了させるにはclose()を使用する
int close(int socket)

ソケットAPIにはsockaddrという汎用のデータ型が定義されている
struct sockadddr {
unsigned short sa_family; // アドレスファミリ
char sa_data[14]; // アドレス情報
}

sockaddr構造体をTCP/IPのソケットアドレスに特化したのがsockaddr_in構造体
struct in_addr {
unsigned long s_addr; // IP address
}

struct sockaddr_in {
unsigned short sin_family; // TCP/IP
unsigned short sin_port; // address port
struct in_addr sin_addr; // ip address
char sin_zero[8];
}

### TCP接続
1. socket()を実行してTCPソケットを作成
2. connect()を実行してサーバへの接続を確立
 int connect(int socket, struct socketaddr *foreignAddress, unsigned int addressLength)
3. send()とrecv()を実行して通信を行う
int send(int socket, const void *msg, unsigned int msgLength, int flags)
int recv(int socket, void *rcvBuffer, unsigned int bufferLength, int flags)
4. closeを実行して接続クローズ

[TCP/IP]基礎

Internetはsocketというプログラミングインターフェイスを利用してネットワーク通信サービスにアクセスしている。
通信チャネルは、ブロードバンド技術を利用したイーサネット、ダイヤルアップモデムを使った接続などが考えられる。
プログラムによって組み立てられ解釈される情報をパケットして扱う
特定の問題を解決するプロトコルを切り分けて、モジュールのような形式で利用する仕組みが取られている
TCP/IPは、このようなプロトコルの集まり

### ソケット
一方のマシンでアプリケーションがソケットに書き込んだ情報を、相手方のマシンのアプリケーションが読み取る
TCP/IPプロトコルファミリのソケットはストリームソケット(TCP)とデータグラムソケット(UDP)
ソケットがリモートのアプルケーションプログラムからメッセージを受信できるのは、ソケットがポート番号にバインドされた後

[C++] ファイルの入出力

#include <iostream>
#include <fstream>
using namespace std;


int main() {
    
    ofstream fout("test0.txt");
    if(!fout){
        cout << "ファイルをオープンできませんでした。\n";
        return 1;
    }
    else
        cout << "ファイルをオープンしました。\n";

    fout.close();
    cout << "ファイルをクローズしました。\n";

    return 0;
}

ファイル出力

    fout << "Hello\n";
    fout << "Goodbye\n";
    fout << "ファイルに書き込みました。\n";

### 書式を設定してファイル出力

#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;


int main() {
    
    ofstream fout("test0.txt");
    if(!fout){
        cout << "ファイルをオープンできませんでした。\n";
        return 1;
    }
    else
        cout << "ファイルをオープンしました。\n";

    const int num = 5;
    int test[num];
    cout << num << "人の点数を入力してください。\n";
    for(int i=0; i<num; i++){
        cin >> test[i];
    }

    for(int j=0; j<num; j++){
        fout << "No." << j+1 << setw(5) << test[j] << '\n';
     }

    fout.close();

    return 0;
}

### ファイルから入力

#include <iostream>
#include <fstream>
using namespace std;


int main() {
    
    ifstream fin("test0.txt");
    if(!fin){
        cout << "ファイルをオープンできませんでした。\n";
        return 1;
    }

    char str1[16];
    char str2[16];
    fin >> str1 >> str2;
    cout << "ファイルに書き込まれている2つの文字列は\n";
    cout << str1 << "です。\n";
    cout << str2 << "です。\n";

    fin.close();

    return 0;
}
int main() {
    
    ifstream fin("test0.txt");
    if(!fin){
        cout << "ファイルをオープンできませんでした。\n";
        return 1;
    }

    const int num = 8;
    int test[num];
    for(int i=0; i<num; i++){
        fin >> test[i];
    }

    int max = test[0];
    int min = test[0];
    for(int j=0; j<num; j++){
        if(max < test[j])
            max = test[j];
        if(min > test[j])
            min = test[j];
        cout << "No." << j+1 << setw(5) << test[j] << "\n";
    }

    cout << "最高点は" << max << "です。\n";
    cout << "最低点は" << min << "です。\n";

    fin.close();

    return 0;
}

$ g++ -o sample sample.cpp && ./sample
No.1 80
No.2 68
No.3 22
No.4 33
No.5 56
No.6 78
No.7 33
No.8 56
最高点は80です。
最低点は22です。

### コマンドライン引数

#include <iostream>
#include <fstream>
using namespace std;


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

    if(argc != 2) {
        cout << "パラメータの数が異なります。\n";
        return 1;
    }
    
    ifstream fin(argv[1]);
    if(!fin){
        cout << "ファイルをオープンできませんでした。\n";
        return 1;
    }

    char ch;
    fin.get(ch);

    while(!fin.eof()){
        cout.put(ch);
        fin.get(ch);
    }

    fin.close();

    return 0;
}

### practice

int main() {

    for (int i=1; i<=30; i++){
        cout.width(3);
        cout.fill('-');
        cout << i;
        if(i % 5 == 0) {
            cout << "\n";
        }
    }

    return 0;
}

$ g++ -o sample sample.cpp && ./sample test0.txt
–1–2–3–4–5
–6–7–8–9-10
-11-12-13-14-15
-16-17-18-19-20
-21-22-23-24-25
-26-27-28-29-30

#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;


int main(int argc, char* argv[]) {
    
    ifstream fin(argv[1]);
    if(!fin){
        cout << "ファイルをオープンできませんでした。\n";
        return 1;
    }

    const int num = 8;
    int test[num];
    for(int i=0; i<num; i++){
        fin >> test[i];
    }

    int max = test[0];
    int min = test[0];
    for(int j=0; j<num; j++){
        if(max < test[j])
            max = test[j];
        if(min > test[j])
            min = test[j];
        cout << "No." << j+1 << setw(5) << test[j] << "\n";
    }

    cout << "最高点は" << max << "です。\n";
    cout << "最低点は" << min << "です。\n";

    fin.close();

    return 0;
}

[C++] マニピュレータ

入出力の書式に関する定義をmanipulatorという

int main() {
    
    cout << "こんにちは!" << endl;
    cout << "さようなら!" << endl;

    return 0;
}

10進数以外の出力

int main() {
    
    cout << "10を10進数で表記すると" << dec << 10 << "です。\n";
    cout << "10を8進数で表記すると" << oct << 10 << "です。\n";
    cout << "12を8進数で表記すると" << 12 << "です。\n";
    cout << "10を16進数で表記すると" << hex << 10 << "です。\n";

    return 0;
}

$ g++ -o sample sample.cpp && ./sample
10を10進数で表記すると10です。
10を8進数で表記すると12です。
12を8進数で表記すると14です。
10を16進数で表記するとaです。

出力幅指定

int main() {
    
    for(int i=0; i<=10; i++){
        cout << setw(3) << i;
    }
    cout << '\n';

    return 0;
}

[C++] ストリーム

int main() {
    
    int i;
    double d;
    char str[100];

    cout << "整数値を入力してください。\n";
    cin >> i;
    cout << "小数値を入力してください。\n";
    cin >> d;
    cout << "文字列を入力してください。\n";
    cin >> str;

    cout << "入力した整数値は" << i << "です。\n";
    cout << "入力した小数値は" << d << "です。\n";
    cout << "入力した文字列は" << str << "です。\n";
    

    return 0;
}

1文字入出力関数

int main() {
    
    char ch;

    cout << "文字を続けて入力してください。\n";

    while(cin.get(ch)){
        cout.put(ch);
    }

    return 0;
}

出力はばの固定

int main() {
    
    for(int i=0; i<=10; i++){
        cout.width(3);
        cout << i;
    }
    cout << "\n";

    return 0;
}

フィル文字指定

int main() {
    
    for(int i=0; i<=10; i++){
        cout.width(3);
        cout.fill('-');
        cout << i;
    }
    cout << "\n";

    return 0;
}

数値の精度

int main() {
    
    double pi = 3.141592;
    int num;

    cout << "円周率を出力します。\n";
    cout << "有効桁数何桁で出力しますか?(1~7)\n";
    cin >> num;

    cout.precision(num);

    cout << "円周率は" << pi << "です。\n";

    return 0;
}

書式状態フラグ
L 左寄せで出力

int main() {
    
    cout.setf(ios::left, ios::adjustfield);
    for(int i=0; i<=5; i++){
        cout.width(5);
        cout.fill('-');
        cout << i;
    }
    cout << '\n';
    cout.unsetf(ios::left);
    cout.setf(ios::right, ios::adjustfield);
    for(int j=0; j<=5; j++){
        cout.width(5);
        cout.fill('-');
        cout << j;
    }
    cout << "\n";

    return 0;
}

[C++] 例外処理

int main() {
    
    int num;
    cout << "1~9までの数を入力してください。\n";
    cin >> num;

    try {
        if(num <= 0)
            throw "0以下を入力しました";
        if(num >= 10)
            throw "10以上を入力しました";
        cout << num << "です。\n";
    }
    catch(char* err) {
        cout << "エラー:" << err << "\n";
        return 1;
    }
    return 0;
}

[C++] テンプレートクラス, STL vector

クラスの雛形から色々な型を扱うクラスを作成できる。

template <class T>
class Array {
    private:
        T data[5];
    public:
        void setData(int num, T d);
        T getData(int num);
};

template <class T>
void Array<T>::setData(int num, T d){
    if ( num < 0 || num > 4)
        cout << "配列の範囲を超えています。\n";
    else
        data[num] = d;
}

template <class T>
T Array<T>::getData(int num){
    if (num < 0 || num > 4){
        cout << "配列の範囲を超えています\n";
        return data[0];
    } 
    else
        return data[num];
}
int main() {
    cout << "int型の配列を作成します\n";
    Array<int> i_array;
    i_array.setData(0, 80);
    i_array.setData(1, 60);
    i_array.setData(2, 58);
    i_array.setData(3, 77);
    i_array.setData(4, 57);

    for(int i=0; i<5; i++)
        cout << i_array.getData(i) << '\n';

    cout << "double型の配列を作成します。\n";
    Array<double> d_array;
    d_array.setData(0, 35.5);
    d_array.setData(1, 45.6);
    d_array.setData(2, 26.8);
    d_array.setData(3, 76.2);
    d_array.setData(4, 85.5);

    for (int j=0; j<5; j++)
        cout << d_array.getData(j) << '\n';

    return 0;
}

C++では standard template libraryをSTLと呼ぶ
vectorは数が決まっていない複数のデータを管理できる構造

#include <iostream>
#include <vector>
using namespace std;


int main() {
    
    int num;
    vector<int> vt;

    cout << "幾つ整数データを入力しますか?\n";
    cin >> num;

    for(int i=0; i<num; i++){
        int data;
        cout << "整数を入力してください。\n";
        cin >> data;
        vt.push_back(data);
    }

    cout << "表示します。\n";
    vector<int>::iterator it = vt.begin();
    while(it != vt.end()){
        cout << *it;
        cout << "-";
        it++;
    }
    cout << "\n";


    return 0;
}

vt.begin(), vt.end()で先頭・末尾のデータを知ることができる

### STLによるアルゴリズム

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;


int main() {
    
    vector<int> vt;
    for(int i=0; i<10; i++){
        vt.push_back(i);
    }

    cout << "並び替え前は";
    vector<int>::iterator it = vt.begin();
    while(it != vt.end()){
        cout << *it;
        it++;
    }
    cout << "です。\n";

    cout << "逆順にすると";
    reverse(vt.begin(), vt.end());
    it = vt.begin();
    while(it != vt.end()){
        cout << *it;
        it++;
    }
    cout << "です。\n";

    cout << "並び替え後は";
    sort(vt.begin(), vt.end());
    it = vt.begin();
    while(it != vt.end()){
        cout << *it;
        it++;
    }
    cout << "です。\n";


    return 0;
}

主なアルゴリズムにはcount, find, reverse, replace, remove, merge, sort, searchなどがある。

[C++] コピーコンストラクタ

オブジェクトが他のオブジェクトで初期化される場合に呼び出されるコンストラクタ
Car::Car(const Car& c){}

class Car {
    private:
        int num;
        double gas;
        char* pName;
    public:
        Car(char* pN, int n, double g);
        ~Car();
        Car(const Car& c);
        Car& operator=(const Car& c);
};

Car::Car(char* pN, int n, double g) {
    pName = new char[strlen(pN)+1];
    strcpy(pName, pN);
    num = n;
    gas = g;
    cout << pName << "を作成しました。\n";
}
Car::~Car(){
    cout << pName << "を破棄します。\n";
    delete[] pName;
}
Car::Car(const Car& c){
    cout << c.pName << "で初期化します。\n";
    pName = new char[strlen(c.pName) + strlen("のコピー1") + 1];
    strcat(pName, c.pName);
    strcat(pName, "のコピー1");
    num = c.num;
    gas = c.gas;
}

Car& Car::operator=(const Car& c){
    cout << pName << "に" << c.pName << "を代入します。\n";
    if(this != &c) {
        delete[] pName;
        pName = new char[strlen(c.pName) + strlen("のコピー2") + 1];
        strcat(pName, c.pName);
        strcat(pName, "のコピー2");
        num = c.num;
        gas = c.gas;
    }
    return *this;
}

int main() {
    Car mycar("mycar", 1234, 25.5);
    
    Car car1 = mycar;

    Car car2("car2", 0, 0);
    car2 = mycar;

    return 0;
}

[C++] クラスのメモリ確保

コンストラクタの中で動的にメモリを確保した場合、デストラクタ(destructor)を定義してメモリを解放する 
デストラクタはチルダをつける
デストラクタはオブジェクトの破棄のタイミングで自動的に解放される

#include <iostream>
#include <string>
#include <cstring>
using namespace std;

class Car {
    private:
        int num;
        double gas;
        char* pName;
    public:
        Car(char* pN, int n, double g);
        ~Car();
        void show();
};

Car::Car(char* pN, int n, double g) {
    pName = new char[strlen(pN)+1];
    strcpy(pName, pN);
    num = n;
    gas = g;
    cout << pName << "を作成しました。\n";
}
Car::~Car(){
    cout << pName << "を破棄します。\n";
    delete[] pName;
}
void Car::show(){
    cout << "車のナンバーは" << num << "です。\n";
    cout << "ガソリン量は" << gas << "です。\n";
    cout << "名前は" << pName << "です。\n";
}

int main() {
    Car car1("mycar", 1234, 25.5);
    car1.show();

    return 0;
}