int my_getop(char s[]) { int c, i; while(((s[0] = c = my_getch()) == ' ' )|| (c == '\t')); s[1] = '\0'; i = 0; if((!isdigit(c)) && (c != '.') && (c != '-')) { return c; } if(c == '-') { if(isdigit(c = my_getch()) || (c = '.')) { s[++i] = c; } else if(c != EOF) { my_ungetch(c); return '-'; } } if(isdigit(c)) { while(isdigit((s[++i]) = c = my_getch())); } if(c == '.') { while(isdigit((s[++i]) = c = my_getch())); } s[i] = '\0'; if(c!= EOF) { my_ungetch(c); } return NUMB; }
char * get_string(int eos) { char buf[DPISTRLEN+1]; char *s, *limit; int c; memset(buf, 0, sizeof(buf)); limit = buf + sizeof(buf); c = my_getch(); for (s = buf;; c = my_getch()) { if (c <= 0 || c == eos || (eos == ' ' && c == '\t')) { *s = 0; break; } else if (c == '\n') { *s = 0; my_ungetch(c); break; } else { *s++ = c; if (s >= limit) return NULL; } } return strdup(buf); }
/* getop: get next operator or numeric operand */ int getop(char s[]) { int i, c, d; while ((s[0] = c = my_getch()) == ' ' || c == '\t') ; s[1] = '\0'; if (!isdigit(c) && c != '.' && c != '-' && c != '+') { if (c >= 'a' && c <= 'z') { /* Handle stack commands */ while (isalpha(s[++i] = my_getch())) ; s[i] = '\0'; if (!strcmp(s, "print")) return PRINT; else if (!strcmp(s, "dup")) return DUP; else if (!strcmp(s, "swap")) return SWAP; else if (!strcmp(s, "clear")) return CLEAR; else if (!strcmp(s, "sin")) return SIN; else if (!strcmp(s, "cos")) return COS; else if (!strcmp(s, "exp")) return EXP; else if (!strcmp(s, "pow")) return POW; else return c; /* Unknown command */ } else { return c; /* Handle operators */ } } /* peek at next character to make sure it is a number */ if (c == '-' || c == '+') { d = my_getch(); my_ungetch(d); if (!isdigit(d)) return c; /* not a number */ } if (!isdigit(c) && c != '.' && c != '-' && c != '+') i = 0; if (isdigit(c) || c == '-' || c == '+') /* collect integer part */ while (isdigit(s[++i] = c = my_getch())) ; if (c == '.') /* collect fraction part */ while (isdigit(s[++i] = c = my_getch())) ; s[i] = '\0'; if (c != EOF) my_ungetch(c); return NUMBER; }
int get_okay(const char *prompt, int default_value) { int c; flush_to_newline(0); printf("%s", prompt); for (;;) { c = my_getch(); if (c <= 0) { break; } else if (c == ' ' || c == '\t') { /* skip blanks and tabs */ } else if (c == '\n') { my_ungetch(c); return default_value; } else if (c == 'y' || c == 'Y') { return 1; } else if (c == 'n' || c == 'N') { return 0; } else { flush_to_newline(0); printf("%s", prompt); } } return -1; }
char * get_dpistr_argument(const char *prompt) { int c; for (;;) { c = my_getch(); if (c <= 0) { break; } else if (c == ' ' || c == '\t') { /* skip blanks and tabs */ } else if (c == '\n') { printf("%s", prompt); } else if (c == '"' || c == '\'') { return get_string(c); } else if (('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z') || (c == '-' || c == '/' || c == '.' || c == ':')) { my_ungetch(c); return get_string(' '); } else { my_ungetch(c); return NULL; } } return NULL; }
int get_number_argument(const char *prompt, long *number) { int c; int result = 0; for (;;) { c = my_getch(); if (c <= 0) { break; } else if (c == ' ' || c == '\t') { /* skip blanks and tabs */ } else if (c == '\n') { printf("%s", prompt); } else if ('0' <= c && c <= '9') { my_ungetch(c); result = get_number(number); break; } else { my_ungetch(c); *number = 0; break; } } return result; }
int getint(int *pn) { int c, sign, d ; while (isspace(c = my_getch())) ; if ((!isdigit(c)) && (c != EOF) && (c != '+') && (c != '-')) { my_ungetch(c); return 0; } sign = (c == '-') ? -1 : 1; if (c == '+' || c == '-') d = c; if(!isdigit(c = my_getch())) { if(c != EOF) my_ungetch(c); my_ungetch(d); return d; } for (*pn = 0; isdigit(c); c = my_getch()) *pn = 10 * *pn + (c - '0'); *pn *= sign; if (c != EOF) my_ungetch(c); return c; }
int cilent_log() { char passwd[20]; int i; msg.action = LOG; printf(" ID : "); scanf("%s",msg.id); printf("密 码 :"); getchar(); for(i = 0; i < 20; i++) { passwd[i] = my_getch(); if(passwd[i] == '\r') { passwd[i] = '\0'; break; } printf("*"); } printf("\n"); strcpy(msg.password,passwd); if(my_strcmp(msg.id,"10000") == 0) { if(my_strcmp(passwd,"admin") == 0) { strcpy(msg.name,"the big admin"); msg.state = LINE; power = 1; } } if(write(sockfd,&msg,sizeof(msg))==-1) { fprintf(stderr,"log write Error:%s\n",strerror(errno)); } printf("log send ok!"); printf("\t\t\033[47;31m 按任意键返回!\033[0m\n"); getchar(); return 0; }
int should_continue_wait(t_lemipc *game) { if (game->started) return (0); else if (game->nb_team < 2) usleep(USLEEP_TIME * 2); else if (have_two_complete_team(game)) { printf("press enter to start\n"); while (!game->started && my_getch(0) != '\n') usleep(USLEEP_TIME * 2); game->started = 1; return (0); } return (1); }
int get_partition_modifier(void) { int c, result; result = 0; c = my_getch(); if (c == 'p' || c == 'P') result = 1; else if (c > 0) my_ungetch(c); return result; }
void flush_to_newline(int keep_newline) { int c; for (;;) { c = my_getch(); if (c <= 0) { break; } else if (c == '\n') { if (keep_newline) my_ungetch(c); break; } else { /* skip */ } } return; }
int get_number(long *number) { long value; int c; value = 0; while ((c = my_getch())) { if (c >= '0' && c <= '9') { value = value * 10 + (c - '0'); } else if (c == ' ' || c == '\t' || c == '\n') { my_ungetch(c); *number = value; return 1; } else { return 0; } } return 0; }
unsigned long get_multiplier(long divisor) { unsigned long result, extra; int c; c = my_getch(); extra = 1; if (c <= 0 || divisor <= 0) { result = 0; } else if (c == 't' || c == 'T') { result = 1024 * 1024; extra = 1024 * 1024; } else if (c == 'g' || c == 'G') { result = 1024 * 1024 * 1024; } else if (c == 'm' || c == 'M') { result = 1024 * 1024; } else if (c == 'k' || c == 'K') { result = 1024; } else { my_ungetch(c); result = 1; } if (result > 1) { if (extra > 1) { result /= divisor; if (result >= 4096) result = 0; /* overflow -> 20bits + >12bits */ else result *= extra; } else if (result >= divisor) { result /= divisor; } else { result = 1; } } return result; }
int get_command(const char *prompt, int promptBeforeGet, int *command) { int c; if (promptBeforeGet) printf("%s", prompt); for (;;) { c = my_getch(); if (c <= 0) { break; } else if (c == ' ' || c == '\t') { /* skip blanks and tabs */ } else if (c == '\n') { printf("%s", prompt); } else { *command = c; return 1; } } return 0; }
int main( void ) { int i, j, c; unsigned long step; char matrix[MAX][MAX]; char class; /*初始化随机数发生器、matrix矩阵、Site结构体*/ srand((unsigned)time(0)); Point Site; /*初始化矩阵matrix,填入顺序的数字标号*/ printf("I'm initializing...\n"); for(i = 0, class = CLASS; i < MAX; i++) for(j = 0; j < MAX; j++, class++) matrix[i][j]= class; matrix[MAX - 1][MAX - 1] = ' '; /*随机移动矩阵matrix中的数字标号,作为游戏起始状态*/ for(step = 0; step < MAX_STEP; step++) { c = roll(4); Site = find_point( &matrix[0] ); switch(c) { case 0: if((Site.y + 1) < MAX) { matrix[Site.x][Site.y] = matrix[Site.x][Site.y + 1]; matrix[Site.x][Site.y + 1] = ' '; } break; case 1: if((Site.y - 1) >= 0) { matrix[Site.x][Site.y] = matrix[Site.x][Site.y - 1]; matrix[Site.x][Site.y - 1] = ' '; } break; case 2: if((Site.x - 1) >= 0) { matrix[Site.x][Site.y] = matrix[Site.x - 1][Site.y]; matrix[Site.x - 1][Site.y] = ' '; } break; case 3: if((Site.x + 1) < MAX) { matrix[Site.x][Site.y] = matrix[Site.x + 1][Site.y]; matrix[Site.x + 1][Site.y] = ' '; } break; } } printf_matrix( &matrix[0] ); printf("I'm working...\n"); /*游戏部分,从键盘接收命令,执行相应的动作,并打印出矩阵*/ for(step = 0; is_win(&matrix[0]) != 1; step++) { c = my_getch(); Site = find_point( &matrix[0] ); switch(c) { case 'a': if((Site.y + 1) < MAX) { matrix[Site.x][Site.y] = matrix[Site.x][Site.y + 1]; matrix[Site.x][Site.y + 1] = ' '; } break; case 'd': if((Site.y - 1) >= 0) { matrix[Site.x][Site.y] = matrix[Site.x][Site.y - 1]; matrix[Site.x][Site.y - 1] = ' '; } break; case 's': if((Site.x - 1) >= 0) { matrix[Site.x][Site.y] = matrix[Site.x - 1][Site.y]; matrix[Site.x - 1][Site.y] = ' '; } break; case 'w': if((Site.x + 1) < MAX) { matrix[Site.x][Site.y] = matrix[Site.x + 1][Site.y]; matrix[Site.x + 1][Site.y] = ' '; } break; case 'Q': printf("Used %lu steps.\n", step); exit(0); } system("clear"); printf_matrix( &matrix[0] ); printf("Used %lu steps.\n", step); } /*打印出总共执行的步数step*/ system("clear"); printf_matrix( &matrix[0] ); printf("I win!\n"); printf("Used %lu steps.\n", step); return 0; }
int main() { char c; int max_num = MAX; init_hanoi(max_num); printf("\n\n\n\n\n\n"); printf("\t\t\t\tHanoi Game\n\t\t\t\t"); printf("Enter '<' to quit!\n\t\t\t\tEnter '>' to show cheats!\n\n"); show_hanoi(); while(!is_complete(c_top)){ while(catch_status == FALSE) { c = my_getch(); if(c == 'd'){ switch(now_status) { case ON_A: now_status = ON_B; break; case ON_B: now_status = ON_C; break; case ON_C: now_status = ON_A; break; } show_hanoi(); } else if(c == 'a'){ switch(now_status) { case ON_A: now_status = ON_C; break; case ON_B: now_status = ON_A; break; case ON_C: now_status = ON_B; break; } show_hanoi(); } else if(c == 's') { catch_status = TRUE; show_hanoi(); break; } else if(c == '>') { dohanoi(max_num,65,66,67); printf("\n"); show_hanoi(); break; } else if(c == '<') { printf("Game Over! Bye!\n"); exit(0); } } while(catch_status == TRUE) { c = my_getch(); if(c == 'd'){ switch(now_status) { case ON_A: a_to_b(); now_status = ON_B; break; case ON_B: b_to_c(); now_status = ON_C; break; case ON_C: c_to_a(); now_status = ON_A; break; } show_hanoi(); } else if(c == 'a'){ switch(now_status) { case ON_A: a_to_c(); now_status = ON_C; break; case ON_B: b_to_a(); now_status = ON_A; break; case ON_C: c_to_b(); now_status = ON_B; break; } show_hanoi(); } else if(c == 's') { catch_status = FALSE; show_hanoi(); break; } else if(c == '>') { dohanoi(max_num,65,66,67); printf("\n"); show_hanoi(); break; } else if(c == '<') { printf("Game Over! Bye!\n"); exit(0); } } } printf("Good Game! Bye!\n"); return 0; }