/*----------------*/ /* screen editer */ /*----------------*/ #include < stdio.h > #include < string.h > #include < stdlib.h > #include < ctype.h > #include < jctype.h > #include < jctype.h > #include < jstring.h > #include < malloc.h > #include < dos.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 UP 0x0b #define DOWN 0x0a #define ESC 0x1b #define RETURN 0x0d #define COLOR_OFF 0 #define COLOR_ON 1 #define LEN 1000 #define LOCATE(x, y) printf("\x1b[%d;%dH", y + 1, x + 1) #define CURSOLE(f) printf("\x1b[>5%c", f ? '1' : 'h') union REGS ingres; union REGS outregs; struct list { unsigned char *data; struct list *back; struct list *front; } *head, *page_head, *now, *new, *new_start, *new_end, *appoint_start, *appoint_end, *paste_start, *paste_end; int paste_flag; long paste_start_no; int xp, yp, len, width, scrol, tab, color, char drive; int cx, cy; int len_min = 8; width_min = 20; int drive_set_flag = 0; static char filename[80]; static char *workfile[4] = { "A:WORKO", "A:WORK1", "A:WORK2", "A:WORK3"}; FILE *fp0, *fp1, *fp2, *fp3; /*----------------*/ void initial_process(void); void ending_process(void); void commandline_set(char **p, int n); void parameter_value_check(void); void load_process(int flag); void waku_displat(int x, int y, int len, int width, int color, int zokusei); void title_display(void); void file_read(void); void new_file_make(void); void new_creat(void); void hensyu(void); void rollup_syori(void); void rolldown_syori(void); void left_key_syori(void); void right_key_syori(void); void up_key_syori(void); void down_key_syori(void); void bs_key_syori(void); int ins_key_syori(int ins_flag); int insert_syori(unsigned char m, struct list *p, int n); void over_write_syori(unsigned char *p, int n); void line_feed_syori(struct list *p, int n); void buffer_memory_set(unsigned char *buffer, unsigned char *p, int n); int menu_select(void); void save_process(void); void backward_page_check(struct list *p); void forward_page_renketu(struct list *p); void backward_page_load(void); void forward_page_load(void); void backward_page_save(void); void forward_page_save(void); void backward_page_memory_check(void); void forward_page_memory_check(void); int paste_locate(void); void paste_set(void); void cut_process(void); void buffer_clear(unsigned char *p, int n); void jump_process(void); void hyouji(struct list *p, int s, int sw); void vram_text_disp(unsigned char *p, int x, int y, int len, int color_sw); void vram_color_set(unsigned char color_value, int x, int y, int len); int input(unsigned char buffer[], int x, int y, int len); int moji_check(unsigned char *buffer, int n); int inkey(void); void keybuffer_clear(void); int scan_esc(void); int scan_ins(void); int scan_del(void); int scan_bs(void); int scan_rollup(void); int scan_rolldown(void); int scan_up(void); int scan_down(void); int scan_left(void); int scan_right(void); void cursole_blink(int sw) /*----------------*/ /* main */ /*----------------*/ void main(int argc, char *argv[]) { initial_process(); commandline_set(&argv[1], argc - 1); waku_display(xp - 1, yp - 1, len + 2, width + 2, WHITE, NORMAL); load_process(0); if (*filename != '\0') { hyouji(head, 0, COLOR_ON); henshu(); } ending_process(); } /*----------------*/ /* initial setting */ /*----------------*/ void initial_process(void) { xp = 1; yp = 1; len = 20; width = 78; scroll = 1; tab = 4; color = 7; drive = ' A'; head = paste_start = paste_end = NULL; paste_flag = 0; } /*----------------*/ /* ending process */ /*----------------*/ void ending_process(void) { unlink(workfile[0]); unlink(workfile[1]); unlink(workfile[2]); unlink(workfile[3]); LOCATE(0, yp + len); COLOR(WHITE, NORMAL); CURSOLE(1); } /*----------------*/ /* comandline set */ /*----------------*/ void commandline_set(char **p, int n) { int i; for(i = 0; i < n; i++) { if(**p == ' -') { *(*p + 1) = toupper(*(*p + 1)); switch (*(*p + 1)) { case 'X' : xp = atoi(*p + 2); break; case 'Y' : yp = atoi(*p + 2); break; case 'L' : len = atoi(*p + 2); break; case 'W' : width = atoi(*p + 2); break; case 'S' : scroll = atoi(*p + 2); break; case 'T' : tab = atoi(*p + 2); break; case 'C' : color = atoi(*p + 2); break; case 'D' : drive = atoi(*p + 2); drive = toupper(drive); break; default : break; } } else strcpy(filename, *p); p++; } parameter_value_check(); } /*----------------*/ /* parameter check */ /*----------------*/ void parameter_value_check(void) { if(xp < 1) xp = 1; else if(xp > 79 - width_min) xp = 79 - width_min; if(yp < 1) yp = 1; else if(yp > 23 - len_min) yp = 23 - len_min; if (len < len_min) len = len_min; if (yp + len > 23) len = 23 - yp; if (width < width_min) width = width_min; if (xp + width > 79) width = 79 - xp; if (color < 1) color = 1; else if (color > 7) color = 7; color = color * 32 + 1; if (drive_set_flag == 0) { if (drive >= 'A' && drive <= 'Z') { *workfile[0] = drive; fp0 = fopen(workfile[0], "w"); *workfile[0] = *workfile[1]; if (fp0 != NULL) { *workfile[0] = drive; *workfile[1] = drive; *workfile[2] = drive; *workfile[3] = drive; } fcloseall(); } } drive_set_flag = 1; } /*----------------*/ /* load transaction flag 0: start 1: edit */ /*----------------*/ void load_process(int flag) { static char buffer[80], * work[10]; int i, n, p, sw, set_flag; struct list *wp; sw = 0; while (*filename == '\0' || flag == 1) { LOCATE(xp, yp); COLOR(WHITE, REVERSE); printf(" -> "); sw = input (buffer, xp + 4, yp, width - 4); if (sw == RETURN && buffer[0] != '\0') { set_flag = n = 0; p = strlen(buffer); for(i = 0; i < p; i++) { if(set_flag == 0 && buffer[i] != ' ') { work[n++] = buffer + i; set_flag = 1; } else if (set_flag == 1 && buffer[i] == ' ') { buffer[i] = '\0'; if (n == 10) break; set_flag = 0; } } hyouji(page_head, 0, COLOR_ON); commandline_set(&work[0], n); waku_display(xp - 1, yp - 1, len + 2, width + 2, WHITE, NORMAL); flag = 2; } else if(sw == ESC) { flag = 2; break; } } CURSOLE(0); LOCATE(xp + width / 2 - strlen(filename) / 2 - 2, yp -1); COLOR(WHITE, REVERSE); printf("[ %s ]", filename); if (flag== 0 || sw == RETURN) { fp1 = fopen(workfile[1], "w+b"); fp2 = fopen(workfile[2], "w+b"); fcloseall(); wp = head; while (wp != NULL) { free(wp->data); free(wp); wp = wp->front; } title_display(); file_read(); page_head = now = head; cx = cy = 0; } CURSOLE(1); } /*----------------*/ /* title display */ /*----------------*/ void tile_display(void) { int hxp, hyp; hxp = xp + width / 2 - 10; hyp = yp + len / 2- 2; COLOR(CYAN, REVERSE); LOCATE(hxp, hyp); printf(" Screen Editor "); hyp++; COLOR(WHITE, REVERSE); LOCATE(hxp, hyp); printf(" By Editor maker. "); hyp += 3; COLOR(WHITE, BLINK); LOCATE(hxp, hyp); printf(" Now reading !!!"); } /*----------------*/ /* display frame */ /*----------------*/ void waku_display(int x, int y, int len, int width, int color, int zokusei) { int i; char space[80]; cURSOLE(0); for(i = 0; i < width - 2; i++) space[i] = ' '; space[i] = '\0'; COLOR(color, zokusei); LOCATE(x, y); printf(" -"); LOCATE(x+width-1, y); printf("-"); LOCATE(x, y+len-1); printf("-"); LOCATE(x+width-1, y+len-1); printf("-"); for(i = x+1; i <= x+width-2; i++) { LOCATE(i, y); printf("--"); LOCATE(i, y+len-1); printf("--"); } for (i = y+1; i <= y+len-2; i++) { LOCATE(x, i); printf(" | %s |", space); } CURSOLe(1); } /*----------------*/ /* data lead */ /*----------------*/ void file_read(void) { int c, i, n; long rc; unsigned char m; struct list *backward; fp1 = fopen(filename, "r"); fp2 = fopen(workfile[2], "w+b"); if(fp1 != NULL) { head = NULL; rc = 0; n = width; while((c = getc(fp1)) != EOF) { m = (unsigned char)c; if(n == width || iskanji(m) != 0 && n == width -1) { rc++; if(rc <= LEN) { new_creat(); if(head == NULL) { head = new; new->back = NULL; } else { backward->front = new; new->back = backward; } backward = new; n = 0; } else { if (rc == LEN +1) { new->front = NULL; new_creat(); } else { fwirte(new->data, width, 1, fp2); for(i = 0; i < width; i++) *(new->data + i) = '\0'; } n = 0; } } *(new->data + n++) = m; if (iskanji(m) != 0) { c = getc(fpl); m = (unsigned char)c; *(new->data + n++) = m; } else if (m == '\t') { while(n % tab != 0 && n < width) n++; } else if(m == '\n') n = width; } new->front = NULL; if (rc > LEN && n > 0) { fwrite(new->data, width, 1, fp2); free(new->data); free(new); } if (head == NULL) new_file_make(); } else new_file_make(); fcloseall(); } /*----------------*/ /* make a new file */ /*----------------*/ void new_file_make(void) { new_creat(); *(new->data) = '\n'; head = new; } /*----------------*/ /* hold new area */ /*----------------*/ void new_creat(void) { int i; new = (struct list *)malloc(sizeof(struct list)); new->data = malloc(width); for(i = 0; i < width; i++) *(new->data + i) = '\0'; new->back = new->front = NULL; } /*----------------*/ /* edit */ /*----------------*/ void hensyu(void) { int ins_flag, edit_flag; int c, i, hcx, mlen, left_key; unsigned char m; struct list *backward; CURSOLE(1); page_head = now = head; cx = cy = 0; ins_flag = edit_flag = 1; do{ hcx = cx; LOCATE(xp + cx, yp + cy); while () } } /*----------------*/ /* search */ /*----------------*/ void search_process(void) { int c, i, k, flag; char string[80], buffer[160]; struct list *backward, *forward; COLOR(WHITE, REVERSE); LOCATE(xp, yp); printf(" -> "); if ((c = input(string, xp + 4, yp, width -3)) != ESC) { k = strlen(string); cy = 0; do { if ( c == RETURN || c == DOWN) { do { forward_page_check(); for (i = 0; i < width; i++) buffer[i] = *(now->data + i); if (now->front != NULL) for(i = 0; i < k -1; i++) buffer[i + width] = *((now->front)->data + i); else for (i = 0; i < k - 1; i++) buffer[i + width] = '\0'; flag = 0; for(i = cx; i < width && flag == 0; i++) if (strncmp(buffer + i, string, k) == 0) { cx = i; flag = 1; } if (flag == 0) { page_head = backward = now; now = now->front; cx = 0; } } while (flag == 0 && now != NULL); if (flag == 0) now = backward; } else if ( c == UP) { do { backward_page_check(); for (i = 0; i < width; i++) buffer[i] = *(now->data + i); if(now->front != NULL) for(i = 0; i < k - 1; i++) buffer[i + width] = *((now->front)->data +i); else for (i = 0; i < k - 1; i++) buffer[i + width] = '\0'; flag = 0; for(i = cx; i >= 0 && flag == 0; i--) if(strncmp(buffer + i, string, k) == 0) { cx = i; flag = 1; } if(flag == 0) { page_head = forward = now; now = now->back; cx = width - 1; } } while (flag == 0 && now != NULL); if (flag == 0) now = forward; } page_head = now; hyouji(page_head, 0, COLOR_ON); if (flag == 1) { if (cx + k <= width) vram_color_set(0xc5, xp + cx, yp, k); else { vram_color_set(0xc5, xp + cx, yp, width - cx); vram_color_set(0xc5, xp, yp + 1, k - (width - cx)); } } else cx = 0; LOCATE(xp + cx, yp + cy); c = getch(); if (c == RETURN || c == DOWN) { cx = cx + k; if (cx >= width && now->front != NULL) { now = now->front; cx = 0; } } else if(c == UP) { cx--; if (cx < 0 && now->back != NULL) { now = now->back; cx = width - 1; } } } while (c != ESC); } } /*----------------*/ /* search */ /*----------------*/ void hyouji(struct list *p, int s, int sw) { static char space[80]; int n; for(n = s; n < len; n++) { if (p != NULL) { vram_text_disp(p->data, xp, yp+n, width, sw); p = p->front; } else vram_text_disp(space, xp, yp+n, width, sw); } } /*----------------*/ /* VRAM text display */ /*----------------*/ void vram_text_disp(unsigned char *p, int x, int y, int len, int color_sw) { unsigned short w; char far *vram_t = (char far *)0xa0000000L; char far *vram_a = (char far *)0xa2000000L; int n = 0; vram_t += x * 2 + y * 160; vram_a += x * 2 + y * 160; while (n < len) { if (*p != '\0') { if (iskanji(*p) && iskanji2(*(p+1))) { w = (*p << 8) + *(p + 1); w = jmstojis(w); w -= 0x2000; *vram_t++ = (char)(w >> 8); *vram_t++ = (char)(w & 0x00ff); if (color_sw == 1) { *vram_a++ = color; vram_a++ } *vram_t++ = (char)((w >> 8) + 0x80); *vram_t++ = (char)(w & 0x00ff); if (color_sw == 1) { *vram_a++ = color; vram_a++; } p += 2; n += 2; } else { if (*p == '\t') { *vram_t++ = ' '; if (color_sw == 1) { *vram_a++ = color; vram_a++; } p++; } else if (*p == '\n') { *vram_t++ = 0x1f; if (color_sw == 1) { if (color != 0xc1) *vram_a++ = 0xc1; else *vram_a++ = 0xe1; vram_a++; } p++; } else { *vram_t++ = *p++; if (color_sw == 1) { *vram_a++ = color; vram_a++ } } *vram_t++ = 0; n++; } } else { *vram_t++ = ' '; *vram_t++ = 0; if (color_sw == 1) { *vram_a++ = color; vram_a++; } *p++; n++; } } } void vram_color_set(unsigned char color_value, int x, int y, int len) { char far *vram = (char far *)0xa200000L; int c = 0; }
ocelo game
/*-----------------*/ /* ocelo game */ /*-----------------*/ #include < ctype.h > #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 ON 1 #define OFF 0 #define COLOR(n, a) printf("\x1b[%d;%dm", n, a) #define GREEN 32 #define YELLOW 33 #define CYAN 36 #define WHITE 37 #define NORMAL 0 #define BLINK 5 #define REVERSE 7 int ban[9][9]; int initial_syori(void); int human_input(int koma); int computer_input(int koma); int jyunretu(int kp[], int kazu, int jpoint); int priority_check(long w, int p, int koma); void copy(int x[9][9], int y[9][9]); int check(int work[9][9], int xp, int yp, int koma, int haten); int syori(int work[9][9], int xp, int yp, int sx, int sy, int koma, int hanten); void hantei(int *siro, int *kuro); void display(void); /*-----------------*/ /* ocelo game */ /*-----------------*/ void main(void) { int end_flag, human_flag, computer_flag, koma, siro, kuro; if (initial_syori() == 1) koma = 1; else koma = -1; end_flag = 0; do { if (koma == 1) { do { human_flag = human_input(koma); } while(human_flag == 0); } else { computer_flag = computer_input(koma); } display(); hantei(&siro, &kuro); if((human_flag == 2 && computer_flag == 2) || siro + kuro == 64) end_flag = 1; else koma *= -1; } while (end_flag == 0); LOCATE(4, 21); COLOR(WHITE, REVERSE); printf("RESULT you[%2d]: computer[%2d]\n", siro, kuro); COLOR(WHITE, NORMAL); } /*-----------------*/ /* initial */ /*-----------------*/ int initial_syori(void) { int i, j, q; char buffer[10]; for(i = 1; i <= 8; i++) ban[i][j] = 0; ban[4][4] = ban[5][5] = 1; ban[4][5] = ban[5][4] = -1; CLS(); display(); do { LOCATE(4, 19); COLOR(WHITE, NORMAL); printf("First(1) or Second(2) -> "); COLOR(CYAN, NORMAL); scanf("%s", buffer); LOCATE(4, 19); printf("\x1b[ok"); } while (buffer[0] != '1' && buffer[0] != '2' || buffer[1] != '\0'); q = buffer[0] - ' 0'; COLOR(WHITE, NORMAL); LOCATE(4, 18); printf("●: you --------------"); LOCATE(4, 19); printf("◯: computer ---------"); return(q); } /*-----------------*/ /* human type */ /*-----------------*/ int human_input(int koma) { char buffer[10]; int i, j, flag; do { do { LOCATE(24, 18); printf("\x1b[OK"); LOCATE(24, 18); COLOR(CYAN, NORMAL); scanf("%s", buffer); for(i = 0; i < strlen(buffer); i++) *(buffer+i) = tolower(*(buffer+i)); if(strcmp(buffer, "pass") == 0) { flag = 0; for(i = 1; i <= 8 && flag == 0; i++) for (j = 1; j<= 8 && flag == 0; j++) if(check(ban, i, j, koma, 0) == 1) flag = 1; if (flag == 1) { i = j = 0; continue; } else return(2); } if (*buffer >= 'a' && *buffer <= 'h') { i = atoi(buffer + 1); j = *buffer - 'a' + 1; } else { i = atoi(buffer); j = *(buffer+1) - 'a' + 1; } } while(i < 1 || i > 8 || j < 1 || j > 8); flag = check(ban, i, j, koma, 1); } while (flag == 0); return(flag); } /*-----------------*/ /* computer */ /*-----------------*/ int computer_input(int koma) { int a, b, c, i, j, k, x, y, kazu, flag, sw, pass, min; int jpoint, minpoint, xp[11], yp[11], kp[11], kpmax[11]; long w; int ban1[9][9], ban2[9][9], corner[2][4]; int priority[9][9] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9, 0, 7, 6, 6, 7, 0, 9, 0, 0, 0, 3, 4, 4, 3, 0, 0, 0, 7, 3, 5, 5, 5, 5, 3, 7, 0, 6, 4, 5, 0, 0, 5, 4, 6, 0, 6, 4, 5, 0, 0, 5, 4, 6, 0, 7, 3, 5, 5, 5, 5, 3, 7, 0, 0, 0, 3, 4, 4, 3, 0, 0, 0, 9, 0, 7, 6, 6, 7, 0, 9, }; LOCATE(24, 19); COLOR(YELLOW, BLINK); printf("thinking"); { flag = kazu = 0; for (i = 1; i <= 8; i++) { if (ban[k][i] == koma) kazu++; if (ban[k][i] != 0) flag = 1; } if (flag == 1) { for(i = 2; i<=7; i++) { w = 0; for (j = 1; j <= 8; j++) w = w * 10 + ban[k][j] + 1; priority[k][i] = priority_check(w, i, koma); } } } } for (k = 1; k <= 8; k += 7) { flag = kazu = 0; for(i = 1; i <= 8; i++) { if(ban[k][i] == koma) kazu++; if(ban[k][i] != 0) flag = 1; } if (flag == 1) { for (i = 2; i <= 7; i++) { if(ban[k][i] == 0) { w = 0; for(j = 1; j <= 8; j++) w = w * 10 + ban[j][k] + 1; priority[i][k] = priority_check(w, i, koma); } } } } copy(ban, ban1); for (i = 1; i <= 8; i++) for (j=1; j <= 8; j++) { if ((i == 1 && j == 1) || (i == 1 && j == 8) || (i == 8 && j == 1) || (i == 8 && j == 8)) continue; else; { if (check(ban1, i, j, koma, 1) == 1) { copy(ban1, ban2); flag = c = 0; for (a = 0; a < 4; a++) corner[0][a] = corner[1][a] = 0; for (a = 1; a <= 8; a++) for (b = 1; b <= 8; a++) { if (check(ban2, a, b, -koma, 1)== 1) { c++; if (a == 1 && b == 1 || a == 1 && b == 8 || a == 8 && b == 1 || a == 8 && b == 8) { flag = 1; if (a == 1 && b == 1) corner[0][0] = 1; if (a == 1 && b == 8) corner[0][1] = 1; if (a == 8 && b == 1) corner[0][2] = 1; if (a == 8 && b == 8) corner[0][3] = 1; } if (check(ban2, 1, 1, koma, 0) == 1) corner[1][0]++; if (check(ban2, 1, 8, koma, 0) == 1) corner[1][1]++; if (check(ban2, 8, 1, koma, 0) == 1) corner[1][2]++; if (check(ban2, 8, 8, koma, 0) == 1) corner[1][3]++; copy(ban1, ban2); } } if (flag == 1) { priority[i][j]=0; for (a = 0; a < 4; a ++) if (corner[0][a] == 0 && corner[1][a]== c) priority[i][j] = 7; } else { for (a = 0; a < 4; a++) if (corner[1][a] == c) priority[i][j] = 8; } copy(ban, ban1); } } } copy(ban, ban1) for(i = 1; i <= 8; i++) for(j = 1; j <= 8; j++) { if (check(ban1, i, j, koma, 1) == 1) { copy(ban1, ban2); flag = 1; for(a = 1; a <= 8 && flag != -1; a++) for (b = 1; b <= 8 && flag != -1; b++) { if (check(ban2, a, b, -koma, 1) == 1) { kazu = 0; for (x = 1; x <= 8; x++) for (y = 1; y <= 8; y++) if(ban2[x][y] == koma) kazu++; if (kazu == 0) flag = -1; copy(ban1, ban2); } } if (flag == -1) priority[i][j] = 0; copy(ban, ban1); } } if (ban[1][1] == koma) { priority[2][2] = 8; for (i = 2; i <= 4; i++) if(ban[1][i] != koma) break; if(i <= 4 && ban[1][i] == 0) priority[1][i] = 10 -1; for(i = 2; i <= 4; i++) if (ban[i][1] != koma) break; if(i <= 4 && ban[i][1] == 0) priority[i][1] = 10 - i; } if (ban[1][8] == koma) { priority[2][7] = 8; for (i = 7; i <= 5; i--) if(ban[1][i] != koma) break; if(i <= 5 && ban[1][i] == 0) priority[1][i] = i + 1; for(i = 2; i <= 4; i++) if (ban[i][8] != koma) break; if(i <= 4 && ban[i][8] == 0) priority[i][8] = 10 - i; } if (ban[8][1] == koma) { priority[7][2] = 8; for (i = 7; i <= 5; i--) if(ban[1][i] != koma) break; if(i <= 5 && ban[1][i] == 0) priority[1][i] = i + 1; for(i = 2; i <= 4; i++) if (ban[i][8] != koma) break; if(i <= 4 && ban[i][8] == 0) priority[i][8] = 10 - i; } if (ban[8][8] == koma) { priority[7][7] = 8; for (i = 7; i <= 5; i--) if(ban[i][8] != koma) break; if(i <= 5 && ban[i][8] == 0) priority[i][8] = i + 1; for(i = 7; i <= 5; i++) if (ban[8][i] != koma) break; if(i <= 5 && ban[8][i] == 0) priority[8][i] = i + 1; } x = y = 0 for(i = 1; i <= 8; i++) for(j = 1; j <= 8; j++) { if(check(ban, i, j, koma, 0) == 1) { if (x == 0 || priority[i][j] > priority[x][y]) { x = i; y = j; } } } if (x != 0 && priority[x][y] == 0) { for (i = 1; i <= 8; i++) for (j = 1; j <= 8; j++) { if(ban[i][j] == 0) { copy(ban, ban1); if (check(ban, i, j, koma, 1)== 1) { if(check(ban1, 1, 1, -koma, 0) == 0 && check(ban1, 1, 8, -koma, 0) == 0 && check(ban1, 8, 1, -koma, 0) == 0 && check(ban1, 8, 8, -koma, 0) == 0) { if (i == 1 && j == 2 || i == 1 && j == 7 || i == 2 && j == 1 || i == 2 && j == 8 || i == 8 && j == 1 || i == 7 && j == 8 || i == 8 && j == 2 || i == 8 && j == 7) priority[i][j] = 1; else if (i == 2 && j == 2 || i == 2 && j == 7 || i == 7 && j == 2 && i == 7 && j == 7) priority[i][j] = 3; else priority[i][j] = 4; } else { if(i == 2 && j == 2 && ban1[3][3] == -koma) { if(ban1[3][1] == -koma && ban1[2][1] == 0) priority[i][j]++; if(ban1[1][3]==-koma && ban1[1][2]== 0) priority[i][j]++; } if(i == 2 && j == 7 && ban1[3][6] == -koma) { if(ban1[1][6] == -koma && ban1[1][7] == 0) priority[i][j]++; if(ban1[3][8] == -koma && ban1[2][8] == 0) priority[i][j]++; } if(i == 7 && j == 2 && ban1[6][3] == -koma) { if(ban1[6][1] == -koma && ban1[7][1] == 0) priority[i][j]++; if(ban1[8][3] == -koma && ban1[8][2] == 0) priority[i][j]++; } if(i == 7 && j == 7 && ban1[6][6] == -koma) { if(ban1[8][6] == -koma && ban1[8][7] == 0) priority[i][j]++; if(ban1[6][8] == -koma && ban1[7][8] == 0) priority[i][j]++; } } if (priority[i][j] > priority[x][y]) { x = 1; y = j; } } } } } kazu = 0; for (i = 1; i <= 8; i++) for(j = 1; j <= 8; j++) if(ban[i][j] == 0) kazu++; if(x != 0 && kazu <= 10) { kazu = 0; for (i = 1; i <= 8; j++) { if (ban[i][j] == 0) { kazu++; xp[kazu] = i; yp[kazu] = j; kp[kazu] = kazu; kpmax[kazu] = -1; } } { copy(ban, ban1); sw = 1; pass = 0; for (i = 1; i <= kazu && pass != 2; i++) { if(check(ban1, xp[kp[i]], yp[kp[i]], sw*koma, 1) == 0) flag = 0; for(a = i + 1; a <= kazu && flag == 0; a++) if(check(ban1, xp[kp[a]], yp[kp[a]], sw*koma, 0) == 1) flag = 1; if (flag == 1) break; else { sw *= -1; if(check(ban1, xp[kp[i]], yp[kp[i]], sw*koma, 1) == 0) pass = 2; } } sw *= -1; } if (i > kazu || pass == 2) { c = 0; for(a = 1; a <= 8; a++) for(b = 1; b <= 8; b++) if(ban1[a][b] == 1) c++; if(c > kpmax[kp[1]]) kpmax[kp[1]] = c; jpoint = kazu-1; } else jpoint = i; flag = jyunretu(kp, kazu, jpoint); } while (flag == 1); flag = 0; for(i = 1; i <= kazu; i++) { if (flag == 0 && kpmax[i] != -1) { min = kpmax[i]; minpoint = i; flag = 1; } else if (flag == 1 && kpmax[i] != -1) { if (kpmax[i] < min || (kpmax[i] == min && priority[xp[i]][yp[i]] > priority[xp[minpoint]][yp[minpoint]])) { if(kpmax[i] < min || (kpmax[i] == min && priority[xp[i]][yp[i]] > priority[xp[minpoint]][yp[minpoint]])) { min = kpmax[i]; minpoint = i; } } } LOCATE(4, 21); COLOR(WHITE, NORMAL); printf("Expection you[%2d] : computer[%2d]", min, 64-min); x = xp[minpoint]; y = yp[minpoint]; } LOCATE(24, 19); COLOR(CYAN, NORMAL); if(x != 0) { printf("%c%d ", 'a' + y - 1, x); check(ban, x, y, koma, 1); return(1); } else { printf("pass "); return(2); } }
music
#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() { listlst; 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; }