stack class

stckとtosの非公開変数

#include 
using namespace std;

#define SIZE 10

class stack {
    char stck[SIZE];
    int tos;
    
public:
    void init();
    void push(char ch);
    char pop();
};

void stack::init()
{
    tos = 0;
}

void stack::push(char ch)
{
    if(tos==SIZE){
        cout << "stack is full";
        return;
    }
    stck[tos] = ch;
    tos++;
}

char stack::pop()
{
    if(tos==0){
        cout << "tack is empty";
        return 0;
    }
    tos--;
    return stck[tos];
}

int main()
{
    stack s1, s2;
    int i;
    
    s1.init();
    s2.init();
    s1.push('a');
    s2.push('x');
    s1.push('b');
    s2.push('y');
    s1.push('c');
    s2.push('z');
    for(i=0; i<3; i++) cout << "s1 pop:" << s1.pop() << "\n";
    for(i=0; i<3; i++) cout << "s2 pop:" << s2.pop() << "\n";
    
    return 0;
}

二分探索木の形状

#include 
#include 
#include 

#define DATANUM 1000

struct BinarySearchTree{
    int data;
    struct BinarySearchTree *left;
    struct BinarySearchTree *right;
};

struct BinarySearchTree bst[DATANUM];

int makeBinarySearchTree(){
    struct BinarySearchTree *current;
    int i, depth, maxDepth;
    
    srand((unsigned int)time(NULL));
    bst[0].data = rand();
    bst[0].left = NULL;
    bst[0].right = NULL;
    maxDepth = 0;
    
    for (i = 1; i < DATANUM; i++){
        bst[i].data = rand();
        bst[i].left = NULL;
        bst[i].right = NULL;
        
        current = &bst[0];
        
        depth = 1;
        while (-1){
            if(bst[i].data < current->data){
                if (current->left == NULL){
                    current->left = &bst[i];
                    break;
                }
                else {
                    current = current->left;
                    depth++;
                }
            }
            else {
                if (current->right = NULL){
                    current->right = &bst[i];
                    break;
                }
                else {
                    current = current->right;
                    depth++;
                }
            }
        }
        
        if (depth > maxDepth) maxDepth = depth;
    }
    
    return maxDepth;
}

int main(){
    int i, sum;
    
    sum = 0;
    for (i = 1; i <= 10000; i++){
        sum += makeBinarySearchTree();
    }
    
    printf("DATANUM = %d\n", DATANUM);
    printf("max depth average is = %lf\n", sum / 10000.0);
    
    return 0;
}

ASCII文字コード表

ASCIIでは、半角英数記号の1文字を7ビットで表します。

#include 
int main(){
    int row, col;
    char data;
    printf("\t[20]\t[30]\t[40]\t[50]\t[60]\t[70]\n");
    for(row = 0x0; row <= 0xF; row++){
        printf("[%X]", row);
        for(col = 0x20; col <= 0x70; col += 0x10){
            data = col + row;
            if (data == 0x7F) break;
            else printf("\t%c", data);
        }
        printf("\n");
    }
    return 0;
}
	[20]	[30]	[40]	[50]	[60]	[70]
