Up

「第6章 関数」Win32コンソールアプリケーション版

 

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


リスト6.1-1Win32

圧縮ファイルsample6_1_1win32.zip

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

#include "stdafx.h"

#include <iostream>

 

using namespace std;

 

//

//  x y の和を求める関数calcSum

//

int calcSum( int x, int y){

                int v;

                v = x + y;

                return v;

}

 

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

{

        int a, b, sum;

        cout << "a = ";

        cin >> a;

        cout << "b = ";

        cin >> b;

        sum = calcSum( a, b );

        cout << "a + b = " << sum << endl << endl;

 

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

        char ck;

        cin >> ck;

        return 0;

}

 


リスト6.2-1Win32

圧縮ファイルsample6_2_1win32.zip

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

#include "stdafx.h"

#include <iostream>

 

using namespace std;

 

void SetSum( double a, double b, double & sum ){

                        sum = a + b;

                        return;

}

 

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

{

        double a, b, sum;

        cout << "a = ";

        cin >> a;

        cout << "b = ";

        cin >> b;

        SetSum( a, b, sum );

        cout << "a + b = " << sum << endl;

 

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

        char ck;

        cin >> ck;

        return 0;

}

 


リスト6.3-1Win32

圧縮ファイルsample6_3_1win32.zip

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

#include "stdafx.h"

#include <iostream>

 

using namespace std;

 

void AddVec( double * x, double y[], double z[1], int n ){

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

                                z[i] = x[i] + y[i];

                return;

}

 

void WriteVec( double * x, int n ){

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

                        cout << "\t" << x[i];

                cout << endl;

                return;

}

 

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

{

        double a[] = { 1, 2, 3 };

        double b[3] = { 10, 20, 30 };

        double c[3];

        AddVec( a, b, c, 3 );

        cout << "a = " << endl;

        WriteVec( a, 3 );

        cout << "b = " << endl;

        WriteVec( b, 3 );

        cout << "a + b =" << endl;

        WriteVec( c, 3 );

 

        double * d, * e, * f;

        d = new double[2];

        d[0] = 5;  d[1] = 6;

        e = new double[2];

        e[0] = 50; e[1] = 60;

        f = new double[2];

        AddVec( d, e, f, 2 );

        cout << endl << "d =" << endl;

        WriteVec( d, 2 );

        cout << "e =" << endl;

        WriteVec( e, 2 );

        cout << "d + e =" << endl;

        WriteVec( f, 2 );

 

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

        char ck;

        cin >> ck;

        return 0;

}

 


リスト6.3-2Win32

圧縮ファイルsample6_3_2win32.zip

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

#include "stdafx.h"

#include <iostream>

 

using namespace std;

 

void AddMat( double ** x, double ** y, double ** z, int m, int n ){

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

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

                                z[i][j] = x[i][j] + y[i][j];

                return;

}

 

void WriteMat( double ** x, int m, int n ){

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

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

                                cout << "\t" << x[i][j];

                        cout << endl;

                }

}

 

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

{

        double ** a, ** b, ** c;

        a = new double*[2];

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

                a[i] = new double[2];

        b = new double*[2];

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

                b[i] = new double[2];

        a[0][0] = 1;  a[0][1] = 2;

        a[1][0] = 3;  a[1][1] = 4;

        b[0][0] = 10; b[0][1] = 20;

        b[1][0] = 30; b[1][1] = 40;

        c = new double*[2];

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

                c[i] = new double[2];

        AddMat( a, b, c, 2, 2 );

        cout << "a = " << endl;

        WriteMat( a, 2, 2 );

        cout << "b = " << endl;

        WriteMat( b, 2, 2 );

        cout << "c = " << endl;

        WriteMat( c, 2, 2 );

 

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

        char ck;

        cin >> ck;

        return 0;

}

 


リスト6.4-1Win32

圧縮ファイルsample6_4_1win32.zip

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

