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;
}

メモリ管理

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");
}

順序付きリスト同士の統合

#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
	

コンストラクタ

#include < iostream >
using namespace std;

class myclass {
    int a;
public:
    myclass(int x){ a= x; }
    int geta() { return a; }
};

int main()
{
    myclass ob(4);
    
    cout << ob.geta();
    
    return 0;
}

new(nothrow)オプション

割り当てエラーを強制的に発生させています。

#include 
#include < new >
using namespace std;

int main()
{
    double *p;
    
    do {
        p = new(nothrow) double[100000];
        if(p) cout << "succeed memory set\n";
        else cout << "memory set error\n";
    } while(p);
    
    return 0;
}

try/catchブロック

#include < iostream >
#include 
using namespace std;

int main()
{
    int *p;
    try {
        p = new int;
    } catch (bad_alloc xa){
        cout << "error memory set.\n";
        return 1;
    }
    
    for(*p = 0; *p < 10; (*p)++)
        cout << *p << " ";
    
    delete p;
    
    return 0;
}