Up

「第5章 型」Win32コンソールアプリケーション版

 

 <岡本安晴「大学生のための心理学VC++プログラミング入門」>の第5章のサンプルプログラムのWin32版をVisual C++2008で作成した。プログラムファイルは圧縮ファイルとしてまとめた。圧縮ファイルはクリックしてダウンロードしたものをマウスの右ボタンでクリックして表示されるメニュから「解凍」あるいは「展開」を選んで解凍することができる。解凍したプログラムファイルはVisual C++2010でも開くことができる。


リスト5.1-1Win32

圧縮ファイルsample5_1_1win32.zip

=============================================

#include "stdafx.h"

#include <iostream>

 

using namespace std;

 

int _tmain(int argc, _TCHAR* argv[])

{

        int i = 1;

        unsigned int ui = 1;

        int ck = 0;

        do{

                i *= 2;  ui *= 2;

                cout << "ck = " << ++ck << "   i = " << i << "   ui = " << ui << endl;

        } while (ui != 0);

        cout << endl << "ck = " << ck << "   i = " << i << "   ui = " << ui << endl;

 

        cout << endl << "何か文字を入力して終了。" << endl;

        char cc;

        cin >> cc;

        return 0;

}

 


リスト5.2-1Win32

圧縮ファイルsample5_2_1win32.zip

===============================================

#include "stdafx.h"

#include <iostream>

 

using namespace std;

 

int _tmain(int argc, _TCHAR* argv[])

{

        for (char c = 'a'; c <= 'z'; c++)

                cout << " " << c << "/" << int(c);

        cout << endl << endl;

        for (char c = 'A'; c <= 'Z'; c++)

                cout << " " << c << "/" << int(c);

        cout << endl << endl;

        for (char c = '0'; c <= '9'; c++)

                cout << " " << c << "/" << int(c);

        cout << endl << endl;

        for (int i = 97; i <= 122; i++)

                cout << char(i);

        cout << endl << endl;

        for (int i = 65; i <= 90; i++)

                cout << char(i);

        cout << endl << endl;

        for (int i = 48; i <= 57; i++)

                cout << char(i);

        cout << endl;

 

        cout << endl << "何か文字を入力して終了。" << endl;

        char ck;

        cin >> ck;

        return 0;

}

 


リスト5.2-2Win32

圧縮ファイルsample5_2_2win32.zip

=====================================================

#include "stdafx.h"

#include <iostream>

 

using namespace std;

 

int _tmain(int argc, _TCHAR* argv[])

{

        cout << "tab/" << int('\t') << endl;

        cout << "new line/" << int('\n') << endl;

 

        for (int i = 0; i < 5; i++)

                cout << 'A' << '\t';

        cout << endl;

        for (int i = 0; i < 5; i++)

                cout << 'a' << char(9);

        cout << endl;

        for (int i = 0; i < 5; i++)

                cout << "ABC" << '\t';

        cout << endl;

        for (int i = 0; i < 5; i++)

                cout << "abc" << char(9);

        cout << endl << endl;

        for (int i = 0; i < 5; i++)

                cout << 'A' << '\n';

        for (int i = 0; i < 5; i++)

                cout << 'a' << char(10);

        cout << endl;

 

        cout << endl << "何か文字を入力して終了。" << endl;

        char ck;

        cin >> ck;

        return 0;

}

 


リスト5.3-1Win32

圧縮ファイルsample5_3_1win32.zip

=================================================

#include "stdafx.h"

#include <iostream>

 

using namespace std;

 

int _tmain(int argc, _TCHAR* argv[])