#include "stdafx.h"

#include <iostream>

 

using namespace std;

 

double sqr(double x){

        return x * x;

}

 

double cube(double x){

        return x * x * x;

}

 

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

{

        typedef double (*t_pfunc)(double);

 

        t_pfunc pf2 = sqr, pf3 = cube;

        for (double v = 0.0; v < 2.01; v += 0.1){

                cout << "v = " << v << "   \tpf2 = " << (*pf2)(v)

                        << "   \tpf3 = " << pf3(v) << endl;

        }

 

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

        char ck;

        cin >> ck;

        return 0;

}

 


リスト6.4-2Win32

圧縮ファイルsample6_4_2win32.zip

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

#include "stdafx.h"

#include <iostream>

#include <cmath>                //              exp の使用のため

 

using namespace std;

 

double sqr( double x ){

        return x * x;

}

 

double myExp( double x ){

        return exp(x);

}

 

void ShowValues( double (*f1)(double x), double f2(double) ){

        for (double v = 0.0; v < 1.01; v+=0.1){

                cout << "v = " << v << "\t   f1 = " << f1(v)

                        << "\t   f2 = " << (*f2)(v) << endl;

        }

        return;

}

 

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

{

        ShowValues( sqr, myExp );

 

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

        char ck;

        cin >> ck;

        return 0;

}

 


リスト6.5-1Win32

圧縮ファイルsample6_5_1win32.zip

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

#include "stdafx.h"

#include <iostream>

 

using namespace std;

 

double func( double x ){

        return 2 * x;

}

 

double func( double x, double y ){

        return x + y;

}

 

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

{

        double x = 1.0, y = 10.0;

        cout << "x = " << x << "   y = " << y << endl;

        cout << "func(x) = " << func( x ) << endl;

        cout << "func(x,y) = " << func( x, y ) << endl;

 

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

        char ck;

        cin >> ck;

        return 0;

}

 


リスト6.6-1Win32

圧縮ファイルsample6_6_1win32.zip

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

#include "stdafx.h"

#include <iostream>

 

using namespace std;

 

int combi( int n, int r ){

        cout << "n = " << n << "   r = " << r << endl;

        if (n == r){

                                return 1;

        } else if (r == 0){

                                return 1;

        } else if (r == 1){

                                return n;

        } else {

                                return combi( n-1, r-1 ) + combi( n-1, r );

        }

}

 

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

{

        int n, r;

        cout << "n = ";

        cin >> n;

        cout << "r = ";

        cin >> r;

        int nCombi = combi( n, r );

        cout << "組合せの数= " << nCombi << endl;

 

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

        char ck;

        cin >> ck;

        return 0;

}

 


リスト6.7-1Win32

圧縮ファイルsample6_7_1win32.zip

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

#include "stdafx.h"

#include <iostream>

 

using namespace std;

 

void qSort( double * d, int Lb, int Ub ){

        if (Lb < Ub){

                int p = (Lb + Ub) / 2;

                double c = d[p];

                int i = Lb;

                int j = Ub;

                do {

                        while (d[i] < c) i++;

                        while (c < d[j]) j--;

                        if (i <= j){

                                double v = d[i];

                                d[i] = d[j];

                                d[j] = v;

                                i++;

                                j--;

                        }

                } while (i <= j);

                qSort(d, Lb, j);

                qSort(d, i, Ub);

        }

        return;

}

 

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

{

        cout << "基準値= ";

        double c;

        cin >> c;

        double x[1000];

        int n = -1;

        do {

                n++;

                cout << "x[" << n << "] = ";

                cin >> x[n];

                if (x[n] < c) break;

        } while (true);

        cout << "入力データ..." << endl;

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

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

 

        qSort( x, 0, n-1 );

        cout << "整列後..." << endl;

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

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

 

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

        char ck;

        cin >> ck;

        return 0;

}

 


 

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

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

 

Up