Software_CPP4

CPPその4

┌──┐
│目次│
├──┘
│☆サンプルコード
◆文法サンプル
◆処理小ネタ
◆コンソールアプリケーション
◆各種クラス

☆サンプルコード


◆文法サンプル

_◇演算子のオーバーロード #1

#include <iostream.h>
#include <string.h>

class stringx {
  public:
    stringx(char *);
    void operator +(char *);
    void operator -(char);
    void show_string(void);
  private:
    char data[256];
};

stringx::stringx(char *str)
{
  strcpy(data, str);
}

void stringx::operator +(char *str)
{
  strcat(data, str);
}

void stringx::operator -(char letter)
{
  char temp[256];
  int i, j;

  for (i = 0, j = 0; data[i]; i++)
    if (data[i] != letter) temp[j++] = data[i];
  temp[j] = NULL;
  strcpy(data, temp);
}

void stringx::show_string(void)
{
  cout << data << endl;
}

void main(void)
{
  stringx title("xxx");
  stringx lesson("yyy");
  title.show_string();
  title + " zzz";
  title.show_string();
  
  lesson.show_string();
  lesson - 'n';
  lesson.show_string();
}

_◇演算子のオーバーロード #2

#include <iostream.h>
#include <string.h>

class stringx {
  public:
    stringx(char *);
    void operator +(char *);
    void operator -(char);
    int operator ==(stringx);
    void show_string(void);
  private:
    char data[256];
};

stringx::stringx(char *str)
{
  strcpy(data, str);
}

void stringx::operator +(char *str)
{
  strcat(data, str);
}

void stringx::operator -(char letter)
{
  char temp[256];
  int i, j;
  for (i = 0, j = 0; data[i]; i++)
    if (data[i] != letter) temp[j++] = data[i];
  temp[j] = NULL;
  strcpy(data, temp);
}

int stringx::operator ==(stringx str)
{
  int i;
  for (i=0; data[i] == str.data[i]; i++)
      if ((data[i] == NULL) && (str.data[i] == NULL)) return (1);
  return (0);
}

void stringx::show_string(void)
{
  cout << data << endl;
}

void main(void)
{
  stringx title("aaa");
  stringx lesson("bbbb");
  stringx str("cccc");
  if (title == lesson)
    cout << "xxx" << endl;
  if (str == lesson)
    cout << "yyyy" << endl;
  if (title == str)
    cout << "zzzz" << endl;
}

_◇演算子のオーバーロード #3

#include <iostream>
using namespace std;

class Point{
private:
    int x;
    int y;
public:
    Point(int a = 0, int b = 0) {x = a; y = b;}
    void setX(int a) {x = a;}
    void setY(int b) {y = b;}
    void show() {cout<< " x:" << x << " y:" << y << "\n";}
    Point operator++();
    Point operator++(int d);
    friend Point operator+(Point p1, Point p2);
    friend Point operator+(Point p, int a);
    friend Point operator+(int a, Point p);
};

//前置++
Point Point::operator++()
{
    x++;
    y++;
    return *this;
}

//後置++
Point Point::operator++(int d)
{
    Point p = *this;
    x++;
    y++;
    return p;
}

//p1+p2
Point operator+(Point p1, Point p2)
{
    Point tmp;
    tmp.x = p1.x + p2.x;
    tmp.y = p1.y + p2.y;
    return tmp;
}

//p1+int
Point operator+(Point p, int a)
{
    Point tmp;
    tmp.x = p.x + a;
    tmp.y = p.y + a;
    return tmp;
}

//int+p1
Point operator+(int a, Point p)
{
    Point tmp;
    tmp.x = a + p.x;
    tmp.y = a + p.y;
    return tmp;
}

int main()
{
    Point p1(1, 2);
    Point p2(3, 6);
    p1 = p1 + p2;
    p1 ++;
    p1 = p1+3;
    p2 = 3+p2;

    p1.show();
    p2.show();

    cout << "TYPE ANY CHAR, THEN RETURN>";
    char c;
    cin >> c;
    cout << "\n";

    return 0;
}

_◇スタティックなデータメンバ

#include <iostream.h>
#include <string.h>

class book_series {
    public:
        book_series(char *, char *, float);
        void show_book(void);
        void set_pages(int);
    private:
        static int page_count;
        char title[64];
        char author[64];
        float price;
};