[0]	 	0	@	P	`	p
[1]	!	1	A	Q	a	q
[2]	"	2	B	R	b	r
[3]	#	3	C	S	c	s
[4]	$	4	D	T	d	t
[5]	%	5	E	U	e	u
[6]	&	6	F	V	f	v
[7]	'	7	G	W	g	w
[8]	(	8	H	X	h	x
[9]	)	9	I	Y	i	y
[A]	*	:	J	Z	j	z
[B]	+	;	K	[	k	{
[C]	,	<	L	\	l	|
[D]	-	=	M	]	m	}
[E]	.	>	N	^	n	~
[F]	/	?	O	_	o

x86系32ビットCPU

一般的なwindowsパソコンが採用しているCPUはx86系と呼ばれるCPUです。
86系32ビットCPUが持つ主要なレジスタです。

汎用レジスタ
EAX、EBX、ECX、EDX、ESI、EDI、EBP

特殊レジスタ
ESP、EIP、EFLAGS
CS、DS、ES、SS、FS、GS

windows EXEファイル

EXEファイルっはPortable Executableファイルと呼ばれ、MZヘッダー、PEヘッダー、セクションデータから構成されます。

MZ header:DOS header、MS-DOS header
PEヘッダー:シグネチャ、ファイルヘッダー、オプションヘッダー
セクションデータ:セクションヘッダー1,セクションヘッダー2・・セクション1,セクション2

MZヘッダーには、Windows用プログラムを、MS-DOS環境で誤って起動してしまったときに実行されるMS-DOS用プログラムが格納されています。
PEヘッダーには、Windows用プログラムの情報が格納されています。

Visual Studioに含まれるdumpbin.exeというコマンドラインツールを使えば、EXEファイルの構造を調べることができます。

ポインタ2

#include 
#include 
#include 

int main() {
    char buffer[1000] = "";
    int length;
    char *line = NULL;
    
    printf("type character and enter key");
    gets(buffer);
    length = strlen(buffer);
    if (length > 0) {
        line = (char *)malloc(length + 1);
        strcpy(line, buffer);
        printf("line = %s\n", line);
        free(line);
    }
    return 0;
}

ポインタ

ポインタはメモリーのアドレスを格納できる容器で、なおかつ、どの型のデータを指し示すかの情報を持っています。

下の例では、整数型のポインタ変数pと、整数型の変数iを宣言、iを1に初期化し、「*p」という表現で、変数iの中身を参照しています。

#include 

int main() {
    int *p, i = 1;
    p = &i;
    printf("%d\n", *p);
    return 0;
}

基本データ型

int, float, double, charの変数の大きさです。C言語には、文字列型がありません。C++はstringクラスがあります。

#include 

int main() {
    int i = 1;
    float f = 1;
    double d = 0.5;
    char c = 'a';
    
    printf("i = %d\n", i);
    printf("f = %f\n", f);
    printf("d = %f\n", d);
    printf("c = %c\n", c);
    
    printf("sizeof(i) = %d\n", sizeof(i));
    printf("sizeof(f) = %d\n", sizeof(f));
    printf("sizeof(d) = %d\n", sizeof(d));
    printf("sizeof(c) = %d\n", sizeof(c));
    
    return 0;
}
i = 1
f = 1.000000
d = 0.500000
c = a
sizeof(i) = 4
sizeof(f) = 4
sizeof(d) = 8
sizeof(c) = 1

comの参照アカウント

参照アカウント方式では、参照された側が死活管理を行っています。
参照する際に、「参照します」と通知し、参照が終了した際に「参照が終わる」と送ります。参照カウントが0になったときに終了処理を実行します。「オブジェクトを参照する」という通知は、.NETにより自動的に行われます。

参照を減らすという通知がMarshal.ReleaseComObjectです。

Application app = null;
try
{
      app = new Application();
      app.DisplayAlerts = false;
      Workbook book = null;
      try
      {
         book = app.Workbooks.Add();
         ((Range)(Worksheet)book.Worksheets[1]).Cells[1, 1].Value =
      book.SaveAs(Filename: @"c:¥temp¥abc.xlsx");
      book.Close(SaveChanges: false);
      app.Quit();
      MessageBox.Show("finish");
      }
      finally
      {
           if (book != null) Marshal.ReleaseComObject(book);
      }
}
finally
{
           if ( app != null )Marshal.ReleaseComObject(app);
}

ボール遊び

import java.awt.*;
import java.awt.event.*;

class MyFrame extends Frame
	implements Runnable{// t
	int w=600, h=600;
	int sleepTime=10;// t
	Thread thread=null;// t
	Ball[] ball=new Ball[200];
	
	MyFrame(String s){
		super(s);
		setSize(w, h);
		setBackground(Color.GRAY);
		setForeground(Color.WHITE);
		for(int i=0; i < ball.length; i=i+1){
			int x=(int)(Math.random()*(double)w);
			int y=(int)(Math.random()*(double)h);
			int r=(int)(Math.random()*255.0);
			int g=(int)(Math.random()*255.0);
			int b=(int)(Math.random()*255.0);
			int vx=(int)(0.02*(Math.random()-0.5)*(double)w);// t
			int vy=(int)(0.02*(Math.random()-0.5)*(double)h);// t
			ball[i]=new Ball(x, y, 5, new Color(r, g, b));
			ball[i].setVelocity(vx, vy);
			ball[i].setMyFrame(this);
		}
	}
	public void paint(Graphics g){
		for(int i=0; i < ball.length; i=i+1){
			ball[i].paint(g);
		}
	}
	public void start(){// t
		if(thread==null){
			thread=new Thread(this);
			thread.start();
		}
	}
	public void stop(){// t
		thread=null;
	}
	public void run(){// t
		while(Thread.currentThread()==thread){
			for(int i=0; i < ball.length; i=i+1){
				ball[i].move();
			}
			repaint();
			try{
				Thread.sleep(sleepTime);
			}catch(InterruptedException e){
			}
		}
	}
	public boolean checkLimit(int x, int y){// c
		return((x < 0)||(w < x)||(y < 0)||(h < y));
	}
}//end of class MyFrame
class Ball{
	MyFrame myFrame;// c
	int x, y, r;
	int vx, vy;// t
	Color c;
	
	public Ball(int x, int y, int r, Color c){
		this.x=x;
		this.y=y;
		this.r=r;
		this.c=c;
	}
	public void paint(Graphics g){
		Color gc=g.getColor();
		g.setColor(c);
		g.fillOval(x-r, y-r, 2*r, 2*r);
		g.setColor(gc);
	}
	public void setVelocity(int vx, int vy){//t
		this.vx=vx;
		this.vy=vy;
	}
	public void setMyFrame(MyFrame myFrame){// c
		this.myFrame=myFrame;
	}
	public void move(){
		x=x+vx;
		if(myFrame.checkLimit(x,y)){// c
			vx=-vx;
			x=x+vx;
		}
		y=y+vy;
		if(myFrame.checkLimit(x, y)){// c
			vy=-vy;
			y=y+vy;
		}
	}
}
public class ballplay{
	public static void main(String[] args){
		MyFrame myFrame=new MyFrame("BallTest");
		myFrame.addWindowListener(
				new WindowAdapter(){
					public void windowClosing(WindowEvent e){
						System.exit(0);
					}
				});
		myFrame.setVisible(true);
		myFrame.start();// t
	}
}// end of class BallTest