Awesome Hacks!

プログラミング初心者なので地道に勉強していきます。分からない人の立場から整理していきます。

C++の自分的速習まとめ

#include <iostream>             // Cでいうところの<stdio.h>
using namespace std;            //これが無いと、各行でstd::coutなどが必要
#include <iomanip>              //setw, left,rightのヘッダー
#include <string>                 //文字列の宣言3(string型)に使用する。

//プロトタイプ宣言
void a_plus_b(void);        // プロトタイプ宣言時の引数には各引数の型名だけ書く
int twice_func(int);
void ab_plusminus_add(double,double, double&, double&);
void ab_plusminus_ptr(double,double, double*, double*);
int array_sum(int[]);   // 配列を引数に扱う関数はちゃんと「[]」をつける

int main(void)
{
    int a=100, b=200;
    cout << "Hello, World." << endl;       // Cでいうところのprintf。endlで改行
    cout << "a+b=" << a+b << endl;    // 書式指定子が不要。かつ、表示順にcoutしてく
    cout << "input a-->" ; cin >> a; cout << "a is " << a << endl;
                                 // 入力受付の方法。そして一行に複数のライン 

    char *str1 = "abc";                      // 文字列の宣言1
    char str2[] = "123456";               // 文字列の宣言2
    string str3 = "7890";                   // **文字列の宣言3  

    // パディング、左寄せ、右寄せ
    cout << setw(20) << str1 << endl    // 20桁スペースパディング時setw(桁数)後に変数
            << setw(20) << str2 << endl;   // 続けてcoutするときは「;」もcoutも不要
    cout << setw(20) << setfill('0') << str1 << endl;   // 20桁ゼロパディング
    cout << left << str1 << str2 << endl;                   // 文字列の連結
    cout << left << str3 << endl;                                // 左寄せ

    // 書式指定する場合 16進、8進、10進表示
    cout << hex << 30 << endl;
    cout << oct << 30 << endl;
    cout << dec << 30 << endl;

    // 関数呼び出し
    a_plus_b();                // 関数(引数・戻り値無し)
    cout << "3*2=" << twice_func(3) << endl;     // 関数(引数1つ・戻り値1つ)

    int seki,shou;
    ab_plusminus_add(7.3, 4.2, seki, shou);
    cout << "7.3*4.2=" << seki << endl;              // 関数(引数2つ・戻り値2つ)
    cout << "7.3/4.2=" << shou << endl;              // 関数(引数2つ・戻り値2つ)

    ab_plusminus_ptr(3.5, 2.8, seki, shou);
    cout << "3.5*2.8=" << seki << endl;              // 関数(引数2つ・戻り値2つ)
    cout << "3.5/2.8=" << shou << endl;              // 関数(引数2つ・戻り値2つ)

    // 関数に配列(1〜10まで昇順に格納されたもの)を渡して合計を返す
    int array[]={1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, sum;
    sum=array_sum(array);      // 配列の先頭アドレスを渡す
    cout << "1から10までの合計は " << sum << endl;

    return 0;
}

//関数(引数・戻り値無し)
void a_plus_b(void)
{
    int a, b;
    cout << "Input a-->"; cin >> a;
    cout << "Input b-->"; cin >> b;
    cout << "a+b=" << a+b << endl;
}

//関数(引数1つ・戻り値1つ)   returnで返す
int twice_func(int ta)
{
    return(ta*2);
}


// 関数(引数2つ・戻り値2つ)   引数で返す
// 参照渡しの場合
void ab_plusminus_add(double pa,double pb, double& seki, double& shou)
{
    seki=pa*pb;        // 入出力しないときはC言語と同じ書き方。
    shou=pa/pb;       // 引数で返す時は、引数にてアドレスで返す(上記三行上参照)
                                // なお、戻り値は使わないので関数名は「void 関数名(〜)」
}

// ポインタ渡しの場合
void ab_plusminus_ptr(double pa,double pb, double* seki, double* shou)
{
    *seki=pa*pb;        // 参照渡し時は計算結果を「*seki」に代入
    *shou=pa/pb;       // ※「*」が無いと「seki」はアドレスを表すため
    // ちなみに、多分以下と同じ
    // double tmp_shou;
    // tmp_shou=pa/pb
    // shou=*tmp_shou;
}

int array_sum(int array[])
{
   int goukei=0;

    for(int i=0; i<10; i++){
        goukei+=x[i];
    }
    return(goukei);
}