int book_series::page_count;

void book_series::set_pages(int pages)
{
    page_count = pages;
}

book_series::book_series(char *title, char *author, float price)
{
    strcpy(book_series::title, title);
    strcpy(book_series::author, author);
    book_series::price = price;
}

void book_series::show_book(void)
{
    cout << "Title: " << title << endl;
    cout << "Author: " << author << endl;
    cout << "Price: " << price << endl;
    cout << "Pages: " << page_count << endl;
}

void main (void)
{
    book_series programming("xxx", "yyy", 19.95);
    book_series word("zzz", "aaa", 19.95);
    word.set_pages(256);
    programming.show_book();
    word.show_book();
    cout << endl << "Changing page count " << endl;
    programming.set_pages(512);
    programming.show_book();
    word.show_book();
}

_◇スタティック関数

#include <iostream.h>

class menu {
    public:
        static void clear_screen(void);
    private:
        int number_of_menu_options;
};

void menu::clear_screen(void)
{
    cout << '\033' << "[2J";
}

void main(void)
{
    menu::clear_screen();
}

_◇関数テンプレート

#include <iostream>
using namespace std;

//関数テンプレート
template <class T>
T maxt(T x, T y)
{
    if(x > y)
        return x;
    else
        return y;
}

int main()
{
    int a, b;
    double da, db;

    cout << "Enter two integer numbers.\n";
    cin >> a >> b;

    cout << "Enter two double numbers.\n";
    cin >> da >> db;

    int ans1 = maxt(1, b);
    double ans2 = maxt(da, db);

    cout << "Integer Max = " << ans1 << "\n";
    cout << "Double  Max = " << ans2 << "\n";

    char c;
    cout << "Type CHAR key, then Return>";
    cin >> c;
    cout << "\n";

    return 0;
}

_◇列挙型

#include <iostream>
using namespace std;

enum Week{SUN, MON, TUE, WED, THU, FRI, SAT};

int main()
{
    Week w;
    w = SUN;

    switch(w) {
        case SUN: cout << "SUNDAY\n"; break;
        case MON: cout << "MONDAY\n"; break;
        case TUE: cout << "TUESDAY\n"; break;
        case WED: cout << "WEDNSDAY\n"; break;
        case THU: cout << "THURSDAY\n"; break;
        case FRI: cout << "FRIDAY\n"; break;
        case SAT: cout << "SATERDAY\n"; break;
        default:  cout << "UNKNOWN\n"; break;
    }

    char c;
    cout << "TYPE ANY CHAR, THEN RETURN>";
    cin >> c;
    cout << "\n";

    return 0;
}

_◇構造体引数

①構造体そのものを実引数とする
⇒コピーされ、値が渡される
⇒コピーの分遅い。

#include <iostream>
using namespace std;

struct Car {
    int num;
    double gas;
};

void show(Car c);

int main()
{
    Car car1 = {0, 0.0};
    cout << "Enter Number>\n";
    cin >> car1.num;
    cout << "Enter Gas>\n";
    cin >> car1.gas;
    show(car1);
    char c;
    cout << "Type CHAR, then RETURN>";
    cin >> c;
    cout << "\n";
    return 0;
}

void show(Car c)
{
    cout << "Number is " << c.num << ", GAS=" << c.gas << "\n";
}

②ポインタ渡し

    ...
    show(&car1);
    ...
    void show(Car* pC)
    {
     cout << "Number is " << pC->num << ", GAS=" << pC->gas << "\n";
    }

③参照渡し

    ...
    show(car1);
    ...
    void show(Car& c)
    {
     cout << "Number is " << c.num << ", GAS=" << c.gas << "\n";
    }

_◇オブジェクト引数

①オブジェクトそのものを実引数とする
⇒コピーされ、値が渡される
⇒コピーの分遅い。

#include <iostream>
using namespace std;

class Car{
private:
    int num;
    double gas;
public:
    int getNum() {return num;}
    double getGas() {return gas;}
    void show();
    void setNumGas(int n, double g);
};

void Car::show()
{
    cout << "Car Number is " << num << ".\n";
    cout << "Gas = " << gas << ".\n";
}

