| 情報学部 | 菅沼ホーム | 全体目次 | 演習解答例 | 付録 | 索引 |
/*******************************/
/* 代入(newを使用しない場合) */
/* coded by Y.Suganuma */
/*******************************/
#include <iostream>
using namespace std;
/***********************/
/* クラスComplexの定義 */
/***********************/
class Complex {
public:
double r, i;
Complex (double r1, double i1) // コンストラクタ
{
r = r1;
i = i1;
}
};
/***********************/
/* クラスVector1の定義 */
/***********************/
class Vector1 {
public:
int n;
double x[2];
Vector1 (int n1, double x1, double x2) // コンストラクタ
{
n = n1;
x[0] = x1;
x[1] = x2;
}
};
/************/
/* main関数 */
/************/
int main()
{
Complex c1(1, 2), c2(3, 4);
Vector1 v1(1, 0.0, 0.0), v2(2, -1.0, -2.0);
// 代入前
cout << endl;
cout << " c1(代入前) " << c1.r << " " << c1.i << endl;
cout << " c2(代入前) " << c2.r << " " << c2.i << endl;
cout << " v1(代入前) " << v1.n << " " << v1.x[0] << " " << v1.x[1] << endl;
cout << " v2(代入前) " << v2.n << " " << v2.x[0] << " " << v2.x[1] << endl;
// 代入
c1 = c2;
v1 = v2;
// 代入後
cout << endl;
cout << " c1(c2をc1に代入後) " << c1.r << " " << c1.i << endl;
cout << " c2(c2をc1に代入後) " << c2.r << " " << c2.i << endl;
cout << " v1(v2をv1に代入後) " << v1.n << " " << v1.x[0] << " " << v1.x[1] << endl;
cout << " v2(v2をv1に代入後) " << v2.n << " " << v2.x[0] << " " << v2.x[1] << endl;
// 値の変更
c2.r = -1.5;
v2.x[0] = 1.5;
// 変更後の値
cout << endl;
cout << " c1(c2.rを-1.5に変更後) " << c1.r << " " << c1.i << endl;
cout << " c2(c2.rを-1.5に変更後) " << c2.r << " " << c2.i << endl;
cout << " v1(v2.x[0]を1.5に変更後) " << v1.n << " " << v1.x[0] << " " << v1.x[1] << endl;
cout << " v2(v2.x[0]を1.5に変更後) " << v2.n << " " << v2.x[0] << " " << v2.x[1] << endl;
return 0;
}
c1(代入前) 1 2
c2(代入前) 3 4
v1(代入前) 1 0 0
v2(代入前) 2 -1 -2
c1(c2をc1に代入後) 3 4
c2(c2をc1に代入後) 3 4
v1(v2をv1に代入後) 2 -1 -2
v2(v2をv1に代入後) 2 -1 -2
c1(c2.rを-1.5に変更後) 3 4
c2(c2.rを-1.5に変更後) -1.5 4
v1(v2.x[0]を1.5に変更後) 2 -1 -2
v2(v2.x[0]を1.5に変更後) 2 1.5 -2
/*****************************/
/* 代入(newを使用する場合) */
/* coded by Y.Suganuma */
/*****************************/
#include <iostream>
using namespace std;
/***********************/
/* クラスVector2の定義 */
/***********************/
class Vector2 {
public:
int n;
double *x;
Vector2 (int n1, double x1, double x2) // コンストラクタ
{
n = n1;
x = new double [2];
x[0] = x1;
x[1] = x2;
}
// ~Vector2 () {delete [] x;} // デストラクタ
};
/************/
/* main関数 */
/************/
int main()
{
Vector2 v3(3, 0.0, 0.0), v4(4, -1.0, -2.0);
// 代入前
cout << endl;
cout << " v3(代入前) " << v3.n << " " << v3.x[0] << " " << v3.x[1] << endl;
cout << " v4(代入前) " << v4.n << " " << v4.x[0] << " " << v4.x[1] << endl;
// 代入
v3 = v4;
// 代入後
cout << endl;
cout << " v3(v4をv3に代入後) " << v3.n << " " << v3.x[0] << " " << v3.x[1] << endl;
cout << " v4(v4をv3に代入後) " << v4.n << " " << v4.x[0] << " " << v4.x[1] << endl;
// 値の変更
v4.n = 5;
v4.x[0] = 1.5;
// 変更後の値(v3.x[0]も変化してしまう)
// (デストラクタが定義されていると実行時にエラー)
cout << endl;
cout << " v3(v4.nを5,v4.x[0]を1.5に変更後) " << v3.n << " " << v3.x[0] << " " << v3.x[1] << endl;
cout << " v4(v4.nを5,v4.x[0]を1.5に変更後) " << v4.n << " " << v4.x[0] << " " << v4.x[1] << endl;
return 0;
}
v3(代入前) 3 0 0
v4(代入前) 4 -1 -2
v3(v4をv3に代入後) 4 -1 -2
v4(v4をv3に代入後) 4 -1 -2
v3(v4.nを5,v4.x[0]を1.5に変更後) 4 1.5 -2
v4(v4.nを5,v4.x[0]を1.5に変更後) 5 1.5 -2
/****************************/
/* 代入問題の解決 */
/* coded by Y.Suganuma */
/****************************/
#include <iostream>
using namespace std;
/***********************/
/* クラスVector2の定義 */
/***********************/
class Vector2 {
public:
int n;
double *x;
Vector2 (int n1, double x1, double x2) // コンストラクタ
{
n = n1;
x = new double [2];
x[0] = x1;
x[1] = x2;
}
~Vector2 () {delete [] x;} // デストラクタ
Vector2& operator= (const Vector2 &); // =のオーバーロード
};
/*******************************/
/* =のオーバーロード */
/* a = b (a.operator=(b)) */
/*******************************/
Vector2& Vector2::operator= (const Vector2 &b)
{
if (&b == this) // 自分自身への代入を防ぐ
return *this;
else {
delete [] x; // 代入する前のメモリを解放
x = new double [2]; // メモリの確保
n = b.n; // 値の代入
x[0] = b.x[0];
x[1] = b.x[1];
return *this;
}
}
/************/
/* main関数 */
/************/
int main()
{
Vector2 v3(3, 0.0, 0.0), v4(4, -1.0, -2.0);
// 代入前
cout << endl;
cout << " v3(代入前) " << v3.n << " " << v3.x[0] << " " << v3.x[1] << endl;
cout << " v4(代入前) " << v4.n << " " << v4.x[0] << " " << v4.x[1] << endl;
// 代入
v3 = v4;
// 代入後
cout << endl;
cout << " v3(v4をv3に代入後) " << v3.n << " " << v3.x[0] << " " << v3.x[1] << endl;
cout << " v4(v4をv3に代入後) " << v4.n << " " << v4.x[0] << " " << v4.x[1] << endl;
// 値の変更
v4.n = 5;
v4.x[0] = 1.5;
// 変更後の値
cout << endl;
cout << " v3(v4.nを5,v4.x[0]を1.5に変更後) " << v3.n << " " << v3.x[0] << " " << v3.x[1] << endl;
cout << " v4(v4.nを5,v4.x[0]を1.5に変更後) " << v4.n << " " << v4.x[0] << " " << v4.x[1] << endl;
return 0;
}
v3(代入前) 3 0 0
v4(代入前) 4 -1 -2
v3(v4をv3に代入後) 4 -1 -2
v4(v4をv3に代入後) 4 -1 -2
v3(v4.nを5,v4.x[0]を1.5に変更後) 4 -1 -2
v4(v4.nを5,v4.x[0]を1.5に変更後) 5 1.5 -2
Complex c3 = c4; Vector1 v5 = v6;
/*********************************/
/* 初期化(newを使用しない場合) */
/* coded by Y.Suganuma */
/*********************************/
#include <iostream>
using namespace std;
/***********************/
/* クラスComplexの定義 */
/***********************/
class Complex {
public:
double r, i;
Complex (double r1, double i1) // コンストラクタ
{
r = r1;
i = i1;
}
};
/***********************/
/* クラスVector1の定義 */
/***********************/
class Vector1 {
public:
int n;
double x[2];
Vector1 (int n1, double x1, double x2) // コンストラクタ
{
n = n1;
x[0] = x1;
x[1] = x2;
}
};
/************/
/* main関数 */
/************/
int main()
{
Complex c4(3, 4);
Complex c3 = c4;
Vector1 v6(2, -1.0, -2.0);
Vector1 v5 = v6;
// 変更前
cout << endl;
cout << " c3(変更前) " << c3.r << " " << c3.i << endl;
cout << " c4(変更前) " << c4.r << " " << c4.i << endl;
cout << " v5(変更前) " << v5.n << " " << v5.x[0] << " " << v5.x[1] << endl;
cout << " v6(変更前) " << v6.n << " " << v6.x[0] << " " << v6.x[1] << endl;
// 値の変更
c4.r = -1.5;
v6.x[0] = 1.5;
// 変更後の値
cout << endl;
cout << " c3(c4.rを-1.5に変更後) " << c3.r << " " << c3.i << endl;
cout << " c4(c4.rを-1.5に変更後) " << c4.r << " " << c4.i << endl;
cout << " v5(v6.x[0]を1.5に変更後) " << v5.n << " " << v5.x[0] << " " << v5.x[1] << endl;
cout << " v6(v6.x[0]を1.5に変更後) " << v6.n << " " << v6.x[0] << " " << v6.x[1] << endl;
return 0;
}
c3(変更前) 3 4
c4(変更前) 3 4
v5(変更前) 2 -1 -2
v6(変更前) 2 -1 -2
c3(c4.rを-1.5に変更後) 3 4
c4(c4.rを-1.5に変更後) -1.5 4
v5(v6.x[0]を1.5に変更後) 2 -1 -2
v6(v6.x[0]を1.5に変更後) 2 1.5 -2
/*******************************/
/* 初期化(newを使用する場合) */
/* coded by Y.Suganuma */
/*******************************/
#include <iostream>
using namespace std;
/***********************/
/* クラスVector2の定義 */
/***********************/
class Vector2 {
public:
int n;
double *x;
Vector2 (int n1, double x1, double x2) // コンストラクタ
{
n = n1;
x = new double [2];
x[0] = x1;
x[1] = x2;
}
// ~Vector2 () {delete [] x;} // デストラクタ
};
/************/
/* main関数 */
/************/
int main()
{
Vector2 v8(4, -1.0, -2.0);
Vector2 v7 = v8;
// 変更前
cout << endl;
cout << " v7(変更前) " << v7.n << " " << v7.x[0] << " " << v7.x[1] << endl;
cout << " v8(変更前) " << v8.n << " " << v8.x[0] << " " << v8.x[1] << endl;
// 値の変更
v8.n = 5;
v8.x[0] = 1.5;
// 変更後の値(v7.x[0]も変化してしまう)
// (デストラクタが定義されていると実行時にエラー)
cout << endl;
cout << " v7(v8.nを5,v8.x[0]を1.5に変更後) " << v7.n << " " << v7.x[0] << " " << v7.x[1] << endl;
cout << " v8(v8.nを5,v8.x[0]を1.5に変更後) " << v8.n << " " << v8.x[0] << " " << v8.x[1] << endl;
return 0;
}
v7(変更前) 4 -1 -2
v8(変更前) 4 -1 -2
v7(v8.nを5,v8.x[0]を1.5に変更後) 4 1.5 -2
v8(v8.nを5,v8.x[0]を1.5に変更後) 5 1.5 -2
/****************************/
/* 初期化問題の解決 */
/* coded by Y.Suganuma */
/****************************/
#include <iostream>
using namespace std;
/***********************/
/* クラスVector2の定義 */
/***********************/
class Vector2 {
public:
int n;
double *x;
Vector2 (int n1, double x1, double x2) // コンストラクタ
{
n = n1;
x = new double [2];
x[0] = x1;
x[1] = x2;
}
Vector2 (const Vector2 &b) // 初期化のためのコンストラクタ
{
x = new double [2]; // メモリの確保
n = b.n; // 値の代入
x[0] = b.x[0];
x[1] = b.x[1];
}
~Vector2 () {delete [] x;} // デストラクタ
Vector2& operator= (const Vector2 &); // =のオーバーロード
};
/************/
/* main関数 */
/************/
int main()
{
Vector2 v8(4, -1.0, -2.0);
Vector2 v7 = v8;
// 変更前
cout << endl;
cout << " v7(変更前) " << v7.n << " " << v7.x[0] << " " << v7.x[1] << endl;
cout << " v8(変更前) " << v8.n << " " << v8.x[0] << " " << v8.x[1] << endl;
// 値の変更
v8.n = 5;
v8.x[0] = 1.5;
// 変更後の値
cout << endl;
cout << " v7(v8.nを5,v8.x[0]を1.5に変更後) " << v7.n << " " << v7.x[0] << " " << v7.x[1] << endl;
cout << " v8(v8.nを5,v8.x[0]を1.5に変更後) " << v8.n << " " << v8.x[0] << " " << v8.x[1] << endl;
return 0;
}
v7(変更前) 4 -1 -2
v8(変更前) 4 -1 -2
v7(v8.nを5,v8.x[0]を1.5に変更後) 4 -1 -2
v8(v8.nを5,v8.x[0]を1.5に変更後) 5 1.5 -2
/****************************/
/* 行列の乗算 */
/* coded by Y.Suganuma */
/****************************/
#include <stdio.h>
#include <stdlib.h>
/**********************/
/* クラスMatrixの定義 */
/**********************/
class Matrix { /* 2次元行列 */
int n; // 行の数
int m; // 列の数
double **mat; // 行列本体
public:
Matrix(int, int); // コンストラクタ(引数あり)
Matrix(const Matrix &); // 初期化のためのコンストラクタ
Matrix() {n = 0;} // コンストラクタ(引数無し)
~Matrix()
{
if (n > 0) {
for (int i1 = 0; i1 < n; i1++)
delete [] mat[i1];
delete [] mat;
}
}
void input(); // 入力
void output(); // 出力
Matrix& operator= (const Matrix &); // =のオーバーロード
friend Matrix operator* (const Matrix &, const Matrix &); // *のオーバーロード
};
/******************************/
/* コンストラクタ(引数あり) */
/******************************/
Matrix::Matrix(int n1, int m1)
{
n = n1;
m = m1;
mat = new double * [n];
for (int i1 = 0; i1 < n; i1++)
mat[i1] = new double [m];
}
/********************************/
/* 初期化のためのコンストラクタ */
/********************************/
Matrix::Matrix(const Matrix &A)
{
n = A.n;
m = A.m;
mat = new double * [n];
for (int i1 = 0; i1 < n; i1++) {
mat[i1] = new double [m];
for (int i2 = 0; i2 < m; i2++)
mat[i1][i2] = A.mat[i1][i2];
}
}
/********/
/* 入力 */
/********/
void Matrix::input()
{
for (int i1 = 0; i1 < n; i1++) {
printf("%d行目のデータ\n", i1+1);
for (int i2 = 0; i2 < m; i2++) {
printf(" %d列目の値は?", i2+1);
scanf("%lf", &mat[i1][i2]);
}
}
}
/********/
/* 出力 */
/********/
void Matrix::output()
{
for (int i1 = 0; i1 < n; i1++) {
for (int i2 = 0; i2 < m; i2++)
printf("%f ", mat[i1][i2]);
printf("\n");
}
}
/**********************/
/* *のオーバーロード */
/**********************/
Matrix operator* (const Matrix &A, const Matrix &B)
{
Matrix C;
if (A.m != B.n) {
printf("***error invalid data\n");
exit(1);
}
else {
C = Matrix(A.n, B.m);
for (int i1 = 0; i1 < A.n; i1++) {
for (int i2 = 0; i2 < B.m; i2++) {
C.mat[i1][i2] = 0.0;
for (int i3 = 0; i3 < A.m; i3++)
C.mat[i1][i2] += A.mat[i1][i3] * B.mat[i3][i2];
}
}
}
return C;
}
/*******************************/
/* =のオーバーロード */
/* a = b (a.operator=(b)) */
/*******************************/
Matrix& Matrix::operator= (const Matrix &B)
{
if (&B != this) { // 自分自身への代入を防ぐ
if (n > 0) { // 代入する前のメモリを解放
for (int i1 = 0; i1 < n; i1++)
delete [] mat[i1];
delete [] mat;
}
n = B.n;
m = B.m;
mat = new double * [n]; // メモリの確保と代入
for (int i1 = 0; i1 < n; i1++) {
mat[i1] = new double [m];
for (int i2 = 0; i2 < m; i2++)
mat[i1][i2] = B.mat[i1][i2];
}
}
return *this;
}
/************/
/* main関数 */
/************/
int main()
{
int n, m, l;
printf("行列Aの行と列の数は? ");
scanf("%d %d", &n, &m);
printf("行列Bの列の数は? ");
scanf("%d", &l);
Matrix A(n, m);
Matrix B(m, l);
Matrix C;
printf("行列A\n");
A.input();
printf("行列B\n");
B.input();
C = A * B;
C.output();
return 0;
}
行列Aの行と列の数は? 3 2 行列Bの列の数は? 2 行列A 1行目のデータ 1列目の値は?1 2列目の値は?2 2行目のデータ 1列目の値は?3 2列目の値は?4 3行目のデータ 1列目の値は?5 2列目の値は?6 行列B 1行目のデータ 1列目の値は?1 2列目の値は?0 2行目のデータ 1列目の値は?0 2列目の値は?1 1.000000 2.000000 3.000000 4.000000 5.000000 6.000000
| 情報学部 | 菅沼ホーム | 全体目次 | 演習解答例 | 付録 | 索引 |