マウスドラッグ

#include<GL/glut.h>

unsigned char mouseFlag = GL_FALSE;
double yAngle = 0;
int xStart, yStart;

void init(char *name) {
	int width = 400, height = 400;

	glutInitDisplayMode(GLUT_RGBA);
	glutInitWindowSize(width, height);
	glutCreateWindow(name);
	glClearColor(0.0, 0.0, 0.0, 1.0);

	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	gluPerspective(90.0, (double) width / (double) height, 0.1, 18);
}

void display(void) {
	glClear(GL_COLOR_BUFFER_BIT);

	glColor3d(0.0, 1.0, 1.0);

	glPushMatrix();
	glTranslatef(0.0, 0.0, -3.0);
	glRotated(yAngle, 0.0, 1.0, 0.0);
	glutWireTeapot(1.5);
	glPopMatrix();

	glFlush();
}
void MouseMotion(int x, int y) {
	int xdis;
	if (mouseFlag == GL_FALSE) {
		return 0;
	}

	yAngle += (double) xdis * 0.00000001;

	xStart = x;
	yStart = y;
	glutPostRedisplay();
}

void mouseFunc(int button, int state, int x, int y) {
	if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN) {
		xStart = x;
		yStart = y;
		mouseFlag = GL_TRUE;
	} else {
		mouseFlag = GL_FALSE;
	}
}

int main(int argc, char **argv) {
	glutInit(&argc, argv);
	init(argv[0]);
	glutDisplayFunc(display);
	glutMouseFunc(mouseFunc);
	glutMotionFunc(MouseMotion);
	glutMainLoop();
	return 0;
}

スレッドの生成 for pthread

#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
#include <unistd.h>

typedef struct {
	char str;
	int sleep_time;
} ThreadArgument;

void *test(ThreadArgument *arg) {
	while (1) {
		fprintf(stderr, "thread\n");
		sleep(arg->sleep_time);
	}
}

int main(int argc, char *argv[]) {
	pthread_t thread;
	ThreadArgument thread_arg;
	thread_arg.str = 'y';
	thread_arg.sleep_time = 1;

	pthread_create(&thread, NULL, test, &thread_arg);
	while (1) {
		fprintf(stderr, "main\n");
		sleep(2);
	}

	return 0;
}

連結リスト

いろいろ手抜きな連結リスト

#include<stdio.h>
#include<stdlib.h>

typedef struct person {
	int score;
	struct person *next;
} Person;

typedef struct {
	Person *head;
	Person *current;
} List;

void insert_front(List *list, int score) {
	Person *tmp = list->head;
	list->head = list->current = ((Person *) calloc(1, sizeof(Person)));
	list->head->next = tmp;
	list->head->score = score;
}

void remove_front(List *list) {
	if (list->head != NULL) {
		Person *tmp = (list->head)->next;
		free(list->head);
		list->head = list->current = tmp;
	}
}

void remove_current(List *list) {
	if (list->head != NULL) {
		if (list->current == list->head)
			remove_front(list);
		else {
			Person *tmp = list->head;

			while (tmp->next != list->current)
				tmp = tmp->next;
			tmp->next = list->current->next;
			free(list->current);
			list->current = tmp;
		}
	}
}

void remove_under_50(List *list) {
	list->current = list->head;

	while (list->current != NULL) {
		if (list->current->score < 50) {
			remove_current(list);
		}
		list->current = list->current->next;
	}

}

void show(List *list) {
	if (list->head == NULL) {
		puts("リストは空です。");
	} else {
		Person *tmp = list->head;

		while (tmp != NULL) {
			printf("score = %d\n", tmp->score);
			tmp = tmp->next;
		}

	}
}

int main(void) {
	List list;
	list.head = NULL;
	list.current = NULL;

	insert_front(&list, 32);
	insert_front(&list, 87);
	insert_front(&list, 50);
	insert_front(&list, 32);
	insert_front(&list, 92);
	show(&list);
	printf("\n");
	remove_under_50(&list);

	show(&list);

	return 0;
}

JDBCその1

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

class test2 {
	public static void main(String[] args) {
		try {
			Class.forName("com.mysql.jdbc.Driver").newInstance();
			Connection con = DriverManager.getConnection(
					"jdbc:mysql://localhost/test", "root", args[0]);
			Statement st = con.createStatement();

			st.executeUpdate("create table person (name text, score int)");

			st.executeUpdate("insert into person values ('natsumi', 32)");
			st.executeUpdate("insert into person values ('yozora', 97)");
			st.executeUpdate("insert into person values ('sena', 86)");
			st.executeUpdate("insert into person values ('chikako', 68)");
			st.executeUpdate("insert into person values ('kaori', 39)");

			ResultSet rs = st.executeQuery("select * from person");

			while (rs.next()) {
				System.out.println(rs.getString("name") + "\t"
						+ rs.getString("score"));
			}
			System.out.println("");

			st.executeUpdate("delete from person where score < 50");

			rs = st.executeQuery("select * from person");

			while (rs.next()) {
				System.out.println(rs.getString("name") + "\t"
						+ rs.getString("score"));
			}

			st.executeUpdate("drop table person");

			rs.close();
			st.close();
			con.close();
		} catch (SQLException e1) {
			System.out.println(e1.toString());
		} catch (Exception e2) {
			e2.printStackTrace();
		}
	}
}

実行結果

natsumi 32
yozora 97
sena 86
chikako 68
kaori 39

yozora 97
sena 86
chikako 68

Rubyで簡単なUDPサーバーを立てる

require "socket"

socket = UDPSocket.open
socket.bind("127.0.0.1", 13)

loop do
  message, sender = socket.recvfrom(1024)
  puts "#{Time.now} : #{sender[3]} : #{message}"
  STDOUT.flush
end

netcatで接続

$ nc -u 127.0.0.1 13
sekai
kouiti
simada
natsumi
punt!

結果

D:\workplace>ruby test.rb
2012-01-07 23:01:21 +0900 : 127.0.0.1 : sekai
2012-01-07 23:01:27 +0900 : 127.0.0.1 : kouiti
2012-01-07 23:01:35 +0900 : 127.0.0.1 : simada
2012-01-07 23:01:37 +0900 : 127.0.0.1 : natsumi

Rubyでスレッド

スレッドの生成

ary = ('a'..'c').to_a
threads = Array.new

ary.each do |elem|
	threads << Thread.new(elem) do |s|
		sleep(rand(0.1))
		3.times{|i| print "#{s} : #{i}\n" }
	end
end

threads.each{|thread| thread.join}

実行結果

D:\workplace>ruby test.r
a : 0
a : 1
a : 2
b : 0
b : 1
b : 2
c : 0
c : 1
c : 2

D:\workplace>ruby test.r
a : 0
a : 1
a : 2
c : 0
c : 1
c : 2
b : 0
b : 1
b : 2

D:\workplace>ruby test.r
c : 0
c : 1
c : 2
b : 0
b : 1
b : 2
a : 0
a : 1
a : 2

D:\workplace>ruby test.r
b : 0
b : 1
b : 2
a : 0
a : 1
a : 2
c : 0
c : 1
c : 2