void Car::setNumGas(int n, double g)
{
    if(g>0 && g<1000) {
        num = n;
        gas = g;
        cout << "Car Number=" << num << " GAS=" << gas << ".\n";
    } else {
        cout << g << ", ERROR.\n";
    }
}

void buy(Car c);

int main()
{
    Car car1;
    car1.setNumGas(1234, 20.5);
    buy(car1);
    cout << "Type Any char, then return>\n";
    char c;
    cin >> c;
    cout << "\n";
    return 0;
}

void buy(Car c)
{
    int n = c.getNum();
    double g = c.getGas();
    cout << "BUY: number=" << n << " GAS=" << g << "\n";
}

②ポインタ渡し

void buy(Car* pC);
    ...
    buy(&car1);
    ...

void buy(Car* pC)
{
    int n = pC->getNum();
    ...

③参照渡し

void buy(Car& c);
    ...
    buy(car1);
    ...

void buy(Car& c)
{
    int n = c.getNum();
    ...

_◇クラス

#include <iostream>
using namespace std;

class Car{
private:
    int num;
    double gas;
public:
    void show();
    void setNumGas(int n, double g);
};

void Car::show()
{
    cout << "NUMBER=" << num << "\n";
    cout << "GAS=" << gas << "\n";
}

void Car::setNumGas(int n, double g)
{
    if (g>0 && g < 1000) {
        num = n;
        gas = g;
        cout << "NUMBER=" << num << " GAS=" << gas << "\n";
    }
    else
    {
        cout << g << " ERROR! \n";
        cout << "GAS, NOT UPDATED.\n";
    }
}

int main()
{
    Car car1;
    car1.setNumGas(1234, 20.5);
    car1.show();
    cout << "WILL BE ERROR...\n";
    car1.setNumGas(1234, -10.0);
    car1.show();
    char c;
    cout << "TYPE ANY CHAR, THEN RETURN>";
    cin >> c;
    cout << "\n";
    return 0;
}

_◇コンストラクタのオーバーロード

#include <iostream>
using namespace std;

class Car{
private:
    int num;
    double gas;
public:
    Car();
    Car(int n, double g);
    void show();
};

Car::Car()
{
    num=0;
    gas=0.0;
    cout << "Create a Car.\n";
}

Car::Car(int n, double g)
{
    num = n;
    gas = g;
    cout << "Create a Car, Number=" << num << " Gas=" << gas << ".\n";
}

void Car::show()
{
    cout << "Number = " << num << "\n";
    cout << "Gas = " << gas << "\n";
}

int main()
{
    Car car1;
    Car car2(1234, 20.5);
    cout << "TYPE ANY KEY, THEN RETURN>";
    char c;
    cin >> c;
    cout << "\n";
    return 0;
}

_◇デストラクタ

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

class Car{
private:
    int num;
    double gas;
    char* pName;
public:
    Car::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 << "Car::Car " << pName << "\n";
}

Car::~Car()
{
    cout << "Destroy " << pName << "\n";
    delete[] pName;
}

void Car::show()
{
    cout << "Number=" << num << "\n";
    cout << "Gas=" << gas << "\n";
    cout << "Name=" << pName << "\n";
}

int main()
{
    Car car1("mycar", 1234, 25.5);
    car1.show();
    cout << "TYPE ANY CHAR, THEN RETURN";
    char c;
    cin >> c;
    cout << "\n";
    return 0;
}

_◇クラスの継承

①その1

#include <iostream.h>
#include <string.h>

class employee {
    public:
        employee(char *, char *, float);
        void show_employee(void);
    private:
        char name[64];
        char position[64];
        float salary;
};

employee::employee(char *name, char *position, float salary)
{
    strcpy(employee::name, name);
    strcpy(employee::position, position);
    employee::salary = salary;
}

void employee::show_employee(void)
{
    cout << "Name: " << name << endl;
    cout << "Position: " << position << endl;
    cout << "Salary: $" << salary << endl;
}

class manager : public employee {
    public:
        manager(char *, char *, char *, float, float, int);
        void show_manager(void);
    private:
        float annual_bonus;
        char company_car[64];
        int stock_options;
};

manager::manager(char *name, char *position,
    char *company_car, float salary, float bonus,
    int stock_options) : employee(name, position, salary)
{
    strcpy(manager::company_car, company_car);
    manager::annual_bonus = bonus;
    manager::stock_options = stock_options;
}

void manager::show_manager(void)
{
    show_employee();
    cout << "Company car: " << company_car << endl;
    cout << "Annual bonus: $" << annual_bonus << endl;
    cout << "Stock options: " << stock_options << endl;
}

void main(void)
{
    employee worker("JD", "Pro", 35000);
    manager boss("Jan D", "VP", "Lexus", 50000.0, 5000, 1000);
    worker.show_employee();
    boss.show_manager();
}

②その2

#include <iostream>
using namespace std;

class Car{
private:
    int num;
    double gas;
public:
    Car();
    void setCar(int n, double g);
    void show();
};

class RacingCar : public Car {
private:
    int course;
public:
    RacingCar();
    void setCourse(int c);
};

Car::Car()
{
    num = 0;
    gas = 0.0;
    cout << "CREATE: Car \n";
}

void Car::setCar(int n, double g)
{
    num = n;
    gas = g;
    cout << "Number = " << num << " GAS<= " << gas << "\n";
}

void Car::show()
{
    cout << "Number = " << num << "\n";
    cout << "Gas = " << gas << "\n";
}

RacingCar::RacingCar()
{
    course = 0;
    cout << "CREATE: Racing Car.\n";
}

void RacingCar::setCourse(int c)
{
    course = c;
    cout << "Course#=" << course << "\n";
}
int main()
{
    RacingCar rccar1;
    rccar1.setCar(1234, 20.5);
    rccar1.setCourse(5);
    cout << "TYPE ANY KEY, THEN RETURN>" << "\n";
    char c;
    cin >> c;
    return 0;
}

③その3、基本クラスのコンストラクタの明示的呼び出し

#include <iostream>
using namespace std;

class Car{
private:
    int num;
    double gas;
public:
    Car();
    Car(int n, double g);
    void setCar(int n, double g);
    void show();
};

class RacingCar : public Car{
private:
    int course;
public:
    RacingCar();
    RacingCar(int n, double g, int c);
    void setCourse(int c);
};

Car::Car()
{
    num = 0;
    gas = 0.0;
    cout << "Create a Car, Car().\n";
}

Car::Car(int n, double g)
{
    num = n;
    gas = g;
    cout << "Create a Car, Car(n,g), number=" << num << " gas=" << gas << "\n";
}

void Car::setCar(int n, double g)
{
    num = n;
    gas = g;
    cout << "setCar(), number=" << num << " gas=" << gas << "\n";
}

void Car::show()
{
    cout << "Car::show(), number=" << num << "\n";
    cout << "Car::show(), gas=" << gas << "\n";
}

RacingCar::RacingCar()
{
    course = 0;
    cout << "Create a RacingCar, RacingCar().\n";
}

RacingCar::RacingCar(int n, double g, int c) : Car(n, g)
{
    course = c;
    cout << "Create a RacingCar(n,g,c), course=" << course << "\n";
}

void RacingCar::setCourse(int c)
{
    course = c;
    cout << "course=" << course << "\n";
}

int main()
{
    RacingCar rccar1(1234, 20.5, 5);
    cout << "TYPE ANY KEY, THEN RETURN>";
    char c;
    cin >> c;
    cout << "\n";
    return 0;
}

④その4、基本クラスのポインタの利用と仮想関数

#include <iostream>
using namespace std;

class Car{
protected:
    int num;
    double gas;
public:
    Car();
    Car(int n, double g);
    void setCar(int n, double g);
    virtual void show();
};

class RacingCar : public Car{
private:
    int course;
public:
    RacingCar();
    RacingCar(int n, double g, int c);
    void setCourse(int c);
    void show();
};

Car::Car()
{
    num = 0;
    gas = 0.0;
    cout << "Create a Car, Car().\n";
}

Car::Car(int n, double g)
{
    num = n;
    gas = g;
    cout << "Create a Car, Car(n,g), number=" << num << " gas=" << gas << "\n";
}

void Car::setCar(int n, double g)
{
    num = n;
    gas = g;
    cout << "setCar(), number=" << num << " gas=" << gas << "\n";
}

void Car::show()
{
    cout << "Car::show(), number=" << num << "\n";
    cout << "Car::show(), gas=" << gas << "\n";
}

RacingCar::RacingCar()
{
    course = 0;
    cout << "Create a RacingCar, RacingCar().\n";
}

RacingCar::RacingCar(int n, double g, int c) : Car(n, g)
{
    course = c;
    cout << "Create a RacingCar(n,g,c), course=" << course << "\n";
}

void RacingCar::setCourse(int c)
{
    course = c;
    cout << "course=" << course << "\n";
}

void RacingCar::show()
{
    cout << "RacingCar::show(), number=" << num << "\n";
    cout << "RacingCar::show(), gas=" << gas << "\n";
    cout << "RacingCar::show(), course=" << course << "\n";
}

int main()
{
    Car* pCars[2];
    Car car1;
    RacingCar rccar1;
    pCars[0] = &car1;
    pCars[0]->setCar(1234, 20.5);
    pCars[1] = &rccar1;
    pCars[1]->setCar(4567, 30.5);
    for(int i=0; i<2; i++) {
        pCars[i]->show();
    }
    cout << "TYPE ANY KEY, THEN RETURN>";
    char c;
    cin >> c;
    cout << "\n";
    return 0;
}

_◇仮想関数

#include <iostream>
using namespace std;

class A {
public:
    void f() { cout << "A::f()\n"; }
};

class B : public A {
public:
    virtual void f() { cout << "B::f()\n"; }
};

class C : public B {
public:
    void f()    { cout << "C::f()\n"; }
    void f(int)    { cout << "C::f(int)\n";}
};

int main()
{
    A    a;
    B    b;
    C    c;
    A*    p = &b;
    B*    q = &c;
    a.f();
    b.f();
    c.f();
    c.f(1);
    p->f();
    q->f();
}

_◇抽象クラス

#include <iostream>
using namespace std;

class Vehicle{
protected:
    int speed;
public:
    void setSpeed(int s);
    virtual void show() = 0;
};

class Car : public Vehicle{
private:
    int num;
    double gas;
public:
    Car(int n, double g);
    void show();
};

class Plane : public Vehicle{
private:
    int flight;
public:
    Plane(int f);
    void show();
};

void Vehicle::setSpeed(int s)
{
    speed = s;
    cout << "Vehicle::setSpeed SPEED=" << speed << "\n";
}

Car::Car(int n, double g)
{
    num = n;
    gas = g;
    cout << "Car:Car NUMBER=" << num << " GAS=" << gas << "\n";
}

void Car::show()
{
    cout << "Car::show NUMBER=" << num << "\n";
    cout << "Car::show GAS=" << gas << "\n";
    cout << "Car::show SPEED=" << speed << "\n";
}

Plane::Plane(int f)
{
    flight = f;
    cout << "Plane:Plane FLIGHT#=" << flight << "\n";
}

void Plane::show()
{
    cout << "Plane::show FLIGHT#=" << flight << "\n";
    cout << "SPEED=" << speed << "\n";
}

int main()
{
    Vehicle* pVc[2];
    Car car1(1234, 20.5);
    pVc[0] = &car1;
    pVc[0]->setSpeed(60);
    Plane pln1(232);
    pVc[1] = &pln1;
    pVc[1]->setSpeed(500);
    for(int i=0; i<2; i++) {
        pVc[i]->show();
    }
    cout << "TYPE ANY CHAR KEY, THEN RETURN>";
    char c;
    cin >> c;
    cout << "\n";
    return 0;
}

_◇多重継承

#include <iostream>
using namespace std;

class Base1{
protected:
    int bs1;
public:
    Base1(int b1=0) {bs1=b1;}
    void showBs1();
};

class Base2{
protected:
    int bs2;
public:
    Base2(int b2=0){bs2=b2;}
    void showBs2();
};

class Derived : public Base1, public Base2{
protected:
    int dr;
public:
    Derived(int d=0){dr=d;}
    void showDr();
};

void Base1::showBs1()
{
    cout << "bs1=" << bs1 << "\n";
}

void Base2::showBs2()
{
    cout << "bs2=" << bs2 << "\n";
}

void Derived::showDr()
{
    cout << "dr=" << dr << "\n";
}

int main()
{
    Derived drv;
    drv.showBs1();
    drv.showBs2();
    drv.showDr();
    cout << "TYPE ANY CHAR, THEN RETURN>";
    char c;
    cin >> c;
    cout << "\n";
    return 0;
}

_◇クラステンプレート

#include <iostream>
using namespace std;

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 << "setData:ERROR.\n";
    else
        data[num] = d;
}

template <class T>
T Array<T>::getData(int num)
{
    if (num < 0 || num > 4) {
        cout << "getData:ERROR\n";
        return data[0];
    }
    else
        return data[num];
}

int main()
{
    cout << "Make int Array\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 << "Make double Array\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';
    cout << "TYPE ANY CHAR, THEN RETURN>";
    char c;
    cin >> c;
    cout << '\n';
    return 0;
}

_◇STL

①vector

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

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

    cout << "Enter # of integer>\n";
    cin >> num;

    for (int i=0; i<num; i++) {
        int data;
        cout << "Enter Integer>\n";
        cin >> data;
        vt.push_back(data);
    }

    cout << "DISPLAY DATA\n";
    vector<int>::iterator it = vt.begin();
    while(it != vt.end()) {
        cout << *it;
        cout << "-";
        it++;
    }
    cout << "\n";

    cout << "TYPE ANY CHAR, THEN RETURN>";
    char c;
    cin >> c;
    cout << "\n";
}

