int main() { char choice; char name[80]; int number; int found; SymbolTable book; int i; /* initialize phone book */ book = createSymbolTable(&makePhoneBook, &comparePhone); do{ choice = getMenu(); switch(choice){ case '1': printf("Enter the name: "); scanf("%[^\n]", name); myfflush(); printf("Enter the number: "); scanf("%d", &number); myfflush(); addEntry(&number, &name, &book); printf("%d\n", book.total); break; case '2': printf("Enter the number you want to find: "); scanf("%d", &number); myfflush(); found = getEntryValue(&number, &book); if(found == -1) printf("Not found\n"); else{ displayTitle(); displayEntry(book.entries[found]); } break; case '3': displayTitle(); for(i = 0; i < book.current; i++){ displayEntry(book.entries[i]); } break; case '4': dropSymbolTable(&book); break; default: printf("Invalid choice\n"); } }while(choice != EXIT); }
int main() { char fileName[50]; Node *root = NULL; printf("Enter data file name: "); scanf("%[^\n]", fileName); myfflush(); root = createTree(fileName); printf("Done\n"); //traversal printf("PreOrder\n"); preOrder(root); printf("\n"); printf("inOrder\n"); inOrder(root); printf("\n"); printf("PostOrder\n"); postOrder(root); printf("\n"); //count Nodes printf("The number of nodes is %d\n", countNode(root)); Node *h = find(root, 2); printf("The height of node h is %d\n", height(h)); printf("The depth of node 11 is %d\n", depth(root, 11)); return 0; }//end main
char getMenu() { char choice; printf("+===============================================+\n"); printf("| 1. Add new phone entry |\n"); printf("| 2. Search for a number |\n"); printf("| 3. List all the contacts |\n"); printf("| 4. Exit |\n"); printf("+===============================================+\n"); printf("Your choice: "); scanf("%c", &choice); myfflush(); return choice; }
int get_input(int low, int high) { int input, got_input = 0; got_input = scanf("%d", &input); myfflush(); if (got_input < 1 || ((input < low || input > high) && (input != 0))) { return -1; } return input; }
void game_loop(int low, int high) { int input; do { int number = RandomInt(low, high); int counter = 1; /* Initial setup */ do { printf("Guess the number I'm thinking of (between %d and %d, " "0 = quit): ", low, high); input = get_input(low, high); } while (input != 0 && input == -1); while (input != number && input != 0) { if (number < input) { printf("Too high. Guess again: "); } else if (number > input) { printf("Too low. Guess again: "); } input = get_input(low, high); ++counter; } if (input) { printf("You guessed the number in %d tries!\n", counter); printf("Play again? (1=yes, 0=no): "); scanf("%d", &input); myfflush(); } } while (input); }
//客户端二级菜单 void usr_control(int fd){ while(1){ int ret=-1; make_menu(); scanf("%d",&ret); myfflush(); switch(ret){ case 1://命令操作 incmd(fd); push_continue(); break; case 2: usr_exit(fd); push_continue(); break; default: break; } } }
int main(){ printf("**Not fully tested, eg, max of 20 file open and such.\n"); printf("**Buffering set small so replenishing of buffer is checked.\n"); int c; myFILE *fp1; myFILE *fp2; char namein[MAXWORD]; char nameout[MAXWORD]; strcpy(namein, "8_1.c"); strcpy(nameout, "testfile"); myfflush(NULL); if ( ( fp1 = myfopen(namein, "r") ) == NULL ){ fprintf(stderr, "Error: can't open file %s for reading\n", namein); exit(1); } else if ( ( fp2 = myfopen(nameout, "w") ) == NULL ){ fprintf(stderr, "Error: can't open file %s for writing\n", nameout); exit(2); } while ( ( c = getc(fp1) ) != EOF ) putc(c, fp2); if ( myfclose(fp1) == EOF){ fprintf(stderr, "Error: can't close file %s.\n", namein); exit(3); } else if ( myfclose(fp1) == EOF){ fprintf(stderr, "Error: can't close file %s.\n", nameout); exit(4); } return 0; }
void clientproc_echo(int fd){ while(1){ int ch=0; reg_menu(); scanf("%d",&ch); myfflush(); switch(ch){ case 1: regusr(fd); getchar(); push_continue(); break; case 2: if(logusr(fd)==1){ usr_control(fd); } break; default: break; } } }