{

        enum week { sun = 1, mon, tue, wed, thu, fri, sat };

        cout << "日曜日-> " << sun << endl;

        cout << "月曜日-> " << mon << endl;

        cout << "火曜日-> " << tue << endl;

        cout << "水曜日-> " << wed << endl;

        cout << "木曜日-> " << thu << endl;

        cout << "金曜日-> " << fri << endl;

        cout << "土曜日-> " << sat << endl;

 

        int n;

        while (true) {

                cout << endl << "今日の曜日の番号? ";

                cin >> n;

                if ((n < 1) || (n > 7)){

                        cout << "入力番号= " << n << "...1から7までの数値を入力。" << endl;

                } else break;

        }

        cout << endl << "今日は";

        switch (n) {

                case sun : cout << "日曜日"; break;

                case mon : cout << "月曜日"; break;

                case tue : cout << "火曜日"; break;

                case wed : cout << "水曜日"; break;

                case thu : cout << "木曜日"; break;

                case fri : cout << "金曜日"; break;

                case sat : cout << "土曜日"; break;

                default :  cout << "???"; break;

        }

        cout << "です。" << endl;

 

        cout << endl << "何か文字を入力して終了。" << endl;

        char ck;

        cin >> ck;

        return 0;

}

 


リスト5.4-1Win32

圧縮ファイルsample5_4_1win32.zip

===================================================

#include "stdafx.h"

#include <iostream>

 

using namespace std;

 

int _tmain(int argc, _TCHAR* argv[])

{

        int i = 1, j = 2;

        int * p, * q;

        p = &i;

        q = &j;

        cout << "i = " << i << "\t   j = " << j << endl;

        cout << "*p = " << *p << "\t   *q = " << *q << endl;

 

        q = p;

        *p = 5;

        cout <<endl << "i = " << i << "\t   j = " << j << endl;

        cout << "*p = " << *p << "\t   *q = " << *q << endl;

 

        cout << endl << "何か文字を入力して終了。" << endl;

        char ck;

        cin >> ck;

        return 0;

}

 


リスト5.4-2Win32

圧縮ファイルsample5_4_2win32.zip

===================================================

#include "stdafx.h"

#include <iostream>

 

using namespace std;

 

int _tmain(int argc, _TCHAR* argv[])

{

        int * p = new int;

        int * q = new int;

        cout << "*p = ";

        cin >> *p;

        cout << "*q = ";

        cin >> *q;

        cout << "*p + *q = " << *p + *q << endl;

        delete p;

        delete q;

 

        cout << endl << "何か文字を入力して終了。" << endl;

        char ck;

        cin >> ck;

        return 0;

}

 


リスト5.5-1Win32

圧縮ファイルsample5_5_1win32.zip

==================================================

#include "stdafx.h"

#include <iostream>

 

using namespace std;

 

int _tmain(int argc, _TCHAR* argv[])

{

        int * a;

        a = new int[3];

        for (int i = 0; i < 3; i++)

                a[i] = i+1;

        for (int i = 0; i < 3; i++)

                cout << "a[" << i << "] = " << a[i] << endl;

        delete [] a;

        double *** c;

        c = new double**[2];

        for (int i = 0; i < 2; i++){

                c[i] = new double *[3];

                for (int j = 0; j < 3; j++)

                        c[i][j] = new double[4];

        }

        for (int i = 0; i < 2; i++)

                for (int j = 0; j < 3; j++)

                        for (int k = 0; k < 4; k++)

                                c[i][j][k] = (i+1) * 100 + (j+1) * 10 + (k+1);

        for (int i = 0; i < 2; i++)

                for (int j = 0; j < 3; j++){

                        for (int k = 0; k < 4; k++)

                                cout << "  c[" << i << "][" << j << "]["

                                                << k << "] = " << c[i][j][k];

                        cout << endl;

                }

        for (int i = 0; i < 2; i++){

                for (int j = 0; j < 3; j++)

                        delete [] c[i][j];

                delete [] c[i];

        }

        delete [] c;

 

        cout << endl << "何か文字を入力して終了。" << endl;

        char ck;

        cin >> ck;

        return 0;

}

 


 

第4章用のWin32版サンプルプログラム

第6章用のWin32版サンプルプログラム

 

Up