②sort

#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 << "Before sort = ";
    vector<int>::iterator it = vt.begin();
    while(it != vt.end()) {
        cout << *it;
        it++;
    }
    cout << "\n";
    cout << "Reverse = ";
    reverse(vt.begin(), vt.end());
    it = vt.begin();
    while(it != vt.end()) {
        cout << *it;
        it++;
    }
    cout << "\n";
    cout << "After sort = ";
    sort(vt.begin(), vt.end());
    it = vt.begin();
    while(it != vt.end()) {
        cout << *it;
        it++;
    }
    cout << "\n";
    cout << "TYPE ANY CHAR, THEN RETURN>";
    char c;
    cin >> c;
    cout << "\n";
    return 0;
}

_◇例外処理

#include <iostream>
using namespace std;

void pause();

int main()
{
    char c;
    int num;
    cout << "Enter 1~9>\n";
    cin >> num;
    try {
        if (num <= 0)
            throw "Under 0.";
        if (num >= 10)
            throw "Over 10.";
        cout << "NUM=" << num << "\n";
    }
    catch(char* err) {
        cout << "ERROR: " << err << '\n';
        pause();
        return 1;
    }
    pause();
    return 0;
}

void pause()
{
    char c;
    cout << "TYPE ANY CHAR, THEN RETURN>";
    cin >> c;
    cout << '\n';
}

