#include < stdio.h >
#include < time.h >
#include < dos.h >
#include < conio.h >
#include < string.h >
#include < malloc.h >
#include < ctype.h >
struct tm *newtime;
newtime->tm_sec
newtime->tm_min
newtime->tm_hour;
newtime->tm_mday;
newtime->tm_mon;
newtime->tm_year;
newtime->tm_wday;
newtime->tm_yday;
newtime->tm_isdst;
time_t long_time;
void sound(char *p, int kaisu, int comment_flag);
int inkey(void);
main(int argc, char *argv[])
{
unsigned datasize;
int i, n, kaisu, comment_flag;
char ongaku[1025];
char *p, *w;
FILE *fp;
if(argc < 2)
{
printf("music play program\n\n");
printf("action way A>SOUND music data file [-option]\n\n");
printf("music data file setting way\n");
printf("to make sound octabe and sound long space");
printf("octabe ---> 1, 2, 4, 8 and 2 n multiply\n");
printf("sound step ----------> "do:D re:R m:M fa:F so:S la:L shi:C\n");
printf(" do#:d re#:r mi#:m fa#:f so#:s la#:l stop:K\n");
printf("sound length---> 1.16m 2.8m 4:4m 8:2m\n");
printf("option is small \n\n");
printf("option direction play count –N\n");
printf(" display count -C \n");
exit(0);
}
kaisu = 1;
comment_flag = 0;
datasize = 0;
for(i = 1; i <= argc-1; i++)
{
if(*argv[i] == '-')
{
*(argv[i]+1) = toupper(*(argv[i]+1));
switch (*(argv[i]+1))
{
case 'N' :
kaisu = atoi(argv[i]+2);
break;
case 'C' :
comment_flag = 1;
break;
default : break;
}
}
else
{
if ((fp = fopen(argv[i], "r")) == NULL)
{
printf("%s is not here!", argv[i]);
exit(0);
}
while(fgets(ongaku, 1025, fp) != NULL)
{
n = strlen(ongaku);
datasize += n;
}
fclose(fp);
}
}
w = p = malloc(datasize + 1);
if (w == NULL)
{
printf("data volume is too large, can not set in memory erea.\n");
exit(0);
}
for(i = 1; i < argc; i++)
{
if (*argv[i] == '-')
continue;
fp = fopen(argv[i], "r");
while(fgets(ongaku, 1025, fp) != NULL)
{
n = strlen(ongaku);
datasize += n;
}
fclose(fp);
}
}
w = p = malloc(datasize + 1);
if (w == NULL)
{
printf("data size is too large, cannot set into memory area.\n");
exit(0);
}
for (i = 1; i < argc; i++)
{
if (*argv[i] == '-')
continue;
fp = fopen(argv[i], "r");
while(fget(ongaku, 1025, fp) != NULL)
{
n = strlen(ongaku);
if(*(ongaku+1-1) == '\n')
*(ongaku+n-1) = ' ';
strcpy(w, ongaku);
w = w + n;
}
fclose(fp);
}
sound(p, kaisu, comment_flag);
}
/*---------------*/
/* music routin */
/*---------------*/
void sound(char *ongaku, int kaisu, int comment_flag)
{
static float hz[ ] = { 261.6, 277.9,
293.7, 311.6,
329.6,
439.2, 370.6,
392.0, 416.0,
440.0, 466.9,
493.9
};
static char *onkai = "DdRrMFfSsL1C";
int i, n, f, ot, ol, p, len, flag;
long w, tn, tb, tw;
/* 1 second roop count ---------*/
time(&long_time);
newtime = (struct tm *)localtime(&long_time);
w = newtime->tm_sec;
flag = 0;
while(flag == 0)
{
time(&long_time);
newtime = (struct tm *)localtime(&long_time);
if (w != newtime->tm_sec)
{
w = newtime->tm_sec;
for(tn = 0L; flag == 0; tn++)
{
time(&long_time);
newtime = (struct tm *)localtime(&long_time);
if(w != newtime->tm_sec)
flag = 1;
}
}
}
tn /= 10;
tb = tn / 5;
len = strlen(onagaku);
outp(0x3fdf, 0x76);
ot = 2;
while(kaisu--)
{
i = 0;
while(i < len)
{
if(*(ongaku+i) == ' ')
{
i++;
continue;
}
if (*(ongaku+i) == ' /')
{
while(*(ongaku+(++i)) != '/')
if (comment_flag == 1)
putchar(*(ongaku+i));
if(comment_flag == 1)
putchar('\n');
i++;
continue;
}
if(strchr(onkai, *(ongaku + i + 1)) != NULL)
{
ot = atoi(ongaku + i);
p = strchr(onkai, *(ongaku + i+1)) - onkai;
f = hz[p] * ot;
n = 2457600L / f;
outp(0x3fdb, n % 256); outp(0x3fdb, n / 256);
outp(0x37, 0x06);
}
else if(*(ongaku+i+1) != 'K')
{
*(ongaku + i + 3) = '\0';
printf("Data error\n%s <--- ?\n", ongaku);
exit(0);
}
ol = atoi(ongaku + i + 2);
tw =tn * ol;
for (w = 0L; w < tw; w++)
{
time(&long_time);
newtime = (struct tm *)localtime(&long_time);
if(w != newtime->tm_sec)
flag = 1;
}
outp(0x37, 0x07);
for (w = 0L; w < tb; w++)
{
time(&long_time);
newtime = (struct tm *)localtime(&long_time);
if (w != newtime->tm_sec)
flag = 1;
}
if (inkey() != 0)
{
kaisu = 0;
break;
}
i+= 3;
}
}
}
/*---------------*/
/* music routin */
/*---------------*/
int inkey(void)
{
union REGS inregs;
union REGS outregs;
inregs.h.ah = 0x06;
inregs.h.dl = 0xff;
intdos(&inregs,&outregs);
return((int)outregs.h.al);
}
menu type batch processing
/*————*/
/* menu type batch processing*/
/*————*/
#include < stdio.h >
#include < string.h >
#include < malloc.h >
#include < conio.h >
/*————–*/
#define COLOR(n, a) printf(“\x1b[%d;%dm”, n, a)
#define BLACK 30
#define RED 31
#define GREEN 32
#define YELLOW 33
#define BLUE 34
#define MAGENTA 35
#define CYAN 36
#define WHITE 37
#define NORMAL 0
#define UNDERLINE 4
#define BLINK 5
#define REVERSE 7
#define CLS() printf(“\x1b[2J”)
#define LOCATE(x, y) printf(“\x1b[%d;%dH”, y + 1, x + 1)
#define CURSOLE(f) printf(“\x1b[>5%c”, f * ‘1’ : ‘h’)
#define UP 0x0b
#define DOWN 0x0a
#define LEFT 0x08
#define RIGHT 0x0c
#define ESC 0x1b
#define RETURN 0x0d
#define MAX 30
/* ———–*/
int datasu;
static char *name[MAX],
*command[MAX];
/* ———–*/
void data_read(void);
void menu_disp(void);
int select(void);
/* ————-*/
/* main*/
/* ————-*/
main(void)
{
int c, n;
data_read();
do {
menu_disp();
if((n = select()) != -1)
{
system(command[n]);
printf(“\n type something new!”);
c = getch();
}
} while(n != -1);
}
/* ————-*/
/* menu transaction*/
/* ————-*/
void data_read(void)
{
FILE *fp;
char buffer[256];
if((fp = fopen(“menu.dat”, “r”)) == 0)
{
printf(“can’t open menu.dat\n”);
exit(1);
}
datasu = 0;
while(fgets(buffer, 256, fp) != 0)
{
*strchr(buffer, ‘\n’) = ‘\0’;
name[datasu] = malloc(strlen(buffer)+1);
strcpy(name[datasu], buffer);
*strchr(name[datasu], ‘:’) = ‘\0’;
command[datasu] = name[datasu] + strlen(name[datasu]) + 1;
datasu++;
if(datasu >= MAX)
break;
}
fclose(fp);
}
/* ————-*/
/* menu display*/
/* ————-*/
void menu_disp(void)
{
int n;
CLS(); COLOR(WHITE, REVERSE); CURSOLE(0);
LOCATE(0, 0);
printf(“transaction menu”);
LOCATE(0, 22);
printf(” [move]↑↓←→ [action]return [finish]ESC”);
COLOR(CYAN, NORMAL);
for(n = 0; n < datasu; n++)
{
LOCATE((n/10)*30, (n%10)*2+2);
printf(" %-20s ", name[n]);
}
}
/* -------------*/
/* select*/
/* -------------*/
int select(void)
{
int n, h, c;
n = h = 0;
do {
COLOR(YELLOW, REVERSE); LOCATE((n/10)*30, (n%10)*2+2);
printf(" %s ", name[n]);
c = getch();
if (c == DOWN || c == UP || c == LEFT || c == RIGHT)
{
COLOR(CYAN, NORMAL); LOCATE((h/10)*30, (h%10)*2 + 2);
printf(" %s ", name[h]);
if(c == DOWN)
n++;
if (c == UP)
n--;
if(c == LEFT)
n -= 10;
if(c == RIGHT)
n += 10;
if(n < 0 || n >= datasu)
n = h;
else
h = n;
} while(c != RETURN && c != ESC);
CLS(); COLOR(WHITE, NORMAL); CURSOLE(1);
if(c === ESC)
return -1;
else
return n;
}
正規表現
基本要素
1.リテラルまたは変数として表される値
2.演算子
すべての文字はリテラルとして解釈され、その文字自体にしかマッチしない。
一部のメタキャラクタは、grepやawkのようなプログラムで使われる拡張セットにしか存在しない。
メモリ管理
struct memblock {
int size;
unsigned char magic;
unsigned char occupied;
struct memblock *next;
struct memblock *prev;
};
#define HEADER_SIZE (sizeof(struct memblock))
#define DELTA 20
#define MEM_MAGIC 0xa5
#define WORDSIZE 4
#define WORD_ALIGN(n) (((n) + WORDSIZE - 1) & -WORDSIZE)
struct memblock block_chain;
#define HEAP_SIZE 10000
char memory_heap[HEAP_SIZE];
void initialize_memory()
{
struct memblock *h;
h = (struct memblock *)memory_heap;
block_chain.size = sizeof(block_chain);
block_chain.magic = MEM_MAGIC;
block_chain.occupied = 1;
block_chain.next = block_chain.prev = h;
h->size = HEAP_SIZE;
h->magic = MEM_MAGIC;
h->occupied = 0;
h->next = h->prev = & block_chain;
}
void *allocate_block(int size)
{
struct memblock *p, *s;
int nbytes;
nbytes = WORD_ALIGN(size + HEADER_SIZE);
for (p = block_chain.next; p != &block_chain; p = p->next){
if(!p->occupied && p->size >= nbytes){
if (p->size - nbytes > DELTA){
s = (struct memblock *)((char *)p + nbytes);
s->size = p->size - nbytes;
s->magic = MEM_MAGIC;
s->occupied = 0;
p->next->prev = s;
s->next = p->next;
p->next = s;
s->prev = p;
p->size = nbytes;
p->occupied = 1;
} else
p->occupied =1;
return (char *)p + HEADER_SIZE;
}
}
return NULL;
}
void free_block(void *block)
{
struct memblock *mem;
mem = (struct memblock *)((char *)block - HEADER_SIZE);
if(mem->magic != MEM_MAGIC)
return;
if (! mem->prev->occupied){
mem->next->prev = mem->prev;
mem->prev->next = mem->next;
mem->prev->size += mem->size;
mem = mem->prev;
}
if(! mem->next->occupied){
mem->next->next->prev = mem;
mem->size += mem->next->size;
mem->next = mem->next->next;
}
mem->occupied = 0;
}
パドック法による8クイーン
#include < stdio.h >
#include < stdlib.h >
#include < string.h >
#define SUCCESS 1
#define FAIL 0
#define FREE 1
#define NOT_FREE 0
#define N 8
int pos[N];
int col[N];
int down[2 * N - 1];
int up[2 * N - 1];
void init_board()
{
int i;
for (i = 0; i < N; i++)
pos[i] = -1;
for (i = 0; i < N; i++)
col[i] = FREE;
for(i = 0; i < 2 * N - 1; i++)
down[i] = FREE;
for (i = 0; i< 2 * N -1; i++)
up[i] = FREE;
}
void print_queens()
{
int i, j;
for(i = 0; i < N; i++){
for(j = 0; j < N; j++){
if(pos[i] == j)
printf("Q ");
else
printf(". ");
}
printf("\n");
}
printf("\n");
}
int try(int a)
{
int b;
for (b = 0; b < N; b++){
pos[a] = b;
col[b] = NOT_FREE;
up[a + b] = NOT_FREE;
down[a - b + (N-1)] = NOT_FREE;
if(a + 1 >= N)
return SUCCESS;
else {
if(try(a + 1) == SUCCESS)
return SUCCESS;
else {
pos[a] = -1;
col[b] = FREE;
up[a + b] = FREE;
down[a-b + (N-1)] = FREE;
}
}
}
return FAIL;
}
main()
{
init_board();
if(try(0) == SUCCESS)
print_queens();
else
printf("Sorry, but there is no solution.\n");
}
プログラマーの定義
仕様書どおりにプログラムを書くのがプログラマーの現場で多いかもしれないが、
力をつけるには、
1.問題を分析して、何をするプログラミングを書くのかを決める
2.どんなアルゴリズム、データ構造を使用すれば良いか選択する
3.実際にプログラムを書く
を繰り返す。
順序付きリスト同士の統合
#include < iostream >
#include < list >
using namespace std;
int main()
{
list< char > lst1, lst2;
int i;
for(i=0; i< 10; i+=2) lst1.push_back('A'+i);
for(i=1; i< 11; i+=2) lst2.push_back('A'+i);
cout << "lst1 content: ";
list< char >::iterator p = lst1.begin();
while(p != lst1.end()){
cout << *p;
p++;
}
cout << endl << endl;
cout << "lst2 content: ";
p = lst2.begin();
while(p != lst2.end()){
cout << *p;
p++;
}
cout << endl << endl;
lst1.merge(lst2);
if(lst2.empty())
cout << "lst2 is now empty\n";
cout << "merged lst1 content\n";
p = lst1.begin();
while(p != lst1.end()){
cout << *p;
p++;
}
return 0;
}
双方向リスト
#include < iostream >
#include < list >
using namespace std;
int main()
{
list lst;
list revlst;
int i;
for(i=0; i< 10; i++) lst.push_back('A'+i);
cout << "lst size = " << lst.size() << endl;
cout << "size: ";
list< char >::iterator p;
while(!lst.empty()){
p = lst.begin();
cout << *p;
lst.pop_front();
revlst.push_front(*p);
}
cout << endl << endl;
cout << "revlst size = ";
cout << revlst.size() << endl;
cout << "content: ";
p = revlst.begin();
while(p != revlst.end()){
cout << *p;
p++;
}
return 0;
}
リスト
#include < iostream >
#include < list >
using namespace std;
int main()
{
list lst;
int i;
for(i=0; i<10; i++) lst.push_back('A'+i);
cout << "size = " << lst.size() << endl;
list::iterator p;
cout << "content: ";
while(!lst.empty()){
p = lst.begin();
cout << *p;
lst.pop_front();
}
return 0;
}
オーバーロードバージョン
#include < iostream >
#include < vector >
using namespace std;
class Demo {
double d;
public:
Demo() { d = 0.0; }
Demo(double x){ d = x; }
Demo &operator=(double x){
d = x; return *this;
}
double getd() { return d; }
};
bool operator<(Demo a, Demo b)
{
return a.getd() < b.getd();
}
bool operator==(Demo a, Demo b)
{
return a.getd() == b.getd();
}
int main()
{
vector v;
int i;
for(i=0; i<10; i++)
v.push_back(Demo(i/3.0));
for(i=0; i