#include <iostream>
using namespace std;
void koukan(int *x, int *y){ // (※)
int tmp = *x; // (※)
*x = *y; // (※)
*y = tmp; // (※)
}
int main(){
int x=3;
int y=4;
cout << "交換前\n";
cout << "x=" << x << ", y=" << y << "\n";
koukan(&x,&y); // (※)
cout << "交換後\n";
cout << "x=" << x << ", y=" << y << "\n";
return 0;
}
|
#include <iostream>
using namespace std;
void koukan(int &x, int &y){ // (※)
int tmp = x;
x = y;
y = tmp;
}
int main(){
int x=3;
int y=4;
cout << "交換前\n";
cout << "x=" << x << ", y=" << y << "\n";
koukan(x,y);
cout << "交換後\n";
cout << "x=" << x << ", y=" << y << "\n";
return 0;
}
|
#include <iostream>
using namespace std;
class AND{
int out;
public:
AND(){ out = 2; } // (※) 引数なしのコンストラクタ
void calc_out(int a, int b);
int get_out(){ return out; }
};
void AND::calc_out(int a, int b){
// (※) if 文による AND 素子の表現
if(a==2 || b==2){
out = 2;
}else if(a==1 && b==1){
out = 1;
}else{
out = 0;
}
}
|
class AND{
int out, in1, in2; // (※1) 注目
public:
AND(){ out = 2; }
void calc_out(int in1, int in2); // (※2) 注目
int get_out(){ return out; }
};
|
// コンストラクタ
Node::Node(){
for(int i=0 ; i<EDGE_NUM ; i++){
edge[i] = 0; // ヌルポインタで初期化
}
edgenum=0; // (1)
}
// デストラクタ。特に処理は必要ない。
Node::~Node(){
}
void Node::edgeAdd(Edge *e){
if(edgenum>=EDGE_NUM){ // (2)
cout << "can not add edge\n";
}else{
edge[edgenum] = e; // (3)
edgenum++; // (4)
}
}
// コンストラクタ
Edge::Edge(){
node = 0; // ヌルポインタで初期化
}
// デストラクタ。特に処理は必要ない。
Edge::~Edge(){
}
void Edge::nodeAdd(Node *n){
if(node!=0){ // 既にノードにつながっていたら
cout << "can not add node\n";
}else{
node = n; // (5)
}
}
|
*edge[edgenum] = *e; |
#include <iostream>
using namespace std;
#define EDGE_NUM 2 // ノードに接続可能なエッジの数
class Edge;
class Node{
private:
Edge *edge[EDGE_NUM]; // エッジへのポインタの配列
int edgenum; // 現在接続しているエッジの数
int out; // (※)
public:
// コンストラクタ
Node();
// デストラクタ
~Node();
void edgeAdd(Edge *e); // ノードにエッジを接続
void set_out(int i){ out=i; } // (※)
int get_out(){ return out;} // (※)
void set_from_edges(); // (※)
};
class Edge{
private:
Node *node;
public:
// コンストラクタ
Edge();
// デストラクタ
~Edge();
void nodeAdd(Node *n); // エッジにノードを接続
int get_out(); // (※)
};
// コンストラクタ
Node::Node(){
for(int i=0 ; i<EDGE_NUM ; i++){
edge[i] = 0; // ヌルポインタで初期化
}
edgenum=0;
}
// デストラクタ。特に処理は必要ない。
Node::~Node(){
}
void Node::edgeAdd(Edge *e){
if(edgenum>=EDGE_NUM){
cout << "can not add edge\n";
}else{
edge[edgenum] = e;
edgenum++;
}
}
// コンストラクタ
Edge::Edge(){
node = 0; // ヌルポインタで初期化
}
// デストラクタ。特に処理は必要ない。
Edge::~Edge(){
}
void Edge::nodeAdd(Node *n){
if(node!=0){ // 既にノードにつながっていたら
cout << "can not add node\n";
}else{
node = n;
}
}
// (※ 以下は全て新たに付け加えられた部分)
void Node::set_from_edges(){
if(edge[0] && edge[1]){ // 「2つのエッジにともに実体があれば」の意味。「if(edge[0]!=0 && edge[1]!=0)」 と同じ
int out0 = edge[0]->get_out();
int out1 = edge[1]->get_out();
if(out0==2 || out1==2){
out = 2;
}else if(out0==1 && out1==1){
out = 1;
}else{
out = 0;
}
}else{
out = 2;
}
}
int Edge::get_out(){
if(node){ // node に実体があれば。「if(node!=0)」と同じ
return node->get_out();
}else{
return 2;
}
}
|
void Node::set_from_edges(){
Edge e0 = *edge[0]; // 代入が起こる
Edge e1 = *edge[1];
out0 = e0.get_out();
(以下略)
|