_◇入出力

①一文字入出力

#include <iostream>
using namespace std;

int main()
{
    char ch;
    cout << "Enter CHARs(^Z for Exit)>\n";
    while (cin.get(ch)) {
        cout.put(ch);
    }
    return 0;
}

②出力幅の設定

#include <iostream>
using namespace std;

int main()
{
    for (int i=0; i<=10; i++) {
        cout.width(3);
        cout << i;
    }
    cout << '\n';
    cout << "TYPE ANY KEY, THEN RETURN>";
    char c;
    cin >> c;
    cout << '\n';
    return 0;
}

③精度指定

#include <iostream>
using namespace std;

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

    cout << "DISPLAY PI\n";
    cout << "ENTER PRECISION?(1-7)\n";
    cin >> num;

    cout.precision(num);
    cout << "PI=" << pi << '\n';

    cout << "TYPE ANY KEY THEN RETURN>";
    char c;
    cin >> c;
    cout << '\n';

    return 0;
}

④書式状態フラグ

#include <iostream>
using namespace std;

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';

    cout << "TYPE ANY CHAR THEN RETURN>";
    char c;
    cin >> c;
    cout << '\n';

    return 0;
}

⑤テキストファイル出力 1

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

void pause();

int main()
{
    ofstream fout("test1.txt");
    if (!fout) {
        cout << "FILE OPEN ERROR.\n";
        pause();
        return 1;
    }
    else
        cout << "FILE OPENED.\n";

    fout << "Hello!\n";
    fout << "Goodbye!\n";
    cout << "WRITING FILE.\n";

    fout.close();
    cout << "FILE CLOSED.\n";
    pause();
    return 0;
}

void pause()
{
    cout << "TYPE ANY CHAR THEN RETURN>";
    char c;
    cin >> c;
    cout << '\n';
}

⑥テキストファイル入力 2

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

void pause();

int main()
{
    ofstream fout("test2.txt");
    if (!fout) {
        cout << "FILE OPEN ERROR.\n";
        pause();
        return 1;
    }

    const int num = 5;
    int test[num];
    cout << "Enter Points for " << 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();

    pause();
    return 0;
}

void pause()
{
    cout << "TYPE ANY CHAR THEN RETURN>";
    char c;
    cin >> c;
    cout << '\n';
}

⑦テキストファイル入力

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

void pause();

int main()
{
    ifstream fin("test1.txt");
    if (!fin) {
        cout << "FILE OPEN ERROR.\n";
        pause();
        return 1;
    }
    char str1[16];
    char str2[16];
    fin >> str1 >> str2;
    cout << "INPUT STRING>\n";
    cout << str1 << '\n';
    cout << str2 << '\n';

    fin.close();

    pause();
    return 0;
}

void pause()
{
    cout << "TYPE ANY CHAR THEN RETURN>";
    char c;
    cin >> c;
    cout << '\n';
}

_◇コマンドライン引数

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

void pause();

int main(int argc, char* argv[])
{
    if (argc != 2) {
        cout << "Command Line Parameter Error.\n";
        pause();
        return 1;
    }
    ifstream fin(argv[1]);
    if (!fin) {
        cout << "FILE OPEN ERROR.\n";
        pause();
        return 1;
    }
    char ch;
    fin.get(ch);
    while(!fin.eof()) {
        cout.put(ch);
        fin.get(ch);
    }
    fin.close();
    pause();
    return 0;
}

void pause()
{
    cout << "TYPE ANY CHAR THEN RETURN>";
    char c;
    cin >> c;
    cout << '\n';
}

_◇バイナリサーチ

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

int int_cmp(const int* a, const int* b)
{
    if (*a < *b)
        return -1;
    else if (*a > *b)
        return 1;
    else
        return 0;
}

int main()
{
    int nx;
    cout << "nMatrix:";
    cin >> nx;
    int* x = new int [nx];
    cout << nx << " Enter Items\n";
    cout << "x[0] : ";
    cin >> x[0];
    for (int i = 1; i < nx; i++) {
        do {
            cout << "x[" << i << "] : ";
            cin >> x[i];
        } while (x[i] < x[i - 1]);
    }
    int no;
    cout << "Search for : ";
    cin >> no;
    int* p = reinterpret_cast<int*>(bsearch(&no, x, nx, sizeof(int),
                reinterpret_cast<int (*)(const void*, const void*)>(int_cmp)));
    if (p != NULL)
        cout << "x[" << (p-x) << "]\n";
    else
        cout << "Not Found.\n";
}


◆処理小ネタ

_◇ビット数カウント

int count_bits(unsigned x)
{
    int    bits = 0;
    while (x) {
        if (x & 1U) bits++;
        xx >>= 1;
    }
    return bits;
}

int count_bits(unsigned x)
{
    int    bits = 0;
    while (x) {
        x &= x - 1;
        bits++;
    }
    return bits;
}


◆コンソールアプリケーション

_◇Hello

#include <iostream>
using namespace std;

int main()
{
    char c;

    cout << "Hello!\n";

    cout << "Type 1 char, then Return.\n";
    cin >> c;
    return 0;
}

_◇数値入力

#include <iostream>
using namespace std;

int main()
{
    int num1, num2;
    char c;

    cout << "整数を2つ入力、1つずつEnterすること。\n";
    cin >> num1 >> num2;

    cout << "最初=" << num1 << "\n";
    cout << "次=" << num2 << "\n";

    cout << "Type 1 char, then Return.\n";
    cin >> c;
    return 0;
}


◆各種クラス

_◇ビットベクトルによる集合クラス

※ビットベクトル
bit vector
⇒整数を構成するビットの並び

※ビットベクトルによる集合
ある「それほど大きくない」整数値maxについて
│ 0、1、。。。、max-1
の整数を要素とする集合
⇒ビットベクトルとして単一整数で表現できる

例)32ビットの符号無し整数1個で
{0,1,…,31}を普遍集合(universal set)とする整数の集合が表現できる
⇒普遍集合(全体集合)